<?php 
session_start();
include('../includes/dbcon.php');

$edate=date("Y-m-d");
$userid=$_SESSION['rm_id'];

$appamt=0;
$cus_id= $_POST["cus_id"];
$db_doc= $_POST["db_doc"];
$cr_doc= $_POST["cr_doc"];
$dd = $_POST["dd"];
$appamt_doc= $_POST['appamt'];

for($i = 0; $i<count($dd); $i++){
	$appamt= $appamt_doc[$i];
	$appdocid=$dd[$i];

}


////////////////////DB balance //////////////////////
$sqlsp=mysqli_query($con,"SELECT *,IFNULL((SELECT SUM(applyamount)FROM sedetail
WHERE sedetail.docid=seheader.docid AND seheader.party_id=sedetail.party_id AND
 docid IN (SELECT docid FROM seheader WHERE cancel<>1 AND docdate<= '$edate' )
 AND Refdocno IN (SELECT docid FROM seheader AS sh2 WHERE cancel<>1 AND docdate<= '$edate' )
 ),0) 
  AS app,
  (seheader.amount-
  IFNULL((SELECT SUM(applyamount)FROM sedetail
WHERE sedetail.docid=seheader.docid AND seheader.party_id=sedetail.party_id AND
 docid IN (SELECT docid FROM seheader WHERE cancel<>1 AND docdate<= '$edate' )
 AND Refdocno IN (SELECT docid FROM seheader AS sh2 WHERE cancel<>1 AND docdate<= '$edate' )
 ),0) )
  AS bal
 FROM seheader
 WHERE seheader.cancel<>1 AND seheader.party_id=$cus_id  AND seheader.docid='$db_doc' ")or die(mysqli_error($con));
	        	$count=mysqli_num_rows($sqlsp);	
	        	if ($count>0) {
	        
	        		while($row=mysqli_fetch_array($sqlsp)){	

	        			 //$docid=$row['docid'];
	        			 $bal_db=$row['bal'];


	        			 //echo  $bal_db;
 					}  ///// while($row=mysqli_fetch_array($sqlsp)){
 					}	

/////////////// End of DB balance /////////////////



////////////////////  Credit balance //////////////////////
$sqlsp=mysqli_query($con,"SELECT *,IFNULL((SELECT SUM(applyamount)FROM sedetail
WHERE sedetail.docid=seheader.docid AND seheader.party_id=sedetail.party_id AND
 docid IN (SELECT docid FROM seheader WHERE cancel<>1 AND docdate<= '$edate' )
 AND Refdocno IN (SELECT docid FROM seheader AS sh2 WHERE cancel<>1 AND docdate<= '$edate' )
 ),0) 
  AS app,
  (seheader.amount-
  IFNULL((SELECT SUM(applyamount)FROM sedetail
WHERE sedetail.docid=seheader.docid AND seheader.party_id=sedetail.party_id AND
 docid IN (SELECT docid FROM seheader WHERE cancel<>1 AND docdate<= '$edate' )
 AND Refdocno IN (SELECT docid FROM seheader AS sh2 WHERE cancel<>1 AND docdate<= '$edate' )
 ),0) )
  AS bal
 FROM seheader
 WHERE seheader.cancel<>1 AND seheader.party_id=$cus_id  AND seheader.docid='$cr_doc' ")or die(mysqli_error($con));
	        	$count=mysqli_num_rows($sqlsp);	
	        	if ($count>0) {
	        
	        		while($row=mysqli_fetch_array($sqlsp)){	

	        			 //$docid=$row['docid'];
	        			 $bal_cr=$row['bal'];


	        			 //echo  $bal_cr;
 					}  ///// while($row=mysqli_fetch_array($sqlsp)){
 					}	

/////////////// End of DB balance /////////////////





if ($appamt<= $bal_db){
	if ($appamt<= abs($bal_cr)) {
		mysqli_begin_transaction($con);
			///////////////////////////////  Insert into table////////////////////////

			 //// doc db///////////////////////
			if ( mysqli_query($con,"INSERT INTO sedetail(docid,applyamount,applydate,Refdocno,initdoc,party_id,userid,edate,sd) 
			 VALUES('$db_doc',$appamt,'$edate','$cr_doc','$db_doc','$cus_id',$userid,'$edate',1)  ")or die(mysqli_error($con)))
			{

if ( mysqli_query($con,"INSERT INTO sedetail(docid,applyamount,applydate,Refdocno,initdoc,party_id,userid,edate,sd) 
			 VALUES('$cr_doc',0-$appamt,'$edate','$db_doc','$db_doc','$cus_id',$userid,'$edate',1)  ")or die(mysqli_error($con)))
			{

				mysqli_commit($con);
				echo "<script>document.location='settle_new.php'</script>";
			}


			}


			////// End of inserting ////////////////////////////////



	}	// if ($appamt<= abs($bal_cr)) {
		else { echo "<script type='text/javascript'>alert('Settle Amount exceed the balance!');</script>";
	          echo "<script>document.location='settle_new.php'</script>";  }
}   // if ($appamt<= $bal_db){
else { 

echo "<script type='text/javascript'>alert('Settle Amount exceed the balance!');</script>";
 echo "<script>document.location='settle_new.php'</script>";



	 }



?>