<?
session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Invalid Login'); parent.document.location='ind.php' </script>";
 return;
}
include('includes/top.php'); 
include("opendb.php");
include("test.php");
?>
<link type="text/css" href="includes/style1.css" rel="stylesheet" />
<link type="text/css" href="css/form1.css" rel="stylesheet" />
<style>
td{font-family:arial narrow;font-size:14;background-color:white}
th{background-color:silver};
</style>
<center>
<?
//print "<b>".$branch."</b>";
if($show)
{
$dt=$y.'-'.$m.'-'.$d;
 $s="select date_add('$dt', interval -30 day)";

$q1=mysqli_query($db,$s)or die(mysqli_error($db));
$row30=mysqli_fetch_array($q1)or die(mysqli_error($db));
$d_30=$row30[0];
$q2=mysqli_query($db,"select date_add('$dt', interval -60 day)");
$row30=mysqli_fetch_array($q2);
$d_60=$row30[0];

$q3=mysqli_query($db,"select date_add('$dt', interval -90 day)");
$row30=mysqli_fetch_array($q3);
$d_90=$row30[0];


print "<br> Creditors aging list for ".datetohtml($dt)."<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 * from ledger where grp='Sundry Creditors' 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' and date<='$dt'";
 $balres=mysqli_query($db,$sql)or die('err1'.mysqli_error($db));
  $balrow=mysqli_fetch_row($balres)or die('err2'.mysqli_error($db));
  if(mysqli_num_rows($balres)<=0)
    $balrow[0]=0;
 $balance=$opbal-$balrow[0];


if($balance>0)
{
$t_bal+=$balance;
  print "<tr><td>".$i++."</td><td><a href='vmledger.php?cd=".str_replace('&','~',$srow[led])."'>$srow[led]</a></td><td align=right>".putComa($balance)."</td>";
 if(mysqli_num_rows($balres)<=0)
 {
//1==================
  print "<td>$balance</td>";
 }
	$sql="select sum(cramt) from journal2 where partic='$srow[led]' and date>='$d_30'  and date<='$dt' and brcode='$brcode'"; 


 $res1=mysqli_query($db,$sql);
  $row=mysqli_fetch_array($res1);
   $r3=$row[0];
//   $r3+=$opbal;
  if($balance>$r3)
  { print "<td align=right>$r3</td>"; 
//2222222---------------
      $tot2+=$r3;
   }
  else
   {print "<td align=right>".putComa($balance)."</td>";
	      $tot2+=$balance;

  continue;}
  $balance-=$r3;
 //************

	$sql="select sum(cramt) from journal2 where partic='$srow[led]' and date<'$d_30' and date>='$d_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>";
//333333333-----------
    $tot3+=$r3; }
  else
   {print "<td align=right>".putComa($balance)."</td>";
     $tot3+=$balance;
     continue;
   }
  $balance-=$r3;

//************
	$sql="select sum(cramt) from journal2 where partic='$srow[led]' and date<'$d_60' and date>='$d_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>"; $tot4+=$r3;}
//4444444444444-------------
  else
   {print "<td align=right>".putComa($balance)."</td>";
	$tot4+=$balance;
    continue;}
  $balance-=$r3;
//************
	$sql="select sum(cramt) from journal2 where partic='$srow[led]' and date<'$d_90'  and brcode='$brcode'"; 


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


}
}
print "<tr><td colspan=2 align=center>Total</td><td align=right>".putComa($t_bal)."</td>";
print "<td align=right>".putComa($tot2)."</td>";
print "<td align=right>".putComa($tot3)."</td>";
print "<td align=right>".putComa($tot4)."</td>";
print "<td align=right>".putComa($tot5)."</td>";
?>
</table>
<?
return;
} // end if show
?>
<center>
<form action='<?Print $PHP_SELF?>'>
<br>
<table cellpadding=3>
<th colspan=3>Creditors aging list</th>
<tr><td>Date </td><td>
  dd <input type=text name='d' size=2 value='<?print date('d')?>'>
  mm <input type=text name='m' size=2 value='<?print date('m')?>'>
  yy <input type=text name='y' size=4 value='<?print date('Y')?>'>
</td><td><input type=submit value='Show' name='show'></td></tr>
<tr><th colspan=3></th></tr>
</table>
</form>

