<?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']);

$sql = mysqli_query($conn,"SELECT a.id,a.ccode,a.cname,a.mobile,sum(a.samount) as samount,sum(payamount) as payamount,sum(discount) as discount 
FROM (
  SELECT cm.id,cm.ccode,cm.cname,cm.mobile,SUM(sd.tamount) as samount,0 as payamount,0 as discount from customer_master as cm LEFT JOIN saleheader sd ON cm.id=sd.customer_id WHERE cm.isdelete=1 and sd.status='' and cm.id='$customerid' GROUP BY cm.id
  
  UNION ALL
  
  SELECT cm.id,cm.ccode,cm.cname,cm.mobile,0 as samount,sum(sc.received_amount) as payamount,sum(discount) as discount from customer_master as cm LEFT JOIN customer_payment_header as sc ON cm.id=sc.customerID WHERE cm.isdelete=1 and sc.status='' and cm.id='$customerid' GROUP by cm.id
  
 

) a GROUP BY a.id  ");



$row=mysqli_fetch_array($sql,MYSQLI_ASSOC);

$sql_return=mysqli_query($conn,"select Sum(tamount) as return_amount from sale_return_header where status='' and customer_id='$customerid' ");
$row_return=mysqli_fetch_array($sql_return,MYSQLI_ASSOC);

$return_amount=$row_return['return_amount'];

$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        = ($row['samount']+$opening_balance)-($row['payamount']+$row['discount']+$return_amount);

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

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

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

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

$cash_amount   = $_POST['cash_amount'];
$bank_amount   = $_POST['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");
}

?>