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

$str=" ";


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

$dd = $_POST["dd"];
$appamt_doc= $_POST['appamt'];

$cus_id= $_POST["cus_id"];
//$rtype= $_POST["rtype"];
$amount= $_POST["amount"];
$cus_id= $_POST["cus_id"];
$emp_id= $_POST["emp_id"];
$ref= $_POST["ref"];
$note= $_POST["note"];
$cost="MAT";
$error=0;
$seq=15;



$sysapp_amt=0;
$tot_app=0;

$error=0;


mysqli_begin_transaction($con);

/////!!!!!!!!!!!!!!!!!!  sequence   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
$querydoc=mysqli_query($con,"select * from sequence where seq_id=$seq ")or die(mysqli_error());
while($row=mysqli_fetch_array($querydoc)){  
	$prefix=$row['prefix'];

//if (empty($prefix)){  $new_receipt_no=$row['cno']; }
//	else { $new_receipt_no=$prefix . $row['cno']; }
$prefix=$row['prefix'];
$numberOfDigits = 6;
$cno=$row['cno'];
$new_receipt_no = $prefix . sprintf('%0' . $numberOfDigits . 'd', $cno);

}
///~~~~~~~~~~~~~~~~~~~~~~~~~~~   End of sequence ~~~~~~~~~~~~~~~~~~


///////  Insert into seheader and chqheader /////////////////

if ( mysqli_query($con,"INSERT INTO seheader(docid,docdate,doctype,cost,amount,refno,empcode,party_id,party_type,cancel,dbcr,userid,edate) 
 VALUES('$new_receipt_no','$edate','Dn','$cost',$amount,'$ref','$emp_id',$cus_id,1,0,'db',$userid,'$edate' )  ")or die(mysqli_error($con)))
	        {

	        	
	        			if ( mysqli_query($con,"update sequence set cno =cno + 1  where seq_id=$seq  ")or die(mysqli_error($con)))			
						{   

	
	     

	      }   //// end of update sequence 
	      else {  $error=1;}     

	  }  //// end of INSERT INTO seheader
	  else {  $error=1;}
				 
	
///~~~~~~~~~~~~~~~ End of   inserting into seheader and chqheader /////////////////


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


if (!empty($appamt)){
if ($appamt>0) {



		// --- getting O/S of a document--------------		
		$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='$appdocid' ")or die(mysqli_error($con));
	        	$count=mysqli_num_rows($sqlsp);	
	        	if ($count>0) {
	        
	        		while($row=mysqli_fetch_array($sqlsp)){			
					 	   $docid=$row['docid'];	
					 	   $bal=$row['bal'];

					 	   if ($appamt<=$bal){  $sysapp_amt=$appamt;    }
					 	   else { $sysapp_amt=$bal;  }

					 	   $tot_app=$tot_app + $sysapp_amt ;
					 	   if ($tot_app<=$amount){

//////////////////settlement detail  /////////////////////
					 	   	///////apply doc settlement
if ( mysqli_query($con,"INSERT INTO sedetail(docid,applyamount,applydate,Refdocno,initdoc,party_id,userid,edate) 
 VALUES('$docid',$sysapp_amt,'$edate','$new_receipt_no','$new_receipt_no','$cus_id',$userid,'$edate')  ")or die(mysqli_error($con)))
{


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


				/////insert into chqdetail

				///~~~~~~~~~~~~~~end of /////insert into chqdetail

		} else {  $error=1;}

}  /////if ( mysqli_query($con,"INSERT INTO sedetail(
 else {  $error=1;}
			

///~~~~~~~~~~~~~end of apply doc settlement ----------------------

					 	   }

				   }	

				}

			} ////if ($appamt>0) {
		}  ///if (!empty($appamt)){



}  //////for($i = 0; $i<count($dd); $i++){
if ($error==0){
mysqli_commit($con);
echo "<script>document.location='creditnote_new.php'</script>";
}

else {
mysqli_rollback($con);
mysqli_close($con);
	echo "<script type='text/javascript'>alert('Some Error Occured!');</script>";
	echo "<script>document.location='creditnote_new.php'</script>";
}



?>