<?php 
error_reporting(0);

include("../config.php");
if(!empty($_POST)){
  $product           = $_POST['product'];
  if($_POST["id"] != ''){
    $code = mysqli_real_escape_string($conn, $_POST['code']);
  }else{
    $code = mysqli_real_escape_string($conn, strtoupper($prifix) . '/' . str_pad(($_POST['code']), 5, '0', STR_PAD_LEFT));
  }
  
 // $bill_date 			= mysqli_real_escape_string($conn, date('Y-m-d', strtotime($product['bill_date'])));	
  $commission_date    = date('Y-m-d', strtotime(mysqli_real_escape_string($conn, $_POST['commission_date'])));

  $vendor_id            = mysqli_real_escape_string($conn, $_POST['vendor_id']);
  

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

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

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

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

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

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

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

	$company_id         = $_SESSION['company_id'];
  	$branch_id          = $_SESSION['branch_id'];
	$created_id         = mysqli_real_escape_string($conn, $_SESSION['user_detail']['id']);
	$created_datetime   = risFormatDate($_POST['bill_date'], 'Y-m-d H:i:s');



  if($_POST["id"] == ''){
    $vendor        = "SELECT * FROM `vendor_master` WHERE `id` = '".$vendor_id."'";
    $ven_res       = mysqli_query($conn,$vendor);
    $ven_row       = mysqli_fetch_array($ven_res);

    $responses = [];
    foreach ($product as $product) {
      if( isset($product['bill_no']) && !empty($product['bill_no']) ) {
        $type            = 'commission_add';
        $user_id         = $_SESSION['user_detail']['id'];
        $data            = serialize($_POST);
        $bill_date  = date('Y-m-d', strtotime(mysqli_real_escape_string($conn, $product['bill_date'])));
        $insert = "INSERT INTO `mst_commission`(`code`, `commission_date`, `vendor_id`, `bill_no`, `bill_date`, `bill_from`, `bill_to`, `vehicle_no`, `bill_amount`, `commission_per`, `commission_amt`, `branch_id`, `company_id`, `created_id`, `created_datetime`) 
			VALUES ('".$code."', '".$commission_date."', '".$vendor_id."', '".$product['bill_no']."', '".$bill_date."', '".$product['bill_from']."', '".$product['bill_to']."', '".$product['vehicle_no']."', '".$product['bill_net_amount']."', '".$product['comission_per']."', '".$product['comission_amount']."', '".$branch_id."', '".$company_id."', '".$created_id."', '".$created_datetime."')";

			$result = mysqli_query($conn, $insert);
        if ($result) {
            $responses[] = ["status" => "success", "message" => "Record inserted successfully for bill number: " . $product['bill_no'], "code" => $code];
        } else {
            $responses[] = ["status" => "error", "message" => "Error inserting record for bill number: " . $product['bill_no'], "code" => $code];
        }
           
		echo $lr 	= "UPDATE `billing` SET `commission_flag`='Y' WHERE `invoice_no`='".$product['bill_no']."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
		$lr_res = mysqli_query($conn,$lr);
      }else {
        $responses[] = ["status" => "error", "message" => "bill_rec value not provided for bill number: " . $product['bill_no'], "code" => $code];
    }
    }
    echo json_encode($responses);

  }else{
    // update
    $temp_mr = "SELECT * FROM mr WHERE code = '".$code."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
    $temp_mr_res = mysqli_query($conn,$temp_mr);
    $codes      = array();
    $mr_details  = array();
    while ($temp_mr_row = mysqli_fetch_assoc($temp_mr_res)) {
      $codes[]     = $temp_mr_row['bill_no'];
      $mr_details[] = $temp_mr_row;
    }

    $lhch = "SELECT * FROM mr_payment_status WHERE code = '".$code."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
    $lhch_res = mysqli_query($conn,$lhch);
    $vou = array();
    while ($lhch_row = mysqli_fetch_array($lhch_res)) {
      $vou[] = $lhch_row['voucher_no'];
    }
    foreach ($vou as $key => $vou) {
      $gl    = "DELETE FROM `general_ledger` WHERE `voucher_no` = '".$vou."' AND `flag` = 'MR' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
      $gl_res  = mysqli_query($conn,$gl);

      $pay   = "DELETE FROM `payment` WHERE `voucher_no` = '".$vou."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
      $pay_res = mysqli_query($conn,$pay);
    }

    $mr_d         = "DELETE FROM `mr` WHERE `code` = '".$code."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
    $mr_d_res     = mysqli_query($conn,$mr_d);

    $mr_de        = "DELETE FROM `mr_details` WHERE `code` = '".$code."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
    $mr_de_res    = mysqli_query($conn,$mr_de);

    $gls          = "DELETE FROM `general_ledger` WHERE `voucher_no` = '".$code."' AND `flag` = 'MR' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
    $gls_res      = mysqli_query($conn,$gls);
    
    $vendor        = "SELECT * FROM `vendor_master` WHERE `id` = '".$vendor_id."'";
    $ven_res       = mysqli_query($conn,$vendor);
    $ven_row       = mysqli_fetch_array($ven_res);

    $act_msetr     = "SELECT * FROM `act_master` WHERE `ac_name` = '".$ven_row['name']."'";
    $act_ma_res    = mysqli_query($conn,$act_msetr);
    $act_ma_row    = mysqli_fetch_array($act_ma_res);
    $vendor_code   = $act_ma_row['act_code'];
    foreach ($product as $product) {
      if($product['bill_rec'] >0){
         $remark_string = $remark_string ." MR No. ".$code." MR Date ".$mr_date." Bill No. ".$product['bill_no']." Date ".$product['bill_date'];
      }
      if( isset($product['bill_no']) && !empty($product['bill_no']) ) {
        $type            = 'add_mr';
        $user_id         = $_SESSION['user_detail']['id'];
        $data            = serialize($_POST);
        $ind_add_per     = $product['bill_rec'] * 100 / $total_received;
        $ind_add_amount  = $other_add * $ind_add_per / 100;

        $ind_deduction_per     = $product['bill_rec'] * 100 / $total_received;
        $ind_deduction_amount  = $other_less * $ind_deduction_per / 100;

        $net_amount = $product['bill_rec'] - ($ind_add_amount + $ind_deduction_amount);
        $bill_date  = date('Y-m-d', strtotime(mysqli_real_escape_string($conn, $product['bill_date'])));

       $insert = "INSERT INTO `mr`(`code`, `vendor_id`, `mr_date`, `bill_no`, `bill_date`, `bill_from`, `bill_to`, `bill_amount`, `bill_rec`, `bill_pending`,`tds`, `cash_diss`, `late_delivery`, `claim`, `remarks`, `net_amt`, `ind_addition`,  `ind_deduction`, `payment_mode`, `cash_name`, `cash`, `payment_type`, `pay_mode_amnt`, `imps_number`, `rtgs_number`, `neft_number`, `cheque_number`, `cheque_date`, `bank_name`, `customer_bank_name`, `customer_bank_branch`, `customer_cheque_date`, `customer_cheque_no`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$code."','".$vendor_id."','".$mr_date."','".$product['bill_no']."','".$bill_date."','".$product['bill_from']."','".$product['bill_to']."','".$product['bill_amnt']."','".$product['bill_rec']."','".$product['bill_due']."','".$product['tds']."','".$product['cash_diss']."','".$product['late_delivery']."','".$product['claim']."','".$product['remarks']."','".$net_amount."','".$ind_add_amount."','".$ind_deduction_amount."','".$payment_mode."','".$cash_name."','".$cash."','".$payment_type."','".$pay_mode_amnt."','".$imps_number."','".$rtgs_number."','".$neft_number."','".$cheque_number."','".$cheque_date."','".$bank_name."','".$customer_bank_name."','".$customer_bank_branch."','".$customer_cheque_date."','".$customer_cheque_no."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
        
        $result = mysqli_query($conn,$insert);
        $last_id = mysqli_insert_id($conn);


        /*$select_trans      = "SELECT * FROM `trans` WHERE `voucher_no` = '".$product['bill_no']."' AND (flag = 'BILLING' OR flag = 'GENERAL SALES') AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
        $select_trans_res  = mysqli_query($conn,$select_trans);
        $select_trans_row  = mysqli_fetch_assoc($select_trans_res);
        $last_trans_adjust = $select_trans_row['adjust'];*/
        /*$total_adust       = $product['bill_rec'] + $last_trans_adjust;*/
        
        $mr = "SELECT * FROM `mr` WHERE `bill_no` = '".$product['bill_no']."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
        $mr_res = mysqli_query($conn,$mr);
        $last_bill_rec = 0;
        while ($mr_row = mysqli_fetch_assoc($mr_res)) {
          $last_bill_rec += $mr_row['bill_rec'];
        }


        /*-------------------*/
        if( isset($product['tds']) && !empty($product['tds']) ) {
          $master_accounts = combine(getAllRecords('act_master'), 'id', 'act_code');
          $tds       = mysqli_query($conn, "SELECT * FROM account_control WHERE descr = 'TDS'");
          $tds_rows  = mysqli_fetch_assoc($tds);
          $tds_code  = @$master_accounts[$tds_rows['account_group']];
          
          
          $tds_am = mysqli_query($conn, "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 ('".$code."','".$mr_date."','MR','".$vendor_code."','".$tds_code."','".$product['tds']."','','','','".$product['tds']."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");
        }

        if( isset($product['cash_diss']) && !empty($product['cash_diss']) ) {
          $master_accounts = combine(getAllRecords('act_master'), 'id', 'act_code');
          $cash_diss       = mysqli_query($conn, "SELECT * FROM account_control WHERE descr = 'CASH DISCOUNT'");
          $cash_diss_rows  = mysqli_fetch_assoc($cash_diss);
          $cash_diss_code  = @$master_accounts[$cash_diss_rows['account_group']];
        
        
          $cash_diss_am = mysqli_query($conn, "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 ('".$code."','".$mr_date."','MR','".$vendor_code."','".$cash_diss_code."','".$product['cash_diss']."','','','','".$product['cash_diss']."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");
        }

        if( isset($product['late_delivery']) && !empty($product['late_delivery']) ) {
          $master_accounts = combine(getAllRecords('act_master'), 'id', 'act_code');
          $late_delivery       = mysqli_query($conn, "SELECT * FROM account_control WHERE descr = 'LATE DELIVERY'");
          $late_delivery_rows  = mysqli_fetch_assoc($late_delivery);
          $late_delivery_code  = @$master_accounts[$late_delivery_rows['account_group']];
          $cash_diss_am = mysqli_query($conn, "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 ('".$code."','".$mr_date."','MR','".$vendor_code."','".$late_delivery_code."','".$product['late_delivery']."','','','',
            '".$product['late_delivery']."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");
        }

        if( isset($product['claim']) && !empty($product['claim']) ) {
          $master_accounts = combine(getAllRecords('act_master'), 'id', 'act_code');
          $claim       = mysqli_query($conn, "SELECT * FROM account_control WHERE descr = 'LATE DELIVERY'");
          $claim_rows  = mysqli_fetch_assoc($claim);
          $claim_code  = @$master_accounts[$claim_rows['account_group']];
          $cash_diss_am = mysqli_query($conn, "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 ('".$code."','".$mr_date."','MR','".$vendor_code."','".$claim_code."','".$product['claim']."','','','','".$product['claim']."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");
        }

        $total_less =  $total_late_delivery + $total_claim;
        
        //$total_bal =  $total_late_delivery + $total_claim + $total_tds + $total_cash_diss;
        
        //$total_tds + $total_cash_diss

        $subs = $_POST['sub'];
        $is_sub_blank = TRUE;
        if(count($subs) > 0) {
          
          foreach ($subs as $sub_key => $sub) {
            if($sub['tax_mst_id'] > 0 && $sub['amount'] > 0 ) {
              $is_sub_blank     = FALSE;
              $tax              = $product['bill_rec'] * $sub['tax_per'] / 100;
              $total_tds        = $tax + $select_trans_row['tds'];      
              $bill_wise_basic  = $product['bill_rec'] + $ind_add_amount - $ind_deduction_amount;
              $last_trans_adjs  = $select_trans_row['adjust'] + $bill_wise_basic;
              $last_add         = abs($select_trans_row['other_add'] + $ind_add_amount);
              $last_less        = abs($select_trans_row['other_less'] + $ind_deduction_amount);
              $balance          = $select_trans_row['balance'] - $product['bill_rec'] - $total_late_delivery - $total_claim - $total_tds - $total_cash_diss;
            }
          }
        }

        if($is_sub_blank) {
          //second updates
          $bill_wise_basic  = $product['bill_rec'] + $ind_add_amount - $ind_deduction_amount;
          $last_trans_adjs  = $select_trans_row['adjust'] + $bill_wise_basic;
          $balance          = $product['bill_due'];

          $update = "UPDATE `trans` SET `adjust`='".$last_trans_adjs."',`balance`='".$balance."',`discount`='0',`tds`='".$product['tds']."',`other_less`='".$tless."',`updated_id`='".$updated_id."',`updated_datetime`='".$updated_datetime."' WHERE voucher_no = '".$product['bill_no']."' AND (flag = 'BILLING' OR flag = 'GENERAL SALES') AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
          $update_res = mysqli_query($conn,$update);

          $trans_details = "UPDATE `trans_details` SET `adjust`='".$last_trans_adjs."',`balance`='".$balance."',`discount`='0',`tds`='".$product['tds']."',`other_less`='".$tless."' WHERE voucher_no = '".$product['bill_no']."' AND (flag = 'BILLING' OR flag = 'GENERAL SALES') AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
          $trans_details_res = mysqli_query($conn,$trans_details);

        } else {
          $bill_wise_basic  = $product['bill_rec'] + $ind_add_amount - $ind_deduction_amount;
          $last_trans_adjs  = $select_trans_row['adjust'] + $bill_wise_basic;
          $balance          = $select_trans_row['balance'] - $product['bill_rec'] - $total_late_delivery - $total_claim - $total_tds - $total_cash_diss;
          
          $update = "UPDATE `trans` SET `adjust`='".$last_trans_adjs."',`balance`='".$balance."',`discount`='0',`tds`='".$product['tds']."',`other_less`='".$tless."',`updated_id`='".$updated_id."',`updated_datetime`='".$updated_datetime."' WHERE voucher_no = '".$product['bill_no']."' AND (flag = 'BILLING' OR flag = 'GENERAL SALES') AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
          $update_res = mysqli_query($conn,$update);

          $trans_details = "UPDATE `trans_details` SET `adjust`='".$last_trans_adjs."',`balance`='".$balance."',`discount`='0',`tds`='".$product['tds']."',`other_less`='".$tless."' WHERE voucher_no = '".$product['bill_no']."' AND (flag = 'BILLING' OR flag = 'GENERAL SALES') AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
          $trans_details_res = mysqli_query($conn,$trans_details);
        }
       $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);
      }

      
    }
    
    
   
     // ADDITION
    $adds = $_POST['add'];
    if(count($adds) > 0) {
      foreach ($adds as $add_key => $add) {
        if($add['tax_mst_id'] > 0 && $add['amount'] > 0 ) {
          $mr_details = "INSERT INTO `mr_details`(`code`, `type`, `key`, `tax_mst_id`, `amount`) VALUES ('".$code."','add','". $add_key ."','".$add['tax_mst_id']."','".$add['amount']."')";
          $mr_del_res = mysqli_query($conn,$mr_details);

          // GENERAL LEDGER
          $get_tax       = "SELECT * FROM `tax_mst` WHERE `id` = '".$add['tax_mst_id']."'";
          $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`) VALUES ('".$code."','".$mr_date."','MR','".$acc_name_row['act_code']."','".$vendor_code."','".$add['amount']."','','','','".$add['amount']."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
          $add_one_res = mysqli_query($conn,$add_two);
        }
      }
    }

    // DEDUCTION
    $subs = $_POST['sub'];
    $tds_amnt;
    if(count($subs) > 0) {
      foreach ($subs as $sub_key => $sub) {
          if($sub['tax_mst_id'] > 0 && $sub['amount'] > 0 ) {
          $mr_details = "INSERT INTO `mr_details`(`code`, `type`, `key`, `percentage`, `tax_mst_id`, `amount`, `branch_id`, `company_id`) VALUES ('".$code."','sub','". $sub_key ."','".$sub['tax_per']."','".$sub['tax_mst_id']."','".$sub['amount']."','".$_SESSION['branch_id']."','".$_SESSION['company_id']."')";
          $mr_del_res = mysqli_query($conn,$mr_details);

          $tax     = "select * from tax_mst WHERE sign = '-' AND FIND_IN_SET('MR', module)";
          $tax_res = mysqli_query($conn,$tax);
          while ($tax_row = mysqli_fetch_array($tax_res)) {
            $tax_name = $tax_row['id'];
            $account  = $tax_row['account_group'];

            if($sub['tax_mst_id'] == $tax_name){
              $tds_amnt    = $sub['amount'];
              $act_tds     = "select * from act_master where id = '".$account."'";
              $act_tds_res = mysqli_query($conn,$act_tds);
              $act_tds_row = mysqli_fetch_array($act_tds_res);

              $tds_am = "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 ('".$code."','".$mr_date."','MR','".$vendor_code."','".$act_tds_row['act_code']."','".$tds_amnt."','','','','".$tds_amnt."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
              $tds_am_res = mysqli_query($conn,$tds_am);

            }else{
              $tds_amnt = '';
            }
          }
        }
      }
    }
    $tds_amnt;

    //CASH ON HAND
    if(in_array("C", $_POST['payment_type'])){
      $voucher_no = crCode($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."','".$mr_date."','".$get_cash_row['id']."','".$vendor_code."','".$_POST['cash']."','','".$code."','".$_POST['cash']."','','','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."','CR')";
      $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."','".$mr_date."','MR','".$vendor_code."','".$get_cash_row['act_code']."','".$cash."','','','','".$cash."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
      $gl_res = mysqli_query($conn,$gl);

      $mr_cash     = "INSERT INTO `mr_payment_status`(`code`, `payment_type`, `amount`, `voucher_no`, `branch_id`, `company_id`) VALUES ('".$code."','CASH','".$_POST['cash']."','".$voucher_no."','".$_SESSION['branch_id']."','".$_SESSION['company_id']."')";
      $mr_cash_res = mysqli_query($conn,$mr_cash);

    }

    //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   = brCode($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."','".$mr_date."','".$bank_name."','".$vendor_code."','".$pay_mode_amnt."','','".$code."','".$pay_mode_amnt."','','','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."','BR')";

      $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."','".$mr_date."','MR','".$vendor_code."','".$get_bank_row['act_code']."','".$pay_mode_amnt."','".$remark_string."','','','".$pay_mode_amnt."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
      $gl_res = mysqli_query($conn,$gl);

      $mr_bank     = "INSERT INTO `mr_payment_status`(`code`, `payment_type`, `amount`, `voucher_no`, `branch_id`, `company_id`) VALUES ('".$code."','BANK','".$pay_mode_amnt."','".$voucher_no."','".$_SESSION['branch_id']."','".$_SESSION['company_id']."')";
      $mr_bank_res = mysqli_query($conn,$mr_bank);  
    }

    
    echo json_encode( array('response' => 1, 'msg' => 'MR UPDATED SUCCESSFULLY', 'alert' => 1, 'redirect' => 'index.php?page=commssion_Flag/list.php') );die();

  }
  
}else{
  echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => 'index.php?page=commssion_Flag/add.php') );die();
}
?>