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

$str=" ";

$data = array();

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



if (isset($_POST['cus_id'])) {
    $cus_id = $_POST['cus_id'];



if (empty($cus_id)){   $item=0;   $error=1;       }   


if ( $error<>1){

///////////////opening inv header //////////////////////

	$sqlinv=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.amount>0  ")or die(mysqli_error($con));
	        	$countinv=mysqli_num_rows($sqlinv);	
	        	if ($countinv>0) {
	        		 $options = '<option value="0">~~~Select ~~~~</option>';
	        		while($row=mysqli_fetch_array($sqlinv)){	

	        		$inv=$row['docid'] . " |  " .  $row['bal']  ;		
					//$loc_id =$row['loc_id'];
	        			  $options .= '<option value="' . $row['docid'] . '">' . $inv . '</option>';
							
				   }
				    echo $options;
				}
				else {  echo '<option value="0">~~~Select ~~~~</option>'; }
///////  end of inv header


} /// if ( $error<>1){


} /////if (isset($_POST['cus_id'])) {
				else {  echo '<option value="0">~~~Select ~~~~</option>'; }
	

?>