<?php
if (isset($_POST["action"])) {
    
	$output = '';
	require('../config/dbcon.php');
	if ($_POST["action"] == "Add") {
		$query = "Select srno from control_mst where description='PAYMENT'";
		$rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
		while ($row = mysqli_fetch_assoc($rs)) {
			$srno = $row['srno'];
			$srnoupd = $srno+1;
			$orgDate1 = $_POST['payment_date'];  
    		$newDate1 = date("Y-m-d H:i:s", strtotime($orgDate1));
    		$inv_date1=$newDate1;
    		$payment_date = $inv_date1;
    		$supp_name = mysqli_escape_string($connect, $_POST["supp_name"]);
    		$supp_place = mysqli_escape_string($connect, $_POST["supp_place"]);
    		$srno1 = mysqli_escape_string($connect, $_POST["srno"]);
    		$bill_number = mysqli_escape_string($connect, $_POST["bill_number"]);
    		$bill_date = mysqli_escape_string($connect, $_POST["bill_date"]);
    		$placefrom = mysqli_escape_string($connect, $_POST["placefrom"]);
    		$destination = mysqli_escape_string($connect, $_POST["destination"]);
    		$inv_number = mysqli_escape_string($connect, $_POST["inv_number"]);
    		$lrno = mysqli_escape_string($connect, $_POST["lrno"]);
    		$lchno = mysqli_escape_string($connect, $_POST["lchno"]);
    		$weight = mysqli_escape_string($connect, $_POST["weight"]);
    		$extra = mysqli_escape_string($connect, $_POST["extra"]);
    		$balance = mysqli_escape_string($connect, $_POST["balance"]);
    		$total_paid = mysqli_escape_string($connect, $_POST["total_paid"]);
    		$payment_amount = mysqli_escape_string($connect, $_POST["payment_amount"]);
    		$payment_neftref = mysqli_escape_string($connect, $_POST["payment_neftref"]);
    		$payment_neftdate = mysqli_escape_string($connect, $_POST["payment_neftdate"]);
    		$payment_status = mysqli_escape_string($connect, $_POST["payment_status"]);
    		$rupeesinwords = mysqli_escape_string($connect, $_POST["rupeesinwords"]);
    		$truck_number = mysqli_escape_string($connect, $_POST["truck_number"]);
    		$truck_owner_name = mysqli_escape_string($connect, $_POST["truck_owner_name"]);
    		$truck_pan_number = mysqli_escape_string($connect, $_POST["truck_pan_number"]);
    		$orgDate2 = $_POST['loading_date'];  
    		$newDate2 = date("Y-m-d H:i:s", strtotime($orgDate2));
    		$inv_date2=$newDate2;
    		$loading_date = $inv_date2;
    		
    		$query = "insert into payment_mst(payment_number,payment_date,supp_name,supp_place,srno,bill_number,bill_date,placefrom,destination,inv_number,lrno,lchno,weight,extra,balance,total_paid,payment_amount,payment_neftref,payment_neftdate,payment_status,rupeesinwords,truck_number,truck_owner_name,truck_pan_number,loading_date) Values ('" . $srno . "','" . $payment_date . "','" . $supp_name . "','" . $supp_place . "','" . $srno1 . "','" . $bill_number . "','" . $bill_date . "','" . $placefrom . "','" . $destination . "','" . $inv_number . "','" . $lrno . "','" . $lchno . "','" . $weight . "','" . $extra . "','" . $balance . "','" . $total_paid . "','" . $payment_amount . "','" . $payment_neftref . "','" . $payment_neftdate . "','" . $payment_status . "','" . $rupeesinwords . "','" . $truck_number . "','" . $truck_owner_name . "','" . $truck_pan_number . "','" . $loading_date . "')";
    		if (mysqli_query($connect, $query)) {
    			$update = "update control_mst set srno = '" . $srnoupd . "' where description='PAYMENT'";
    			mysqli_query($connect, $update) or die(mysqli_error());
    			
    			$update1 = "update bill_mst set payment_status = '" . $payment_status . "' where id = '" . $srno1 . "'";
        		if (mysqli_query($connect, $update1)) {
        		    
        		}
    		}
		}
	}
	/*
	if ($_POST["action"] == "Update") {
	    
		$id = mysqli_escape_string($connect, $_POST["id"]);
		$payment_date = mysqli_escape_string($connect, $_POST["payment_date"]);
		supp_name = mysqli_escape_string($connect, $_POST["party_name"]);
		$supp_place = mysqli_escape_string($connect, $_POST["supp_place"]);
		$srno = mysqli_escape_string($connect, $_POST["srno"]);
		$bill_number = mysqli_escape_string($connect, $_POST["bill_number"]);
		$bill_date = mysqli_escape_string($connect, $_POST["bill_date"]);
		$placefrom = mysqli_escape_string($connect, $_POST["placefrom"]);
		$destination = mysqli_escape_string($connect, $_POST["destination"]);
		$inv_number = mysqli_escape_string($connect, $_POST["inv_number"]);
		$lrno = mysqli_escape_string($connect, $_POST["lrno"]);
		$lchno = mysqli_escape_string($connect, $_POST["lchno"]);
		$weight = mysqli_escape_string($connect, $_POST["weight"]);
		$extra = mysqli_escape_string($connect, $_POST["extra"]);
		$balance = mysqli_escape_string($connect, $_POST["balance"]);
		$total_paid = mysqli_escape_string($connect, $_POST["total_paid"]);
		$payment_amount = mysqli_escape_string($connect, $_POST["payment_amount"]);
		$payment_neftref = mysqli_escape_string($connect, $_POST["payment_neftref"]);
		$payment_neftdate = mysqli_escape_string($connect, $_POST["payment_neftdate"]);
		$payment_status = mysqli_escape_string($connect, $_POST["payment_status"]);
		$rupeesinwords = mysqli_escape_string($connect, $_POST["rupeesinwords"]);
		$truck_number = mysqli_escape_string($connect, $_POST["truck_number"]);
		
		$update = "update party_mst set party_name = '" . supp_name . "',party_address_one = '" . $party_address_one . "',party_address_two = '" . $party_address_two . "',supp_place = '" . $supp_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 . "' where id = '" . $id . "'";
		if (mysqli_query($connect, $update)) {
		    
		}
		
	}
	*/
	if ($_POST["action"] == "Delete") {
		$delete = "Delete from party_mst where id = '" . $_POST["id"] . "'";
		if (mysqli_query($connect, $delete)) {
		    
		}
	}
	
}