<?php 
include("../config.php");
if(!empty($_POST)){
	//$product             = $_POST['product'];
  $lhc_no              = mysqli_real_escape_string($conn, $_POST['lhc_no']);
  $l_date              = mysqli_real_escape_string($conn, date('Y-m-d', strtotime($_POST['lhc_date'])));
  $truck_no            = mysqli_real_escape_string($conn, $_POST['truck_no']);
  $from_place          = mysqli_real_escape_string($conn, $_POST['from_place']);
  $to_place            = mysqli_real_escape_string($conn, $_POST['to_place']);
  $agent               = mysqli_real_escape_string($conn, $_POST['agent']);
  $lorry_type          = mysqli_real_escape_string($conn, $_POST['lorry_type']);
  $chasis_no           = mysqli_real_escape_string($conn, $_POST['chasis_no']);
  $engine_no           = mysqli_real_escape_string($conn, $_POST['engine_no']);
  $permit_no           = mysqli_real_escape_string($conn, $_POST['permit_no']);
  $p_from              = mysqli_real_escape_string($conn, $_POST['p_from']);
  $up_to               = mysqli_real_escape_string($conn, $_POST['up_to']);
  $insurance_no        = mysqli_real_escape_string($conn, $_POST['insurance_no']);
  $branch              = mysqli_real_escape_string($conn, $_POST['branch']);
  $policy_no           = mysqli_real_escape_string($conn, $_POST['policy_no']);
  $p_up_to             = mysqli_real_escape_string($conn, $_POST['p_up_to']);
  $d_name              = mysqli_real_escape_string($conn, $_POST['d_name']);
  $d_address           = mysqli_real_escape_string($conn, $_POST['d_address']);
  $lic_no              = mysqli_real_escape_string($conn, $_POST['lic_no']);
  $date                = date('Y-m-d', strtotime(mysqli_real_escape_string($conn, $_POST['date'])));
  $issued_from         = mysqli_real_escape_string($conn, $_POST['issued_from']);
  $mobile              = mysqli_real_escape_string($conn, $_POST['mobile']);
  $owner_name          = mysqli_real_escape_string($conn, $_POST['owner_name']);
  $owner_address       = mysqli_real_escape_string($conn, $_POST['owner_address']);
  $pan_no              = mysqli_real_escape_string($conn, $_POST['pan_no']);
  //$disel_amnt          = mysqli_real_escape_string($conn, $_POST['disel_amnt']);
  $o_mobile            = mysqli_real_escape_string($conn, $_POST['o_mobile']);
  $lorry_hire          = mysqli_real_escape_string($conn, $_POST['lorry_hire']);
 	$balance             = mysqli_real_escape_string($conn, $_POST['balance']);
  $net_amnt            = mysqli_real_escape_string($conn, $_POST['net_amnt']);
  $payment_mode        = mysqli_real_escape_string($conn, $_POST['payment_mode']);
  $payment_to          = mysqli_real_escape_string($conn, $_POST['payment_to']);
  $cash                = mysqli_real_escape_string($conn, $_POST['cash']);
  $cash_name           = mysqli_real_escape_string($conn, $_POST['cash_name']);
  $payment_type        = mysqli_real_escape_string($conn, implode(",", $_POST['payment_type']));
  $adj_amnt            = mysqli_real_escape_string($conn, $_POST['adj_amnt']);
  $last_adj_amnt       = mysqli_real_escape_string($conn, $_POST['last_adj_amnt']);
  $pay_mode_amnt       = mysqli_real_escape_string($conn, $_POST['pay_mode_amnt']);
  $last_bank_amnt      = mysqli_real_escape_string($conn, $_POST['last_bank_amnt']);
  $last_cash_amnt      = mysqli_real_escape_string($conn, $_POST['last_cash_amnt']);
  
  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         = '';
  }  

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

  $adv_amnt            = mysqli_real_escape_string($conn, $_POST['adv_amnt']);
  //$other_add           = mysqli_real_escape_string($conn, $_POST['other_add']);
  //$other_less          = mysqli_real_escape_string($conn, $_POST['other_less']);
  //$total_del           = mysqli_real_escape_string($conn, $_POST['total_del']);
  $bank_name           = mysqli_real_escape_string($conn, $_POST['bank_name']);
  $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');
  $p_date              = date('Y-m-d H:i:s');
  $updated_id          = $_SESSION['user_detail']['id'];
  $new_adj_amount      = $last_adj_amnt + $adj_amnt;
  $new_cash_amnt       = $last_cash_amnt + $cash;
  $new_bank_amnt       = $last_bank_amnt + $pay_mode_amnt;

if($_POST["id"] != ''){
  if($payment_to == 'AGENT'){
    $act_msetr  = "SELECT * FROM `act_master` WHERE `ac_name` = '".$agent."'";
    $act_ma_res = mysqli_query($conn,$act_msetr);
    $act_ma_row = mysqli_fetch_array($act_ma_res);
  }else{
    $tr_mst = "SELECT * FROM `truck_mst` WHERE `id` = '".$owner_name."'";
    $tr_mst_res = mysqli_query($conn,$tr_mst);
    $tr_mst_row = mysqli_fetch_array($tr_mst_res);

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

  //UPDATE LHC
  $type                = 'update_lhc_balance';
  $user_id             = $_SESSION['user_detail']['id'];
  $data                = serialize($_POST);

  $update = "UPDATE `lhc` SET `balance`='".$balance."' WHERE `lhc_no`='".$lhc_no."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";

  $result = mysqli_query($conn,$update);
 
  // LHC BALANCE HISTORY

  $lbh = "INSERT INTO `lhc_balance_history`(`p_date`, `lhc_no`, `lorry_hire`, `adv_amnt`, `balance`, `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`, `branch_id`, `company_id`) VALUES ('".$p_date."','".$lhc_no."','".$lorry_hire."','".$adv_amnt."','".$balance."','".$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."','".$branch_id."','".$company_id."')";
  $lbh_res = mysqli_query($conn,$lbh);

  //CASH ON HAND
  if(in_array("C", $_POST['payment_type'])){
    $voucher_no = cpCode($conn);
    $get_cash     = "SELECT * FROM `act_master` WHERE `id` = '".$cash_name."'";
    $get_cash_res = mysqli_query($conn,$get_cash);
    $get_cash_row = mysqli_fetch_array($get_cash_res);

    // CASH PAYMENT INSERT 
    $cp = "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."','','".$lhc_no."','','','','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."','CP')";
    $cp_res = mysqli_query($conn,$cp);

    $gl = "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`) VALUES ('".$voucher_no."','".$payment_date."','LHC','".$get_cash_row['act_code']."','".$act_ma_row['act_code']."','".$cash."','','','LHC BALANCE','".$net_amnt."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
    $gl_res = mysqli_query($conn,$gl);

    $lhc_hi     = "INSERT INTO `lhc_payment_status`(`lhc_no`, `payment_type`, `amount`, `voucher_no`, `flag`, `branch_id`, `company_id`) VALUES ('".$lhc_no."','CASH','".$_POST['cash']."','".$voucher_no."','BALANCE','".$branch_id."','".$company_id."')";
    $lhc_hi_res = mysqli_query($conn,$lhc_hi);
    
  }
  //BANK
  if(in_array("B", $_POST['payment_type'])){
    $get_bank     = "SELECT * FROM `act_master` WHERE `id` = '".$bank_name."'";
    $get_bank_res = mysqli_query($conn,$get_bank);
    $get_bank_row = mysqli_fetch_array($get_bank_res);
    $voucher_no   = bpCode($conn);
    // PAYMENT TABLE 
    $bp = "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."','','".$lhc_no."','','','','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."','BP')";

    $bp_res = mysqli_query($conn,$bp);


    $gl = "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`) VALUES ('".$voucher_no."','".$payment_date."','LHC','".$get_bank_row['act_code']."','".$act_ma_row['act_code']."','".$pay_mode_amnt."','','','LHC BALANCE','".$net_amnt."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
    $gl_res = mysqli_query($conn,$gl);

    $lhc_hi     = "INSERT INTO `lhc_payment_status`(`lhc_no`, `payment_type`, `amount`, `voucher_no`, `flag`, `branch_id`, `company_id`) VALUES ('".$lhc_no."','BANK','".$pay_mode_amnt."','".$voucher_no."','BALANCE','".$branch_id."','".$company_id."')";
    $lhc_hi_res = mysqli_query($conn,$lhc_hi);  
  }

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

  //TRANS LAST ID
    
  $last_id = mysqli_insert_id($conn);

  $trans_det ="UPDATE `trans_details` SET `adjust`='".$new_adj_amount."',`balance`='".$balance."' WHERE `voucher_no` = '".$lhc_no."' AND flag = 'LHC' AND `branch_id`='".$branch_id."' AND `company_id`='".$company_id."'";
  $trans_det_res = mysqli_query($conn,$trans_det);

  // activity log
  $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);

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