<?php
if (isset($_POST["action"])) {
	$output = '';
	require('../config/dbcon.php');
	if ($_POST["action"] == "Add") {
	    $id = mysqli_escape_string($connect, $_POST["id"]);
	    $query = "select srno from control_mst where description='SRNO'";
		$rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
		while ($row = mysqli_fetch_assoc($rs)) {
			$srno = $row['srno'];
			$srnoupd = $srno+1;
		}
		$query2 = "select srno from control_mst where description='BILL'";
		$rs2 = mysqli_query($connect, $query2) or die("Cannot execute query: $query\n");
		while ($row2 = mysqli_fetch_assoc($rs2)) {
			$billno = $row2['srno'];
			$billupd = $billno+1;
		}
		$orgDate = $_POST['date_one'];  
		$newDate = date("Y-m-d H:i:s", strtotime($orgDate));
		$inv_date=$newDate;
		$date_one = $inv_date;
		$party_name = mysqli_escape_string($connect, $_POST["party_name"]);
		$party_address_one = mysqli_escape_string($connect, $_POST["party_address_one"]);
		$party_address_two = mysqli_escape_string($connect, $_POST["party_address_two"]);
		$party_place = mysqli_escape_string($connect, $_POST["party_place"]);
		$party_contact_person = mysqli_escape_string($connect, $_POST["party_contact_person"]);
		$party_phone_number = mysqli_escape_string($connect, $_POST["party_phone_number"]);
		$party_mobile_number = mysqli_escape_string($connect, $_POST["party_mobile_number"]);
		$party_gst_number = mysqli_escape_string($connect, $_POST["party_gst_number"]);
		$party_pan_number = mysqli_escape_string($connect, $_POST["party_pan_number"]);
		$party_aadhar_number = mysqli_escape_string($connect, $_POST["party_aadhar_number"]);
		$party_payment_terms = mysqli_escape_string($connect, $_POST["party_payment_terms"]);
		$party_goods = mysqli_escape_string($connect, $_POST["party_goods"]);
		$party_remarks = mysqli_escape_string($connect, $_POST["party_remarks"]);
		$inv_number = mysqli_escape_string($connect, $_POST["inv_number"]);
		$lchno = mysqli_escape_string($connect, $_POST["lchno"]);
		$lrno = mysqli_escape_string($connect, $_POST["lrno"]);
		$truck_number = mysqli_escape_string($connect, $_POST["truck_number"]);
		$truck_owner_name = mysqli_escape_string($connect, $_POST["truck_owner_name"]);
		$truck_address_one = mysqli_escape_string($connect, $_POST["truck_address_one"]);
		$truck_address_two = mysqli_escape_string($connect, $_POST["truck_address_two"]);
		$truck_place = mysqli_escape_string($connect, $_POST["truck_place"]);
		$truck_mobile_number = mysqli_escape_string($connect, $_POST["truck_mobile_number"]);
		$truck_pan_number = mysqli_escape_string($connect, $_POST["truck_pan_number"]);
		$truck_bank_name = mysqli_escape_string($connect, $_POST["truck_bank_name"]);
		$truck_bank_acc_number = mysqli_escape_string($connect, $_POST["truck_bank_acc_number"]);
		$truck_bank_branch = mysqli_escape_string($connect, $_POST["truck_bank_branch"]);
		$truck_ifsc_code = mysqli_escape_string($connect, $_POST["truck_ifsc_code"]);
		$truck_aadhar_no = mysqli_escape_string($connect, $_POST["truck_aadhar_no"]);
		$truck_remarks = mysqli_escape_string($connect, $_POST["truck_remarks"]);
		$placefrom = mysqli_escape_string($connect, $_POST["placefrom"]);
		$destination = mysqli_escape_string($connect, $_POST["destination"]);
		$supp_name = mysqli_escape_string($connect, $_POST["supp_name"]);
		$supp_address_one = mysqli_escape_string($connect, $_POST["supp_address_one"]);
		$supp_address_two = mysqli_escape_string($connect, $_POST["supp_address_two"]);
		$supp_place = mysqli_escape_string($connect, $_POST["supp_place"]);
		$supp_contact_person = mysqli_escape_string($connect, $_POST["supp_contact_person"]);
		$supp_mobile_number = mysqli_escape_string($connect, $_POST["supp_mobile_number"]);
		$supp_pan_number = mysqli_escape_string($connect, $_POST["supp_pan_number"]);
		$supp_bank_name = mysqli_escape_string($connect, $_POST["supp_bank_name"]);
		$supp_bank_acc_number = mysqli_escape_string($connect, $_POST["supp_bank_acc_number"]);
		$supp_bank_branch = mysqli_escape_string($connect, $_POST["supp_bank_branch"]);
		$supp_ifsc_code = mysqli_escape_string($connect, $_POST["supp_ifsc_code"]);
		$supp_supp_aadhar_no = mysqli_escape_string($connect, $_POST["supp_supp_aadhar_no"]);
		$supp_remarks = mysqli_escape_string($connect, $_POST["supp_remarks"]);
		$weight = mysqli_escape_string($connect, $_POST["weight"]);
		$booking_rate = mysqli_escape_string($connect, $_POST["booking_rate"]);
		$lorry_freight = mysqli_escape_string($connect, $_POST["lorry_freight"]);
		$advance = mysqli_escape_string($connect, $_POST["advance"]);
		$cash_advance = mysqli_escape_string($connect, $_POST["cash_advance"]);
		$diesel = mysqli_escape_string($connect, $_POST["diesel"]);
		$neft_amount = mysqli_escape_string($connect, $_POST["neft_amount"]);
		$neft_date = mysqli_escape_string($connect, $_POST["neft_date"]);
		$unloading_charges = mysqli_escape_string($connect, $_POST["unloading_charges"]);
		$others = mysqli_escape_string($connect, $_POST["others"]);
		$extra = mysqli_escape_string($connect, $_POST["extra"]);
		$balance = mysqli_escape_string($connect, $_POST["balance"]);
		$total_paid = mysqli_escape_string($connect, $_POST["total_paid"]);
		$bal_paid_date = mysqli_escape_string($connect, $_POST["bal_paid_date"]);
		$delivery_date = mysqli_escape_string($connect, $_POST["delivery_date"]);
		$acc_rec_date = mysqli_escape_string($connect, $_POST["acc_rec_date"]);
		$driver_name = mysqli_escape_string($connect, $_POST["driver_name"]);
		$driver_mob_no = mysqli_escape_string($connect, $_POST["driver_mob_no"]);
		$mamul_charges = mysqli_escape_string($connect, $_POST["mamul_charges"]);
		$profit = mysqli_escape_string($connect, $_POST["profit"]);
		$remarks = mysqli_escape_string($connect, $_POST["remarks"]);
		$status_one = mysqli_escape_string($connect, $_POST["status_one"]);
		$orgDate1 = $_POST['bill_date'];  
		$newDate1 = date("Y-m-d H:i:s", strtotime($orgDate1));
		$inv_date1=$newDate1;
		$bill_date = $inv_date1;
		$rupeesinwords = mysqli_escape_string($connect, $_POST["rupeesinwords"]);
		
		$payment_amount = '0.00';
		$payment_neftref = '-';
		$payment_neftdate = '-';
		$payment_status = 'PENDING';
		$receipt_amount = '0.00';
		$receipt_neftref = '-';
		$receipt_neftdate = '-';
		$receipt_status = 'PENDING';
		$receipt_number = '-';
		$receipt_date = '-';
		$payment_number = '-';
		$payment_date = '-';
        if($status_one == 'CLEARED'){
		    $query = "insert into loading_mst(id,date_one,party_name,party_address_one,party_address_two,party_place,party_contact_person,party_phone_number,party_mobile_number,party_gst_number,party_pan_number,party_aadhar_number,party_payment_terms,party_goods,party_remarks,inv_number,lchno,lrno,truck_number,truck_owner_name,truck_address_one,truck_address_two,truck_place,truck_mobile_number,truck_pan_number,truck_bank_name,truck_bank_acc_number,truck_bank_branch,truck_ifsc_code,truck_aadhar_no,truck_remarks,destination,supp_name,supp_address_one,supp_address_two,supp_place,supp_contact_person,supp_mobile_number,supp_pan_number,supp_bank_name,supp_bank_acc_number,supp_bank_branch,supp_ifsc_code,supp_supp_aadhar_no,supp_remarks,weight,booking_rate,lorry_freight,advance,cash_advance,diesel,neft_amount,neft_date,unloading_charges,others,extra,balance,total_paid,bal_paid_date,delivery_date,acc_rec_date,profit,driver_name,driver_mob_no,remarks,status_one,bill_date,placefrom,rupeesinwords,mamul_charges) Values ('" . $srno . "','" . $date_one . "','" . $party_name . "','" . $party_address_one . "','" . $party_address_two . "','" . $party_place . "','" . $party_contact_person . "','" . $party_phone_number . "','" . $party_mobile_number . "','" . $party_gst_number . "','" . $party_pan_number . "','" . $party_aadhar_number . "','" . $party_payment_terms . "','" . $party_goods . "','" . $party_remarks . "','" . $inv_number . "','" . $lchno . "','" . $lrno . "','" . $truck_number . "','" . $truck_owner_name . "','" . $truck_address_one . "','" . $truck_address_two . "','" . $truck_place . "','" . $truck_mobile_number . "','" . $truck_pan_number . "','" . $truck_bank_name . "','" . $truck_bank_acc_number . "','" . $truck_bank_branch . "','" . $truck_ifsc_code . "','" . $truck_aadhar_no . "','" . $truck_remarks . "','" . $destination . "','" . $supp_name . "','" . $supp_address_one . "','" . $supp_address_two . "','" . $supp_place . "','" . $supp_contact_person . "','" . $supp_mobile_number . "','" . $supp_pan_number . "','" . $supp_bank_name . "','" . $supp_bank_acc_number . "','" . $supp_bank_branch . "','" . $supp_ifsc_code . "','" . $supp_supp_aadhar_no . "','" . $supp_remarks . "','" . $weight . "','" . $booking_rate . "','" . $lorry_freight . "','" . $advance . "','" . $cash_advance . "','" . $diesel . "','" . $neft_amount . "','" . $neft_date . "','" . $unloading_charges . "','" . $others . "','" . $extra . "','" . $balance . "','" . $total_paid . "','" . $bal_paid_date . "','" . $delivery_date . "','" . $acc_rec_date . "','" . $profit . "','" . $driver_name . "','" . $driver_mob_no . "','" . $remarks . "','" . $status_one. "','" . $bill_date . "','" . $placefrom . "','" . $rupeesinwords . "','" . $mamul_charges . "')";
        }
        if($status_one == 'PENDING'){
		    $query = "insert into loading_mst(id,date_one,party_name,party_address_one,party_address_two,party_place,party_contact_person,party_phone_number,party_mobile_number,party_gst_number,party_pan_number,party_aadhar_number,party_payment_terms,party_goods,party_remarks,inv_number,lchno,lrno,truck_number,truck_owner_name,truck_address_one,truck_address_two,truck_place,truck_mobile_number,truck_pan_number,truck_bank_name,truck_bank_acc_number,truck_bank_branch,truck_ifsc_code,truck_aadhar_no,truck_remarks,destination,supp_name,supp_address_one,supp_address_two,supp_place,supp_contact_person,supp_mobile_number,supp_pan_number,supp_bank_name,supp_bank_acc_number,supp_bank_branch,supp_ifsc_code,supp_supp_aadhar_no,supp_remarks,weight,booking_rate,lorry_freight,advance,cash_advance,diesel,neft_amount,neft_date,unloading_charges,others,extra,balance,total_paid,bal_paid_date,delivery_date,acc_rec_date,profit,driver_name,driver_mob_no,remarks,status_one,bill_date,placefrom,rupeesinwords,mamul_charges) Values ('" . $srno . "','" . $date_one . "','" . $party_name . "','" . $party_address_one . "','" . $party_address_two . "','" . $party_place . "','" . $party_contact_person . "','" . $party_phone_number . "','" . $party_mobile_number . "','" . $party_gst_number . "','" . $party_pan_number . "','" . $party_aadhar_number . "','" . $party_payment_terms . "','" . $party_goods . "','" . $party_remarks . "','" . $inv_number . "','" . $lchno . "','" . $lrno . "','" . $truck_number . "','" . $truck_owner_name . "','" . $truck_address_one . "','" . $truck_address_two . "','" . $truck_place . "','" . $truck_mobile_number . "','" . $truck_pan_number . "','" . $truck_bank_name . "','" . $truck_bank_acc_number . "','" . $truck_bank_branch . "','" . $truck_ifsc_code . "','" . $truck_aadhar_no . "','" . $truck_remarks . "','" . $destination . "','" . $supp_name . "','" . $supp_address_one . "','" . $supp_address_two . "','" . $supp_place . "','" . $supp_contact_person . "','" . $supp_mobile_number . "','" . $supp_pan_number . "','" . $supp_bank_name . "','" . $supp_bank_acc_number . "','" . $supp_bank_branch . "','" . $supp_ifsc_code . "','" . $supp_supp_aadhar_no . "','" . $supp_remarks . "','" . $weight . "','" . $booking_rate . "','" . $lorry_freight . "','" . $advance . "','" . $cash_advance . "','" . $diesel . "','" . $neft_amount . "','" . $neft_date . "','" . $unloading_charges . "','" . $others . "','" . $extra . "','" . $balance . "','" . $total_paid . "','" . $bal_paid_date . "','" . $delivery_date . "','" . $acc_rec_date . "','" . $profit . "','" . $driver_name . "','" . $driver_mob_no . "','" . $remarks . "','" . $status_one. "','" . $bill_date . "','" . $placefrom . "','" . $rupeesinwords . "','" . $mamul_charges . "')";
        }
		if (mysqli_query($connect, $query)) {
		    
		    if($status_one == 'CLEARED'){
		        $delete = "Delete from bill_mst where bill_number = '" . $billno . "'";
		        if (mysqli_query($connect, $delete)) {
    		        $query1 = "insert into bill_mst(id,bill_number,date_one,party_name,party_address_one,party_address_two,party_place,party_contact_person,party_phone_number,party_mobile_number,party_gst_number,party_pan_number,party_aadhar_number,party_payment_terms,party_goods,party_remarks,inv_number,lchno,lrno,truck_number,truck_owner_name,truck_address_one,truck_address_two,truck_place,truck_mobile_number,truck_pan_number,truck_bank_name,truck_bank_acc_number,truck_bank_branch,truck_ifsc_code,truck_aadhar_no,truck_remarks,destination,supp_name,supp_address_one,supp_address_two,supp_place,supp_contact_person,supp_mobile_number,supp_pan_number,supp_bank_name,supp_bank_acc_number,supp_bank_branch,supp_ifsc_code,supp_supp_aadhar_no,supp_remarks,weight,booking_rate,lorry_freight,advance,cash_advance,diesel,neft_amount,neft_date,unloading_charges,others,extra,balance,total_paid,bal_paid_date,delivery_date,acc_rec_date,profit,driver_name,driver_mob_no,remarks,status_one,bill_date,placefrom,rupeesinwords,payment_amount,payment_neftref,payment_neftdate,payment_status,receipt_amount,receipt_neftref,receipt_neftdate,receipt_status,receipt_number,receipt_date,payment_number,payment_date,mamul_charges) Values ('" . $id . "','" . $billno . "','" . $date_one . "','" . $party_name . "','" . $party_address_one . "','" . $party_address_two . "','" . $party_place . "','" . $party_contact_person . "','" . $party_phone_number . "','" . $party_mobile_number . "','" . $party_gst_number . "','" . $party_pan_number . "','" . $party_aadhar_number . "','" . $party_payment_terms . "','" . $party_goods . "','" . $party_remarks . "','" . $inv_number . "','" . $lchno . "','" . $lrno . "','" . $truck_number . "','" . $truck_owner_name . "','" . $truck_address_one . "','" . $truck_address_two . "','" . $truck_place . "','" . $truck_mobile_number . "','" . $truck_pan_number . "','" . $truck_bank_name . "','" . $truck_bank_acc_number . "','" . $truck_bank_branch . "','" . $truck_ifsc_code . "','" . $truck_aadhar_no . "','" . $truck_remarks . "','" . $destination . "','" . $supp_name . "','" . $supp_address_one . "','" . $supp_address_two . "','" . $supp_place . "','" . $supp_contact_person . "','" . $supp_mobile_number . "','" . $supp_pan_number . "','" . $supp_bank_name . "','" . $supp_bank_acc_number . "','" . $supp_bank_branch . "','" . $supp_ifsc_code . "','" . $supp_supp_aadhar_no . "','" . $supp_remarks . "','" . $weight . "','" . $booking_rate . "','" . $lorry_freight . "','" . $advance . "','" . $cash_advance . "','" . $diesel . "','" . $neft_amount . "','" . $neft_date . "','" . $unloading_charges . "','" . $others . "','" . $extra . "','" . $balance . "','" . $total_paid . "','" . $bal_paid_date . "','" . $delivery_date . "','" . $acc_rec_date . "','" . $profit . "','" . $driver_name . "','" . $driver_mob_no . "','" . $remarks . "','" . $status_one. "','" . $bill_date . "','" . $placefrom . "','" . $rupeesinwords . "','" . $payment_amount . "','" . $payment_neftref . "','" . $payment_neftdate . "','" . $payment_status . "','" . $receipt_amount . "','" . $receipt_neftref . "','" . $receipt_neftdate . "','" . $receipt_status . "','" . $receipt_number . "','" . $receipt_date . "','" . $payment_number . "','" . $payment_date . "','" . $mamul_charges . "')";
    		        $delete1 = "Delete from audit_mst where bill_number = '" . $billno . "'";
		            if (mysqli_query($connect, $delete1)) {
        		        if(mysqli_query($connect, $query1)){
        		            $query2 = "insert into audit_mst(id,bill_number,date_one,party_name,party_address_one,party_address_two,party_place,party_contact_person,party_phone_number,party_mobile_number,party_gst_number,party_pan_number,party_aadhar_number,party_payment_terms,party_goods,party_remarks,inv_number,lchno,lrno,truck_number,truck_owner_name,truck_address_one,truck_address_two,truck_place,truck_mobile_number,truck_pan_number,truck_bank_name,truck_bank_acc_number,truck_bank_branch,truck_ifsc_code,truck_aadhar_no,truck_remarks,destination,supp_name,supp_address_one,supp_address_two,supp_place,supp_contact_person,supp_mobile_number,supp_pan_number,supp_bank_name,supp_bank_acc_number,supp_bank_branch,supp_ifsc_code,supp_supp_aadhar_no,supp_remarks,weight,booking_rate,lorry_freight,advance,cash_advance,diesel,neft_amount,neft_date,unloading_charges,others,extra,balance,total_paid,bal_paid_date,delivery_date,acc_rec_date,profit,driver_name,driver_mob_no,remarks,status_one,bill_date,placefrom,rupeesinwords,payment_amount,payment_neftref,payment_neftdate,payment_status,receipt_amount,receipt_neftref,receipt_neftdate,receipt_status,receipt_number,receipt_date,payment_number,payment_date,mamul_charges) Values ('" . $id . "','" . $billno . "','" . $date_one . "','" . $party_name . "','" . $party_address_one . "','" . $party_address_two . "','" . $party_place . "','" . $party_contact_person . "','" . $party_phone_number . "','" . $party_mobile_number . "','" . $party_gst_number . "','" . $party_pan_number . "','" . $party_aadhar_number . "','" . $party_payment_terms . "','" . $party_goods . "','" . $party_remarks . "','" . $inv_number . "','" . $lchno . "','" . $lrno . "','" . $truck_number . "','" . $truck_owner_name . "','" . $truck_address_one . "','" . $truck_address_two . "','" . $truck_place . "','" . $truck_mobile_number . "','" . $truck_pan_number . "','" . $truck_bank_name . "','" . $truck_bank_acc_number . "','" . $truck_bank_branch . "','" . $truck_ifsc_code . "','" . $truck_aadhar_no . "','" . $truck_remarks . "','" . $destination . "','" . $supp_name . "','" . $supp_address_one . "','" . $supp_address_two . "','" . $supp_place . "','" . $supp_contact_person . "','" . $supp_mobile_number . "','" . $supp_pan_number . "','" . $supp_bank_name . "','" . $supp_bank_acc_number . "','" . $supp_bank_branch . "','" . $supp_ifsc_code . "','" . $supp_supp_aadhar_no . "','" . $supp_remarks . "','" . $weight . "','" . $booking_rate . "','" . $lorry_freight . "','" . $advance . "','" . $cash_advance . "','" . $diesel . "','" . $neft_amount . "','" . $neft_date . "','" . $unloading_charges . "','" . $others . "','" . $extra . "','" . $balance . "','" . $total_paid . "','" . $bal_paid_date . "','" . $delivery_date . "','" . $acc_rec_date . "','" . $profit . "','" . $driver_name . "','" . $driver_mob_no . "','" . $remarks . "','" . $status_one. "','" . $bill_date . "','" . $placefrom . "','" . $rupeesinwords . "','" . $payment_amount . "','" . $payment_neftref . "','" . $payment_neftdate . "','" . $payment_status . "','" . $receipt_amount . "','" . $receipt_neftref . "','" . $receipt_neftdate . "','" . $receipt_status . "','" . $receipt_number . "','" . $receipt_date . "','" . $payment_number . "','" . $payment_date . "','" . $mamul_charges . "')";
            		        if(mysqli_query($connect, $query2)){
            		            $update1 = "update control_mst set srno = '" . $billupd . "' where description='BILL'";
            		            //mysqli_query($connect, $update1) or die(mysqli_error());
            		        }
        		        }
		            }
		        }
		    }
			$update = "update control_mst set srno = '" . $srnoupd . "' where description='SRNO'";
    		//mysqli_query($connect, $update) or die(mysqli_error());
		}
	}
	if ($_POST["action"] == "Update") {
		$id = mysqli_escape_string($connect, $_POST["id"]);
	    $query = "select srno from control_mst where description='SRNO'";
		$rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
		while ($row = mysqli_fetch_assoc($rs)) {
			$srno = $row['srno'];
			$srnoupd = $srno+1;
		}
		$query2 = "select srno from control_mst where description='BILL'";
		$rs2 = mysqli_query($connect, $query2) or die("Cannot execute query: $query\n");
		while ($row2 = mysqli_fetch_assoc($rs2)) {
			$billno = $row2['srno'];
			$billupd = $billno+1;
		}
		$orgDate = $_POST['date_one'];  
		$newDate = date("Y-m-d H:i:s", strtotime($orgDate));
		$inv_date=$newDate;
		$date_one = $inv_date;
		$party_name = mysqli_escape_string($connect, $_POST["party_name"]);
		$party_address_one = mysqli_escape_string($connect, $_POST["party_address_one"]);
		$party_address_two = mysqli_escape_string($connect, $_POST["party_address_two"]);
		$party_place = mysqli_escape_string($connect, $_POST["party_place"]);
		$party_contact_person = mysqli_escape_string($connect, $_POST["party_contact_person"]);
		$party_phone_number = mysqli_escape_string($connect, $_POST["party_phone_number"]);
		$party_mobile_number = mysqli_escape_string($connect, $_POST["party_mobile_number"]);
		$party_gst_number = mysqli_escape_string($connect, $_POST["party_gst_number"]);
		$party_pan_number = mysqli_escape_string($connect, $_POST["party_pan_number"]);
		$party_aadhar_number = mysqli_escape_string($connect, $_POST["party_aadhar_number"]);
		$party_payment_terms = mysqli_escape_string($connect, $_POST["party_payment_terms"]);
		$party_goods = mysqli_escape_string($connect, $_POST["party_goods"]);
		$party_remarks = mysqli_escape_string($connect, $_POST["party_remarks"]);
		$inv_number = mysqli_escape_string($connect, $_POST["inv_number"]);
		$lchno = mysqli_escape_string($connect, $_POST["lchno"]);
		$lrno = mysqli_escape_string($connect, $_POST["lrno"]);
		$truck_number = mysqli_escape_string($connect, $_POST["truck_number"]);
		$truck_owner_name = mysqli_escape_string($connect, $_POST["truck_owner_name"]);
		$truck_address_one = mysqli_escape_string($connect, $_POST["truck_address_one"]);
		$truck_address_two = mysqli_escape_string($connect, $_POST["truck_address_two"]);
		$truck_place = mysqli_escape_string($connect, $_POST["truck_place"]);
		$truck_mobile_number = mysqli_escape_string($connect, $_POST["truck_mobile_number"]);
		$truck_pan_number = mysqli_escape_string($connect, $_POST["truck_pan_number"]);
		$truck_bank_name = mysqli_escape_string($connect, $_POST["truck_bank_name"]);
		$truck_bank_acc_number = mysqli_escape_string($connect, $_POST["truck_bank_acc_number"]);
		$truck_bank_branch = mysqli_escape_string($connect, $_POST["truck_bank_branch"]);
		$truck_ifsc_code = mysqli_escape_string($connect, $_POST["truck_ifsc_code"]);
		$truck_aadhar_no = mysqli_escape_string($connect, $_POST["truck_aadhar_no"]);
		$truck_remarks = mysqli_escape_string($connect, $_POST["truck_remarks"]);
		$placefrom = mysqli_escape_string($connect, $_POST["placefrom"]);
		$destination = mysqli_escape_string($connect, $_POST["destination"]);
		$supp_name = mysqli_escape_string($connect, $_POST["supp_name"]);
		$supp_address_one = mysqli_escape_string($connect, $_POST["supp_address_one"]);
		$supp_address_two = mysqli_escape_string($connect, $_POST["supp_address_two"]);
		$supp_place = mysqli_escape_string($connect, $_POST["supp_place"]);
		$supp_contact_person = mysqli_escape_string($connect, $_POST["supp_contact_person"]);
		$supp_mobile_number = mysqli_escape_string($connect, $_POST["supp_mobile_number"]);
		$supp_pan_number = mysqli_escape_string($connect, $_POST["supp_pan_number"]);
		$supp_bank_name = mysqli_escape_string($connect, $_POST["supp_bank_name"]);
		$supp_bank_acc_number = mysqli_escape_string($connect, $_POST["supp_bank_acc_number"]);
		$supp_bank_branch = mysqli_escape_string($connect, $_POST["supp_bank_branch"]);
		$supp_ifsc_code = mysqli_escape_string($connect, $_POST["supp_ifsc_code"]);
		$supp_supp_aadhar_no = mysqli_escape_string($connect, $_POST["supp_supp_aadhar_no"]);
		$supp_remarks = mysqli_escape_string($connect, $_POST["supp_remarks"]);
		$weight = mysqli_escape_string($connect, $_POST["weight"]);
		$booking_rate = mysqli_escape_string($connect, $_POST["booking_rate"]);
		$lorry_freight = mysqli_escape_string($connect, $_POST["lorry_freight"]);
		$advance = mysqli_escape_string($connect, $_POST["advance"]);
		$cash_advance = mysqli_escape_string($connect, $_POST["cash_advance"]);
		$diesel = mysqli_escape_string($connect, $_POST["diesel"]);
		$neft_amount = mysqli_escape_string($connect, $_POST["neft_amount"]);
		$neft_date = mysqli_escape_string($connect, $_POST["neft_date"]);
		$unloading_charges = mysqli_escape_string($connect, $_POST["unloading_charges"]);
		$others = mysqli_escape_string($connect, $_POST["others"]);
		$extra = mysqli_escape_string($connect, $_POST["extra"]);
		$balance = mysqli_escape_string($connect, $_POST["balance"]);
		$total_paid = mysqli_escape_string($connect, $_POST["total_paid"]);
		$bal_paid_date = mysqli_escape_string($connect, $_POST["bal_paid_date"]);
		$delivery_date = mysqli_escape_string($connect, $_POST["delivery_date"]);
		$acc_rec_date = mysqli_escape_string($connect, $_POST["acc_rec_date"]);
		$driver_name = mysqli_escape_string($connect, $_POST["driver_name"]);
		$driver_mob_no = mysqli_escape_string($connect, $_POST["driver_mob_no"]);
		$mamul_charges = mysqli_escape_string($connect, $_POST["mamul_charges"]);
		$profit = mysqli_escape_string($connect, $_POST["profit"]);
		$remarks = mysqli_escape_string($connect, $_POST["remarks"]);
		$status_one = mysqli_escape_string($connect, $_POST["status_one"]);
		$orgDate1 = $_POST['bill_date'];  
		$newDate1 = date("Y-m-d H:i:s", strtotime($orgDate1));
		$inv_date1=$newDate1;
		$bill_date = $inv_date1;
		$rupeesinwords = mysqli_escape_string($connect, $_POST["rupeesinwords"]);
		
		$payment_amount = '0.00';
		$payment_neftref = '-';
		$payment_neftdate = '-';
		$payment_status = 'PENDING';
		$receipt_amount = '0.00';
		$receipt_neftref = '-';
		$receipt_neftdate = '-';
		$receipt_status = 'PENDING';
		$receipt_number = '-';
		$receipt_date = '-';
		$payment_number = '-';
		$payment_date = '-';
    	if($status_one == 'CLEARED'){
    	    $update = "update loading_mst set date_one = '" . $date_one . "',party_name = '" . $party_name . "',party_address_one = '" . $party_address_one . "',party_address_two = '" . $party_address_two . "',party_place = '" . $party_place . "',party_contact_person = '" . $party_contact_person . "',party_phone_number = '" . $party_phone_number . "',party_mobile_number = '" . $party_mobile_number . "',party_gst_number = '" . $party_gst_number . "',party_pan_number = '" . $party_pan_number . "',party_aadhar_number = '" . $party_aadhar_number . "',party_payment_terms = '" . $party_payment_terms . "',party_goods = '" . $party_goods . "',party_remarks = '" . $party_remarks . "',inv_number = '" . $inv_number . "',lchno = '" . $lchno . "',lrno = '" . $lrno . "',truck_number = '" . $truck_number . "',truck_owner_name = '" . $truck_owner_name . "',truck_address_one = '" . $truck_address_one . "',truck_address_two = '" . $truck_address_two . "',truck_place = '" . $truck_place . "',truck_mobile_number = '" . $truck_mobile_number . "',truck_pan_number = '" . $truck_pan_number . "',truck_bank_name = '" . $truck_bank_name . "',truck_bank_acc_number = '" . $truck_bank_acc_number . "',truck_bank_branch = '" . $truck_bank_branch . "',truck_ifsc_code = '" . $truck_ifsc_code . "',truck_aadhar_no = '" . $truck_aadhar_no . "',truck_remarks = '" . $truck_remarks . "',destination = '" . $destination . "',supp_name = '" . $supp_name . "',supp_address_one = '" . $supp_address_one . "',supp_address_two = '" . $supp_address_two . "',supp_place = '" . $supp_place . "',supp_contact_person = '" . $supp_contact_person . "',supp_mobile_number = '" . $supp_mobile_number . "',supp_pan_number = '" . $supp_pan_number . "',supp_bank_name = '" . $supp_bank_name . "',supp_bank_acc_number = '" . $supp_bank_acc_number . "',supp_bank_branch = '" . $supp_bank_branch . "',supp_ifsc_code = '" . $supp_ifsc_code . "',supp_supp_aadhar_no = '" . $supp_supp_aadhar_no . "',supp_remarks = '" . $supp_remarks . "',weight = '" . $weight . "',booking_rate = '" . $booking_rate . "',lorry_freight = '" . $lorry_freight . "',advance = '" . $advance . "',cash_advance = '" . $cash_advance . "',diesel = '" . $diesel . "',neft_amount = '" . $neft_amount . "',neft_date = '" . $neft_date . "',unloading_charges = '" . $unloading_charges . "',others = '" . $others . "',extra = '" . $extra . "',balance = '" . $balance . "',total_paid = '" . $total_paid . "',bal_paid_date = '" . $bal_paid_date . "',delivery_date = '" . $delivery_date . "',acc_rec_date = '" . $acc_rec_date . "',profit = '" . $profit . "',driver_name = '" . $driver_name . "',driver_mob_no = '" . $driver_mob_no . "',remarks = '" . $remarks . "',status_one = '" . $status_one . "',bill_date = '" . $bill_date . "',placefrom = '" . $placefrom . "',rupeesinwords = '" . $rupeesinwords . "',mamul_charges = '" . $mamul_charges . "' where id = '". $id . "'";
    	}
    	if($status_one == 'PENDING'){
    	    $update = "update loading_mst set date_one = '" . $date_one . "',party_name = '" . $party_name . "',party_address_one = '" . $party_address_one . "',party_address_two = '" . $party_address_two . "',party_place = '" . $party_place . "',party_contact_person = '" . $party_contact_person . "',party_phone_number = '" . $party_phone_number . "',party_mobile_number = '" . $party_mobile_number . "',party_gst_number = '" . $party_gst_number . "',party_pan_number = '" . $party_pan_number . "',party_aadhar_number = '" . $party_aadhar_number . "',party_payment_terms = '" . $party_payment_terms . "',party_goods = '" . $party_goods . "',party_remarks = '" . $party_remarks . "',inv_number = '" . $inv_number . "',lchno = '" . $lchno . "',lrno = '" . $lrno . "',truck_number = '" . $truck_number . "',truck_owner_name = '" . $truck_owner_name . "',truck_address_one = '" . $truck_address_one . "',truck_address_two = '" . $truck_address_two . "',truck_place = '" . $truck_place . "',truck_mobile_number = '" . $truck_mobile_number . "',truck_pan_number = '" . $truck_pan_number . "',truck_bank_name = '" . $truck_bank_name . "',truck_bank_acc_number = '" . $truck_bank_acc_number . "',truck_bank_branch = '" . $truck_bank_branch . "',truck_ifsc_code = '" . $truck_ifsc_code . "',truck_aadhar_no = '" . $truck_aadhar_no . "',truck_remarks = '" . $truck_remarks . "',destination = '" . $destination . "',supp_name = '" . $supp_name . "',supp_address_one = '" . $supp_address_one . "',supp_address_two = '" . $supp_address_two . "',supp_place = '" . $supp_place . "',supp_contact_person = '" . $supp_contact_person . "',supp_mobile_number = '" . $supp_mobile_number . "',supp_pan_number = '" . $supp_pan_number . "',supp_bank_name = '" . $supp_bank_name . "',supp_bank_acc_number = '" . $supp_bank_acc_number . "',supp_bank_branch = '" . $supp_bank_branch . "',supp_ifsc_code = '" . $supp_ifsc_code . "',supp_supp_aadhar_no = '" . $supp_supp_aadhar_no . "',supp_remarks = '" . $supp_remarks . "',weight = '" . $weight . "',booking_rate = '" . $booking_rate . "',lorry_freight = '" . $lorry_freight . "',advance = '" . $advance . "',cash_advance = '" . $cash_advance . "',diesel = '" . $diesel . "',neft_amount = '" . $neft_amount . "',neft_date = '" . $neft_date . "',unloading_charges = '" . $unloading_charges . "',others = '" . $others . "',extra = '" . $extra . "',balance = '" . $balance . "',total_paid = '" . $total_paid . "',bal_paid_date = '" . $bal_paid_date . "',delivery_date = '" . $delivery_date . "',acc_rec_date = '" . $acc_rec_date . "',profit = '" . $profit . "',driver_name = '" . $driver_name . "',driver_mob_no = '" . $driver_mob_no . "',remarks = '" . $remarks . "',status_one = '" . $status_one . "',bill_date = '" . $bill_date . "',placefrom = '" . $placefrom . "',rupeesinwords = '" . $rupeesinwords . "',mamul_charges = '" . $mamul_charges . "' where id = '". $id . "'";
    	}
		if (mysqli_query($connect, $update)) {
		    if($status_one == 'CLEARED'){
		         $delete = "Delete from bill_mst where bill_number = '" . $billno . "'";
		        if (mysqli_query($connect, $delete)) {
    		        $query1 = "insert into bill_mst(id,bill_number,date_one,party_name,party_address_one,party_address_two,party_place,party_contact_person,party_phone_number,party_mobile_number,party_gst_number,party_pan_number,party_aadhar_number,party_payment_terms,party_goods,party_remarks,inv_number,lchno,lrno,truck_number,truck_owner_name,truck_address_one,truck_address_two,truck_place,truck_mobile_number,truck_pan_number,truck_bank_name,truck_bank_acc_number,truck_bank_branch,truck_ifsc_code,truck_aadhar_no,truck_remarks,destination,supp_name,supp_address_one,supp_address_two,supp_place,supp_contact_person,supp_mobile_number,supp_pan_number,supp_bank_name,supp_bank_acc_number,supp_bank_branch,supp_ifsc_code,supp_supp_aadhar_no,supp_remarks,weight,booking_rate,lorry_freight,advance,cash_advance,diesel,neft_amount,neft_date,unloading_charges,others,extra,balance,total_paid,bal_paid_date,delivery_date,acc_rec_date,profit,driver_name,driver_mob_no,remarks,status_one,bill_date,placefrom,rupeesinwords,payment_amount,payment_neftref,payment_neftdate,payment_status,receipt_amount,receipt_neftref,receipt_neftdate,receipt_status,receipt_number,receipt_date,payment_number,payment_date,mamul_charges) Values ('" . $id . "','" . $billno . "','" . $date_one . "','" . $party_name . "','" . $party_address_one . "','" . $party_address_two . "','" . $party_place . "','" . $party_contact_person . "','" . $party_phone_number . "','" . $party_mobile_number . "','" . $party_gst_number . "','" . $party_pan_number . "','" . $party_aadhar_number . "','" . $party_payment_terms . "','" . $party_goods . "','" . $party_remarks . "','" . $inv_number . "','" . $lchno . "','" . $lrno . "','" . $truck_number . "','" . $truck_owner_name . "','" . $truck_address_one . "','" . $truck_address_two . "','" . $truck_place . "','" . $truck_mobile_number . "','" . $truck_pan_number . "','" . $truck_bank_name . "','" . $truck_bank_acc_number . "','" . $truck_bank_branch . "','" . $truck_ifsc_code . "','" . $truck_aadhar_no . "','" . $truck_remarks . "','" . $destination . "','" . $supp_name . "','" . $supp_address_one . "','" . $supp_address_two . "','" . $supp_place . "','" . $supp_contact_person . "','" . $supp_mobile_number . "','" . $supp_pan_number . "','" . $supp_bank_name . "','" . $supp_bank_acc_number . "','" . $supp_bank_branch . "','" . $supp_ifsc_code . "','" . $supp_supp_aadhar_no . "','" . $supp_remarks . "','" . $weight . "','" . $booking_rate . "','" . $lorry_freight . "','" . $advance . "','" . $cash_advance . "','" . $diesel . "','" . $neft_amount . "','" . $neft_date . "','" . $unloading_charges . "','" . $others . "','" . $extra . "','" . $balance . "','" . $total_paid . "','" . $bal_paid_date . "','" . $delivery_date . "','" . $acc_rec_date . "','" . $profit . "','" . $driver_name . "','" . $driver_mob_no . "','" . $remarks . "','" . $status_one. "','" . $bill_date . "','" . $placefrom . "','" . $rupeesinwords . "','" . $payment_amount . "','" . $payment_neftref . "','" . $payment_neftdate . "','" . $payment_status . "','" . $receipt_amount . "','" . $receipt_neftref . "','" . $receipt_neftdate . "','" . $receipt_status . "','" . $receipt_number . "','" . $receipt_date . "','" . $payment_number . "','" . $payment_date . "','" . $mamul_charges . "')";
    		         $delete2 = "Delete from audit_mst where bill_number = '" . $billno . "'";
		            if (mysqli_query($connect, $delete2)) {
        		        if(mysqli_query($connect, $query1)){
        		            $query2 = "insert into audit_mst(id,bill_number,date_one,party_name,party_address_one,party_address_two,party_place,party_contact_person,party_phone_number,party_mobile_number,party_gst_number,party_pan_number,party_aadhar_number,party_payment_terms,party_goods,party_remarks,inv_number,lchno,lrno,truck_number,truck_owner_name,truck_address_one,truck_address_two,truck_place,truck_mobile_number,truck_pan_number,truck_bank_name,truck_bank_acc_number,truck_bank_branch,truck_ifsc_code,truck_aadhar_no,truck_remarks,destination,supp_name,supp_address_one,supp_address_two,supp_place,supp_contact_person,supp_mobile_number,supp_pan_number,supp_bank_name,supp_bank_acc_number,supp_bank_branch,supp_ifsc_code,supp_supp_aadhar_no,supp_remarks,weight,booking_rate,lorry_freight,advance,cash_advance,diesel,neft_amount,neft_date,unloading_charges,others,extra,balance,total_paid,bal_paid_date,delivery_date,acc_rec_date,profit,driver_name,driver_mob_no,remarks,status_one,bill_date,placefrom,rupeesinwords,payment_amount,payment_neftref,payment_neftdate,payment_status,receipt_amount,receipt_neftref,receipt_neftdate,receipt_status,receipt_number,receipt_date,payment_number,payment_date,mamul_charges) Values ('" . $id . "','" . $billno . "','" . $date_one . "','" . $party_name . "','" . $party_address_one . "','" . $party_address_two . "','" . $party_place . "','" . $party_contact_person . "','" . $party_phone_number . "','" . $party_mobile_number . "','" . $party_gst_number . "','" . $party_pan_number . "','" . $party_aadhar_number . "','" . $party_payment_terms . "','" . $party_goods . "','" . $party_remarks . "','" . $inv_number . "','" . $lchno . "','" . $lrno . "','" . $truck_number . "','" . $truck_owner_name . "','" . $truck_address_one . "','" . $truck_address_two . "','" . $truck_place . "','" . $truck_mobile_number . "','" . $truck_pan_number . "','" . $truck_bank_name . "','" . $truck_bank_acc_number . "','" . $truck_bank_branch . "','" . $truck_ifsc_code . "','" . $truck_aadhar_no . "','" . $truck_remarks . "','" . $destination . "','" . $supp_name . "','" . $supp_address_one . "','" . $supp_address_two . "','" . $supp_place . "','" . $supp_contact_person . "','" . $supp_mobile_number . "','" . $supp_pan_number . "','" . $supp_bank_name . "','" . $supp_bank_acc_number . "','" . $supp_bank_branch . "','" . $supp_ifsc_code . "','" . $supp_supp_aadhar_no . "','" . $supp_remarks . "','" . $weight . "','" . $booking_rate . "','" . $lorry_freight . "','" . $advance . "','" . $cash_advance . "','" . $diesel . "','" . $neft_amount . "','" . $neft_date . "','" . $unloading_charges . "','" . $others . "','" . $extra . "','" . $balance . "','" . $total_paid . "','" . $bal_paid_date . "','" . $delivery_date . "','" . $acc_rec_date . "','" . $profit . "','" . $driver_name . "','" . $driver_mob_no . "','" . $remarks . "','" . $status_one. "','" . $bill_date . "','" . $placefrom . "','" . $rupeesinwords . "','" . $payment_amount . "','" . $payment_neftref . "','" . $payment_neftdate . "','" . $payment_status . "','" . $receipt_amount . "','" . $receipt_neftref . "','" . $receipt_neftdate . "','" . $receipt_status . "','" . $receipt_number . "','" . $receipt_date . "','" . $payment_number . "','" . $payment_date . "','" . $mamul_charges . "')";
        		            if(mysqli_query($connect, $query2)){
                		        $update1 = "update control_mst set srno = '" . $billupd . "' where description='BILL'";
                    		    //mysqli_query($connect, $update1) or die(mysqli_error());
        		            }
        		        }
		            }
		        }
		    }
		}
	}
	if ($_POST["action"] == "Delete") {
		$delete = "Delete from loading_mst where id = '" . $_POST["id"] . "'";
		if (mysqli_query($connect, $delete)) {
		    
		}
	}
}