<?
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>";
if($show)
{
	$dt=$y.'-'.$m.'-'.$d;
	$s="select date_add('$dt', interval -30 day)";
	$q1=mysqli_query($db,$s)or die(mysqli_error());
	$row30=mysqli_fetch_array($q1)or die(mysqli_error());
	$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> Debtors 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;
//	$sql="select sum(dramt)-sum(cramt) from journal2 where partic='$srow[led]' and brcode='$brcode' and date<='$dt'";
	$re="select (sum(dramt)-sum(cramt)) as bal,partic,op from journal2,ledger where ledger.led=journal2.partic and journal2.date<='$dt' and ledger.grp='Sundry Debtors' and journal2.brcode='$brcode' group by journal2.partic";
//	$re=mysqli_query($db,"select * from ledger where grp='Sundry Debtors' and brcode='$brcode' order by led ")or die(mysqli_error());
	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());
//		$balrow=mysqli_fetch_row($balres)or die('err2'.mysqli_error());
		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>$srow[led]</td><td align=right>".putComa($balance)."</td>";
			if(mysqli_num_rows($balres)<=0)
			{
				print "<td>$balance</td>";
			}
			$sql="select sum(dramt) 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>"; 
				$tot2+=$r3;
   			}
  			else
   			{
				print "<td align=right>".putComa($balance)."</td>";
	      			$tot2+=$balance;
  				continue;
			}
  			$balance-=$r3;
			//************
			$sql="select sum(dramt) 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>";
    				$tot3+=$r3; 
			}
  			else
   			{
				print "<td align=right>".putComa($balance)."</td>";
     				$tot3+=$balance;
     				continue;
   			}
  			$balance-=$r3;
			//************
			$sql="select sum(dramt) 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;
			}
  			else
   			{
				print "<td align=right>".putComa($balance)."</td>";
				$tot4+=$balance;
			    	continue;
			}
			$balance-=$r3;
			//************
			$sql="select sum(dramt) 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;		
			}
			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?>' method='post'>
<table cellpadding=3>
<th colspan=3>Debtors 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>

