<?php

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

//require_once("checkLoginStatus.php"); 
$createdby      = $_SESSION['username'];

$saleID         = stripslashes($_POST['edit_id']); 

$cashno   = stripslashes($_POST['invoice']);
$collectiondate   = new DateTime(stripslashes($_POST['invoicedate']));
$collectiondate 		= $collectiondate->format('Y-m-d');
$customerid   = stripslashes($_POST['supplierid']);
$sales_inv=$_POST['sales_inv'];

$pending   = $_POST['tamount'];
$discount   = $_POST['discount'];
$tray_amount   = stripslashes($_POST['tray_amount']);

$ramount   = stripslashes($_POST['payamount']);

$paymenttype   = stripslashes($_POST['paymenttype']);
$paymentreference   = $_POST['paymentreference'];

$cash_amount   = "";
$bank_amount   = "";

$bank_name   = '';
$handover   = '';
$transactionno   = '';

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




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

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

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

}		
else
{


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

$sql_update_sales=mysqli_query($conn,"update saleheader set pending_amount='$pending',collection_amount=$ramount,discount='$discount',payment_type='$paymenttype',cash_amount='$cash_amount',bank_amount='$bank_amount' where invoice_no='$sales_inv' and status=''");

//echo "update saleheader set pending_amount='$pending',collection_amount=$ramount,discount='$discount',payment_type='$paymenttype' where invoice_no='$sales_inv' where status=''";

$sqlh  = "INSERT INTO customer_payment_header   (cashno,sales_inv,collectiondate,customerID,pending, paymenttype,paymentreference ,bank_name,handover,transactionno,remark,paidby,received_amount,cash_amount,bank_amount,discount,tray_amount,saleid,payment_through,status,flag,accountyear) VALUES ('$cashno','$sales_inv','$collectiondate','$customerid','$pending','$paymenttype','$paymentreference','$bank_name','$handover','$transactionno','$remark','$createdby','$ramount','$cash_amount','$bank_amount','$discount','$tray_amount','0','Accounts','','1','$accountyear')";

//echo $sql;
mysqli_query($conn,$sqlh);




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

?>