<?php
if (isset($_POST["action"])) {
    
	$output = '';
	require('../config/dbcon.php');
	if ($_POST["action"] == "Add") {
		$query = "Select srno from control_mst where description='RECEIVE'";
		$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['receipt_date'];  
    		$newDate1 = date("Y-m-d H:i:s", strtotime($orgDate1));
    		$inv_date1=$newDate1;
    		$receipt_date = $inv_date1;
    		$party_name = mysqli_escape_string($connect, $_POST["party_name"]);
    		$party_place = mysqli_escape_string($connect, $_POST["party_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"]);
    		$booking_rate = mysqli_escape_string($connect, $_POST["booking_rate"]);
    		$others = mysqli_escape_string($connect, $_POST["others"]);
    		$total_amount = mysqli_escape_string($connect, $_POST["total_amount"]);
    		$receipt_amount = mysqli_escape_string($connect, $_POST["receipt_amount"]);
    		$receipt_neftref = mysqli_escape_string($connect, $_POST["receipt_neftref"]);
    		$receipt_neftdate = mysqli_escape_string($connect, $_POST["receipt_neftdate"]);
    		$receipt_status = mysqli_escape_string($connect, $_POST["receipt_status"]);
    		$rupeesinwords = mysqli_escape_string($connect, $_POST["rupeesinwords"]);
    		$truck_number = mysqli_escape_string($connect, $_POST["truck_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 receipt_mst(receipt_number,receipt_date,party_name,party_place,srno,bill_number,bill_date,placefrom,destination,inv_number,lrno,lchno,weight,booking_rate,others,total_amount,receipt_amount,receipt_neftref,receipt_neftdate,receipt_status,rupeesinwords,truck_number,loading_date) Values ('" . $srno . "','" . $receipt_date . "','" . $party_name . "','" . $party_place . "','" . $srno1 . "','" . $bill_number . "','" . $bill_date . "','" . $placefrom . "','" . $destination . "','" . $inv_number . "','" . $lrno . "','" . $lchno . "','" . $weight . "','" . $booking_rate . "','" . $others . "','" . $total_amount . "','" . $receipt_amount . "','" . $receipt_neftref . "','" . $receipt_neftdate . "','" . $receipt_status . "','" . $rupeesinwords . "','" . $truck_number . "','" . $loading_date . "')";
    		if (mysqli_query($connect, $query)) {
    			$update = "update control_mst set srno = '" . $srnoupd . "' where description='RECEIVE'";
    			mysqli_query($connect, $update) or die(mysqli_error());
    			
    			$update1 = "update bill_mst set receipt_status = '" . $receipt_status . "' where id = '" . $srno1 . "'";
        		if (mysqli_query($connect, $update1)) {
        		    
        		}
    		}
		}
	}
	/*
	if ($_POST["action"] == "Update") {
	    
		$id = mysqli_escape_string($connect, $_POST["id"]);
		$receipt_date = mysqli_escape_string($connect, $_POST["receipt_date"]);
		$party_name = mysqli_escape_string($connect, $_POST["party_name"]);
		$party_place = mysqli_escape_string($connect, $_POST["party_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"]);
		$booking_rate = mysqli_escape_string($connect, $_POST["booking_rate"]);
		$others = mysqli_escape_string($connect, $_POST["others"]);
		$total_amount = mysqli_escape_string($connect, $_POST["total_amount"]);
		$receipt_amount = mysqli_escape_string($connect, $_POST["receipt_amount"]);
		$receipt_neftref = mysqli_escape_string($connect, $_POST["receipt_neftref"]);
		$receipt_neftdate = mysqli_escape_string($connect, $_POST["receipt_neftdate"]);
		$receipt_status = mysqli_escape_string($connect, $_POST["receipt_status"]);
		$rupeesinwords = mysqli_escape_string($connect, $_POST["rupeesinwords"]);
		$truck_number = mysqli_escape_string($connect, $_POST["truck_number"]);
		
		$update = "update party_mst set 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 . "' 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)) {
		    
		}
	}
	
}