<?php

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

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

$sql_InvoiceQuery   = mysqli_query($conn,"SELECT count(*) as invoice FROM customer_payment_header WHERE accountyear='$accountyear' and status='' ");
$rowInvoiceCount    = mysqli_fetch_array($sql_InvoiceQuery,MYSQLI_ASSOC);

$invoice_count      = $rowInvoiceCount['invoice']+1;

$acyear="SCL/".$syr."-".$eyr."/";

$invoice= $acyear.$invoice_count;
$cashno   = $invoice;

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

$discount=$_POST['discount'];



$row=mysqli_fetch_array($sql,MYSQLI_ASSOC);

/*

$sql_opening=mysqli_query($conn,"select opening_balance from customer_master where id='$customerid' ");
$row_opening=mysqli_fetch_array($sql_opening,MYSQLI_ASSOC);

$opening_balance=$row_opening['opening_balance'];*/

$pamount1        = $_POST['tamount'];

$pamount=number_format($pamount1,2,'.','');
$pending   = $pamount;

//$pending   = $_POST['tamount'];

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


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

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

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

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




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



$msql2=mysqli_query($conn,"select * from customer_master where id='".$customerid."'");
        
		while($rw2=mysqli_fetch_array($msql2))
		{
			
		$cname=$rw2['cname'];
			
		}
		
		$autoinc_collection=mysqli_query($conn,"ALTER TABLE customer_payment_header CHANGE id id INT(11) NOT NULL AUTO_INCREMENT ");

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



$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','','$collectiondate','$customerid','$pending','$paymenttype','$paymentreference','$bank_name','$handover','$transactionno','$remark','$createdby','$ramount','$cash_amount','$bank_amount','$discount','0','0','Accounts','','1','$accountyear')";

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




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

?>