<?php 
include("../config.php");
if(!empty($_POST)){

	if($_POST["id"] != ''){
		$code  	= mysqli_real_escape_string($conn, $_POST['code']);
	}else{
		$code   = mysqli_real_escape_string($conn, $_POST['code']);
		$checkpoint = mysqli_query($conn,'SELECT id FROM lhc_balance_history WHERE code = "'. $code .'"');
		if(mysqli_num_rows($checkpoint) > 0) {
	      $code = LhcBalance($conn);
	   }	
	}

	if(!empty($_POST['date'])){
		$date  = date('Y-m-d', strtotime(mysqli_real_escape_string($conn, $_POST['date'])));
	}else{
		$date  = '';
	}
	
	if(!empty($_POST['bank_name'])){
		$bank_name         = mysqli_real_escape_string($conn, $_POST['bank_name']);
	}else{
		$bank_name         = '';
	}

	if(!empty($_POST['imps_number'])){
		$imps_number         = mysqli_real_escape_string($conn, $_POST['imps_number']);
	}else{
		$imps_number         = '';
	}

	if(!empty($_POST['neft_number'])){
		$neft_number         = mysqli_real_escape_string($conn, $_POST['neft_number']);
	}else{
		$neft_number         = '';
	}

	if(!empty($_POST['rtgs_number'])){
		$rtgs_number         = mysqli_real_escape_string($conn, $_POST['rtgs_number']);
	}else{
		$rtgs_number         = '';
	}

	if(!empty($_POST['cheque_number'])){
		$cheque_number         = mysqli_real_escape_string($conn, $_POST['cheque_number']);
	}else{
		$cheque_number         = '';
	}  

	if(!empty($_POST['cheque_date'])){
		$cheque_date  = date('Y-m-d', strtotime(mysqli_real_escape_string($conn, $_POST['cheque_date'])));
	}else{
		$cheque_date  = '0000-00-00';
	}  

	if(!empty($_POST['payment_date'])){
		$payment_date  = date('Y-m-d', strtotime(mysqli_real_escape_string($conn, $_POST['payment_date'])));
	}else{
		$payment_date  = '';
	}

	$payment_mode        = mysqli_real_escape_string($conn, $_POST['payment_mode']);
  	$cash                = mysqli_real_escape_string($conn, $_POST['cash']);
  	$pay_mode_amnt       = mysqli_real_escape_string($conn, $_POST['pay_mode_amnt']);
  	$cash_name           = mysqli_real_escape_string($conn, $_POST['cash_name']);
  	$payment_type        = mysqli_real_escape_string($conn, implode(",", $_POST['payment_type']));
	$lhc_data            = $_POST['lhc'];
	$company_id          = $_SESSION['company_id'];
  	$branch_id           = $_SESSION['branch_id'];
  	$created_datetime    = date('Y-m-d H:i:s');
  	$created_id          = $_SESSION['user_detail']['id'];
  	$updated_datetime    = date('Y-m-d H:i:s');
 	$updated_id          = $_SESSION['user_detail']['id'];

	if($_POST["id"] == ''){
		$type       = 'add_lhc_balance';
	  	$user_id    = $_SESSION['user_detail']['id'];
	  	$data       = serialize($_POST);
	  	if(count($lhc_data) > 0){
   		foreach ($lhc_data as $key => $lhc_data) {
   			 if($lhc_data['adjust_amount'] >0){
      		    $remark_string = $remark_string ." LHCNo. ".$lhc_data['lhc_no']." Date. ".$lhc_data['lhc_date']." Truck No ".$lhc_data['truck_no']." From Place ".$lhc_data['from_place']." To Place ".$lhc_data['to_place'];
   		    }  

	   		if( isset($lhc_data['adjust_amount']) && !empty($lhc_data['adjust_amount']) ) {
    				$common_remark = 'LHC NO - '.$lhc_data['lhc_no'].' DT - '.$lhc_data['lhc_date'];
	  				if($lhc_data['payment_to'] == 'AGENT'){
	  					$act_msetr = mysqli_query($conn, "SELECT * FROM `act_master` WHERE `ac_name` = '".$lhc_data['agent']."'");
	  					$act_ma_row = mysqli_fetch_assoc($act_msetr);
	  				}else{
	  					$tr_mst = mysqli_query($conn, "SELECT * FROM `truck_mst` WHERE `id` = '".$lhc_data['owner_name']."'");
	  					$tr_mst_row = mysqli_fetch_assoc($tr_mst);

	  					$act_msetr = mysqli_query($conn, "SELECT * FROM `act_master` WHERE `ac_name` = '".$tr_mst_row['o_name']."'");
	  					$act_ma_row = mysqli_fetch_assoc($act_msetr);
	  				}

	  				//COMMON GENERAL LEDGER
				  	$checkpontgl = mysqli_query($conn, "SELECT * FROM `general_ledger` WHERE `voucher_serial` = '".$lhc_data['lhc_no']."' AND `mark_flag` = 'LHC PAYMENT' AND `lhc_balance_serial` = '".$code."'");
				  	if(mysqli_num_rows($checkpontgl) > 0){
				   	$gl_delete = mysqli_query($conn, "DELETE FROM `general_ledger` WHERE `voucher_serial` = '".$lhc_data['lhc_no']."' AND `mark_flag` ='LHC PAYMENT'");   
				  	}
				  	
				  	if($payment_type == 'C'){
				  		$cash_amount = $lhc_data['adjust_amount'];
				  	}else{
				  		$bank_amount = $lhc_data['adjust_amount'];
				  	}

	  				if($cash > 0){
	  					$voucher_no = cpCode($conn);

	  					$get_cash = mysqli_query($conn, "SELECT * FROM `act_master` WHERE `id` = '".$cash_name."'");
    					$get_cash_row = mysqli_fetch_assoc($get_cash);
    	  				$insert = mysqli_query($conn, "INSERT INTO `lhc_balance_history`(`p_date`, `lhc_no`, `lorry_hire`, `adv_amnt`, `balance`,`other_add_amount`,`other_less_amount`, `net_amnt`, `payment_to`, `adj_amnt`, `payment_type`, `cash`, `cash_name`, `pay_mode_amnt`, `payment_mode`, `imps_number`, `rtgs_number`, `neft_number`, `cheque_number`, `cheque_date`, `bank_name`, `payment_date`, `code`, `date`, `branch_id`, `company_id`, `created_id`, `created_datetime`) VALUES ('".risFormatDate($lhc_data['lhc_date'], 'Y-m-d')."','".$lhc_data['lhc_no']."','".$lhc_data['lorry_hire']."','".$lhc_data['adv_amnt']."','".$lhc_data['balance']."','".$lhc_data['other_add_amount']."','".$lhc_data['other_less_amount']."','".$lhc_data['lorry_hire']."','".$lhc_data['payment_to']."','".$cash."','C','".$cash."','".$cash_name."','0','','','','','','".$cheque_date."','','".$payment_date."','".$code."','".$date."','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."')");
	  				   $last_id = mysqli_insert_id($conn);
    					$lps = mysqli_query($conn, "INSERT INTO `lhc_payment_status`(`lhc_no`, `payment_type`, `amount`, `voucher_no`, `flag`, `master_id`, `branch_id`, `company_id`) VALUES ('".$lhc_data['lhc_no']."','CASH','".$lhc_data['adjust_amount']."','".$voucher_no."','LHC BALANCE','".$last_id."','".$branch_id."','".$company_id."')");
	  				}

	  				if($pay_mode_amnt > 0){
	  					$master_bank = combine(getAllRecords('act_master'), 'id', 'ac_name');
      				$bank_remark = 'LHC NO - '.$lhc_data['lhc_no'].' DT. '.$lhc_data['lhc_date'].' BY '.$payment_mode.'-'.$neft_number.'-'.@$master_bank[$bank_name];
	  					$voucher_no   = bpCode($conn);
	  					$get_bank = mysqli_query($conn, "SELECT * FROM `act_master` WHERE `id` = '".$bank_name."'");
	  					$get_bank_row = mysqli_fetch_assoc($get_bank);

   	  				$insert = mysqli_query($conn, "INSERT INTO `lhc_balance_history`(`p_date`, `lhc_no`, `lorry_hire`, `adv_amnt`, `balance`,`other_add_amount`,`other_less_amount`, `net_amnt`, `payment_to`, `adj_amnt`, `payment_type`, `cash`, `cash_name`, `pay_mode_amnt`, `payment_mode`, `imps_number`, `rtgs_number`, `neft_number`, `cheque_number`, `cheque_date`, `bank_name`, `payment_date`, `code`, `date`, `branch_id`, `company_id`, `created_id`, `created_datetime`) VALUES ('".risFormatDate($lhc_data['lhc_date'], 'Y-m-d')."','".$lhc_data['lhc_no']."','".$lhc_data['lorry_hire']."','".$lhc_data['adv_amnt']."','".$lhc_data['balance']."','".$lhc_data['other_add_amount']."','".$lhc_data['other_less_amount']."','".$lhc_data['lorry_hire']."','".$lhc_data['payment_to']."','".$pay_mode_amnt."','B','0','','".$bank_amount."','".$payment_mode."','".$imps_number."','".$rtgs_number."','".$neft_number."','".$cheque_number."','".$cheque_date."','".$bank_name."','".$payment_date."','".$code."','".$date."','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."')");
   	  				$last_id = mysqli_insert_id($conn);
    					$lps = mysqli_query($conn, "INSERT INTO `lhc_payment_status`(`lhc_no`, `payment_type`, `amount`, `voucher_no`, `flag`, `master_id`, `branch_id`, `company_id`) VALUES ('".$lhc_data['lhc_no']."','BANK','".$lhc_data['adjust_amount']."','".$voucher_no."','LHC BALANCE','".$last_id."','".$branch_id."','".$company_id."')");
	  				}

	  				$new_adj_amount  = $lhc_data['last_adjust_amount'] + $lhc_data['adjust_amount'];

	  				$balance = ($lhc_data['balance'] + $lhc_data['other_add_amount']) - ($lhc_data['adjust_amount'] + $lhc_data['other_less_amount']);

					
					$get_lhc_nt = mysqli_query($conn, "SELECT * FROM `lhc` WHERE `lhc_no` = '".$lhc_data['lhc_no']."'");
    				$get_lhc_row_nt = mysqli_fetch_assoc($get_lhc_nt);
					  
					$new_net_amount = $get_lhc_row_nt['net_amnt'] + $lhc_data['other_add_amount'];

					//echo $new_net_amount;


	  				$trans = mysqli_query($conn, "UPDATE `trans` SET `adjust`='".$new_adj_amount."',`balance`='".$balance."',`updated_id`='".$updated_id."',`updated_datetime`='".$updated_datetime."' WHERE `voucher_no`='".$lhc_data['lhc_no']."' AND flag = 'LHC' AND `branch_id`='".$branch_id."' AND `company_id`='".$company_id."'");

	  				$lhc_update = mysqli_query($conn, "UPDATE `lhc` SET `balance`='".$balance."',`net_amnt`= '".$new_net_amount."',`updated_id`='".$updated_id."',`updated_datetime`='".$updated_datetime."' WHERE `lhc_no`='".$lhc_data['lhc_no']."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'");
	  				if($lhc_data['other_add_amount'] > 0){

					$add_query_ot = mysqli_query($conn, "INSERT INTO `lhc_details` (`lhc_id`, `type`, `key`, `tax_mst_id`, `amount`, `flag`, `branch_id`, `company_id`) VALUES ('".$lhc_data['lhc_no']."', 'add', '4', '12', '".$lhc_data['other_add_amount']."', 'P', '".$branch_id."','".$company_id."')");

					  $get_tax       = "SELECT * FROM `tax_mst` WHERE `descr` = 'OTHER ADD'";
			        $get_tax_res   = mysqli_query($conn,$get_tax);
			        $get_tax_row   = mysqli_fetch_array($get_tax_res);
			        $ac_id         = $get_tax_row['account_group'];

			        $acc_name      = "SELECT * FROM `act_master` WHERE `id` = '".$ac_id."'";
			        $acc_name_res  = mysqli_query($conn,$acc_name);
			        $acc_name_row  = mysqli_fetch_array($acc_name_res);
			              
			        $add_two = "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`, `mark_flag`) VALUES ('".$lhc_data['lhc_no']."','".$payment_date."','G','".$act_ma_row['act_code']."','".$acc_name_row['act_code']."','".$lhc_data['other_add_amount']."','','','P','0','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."','LHC PAYMENT OTHER')";
			        $add_one_res = mysqli_query($conn,$add_two);

					}
					if($lhc_data['other_less_amount'] > 0){

					$less_query_ot = mysqli_query($conn, "INSERT INTO `lhc_details` (`lhc_id`, `type`, `key`, `tax_mst_id`, `amount`, `flag`, `branch_id`, `company_id`) VALUES ('".$lhc_data['lhc_no']."', 'sub', '4', '13', '".$lhc_data['other_less_amount']."', 'P', '".$branch_id."','".$company_id."')");


						$get_tax       = "SELECT * FROM `tax_mst` WHERE `descr` = 'OTHER ADD'";
			        $get_tax_res   = mysqli_query($conn,$get_tax);
			        $get_tax_row   = mysqli_fetch_array($get_tax_res);
			        $ac_id         = $get_tax_row['account_group'];

			        $acc_name      = "SELECT * FROM `act_master` WHERE `id` = '".$ac_id."'";
			        $acc_name_res  = mysqli_query($conn,$acc_name);
			        $acc_name_row  = mysqli_fetch_array($acc_name_res);
			              
			        $sub_two = "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`,`mark_flag`) VALUES ('".$lhc_data['lhc_no']."','".$payment_date."','G','".$acc_name_row['act_code']."','".$act_ma_row['act_code']."','".$lhc_data['other_less_amount']."','','','P','0','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."','LHC PAYMENT OTHER')";
			        $sub_one_res = mysqli_query($conn,$sub_two);

					}
			   
			   }
	  			$activiti = "INSERT INTO `activities`(`type`, `user_id`, `data`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$type."','".$user_id."','".$data."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
				$acttivity_res = mysqli_query($conn,$activiti);
	  		}
	  	}

		if($cash > 0){
			$voucher_no = cpCode($conn);
			$get_cash = mysqli_query($conn, "SELECT * FROM `act_master` WHERE `id` = '".$cash_name."'");
			$get_cash_row = mysqli_fetch_assoc($get_cash);
			$cp = mysqli_query($conn, "INSERT INTO `payment`(`voucher_no`, `b_date`, `bank`, `account_name`, `amount`, `narration`, `lhc_no`, `total_amount`, `adjust_amnt`, `balance`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`, `flag`) VALUES ('".$voucher_no."','".$payment_date."','".$get_cash_row['id']."','".$act_ma_row['act_code']."','".$cash."','$remark_string','".$lhc_data['lhc_no']."','','','','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."','CP')");

			$gl = mysqli_query($conn, "INSERT INTO `general_ledger`(`voucher_no`, `voucher_serial`, `lhc_balance_serial`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `mark_flag`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$voucher_no."','".$lhc_data['lhc_no']."','".$code."','".$payment_date."','C','".$get_cash_row['act_code']."','".$act_ma_row['act_code']."','".$cash."','".$remark_string."','','LHC','LHC PAYMENT','','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");

		}

		if($pay_mode_amnt > 0){
			$master_bank = combine(getAllRecords('act_master'), 'id', 'ac_name');
			$bank_remark = 'LHC NO - '.$lhc_data['lhc_no'].' DT. '.$lhc_data['lhc_date'].' BY '.$payment_mode.'-'.$neft_number.'-'.@$master_bank[$bank_name];
			$voucher_no   = bpCode($conn);
			$get_bank = mysqli_query($conn, "SELECT * FROM `act_master` WHERE `id` = '".$bank_name."'");
			$get_bank_row = mysqli_fetch_assoc($get_bank);
			$bp = mysqli_query($conn, "INSERT INTO `payment`(`voucher_no`, `b_date`, `bank`, `account_name`, `amount`, `narration`, `lhc_no`, `total_amount`, `adjust_amnt`, `balance`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`, `flag`) VALUES ('".$voucher_no."','".$payment_date."','".$bank_name."','".$act_ma_row['act_code']."','".$pay_mode_amnt."','$remark_string','".$lhc_data['lhc_no']."','','','','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."','BP')");

			$gl = mysqli_query($conn, "INSERT INTO `general_ledger`(`voucher_no`, `voucher_serial`, `lhc_balance_serial`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `mark_flag`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$voucher_no."','".$lhc_data['lhc_no']."','".$code."','".$payment_date."','B','".$get_bank_row['act_code']."','".$act_ma_row['act_code']."','".$pay_mode_amnt."','".$remark_string."','','LHC','LHC PAYMENT','','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");
   	}

	}
	echo json_encode( array('response' => 1, 'msg' => 'DATA SAVED SUCCESSFULLY....!!!', 'alert' => 1, 'redirect' => 'index.php?page=lhc_balance_payment/list.php') );die();
}else{
   echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => 'index.php?page=lhc_balance_payment/list.php') );die();
}
?>