<?php

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

if ($_SERVER["REQUEST_METHOD"] == "POST") {

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

$salePO         = "";

$sales_mode=$_POST['sales_mode'];

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

$category    = $_POST['category'];
$gross_weight    = $_POST['gross_weight'];
$wastage_percentage=$_POST['wastage_percentage'];
$wastage    = $_POST['wastage'];
$making_charge    = $_POST['making_charge'];

$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'];
			
		}
		
		
		$count=0;
$check_query	= mysqli_query($conn,"SELECT count(invoice_no) as counts FROM saleheaderwo 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 = "saleDashboardwo.php";</script>
<?php
		

}		
else
{

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

$autoinc2=mysqli_query($conn,"ALTER TABLE saledetailswo 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 saleheaderwo (invoice_no, sale_date, customer_id,billing_address,delivery_address,remarks,  po_num,saleGSTNO,sales_mode, itemcount, tamount, create_date, flag, accountyear,status) VALUES ('$saleinvoice','$saledate','$customerName','$billing_address','$delivery_address','$remarks','$salePO','$saleGSTNO','$sales_mode','$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 saledetailswo( saleheader_id, category,productID,gross_weight,wastage_percentage,wastage,making_charge, sale_eqty, sale_prate, sale_gst, sale_cgst, sale_sgst, salenetamt,create_date, flag,accountyear) VALUES ('$last_id','$category[$key]','$productID[$key]','$gross_weight[$key]','$wastage_percentage[$key]','$wastage[$key]','$making_charge[$key]',$eqtyinput[$key],'$prateinput[$key]','$gstinput[$key]','$cgstinput[$key]','$sgstinput[$key]','$salenetamt[$key]','$create_date','1','$accountyear')";

    mysqli_query( $conn, $sql_saledetails );

}

header("Location:saleDashboardwo.php");

}
}

?>