<?php
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
    print "<script> alert('Session timed out'); parent.document.location='index.php'; </script>";
    return;
}
$cname=$_REQUEST[cname];
include("opendb.php");
$brnchtp=$_REQUEST[brnchtp];
$tbl="advamt".$brcode.$user;
mysqli_query($db,"drop table $tbl"); 
$mri_cname = substr(str_replace(0,'',$cname),-8);
$mle_cname = substr($cname,0,10);
//$sq=mysqli_query($db,"select invno,sum(tot),entrydate from spareinvoice where brcode='$brcode' and right(customer,9)='$mri_cname' and left(customer,5)='$mle_cname' group by invno");  
$sq=mysqli_query($db,"select invno,sum(tot),entrydate from spareinvoice where brcode='$brcode' and left(customer,10)='$mle_cname' and right(REPLACE(customer,'0',''),8)='$mri_cname' group by invno");  
$sql12 = "select invno,sum(tot),entrydate from spareinvoice where brcode='$brcode' and left(customer,10)='$mle_cname' and right(REPLACE(customer,'0',''),8)='$mri_cname' group by invno";
//echo $sql12;
print "<td>INV NO: </td><td><select name='invno' id='invno' onchange='getadvamt()'>";
print "<option value='select'>Advance</option>";
while($rw=mysqli_fetch_array($sq))
{
    $tot_payment=0;
    $outstanding=0;
    //$sql11=mysqli_query($db,"select sum(amt) from downpayment where invno='$rw[0]' and right(customer,9)='$mri_cname' and left(customer,5)='$mle_cname' and brcode='$brcode' ");
    $sql11=mysqli_query($db,"select sum(amt) from downpayment where invno='$rw[0]' and right(REPLACE(customer,'0',''),8)='$mri_cname' and left(customer,10)='$mle_cname' and brcode='$brcode' ");
    $res11=mysqli_fetch_array($sql11);
    $adv=$res11[0];
    if((mysqli_num_rows($sql11)<=0)||($res11[0]==''))
    {
        $adv=0; 
    }
//	    $sql1=mysqli_query($db,"select sum(tot) from spareinvoice where invno='$rw[0]' and customer='$cname' and brcode='$brcode' group by invno");
//   	$res1=mysqli_fetch_array($sql1);
//      $tot_payment=$res1[0];
	$tot_payment=$rw[1];
    if($tot_payment<=0)
   	{
  		$tot_payment=0; 
   	}
    if($tot_payment>0)
    {      
   		$outstanding=$tot_payment-$adv;
	}
	//echo $tot_payment;
	//echo "<br>";
	//echo $adv;
	//echo "<br>";
	//echo $outstanding;
	//echo "<br>";
	
    if($outstanding>0)
    {
        ?>
        
        <option value='<? echo $rw[0] ?>'><? echo $rw[0]?></option>
        
        <?
    }
    ?>
    
<?    
}
?>
</select>
<?
$crquery=mysqli_query($db,"select * from crnote where brcode='$brcode' and partic='$cname'");

?>
        </td>
        <td >Credit Note No.:</td><td ><select name='crnoteno' onchange='getcrdamt(this.value)'>
            <option value='select'>select</option>
            <?
            while($crrw=mysqli_fetch_array($crquery))
            {
                $sql=mysqli_query($db,"select sum(amt) from downpayment where brcode='$brcode' and trno='$crrw[1]' and customer='$crrw[partic]'");
                
                $rsrow=mysqli_fetch_row($sql);
                $downamt=$rsrow[0];
                if($crrw[cramt]>$downamt)
                {
            ?>
            <option value='<? echo $crrw[1] ?>'><? echo $crrw[1]?></option>
            <?
            }
            }
            ?>
            </select></td>