<?php

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

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$saleID         = stripslashes($_POST['edit_id']); 
$purchaseinvoice    = stripslashes($_POST['saleinvoice']);
$purchasedate       = new DateTime(stripslashes($_POST['saledate']));
$purchasedate 		= $purchasedate->format('Y-m-d');
$supplierName   = stripslashes($_POST['customerName']);
$supplier_invoice   = stripslashes($_POST['supplier_invoice']);
$supplier_invoice_date= stripslashes($_POST['supplier_invoice_date']);


$remark    = $_POST['remark'];

$po_number    = '';





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

$purchasenetamt   = $_POST['amtinput'];

$totalamt       = $_POST['salenetamt'];

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

$user    = $_POST['user'];

$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;
		
		$delhdr=mysqli_query($conn,"update  purchaseheader set status='DELETED'  where id='$saleID' and status=''");
$deldtl=mysqli_query($conn,"update  purchasedetails set status='DELETED'  where purchaseheader_id='$saleID' and status=''");
		

	$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,sname,po_number, supplier_invoice, supplier_invoice_date, itemcount, tamount,  remark, status,user, create_date, flag, accountyear) VALUES ('$purchaseinvoice','$purchasedate','$supplierName','$sname','$po_number','$supplier_invoice','$supplier_invoice_date','$itemcount','$totalNetamt','$remark','','$user','$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'];
		$productname=$rwpr['productname'];
		$qty_type=$rwpr['qty_type'];
		
			}
		
	

    $sql_purchasedetails  = "INSERT INTO purchasedetails( purchaseheader_id, productID,productname,qty_type, purchase_eqty, purchase_prate,  purchasenetamt,status,create_date, flag,accountyear) VALUES ('$last_id','$productID[$key]','$productname','$qty_type','$eqtyinput[$key]','$prateinput[$key]','$totalamt[$key]','','$create_date','1','$accountyear')";
	
	

    mysqli_query( $conn, $sql_purchasedetails );

}




header("Location:purchaseDashboard.php");



}
?>