<?php

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

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

$salePO         = "";

$billing_address=$_POST['billing_address'];
$delivery_address="";
$remarks=$_POST['remarks'];


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

$salenetamt   = $_POST['salenetamt'];

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

$accountyear    = $financialYearStart."-".$financialYearEnd;

$create_date  	= date("Y-m-d H:i:s"); 
$itemcount      = count($productID);

 $msql2=mysqli_query($conn,"select * from customer_master where id='".$customerName."'");
        
		while($rw2=mysqli_fetch_array($msql2))
		{
			
		$cname=$rw2['cname'];
			
		}
		
		$delhdr=mysqli_query($conn,"update  silver_purchaseheader set status='DELETED'  where id='$saleID' and status=''");
$deldtl=mysqli_query($conn,"update  silver_purchasedetails set status='DELETED'  where saleheader_id='$saleID' and status=''");
		
		$count=0;
$check_query	= mysqli_query($conn,"SELECT count(invoice_no) as counts FROM silver_purchaseheader WHERE invoice_no = '$saleinvoice' 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 = "silver_purchaseDashboard.php";</script>
<?php
		

}		
else
{

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

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

//SELECT invoice_no, date, customer_id, dc_num, po_num, itemcount, tamount, create_date, flag FROM `` WHERE 1
$sql_saleheader  = "INSERT INTO silver_purchaseheader (invoice_no, sale_date, customer_id,billing_address,delivery_address,remarks,  po_num,saleGSTNO, itemcount, tamount, create_date, flag, accountyear,status) VALUES ('$saleinvoice','$saledate','$customerName','$billing_address','$delivery_address','$remarks','$salePO','$saleGSTNO','$itemcount','$totalNetamt','$create_date', '1','$accountyear','')";

mysqli_query( $conn, $sql_saleheader ); 			

$last_id = mysqli_insert_id($conn);
//INSERT INTO saledetails( saleheader_id, productID, sale_eqty, sale_prate, sale_gst, sale_cgst, sale_sgst, salenetamt,create_date, flag) VALUES ( )

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

    $sql_saledetails  = "INSERT INTO silver_purchasedetails( saleheader_id, category,productID, sale_eqty, sale_prate,  salenetamt,create_date, flag,accountyear) VALUES ('$last_id','$category[$key]','$productID[$key]',$eqtyinput[$key],'$prateinput[$key]','$salenetamt[$key]','$create_date','1','$accountyear')";

    mysqli_query( $conn, $sql_saledetails );

}

header("Location:silver_purchaseDashboard.php");

}
}

?>