<?php

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

if ($_SERVER["REQUEST_METHOD"] == "POST") {
     $saleID         = stripslashes($_POST['edit_id']);  
     $accountyear    = $financialYearStart."-".$financialYearEnd;
    

    $delhdr1=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=''");
    

$saleinvoice= stripslashes($_POST['saleinvoice']);
    

//$saleinvoice    = stripslashes($_POST['saleinvoice']);
$saledate       = new DateTime(stripslashes($_POST['saledate']));
$saledate 		= $saledate->format('Y-m-d');
$customer_id   = stripslashes($_POST['customerName']);
$customerid=$customer_id;


$billing_address=$_POST['billing_address'];
$kot=$_POST['kot'];
$notes=$_POST['notes'];



$user=$_SESSION['username'];

$delivery_date=$_POST['delivery_date'];



$productID    = $_POST['productID'];
$category    = $_POST['category'];
$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);
$tqty      = $_POST['totalqty'];;

 $msql2=mysqli_query($conn,"select * from customer_master where id='".$customer_id."'");
        
		while($rw2=mysqli_fetch_array($msql2))
		{
			
		$cname=$rw2['cname'];
			
		}
		
		
		
$autoinc=mysqli_query($conn,"ALTER TABLE saleheader CHANGE id id INT(11) NOT NULL AUTO_INCREMENT ");
$autoinc1=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
{


	


		
		
$sql_saleheader  = "INSERT INTO saleheader (invoice_no, sale_date, customer_id,cname, billing_address,delivery_date,kot,notes,itemcount, tamount,status,user, create_date, flag, accountyear) VALUES ('$saleinvoice','$saledate','$customer_id','$cname','$billing_address','$delivery_date','$kot','$notes','$tqty','$totalNetamt','','$user','$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) {
   
   $prid=$productID[$key];


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

    mysqli_query( $conn, $sql_saledetails );

}



}
header("Location:saleDashboard.php");


}

?>