<?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") {

    $edit_id   = stripslashes($_POST['edit_id']);
    $invoice   = stripslashes($_POST['invoice']);
    
    $tdate_temp         = new DateTime(stripslashes($_POST['internalprodate']));
    $internalprodate 	= $tdate_temp->format('Y-m-d');
    $vhid          = stripslashes($_POST['vhid']);
    $intakekm           = stripslashes($_POST['intakekm']);
    $intakehrs           = stripslashes($_POST['intakehrs']);
    $qty                = stripslashes($_POST['eqty']);
	$sname                = stripslashes($_POST['sname']);
	$amount                = stripslashes($_POST['amount']);
	$rate                = stripslashes($_POST['rate']);
	
	$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'];
	
	$driverid                = stripslashes($_POST['driverid']);
	$sql=mysqli_query($conn,"select * from employee_master where id='".$driverid."'");
	
	while($row1=mysqli_fetch_array($sql))
	{
		$drivername=$row1['ename'];
	}
    
    
	
	$sw=mysqli_query($conn,"Select * from supplier_master where id='$sname '");
	while($rw=mysqli_fetch_array($sw))
	{
		$sname1=$rw['sname'];
	}
	
	$sq=mysqli_query($conn,"select vehicleno as a from vehicle_master where id='".$vhid."'");
	
		while($row=mysqli_fetch_array($sq))
		{
		
	
    $vehicleno          = $row['a'];
	
		}
		
		$deldtl=mysqli_query($conn,"update  supplier_payment set status='DELETED'  where purchaseid='$edit_id' and status='' and process='External Unfill'");

    $sql_update = "UPDATE externalpro_diesel SET ";
    $sql_update .= "dieseldate='$internalprodate',";
	$sql_update .= "vehicleid='$vhid',";
    $sql_update .= "vehicleno='$vehicleno',";
	$sql_update .= "driverid='$driverid',";
	$sql_update .= "drivername='$drivername',";
	$sql_update .= "supplierid='$sname',";
	$sql_update .= "sname='$sname1',";
    $sql_update .= "intakekm='$intakekm',";
    $sql_update .= "intakehrs='$intakehrs',";
    $sql_update .= "litres='$qty',";
	$sql_update .= "rate='$rate',";
	$sql_update .= "amount='$amount',";
	
	$sql_update .= "payment='$payment',";
	$sql_update .= "received_amt='$received_amt',";
	$sql_update .= "paymenttype='$paymenttype',";
	$sql_update .= "remark='$remark',";
	
    $sql_update .= "lastupdated='$createdate'";
    $sql_update .= "WHERE id='$edit_id' ";

    mysqli_query($conn,$sql_update);
	
	$sql_header2  = "update total_litres set dieseldate='".$internalprodate."',vehicleid='".$vhid."', vehicleno='".$vehicleno."',driverid='".$driverid."',drivername='".$drivername."',intakekm='".$intakekm."',intakehrs='".$intakehrs."',litres='".$qty."',createdate='".$createdate."',createdby='".$createdby."',userID='".$userID."' WHERE invoice='".$invoice."' and filling_type='externalunfill'";
    
    mysqli_query($conn,$sql_header2);
	
	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."', '".$internalprodate."', '".$sname."','$edit_id','$payment','External Unfill', '".$received_amt."','".$paymenttype."', '".$remark."' ,'','".$accountyear."', '1','".$createdby."','".$userID."','".$createdate."')";
	mysqli_query($conn,$sql_header);
    
	}
    
    header("Location:exDieselProDashboard.php");

}
?>
