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

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


$productID    = $_POST['productID'];
$eqtyinput    = $_POST['eqtyinput'];
$prateinput   = $_POST['prateinput'];
$gstinput     = $_POST['gstinput'];
$cgstinput    = $_POST['cgstinput'];
$sgstinput    = $_POST['sgstinput'];
$salenetamt   = $_POST['salenetamt'];

$totalamt       = stripslashes($_POST['totalamt']);
$totalGSTamt    = stripslashes($_POST['totalGSTamt']);
$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  saleheader set status='DELETED'  where id='$saleID' and status=''");
$deldtl=mysqli_query($conn,"update  saledetails set status='DELETED'  where saleheader_id='$saleID' and status=''");

	
		

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

$autoinc2=mysqli_query($conn,"ALTER TABLE saledetails CHANGE id id INT(11) NOT NULL AUTO_INCREMENT ");
		
		$count=0;
$check_query	= mysqli_query($conn,"SELECT count(invoice_no) as counts FROM saleheader 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 = "saleDashboard.php";</script>
<?php
		

}		
else
{

//SELECT invoice_no, date, customer_id, dc_num, po_num, itemcount, tamount, create_date, flag FROM `` WHERE 1
$sql_saleheader  = "INSERT INTO saleheader (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 saledetails( saleheader_id, productID, sale_eqty, sale_prate, sale_gst, sale_cgst, sale_sgst, salenetamt,create_date, flag,accountyear) VALUES ('$last_id','$productID[$key]',$eqtyinput[$key],'$prateinput[$key]','$gstinput[$key]','$cgstinput[$key]','$sgstinput[$key]','$salenetamt[$key]','$create_date','1','$accountyear')";

    mysqli_query( $conn, $sql_saledetails );

}

header("Location:saleDashboard.php");

}
}

?>