<?php
session_start();
require_once("config/configDB.php"); 
require "financial_year_calculation.php";

function validation_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    //invoiceno,invoicedate,supplierdetails,supplierinvoice_no,amount,vehicleno,litres
    $dieselinvoice        = stripslashes($_POST['dieselinvoice']);
    //$invoicedate      = stripslashes($_POST['invoicedate']);
$edit_id            =  validation_input($_POST['edit_id']);
    $invoicedate_temp   = new DateTime(stripslashes($_POST['invoicedate']));
    $invoicedate 		= $invoicedate_temp->format('Y-m-d');

    $supplierdetails     = stripslashes($_POST['supplierdetails']);
	
	$sq=mysqli_query($conn,"select sname from supplier_master where id='".$supplierdetails."'");
	while($rw=mysqli_fetch_array($sq))
	{
		$sname=$rw['sname'];
		
	}
	
    $supplierinvoice_no  = stripslashes($_POST['supplierinvoice_no']);

    $amount         = stripslashes($_POST['amount']);
    $vehicleno      = stripslashes($_POST['vehicleno']);
   
    $litres         = stripslashes($_POST['litres']);
	 $rate1         = stripslashes($_POST['rate1']);
	 
	  $payment         = stripslashes($_POST['payment']);
	   $received_amt         = stripslashes($_POST['received_amt']);
	    $paymenttype         = stripslashes($_POST['paymenttype']);
		 $remark         = stripslashes($_POST['remark']);
    
    $accountyear    = $financialYearStart."-".$financialYearEnd;
    $createdate  	= date("Y-m-d");
    $createdby      = $_SESSION['username'];
    $userID         = $_SESSION['user_id'];
	
	
	$delhdr=mysqli_query($conn,"update  purchase_dieselinvoice set status='DELETED'  where id='$edit_id' and status=''");
$deldtl=mysqli_query($conn,"update  supplier_payment set status='DELETED'  where purchaseid='$edit_id' and status='' and process='Internal Purchase'");

//echo $delhdr;
//echo $deldtl;

$autoinc=mysqli_query($conn,"ALTER TABLE purchase_dieselinvoice CHANGE id id INT(11) NOT NULL AUTO_INCREMENT ");

    $sql_dieselInvoice  = "INSERT INTO purchase_dieselinvoice (dieselinvoice, dieseldate, supplierid,sname, supplierinvoice,vehicleno,litres, rate1, amount,payment,received_amt,paymenttype,remark,status, accountyear,isdelete,createdby,userID,createdate) VALUES ('".$dieselinvoice."', '".$invoicedate."', '".$supplierdetails."','".$sname."', '".$supplierinvoice_no."','".$vehicleno."','".$litres."','".$rate1."', '".$amount."','$payment','$received_amt','$paymenttype','$remark','','".$accountyear."', '1','".$createdby."','".$userID."','".$createdate."')";
    
    mysqli_query($conn,$sql_dieselInvoice);
	
	$last_id = mysqli_insert_id($conn);
	
	
	if($payment=="Cash And Carry")
{
	
	$sql_InvoiceQuery   = mysqli_query($conn,"SELECT count(*) as invoice FROM supplier_payment WHERE status='' ");
$rowInvoiceCount    = mysqli_fetch_array($sql_InvoiceQuery,MYSQLI_ASSOC);
$invoice_count      = $rowInvoiceCount['invoice']+1;
	
	$autoinc=mysqli_query($conn,"ALTER TABLE supplier_payment CHANGE id id INT(11) NOT NULL AUTO_INCREMENT ");

   $sql_header  = "INSERT INTO supplier_payment ( invoice, invoicedate,supplierid,purchaseid,purchase_type,process, amount, pay_type,paymentreference,status, accountyear,isdelete,createdby,userID,createdate) VALUES ('".$invoice_count."', '".$invoicedate."', '".$supplierdetails."','$last_id','$payment','Internal Purchase', '".$received_amt."','".$paymenttype."', '".$remark."' ,'','".$accountyear."', '1','".$createdby."','".$userID."','".$createdate."')";
	mysqli_query($conn,$sql_header);
    
	}
	
    header("Location:piDieselInvoiceDashboard.php");

}





?>