<?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=$_POST['supplier_invoice_date'];
$btime= stripslashes($_POST['btime']);

$payment=0;
$amount_paid=0;
$paymenttype=0;
$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;
		
		$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=''");

$deldt2=mysqli_query($conn,"update  supplier_payment_header set status='DELETED'  where payment_through='Cash And Carry' and  saleid='$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 ");

$count=0;
$check_query	= mysqli_query($conn,"SELECT count(invoice_no) as counts FROM purchaseheader WHERE invoice_no = '$purchaseinvoice' and accountyear='$accountyear'  and status=''"); // Check the database


while($row = mysqli_fetch_array($check_query)){
$count=$row['counts'];
}
if($count!=0){
// Already Exsist


		echo "<script> alert ('Data Inserted Success.');</script>";
		?>
<script language="javascript">window.location.href = "purchaseDashboardwo.php";</script>
<?php
		

}		
else
{
$sql_purchaseheader  = "INSERT INTO purchaseheader ( invoice_no, purchase_date, supplier_id, supplier_invoice,supplier_invoice_date, btime, itemcount, tamount, payment, amount_paid, paymenttype, remark, status, create_date, flag, accountyear) VALUES ('$purchaseinvoice','$purchasedate','$supplierName','$supplier_invoice','$supplier_invoice_date','$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))
		{
			
		$qty_type=$rwpr['qty_type'];
		
			}
		
	

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

    mysqli_query( $conn, $sql_purchasedetails );

}

}



header("Location:purchaseDashboard.php");



}
?>