<?php

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

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$purchaseinvoice    = stripslashes($_POST['saleinvoice']);
$purchasedate       = new DateTime(stripslashes($_POST['saledate']));
$purchasedate 		= $purchasedate->format('Y-m-d');
$supplierName   = stripslashes($_POST['customerName']);
$purchasePO   = stripslashes($_POST['salePONO']);
$btime= stripslashes($_POST['btime']);

$payment=$_POST['payment'];
$amount_paid=$_POST['amount_paid'];
$paymenttype=$_POST['paymenttype'];
$remark    = $_POST['remark'];





$productID    = $_POST['productID'];
$eqtyinput    = $_POST['eqtyinput'];
$prateinput   = $_POST['prateinput'];
$gstinput     = $_POST['gstinput'];

$purchasenetamt   = $_POST['amtinput'];

$totalamt       = $_POST['salenetamt'];

$cgstinput    = $_POST['cgstinput'];
$sgstinput    = $_POST['sgstinput'];



$totalNetamt    = round($_POST['totalNetamt']);



$accountyear    = $financialYearStart."-".$financialYearEnd;
$create_date  	= date("Y-m-d H:i:s");

 
$itemcount      = count($productID);

 $msql2=mysqli_query($conn,"select * from supplier_master where id='".$supplierName."'");
        
		while($rw2=mysqli_fetch_array($msql2))
		{
			
		$sname=$rw2['sname'];
			
		}
		
		//echo $payment;
		

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

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


$sql_purchaseheader  = "INSERT INTO purchaseheader ( invoice_no, purchase_date, supplier_id, po_num, btime, itemcount, tamount, payment, amount_paid, paymenttype, remark, status, create_date, flag, accountyear) VALUES ('$purchaseinvoice','$purchasedate','$supplierName','$purchasePO','$btime','$itemcount','$totalNetamt','$payment','$amount_paid','$paymenttype','$remark','','$create_date', '1','$accountyear')";

//echo $sql_purchaseheader ;

mysqli_query( $conn, $sql_purchaseheader );

$last_id = mysqli_insert_id($conn);


	
	

		
		foreach ($productID as $key => $value) {

$qty=$eqtyinput[$key];
$prid=$productID[$key];
$msqpr=mysqli_query($conn,"select * from product_master where id='".$prid."'");
        
		while($rwpr=mysqli_fetch_array($msqpr))
		{
			
		$product_type=$rwpr['product_type'];
		
			}
		
	

    $sql_purchasedetails  = "INSERT INTO purchasedetails( purchaseheader_id, productID,product_type, purchase_eqty, purchase_prate, purchase_gst, purchase_cgst, purchase_sgst, purchasenetamt,status,create_date, flag,accountyear) VALUES ('$last_id','$productID[$key]','$product_type','$eqtyinput[$key]','$prateinput[$key]','$gstinput[$key]','$cgstinput[$key]','$sgstinput[$key]','$totalamt[$key]','','$create_date','1','$accountyear')";
	
	

    mysqli_query( $conn, $sql_purchasedetails );

}

if($payment=="Cash And Carry")
{


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


$sql_InvoiceQuery   = mysqli_query($conn,"SELECT count(*) as invoice FROM supplier_payment_header WHERE accountyear='$accountyear' and status='' ");
$rowInvoiceCount    = mysqli_fetch_array($sql_InvoiceQuery,MYSQLI_ASSOC);

$invoice_count      = $rowInvoiceCount['invoice']+1;

$acyear="GC/".$syr."-".$eyr."/";

$invoice= $acyear.$invoice_count;


$sqlh  = "INSERT INTO supplier_payment_header (cashno,collectiondate,supplierID,pending, paymenttype,paymentreference ,bank_name,handover,transactionno,remark,paidby,received_amount,saleid,payment_through,status,flag,accountyear) VALUES ('$invoice','$purchasedate','$supplierName','0.00','$paymenttype','','','','','$remark','$createdby','$amount_paid','$last_id','$payment','','1','$accountyear')";

 mysqli_query( $conn, $sqlh );

}



header("Location:purchaseDashboard.php");



}
?>