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

    $invoice   = stripslashes($_POST['invoice']);
    //$tripedate      = stripslashes($_POST['internalprodate']);

    $tdate_temp         = new DateTime(stripslashes($_POST['internalprodate']));
    $internalprodate 	= $tdate_temp->format('Y-m-d');
    $vhid          = stripslashes($_POST['vhid']);
	
	  $payment         = stripslashes($_POST['payment']);
	   $received_amt         = stripslashes($_POST['received_amt']);
	    $paymenttype         = stripslashes($_POST['paymenttype']);
		 $remark         = stripslashes($_POST['remark']);
	
	
	$sq=mysqli_query($conn,"select vehicleno as a from vehicle_master where id='".$vhid."'");
	
		while($row=mysqli_fetch_array($sq))
		{
		
	
    $vehicleno          = $row['a'];
	
		}
	
	
    $odometerkm           = stripslashes($_POST['odometerkm']);
    $odometerhours           = stripslashes($_POST['odometerhours']);
    $qty                = stripslashes($_POST['eqty']);
	$rate                = stripslashes($_POST['rate']);
	$amount                = stripslashes($_POST['amount']);
	$sname                = stripslashes($_POST['sname']);
    
    $accountyear    = $financialYearStart."-".$financialYearEnd;
    $createdate  	= date("Y-m-d");
    $createdby      = $_SESSION['username'];
    $userID         = $_SESSION['user_id'];
	
	$sw=mysqli_query($conn,"Select * from supplier_master where id='$sname '");
	while($rw=mysqli_fetch_array($sw))
	{
		$sname1=$rw['sname'];
	}
	
	
	$driverid                = stripslashes($_POST['driverid']);
	$sql=mysqli_query($conn,"select * from employee_master where id='".$driverid."'");
	
	while($row1=mysqli_fetch_array($sql))
	{
		$drivername=$row1['ename'];
	}
	
	$autoinc=mysqli_query($conn,"ALTER TABLE externalpro_diesel CHANGE id id INT(11) NOT NULL AUTO_INCREMENT ");
	$autoinc1=mysqli_query($conn,"ALTER TABLE total_litres CHANGE id id INT(11) NOT NULL AUTO_INCREMENT ");

    $sql_header  = "INSERT INTO externalpro_diesel (invoice, dieseldate,supplierid,sname,vehicleid, vehicleno,driverid,drivername,intakekm,intakehrs,litres,rate, amount ,filling_type,payment,received_amt,paymenttype,remark,status,accountyear,isdelete,createdby,userID,createdate) VALUES ('".$invoice."', '".$internalprodate."','".$sname."','".$sname1."', '".$vhid."', '".$vehicleno."','".$driverid."' ,'".$drivername."','".$odometerkm."' ,'".$odometerhours."','".$qty."','".$rate."','".$amount."','unfill','$payment','$received_amt','$paymenttype','$remark','', '".$accountyear."', '1','".$createdby."','".$userID."','".$createdate."')";
    
    mysqli_query($conn,$sql_header);
	
	$last_id = mysqli_insert_id($conn);
	
	$sql_header2  = "INSERT INTO total_litres (invoice,dieseldate,vehicleid, vehicleno,driverid,drivername,intakekm,intakehrs,litres,filling_type,accountyear,createdate,createdby,userID,isdelete) VALUES ('".$invoice."', '".$internalprodate."','".$vhid."', '".$vehicleno."','".$driverid."' ,'".$drivername."','".$odometerkm."','".$odometerhours."','".$qty."','externalunfill', '".$accountyear."','".$createdate."', '".$createdby."','".$userID."','1')";
    
    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."','$last_id','$payment','External Unfill', '".$received_amt."','".$paymenttype."', '".$remark."' ,'','".$accountyear."', '1','".$createdby."','".$userID."','".$createdate."')";
	mysqli_query($conn,$sql_header);
    
	}
    
    header("Location:exDieselProDashboard.php");

}





?>