<?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');
$customer_id   = stripslashes($_POST['customerName']);
$area      = stripslashes($_POST['area']);
$area_tamil         = stripslashes($_POST['area_tamil']);
$employeeid         = stripslashes($_POST['employeeid']);
$vehicleno=$_POST['vehicleno'];
$billing_address=$_POST['billing_address'];
$del_address=$_POST['del_address'];
$notes=$_POST['notes'];

$pending_tamount=$_POST['pending_tamount'];


$user=$_SESSION['username'];




$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='".$customer_id."'");
        
		while($rw2=mysqli_fetch_array($msql2))
		{
			
		$cname=$rw2['cname'];
			
		}
		
		$msql3=mysqli_query($conn,"select * from employee_master where id='".$employeeid."'");
        
		while($rwemp=mysqli_fetch_array($msql3))
		{
			
		$ename=$rwemp['ename'];
			
		}
		
		$ename=$rw2['cname'];
			
		}
		
		
		
		


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



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

}		
else
{

		
		
$sql_saleheader  = "INSERT INTO sale_return_header (invoice_no, sale_date, customer_id,cname, area, area_tamil,employeeid,ename,vehicleno,billing_address,del_address,notes, itemcount, tamount,pending_amount,collection_amount,discount,pending_tray,in_tray,out_tray,status,user, create_date, flag, accountyear) VALUES ('$saleinvoice','$saledate','$customer_id','$cname','$area','$area_tamil','$employeeid','$ename','$vehicleno','$billing_address','$del_address','$notes','$itemcount','$totalNetamt','$pending_tamount','$payamount','$discount','$pending_tray','$tray_eqty ','$tray_out_qty','','$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) {

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

    mysqli_query( $conn, $sql_saledetails );

}

header("Location:sale_return_Dashboard.php");


}

?>