<?
session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Invalid Login'); parent.document.location='ind.php' </script>";
 return;
}
include("../opendb.php");
include("../test.php");
?>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
body,td{font-family:arial narrow;font-size:14;background-color:white}
th{background-color:silver};
</style>
<center>
<?
print "<b>".$branch."</b>";
print "<br> Debtors aging list as on ".date("r")."<br>[printed by $user]";
print "<table border=0 cellspacing=1 cellpadding=3 bgcolor='gray'>";
print "<th>No. <th>Debtor<th>Total <th><30 <th> >=30 & <60 <th> >=60 & <90 <th> >=90";
$i=1;
$t_bal=0;
$re=mysqli_query($db,"select distinct led,grp,op,drcr from ledger where grp='Sundry Debtors' and brcode='$brcode' order by led")or die(mysqli_error($db));
while($srow=mysqli_fetch_array($re))
{
 $opbal=$srow[op];
 $sql="select sum(dramt)-sum(cramt) from journal2 where partic='$srow[led]' and brcode='$brcode'";
 $balres=mysqli_query($db,$sql)or die('err1'.mysqli_error($db));
  $balrow=mysqli_fetch_row($balres)or die('err2'.mysqli_error($db));
 $balance=$opbal+$balrow[0];
if($balance>0)
{
$t_bal+=$balance;
  print "<tr><td>".$i++."</td><td>$srow[led]</td><td align=right>".putComa(round($balance,2))."</td>";
	$sql="select sum(dramt) from journal2 where partic='$srow[led]' and (to_days(curdate())-to_days(date))<=30 and brcode='$brcode'"; 

 $res1=mysqli_query($db,$sql);
  $row=mysqli_fetch_array($res1);
   $r3=$row[0];
  if($balance>$r3)
  { print "<td align=right> $r3</td>"; }
  else
   {print "<td align=right>".putComa(round($balance,2))."</td>";continue;}
  $balance-=$r3;
 //************
	$sql="select sum(dramt) from journal2 where partic='$srow[led]' and to_days(curdate())-to_days(date)>30 and to_days(curdate())-to_days(date)<=60 and brcode='$brcode'"; 

 
 $res2=mysqli_query($db,$sql);
  $row=mysqli_fetch_array($res2);
   $r3=$row[0];
  if($balance>$r3)
  { print "<td align=right> $r3</td>"; }
  else
   {print "<td align=right>".putComa(round($balance,2))."</td>";continue;}
  $balance-=$r3;

//************
	$sql="select sum(dramt) from journal2 where partic='$srow[led]' and to_days(curdate())-to_days(date)>60 and to_days(curdate())-to_days(date)<=90 and brcode='$brcode'"; 

 
 $res3=mysqli_query($db,$sql);
  $row=mysqli_fetch_array($res3);
   $r3=$row[0];
  if($balance>$r3)
  { print "<td align=right> $r3</td>"; }
  else
   {print "<td align=right>".putComa(round($balance,2))."</td>";continue;}
  $balance-=$r3;
//************
	$sql="select sum(dramt) from journal2 where partic='$srow[led]' and to_days(curdate())-to_days(date)>90  and brcode='$brcode'"; 

 
 $res4=mysqli_query($db,$sql);
  $row=mysqli_fetch_array($res4);
   $r3=$row[0];
  if($balance>$r3)
  { print "<td align=right> $r3</td>"; }
  else
   {print "<td align=right>".putComa(round($balance,2))."</td>";continue;}
  $balance-=$r3;


}
}
print "<tr><td colspan=2 align=center>Total</td><td align=right>".putComa($t_bal)."</td>";
?>
</table>
