<?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;
}
include("opendb.php");
$cname=$_REQUEST[cname];
$invno=$_REQUEST[invno];
$dwnamt=$_REQUEST[dwnamt];
$brnchtp=$_REQUEST[brnchtp];

$tbl="advamt".$brcode.$user;
//mysqli_query($db,"drop table $tbl");
mysqli_query($db,"CREATE TABLE $tbl (invno varchar(100),invamt double(10,2) default 0,advamt double(10,2) default 0,customer varchar(200))");
//mysqli_query($db,"delete from $tbl");	
$tot_payment=0;
$outstanding=0;
$mri_cname = substr($cname,-9);
$mle_cname = substr($cname,0,5);
   $sql=mysqli_query($db,"select sum(amt) from downpayment where invno='$invno'  and right(customer,9)='$mri_cname' and left(customer,5)='$mle_cname'  and brcode='$brcode'");
   $res=mysqli_fetch_array($sql);

    $adv=$res[0];


   if((mysqli_num_rows($sql)<=0)||($res[0]==''))
    {
      $adv=0; 
    }
/*
if($brnchtp=='ac')
{
    $sql1=mysqli_query($db,"select sum(tot) from spareinvoice_oth where invno='$invno' and customer='$cname' and brcode='$brcode'");
   	$res1=mysqli_fetch_array($sql1);
	$tot_payment=round($res1[0],0);
	if(mysqli_num_rows($sql1)<=0)
    {
        $tot_payment=0; 
    }
   	if($tot_payment>0)
	{      
        $outstanding=$tot_payment-$adv;
    }
}

if($brnchtp=='bo')
{
*/
	$sql1=mysqli_query($db,"select sum(tot) from psn_ampleicher1819.spareinvoice where invno='$invno'  and right(customer,9)='$mri_cname' and left(customer,5)='$mle_cname'  and brcode='$brcode'");
   	$res1=mysqli_fetch_array($sql1);
	$tot_payment=round($res1[0],0);
	if(mysqli_num_rows($sql1)<=0)
    {
        $tot_payment=0; 
    }
   	if($tot_payment>0)
	{      
        $outstanding=$tot_payment-$adv;
    }
//}

$rr=mysqli_query($db,"select invno from $tbl where invno='$invno'");
$rrrw=mysqli_fetch_array($rr);
if($rrrw[0]<=0)
{
    mysqli_query($db,"insert into $tbl (invno,invamt,customer,advamt) values('$invno','$tot_payment','$cname','$dwnamt')") or die('err12'.mysqli_error($db));
}
else
{
    mysqli_query($db,"update $tbl set advamt='$dwnamt' where invno='$invno' and customer='$cname'") or die('err12'.mysqli_error($db));
}

//mysqli_query($db,"insert into $tbl (invno,invamt,customer,advamt) values('$invno','$tot_payment','$cname','$dwnamt')") or die('err12'.mysqli_error($db));
     

?>
<input type='hidden' name='advamtnw' id='advamtnw' value='<? echo $adv ?>'>
<input type='hidden' name='outstamtnw' id='outstamtnw' value='<? echo $outstanding ?>'>


<?
$tbl="advamt".$brcode.$user;
$query="select * from $tbl";
$res=mysqli_query($db,$query);
?>
<table border=1 class='report' id='tbldiv'>
<tr><th>Customer</th><th>INVNO</th>
    <th>Bill Amt</th><th>Advance</th>
</tr>
<?
while($row=mysqli_fetch_array($res))
{
?>
<tr><td><? echo $row[customer] ?></td><td><? echo $row[invno] ?></td>
    <td><? echo $row[invamt] ?></td><td><? echo $row[advamt] ?></td>
</tr>
<?
$totamt+=$row[invamt];
$totdwnamt+=$row[advamt];
}
?>
<tr><th colspan=2>Total Amount</th><th><? echo $totamt ?></th>
    <th><? echo $totdwnamt ?></th>
</tr>

</table>

 



