<?
 session_start();
if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); parent.document.location='ind.php' </script>";
  return;
}
include("opendmsdb.php");
include("../test.php");
?>
<link rel="stylesheet" href="style.css" type="text/css" />
<style type="text/css" media="print">
.dontprintme { display: none; }
</style>
<style>
body,td{font-family:'arial narrow';letter-spacing:1;font-size:.9em}
th{background-color:c1d799;font-size:.9em}
.mnu{margin-left:5}
</style>
<center>
<table width=100% bgcolor='lightsteelblue'>
<tr><td align=center><h3><?print $branch?> <br>DEBTORS LIST </TD></tr>
</table>
<br>
<table border=0 width=90%>
<tr><td align=center valign=top>

<table border=1> 
<th colspan=8>SUMMARY - PSN</th>
<TR>
<TH>BRANCH</TH><TH>TOTAL 
<?
$br=mysqli_query($db,"select distinct a.brcode,b.brname,date_format(a.upddate,'%W %d %M %Y %r') from debt1 a, branch b where a.brcode=b.brcode and a.brcode like 'psn%' group by a.brcode") or die(mysqli_error());
$t1=$t2=0;
while($brrow=mysqli_fetch_array($br))
{
 $sq="select sum(bal) from debt1 where brcode='$brrow[0]'";
  $res=mysqli_query($db,$sq) or die('err'.mysqli_error());
  print "<tr><td rowspan=2 align=center><b>$brrow[1]</B><br>As on ".$brrow[2]."</td>";
   while($row=mysqli_fetch_array($res))
    {
      print "<tr><td align=right>$row[0]</td>";
      $t1+=$row[0];
    }
}
  print "<tr bgcolor='c1d799'><td align=center><b>Total</td><td align=RIGHT>".putcoma($t1)."</td></tr>";
?>
</table>
<!--------------------------->
<hr>
<table border=1> 
<th colspan=8>SUMMARY - AMPL CALICUT COMMAND AREA</th>
<TR>
<TH>BRANCH</TH><TH>TOTAL 
<?
$br=mysqli_query($db,"select distinct a.brcode,b.brname,date_format(a.upddate,'%W %d %M %Y %r') from debt1 a, branch b where a.brcode=b.brcode and a.brcode like 'amp%' group by a.brcode") or die(mysqli_error());
$t2=0;
while($brrow=mysqli_fetch_array($br))
{
 $sq="select sum(bal) from debt1 where brcode='$brrow[0]'";
  $res=mysqli_query($db,$sq) or die('err'.mysqli_error());
  print "<tr><td rowspan=2 align=center><b>$brrow[1]</B><br>As on ".$brrow[2]."</td>";
   while($row=mysqli_fetch_array($res))
    {
      print "<tr><td align=right>$row[0]</td>";
      $t2+=$row[0];
    }
}
  print "<tr bgcolor='c1d799'><td align=center><b>Total</td><td align=RIGHT>".putcoma($t2)."</td></tr>";
 // print "<tr bgcolor='c1d799'><td align=center><b>Grant Total</td><td align=RIGHT>".putcoma($t1+$t2)."</td></tr>";

?>
</table>

<!--------------------------->
<hr>
<table border=1> 
<th colspan=8>SUMMARY - AMPL MANGALORE COMMAND AREA</th>
<TR>
<TH>BRANCH</TH><TH>TOTAL 
<?
$br=mysqli_query($db,"select distinct a.brcode,b.brname,date_format(a.upddate,'%W %d %M %Y %r') from debt1 a, branch b where a.brcode=b.brcode and a.brcode like 'MAN%' group by a.brcode") or die(mysqli_error());
$t3=0;
while($brrow=mysqli_fetch_array($br))
{
 $sq="select sum(bal) from debt1 where brcode='$brrow[0]'";
  $res=mysqli_query($db,$sq) or die('err'.mysqli_error());
  print "<tr><td rowspan=2 align=center><b>$brrow[1]</B><br>As on ".$brrow[2]."</td>";
   while($row=mysqli_fetch_array($res))
    {
      print "<tr><td align=right>$row[0]</td>";
      $t3+=$row[0];
    }
}
  print "<tr bgcolor='c1d799'><td align=center><b>Total</td><td align=RIGHT>".putcoma($t3)."</td></tr>";
//  print "<tr bgcolor='c1d799'><td align=center><b>Grant Total</td><td align=RIGHT>".putcoma($t1+$t2)."</td></tr>";

?>
</table>


<!--------------------------->
<hr>
<table border=1> 
<th colspan=8>SUMMARY - AMPL ERNAKULAM (SPARES)</th>
<TR>
<TH>BRANCH</TH><TH>TOTAL 
<?
$br=mysqli_query($db,"select distinct a.brcode,b.brname,date_format(a.upddate,'%W %d %M %Y %r') from debt1 a, branch b where a.brcode=b.brcode and a.brcode like 'emp%' group by a.brcode") or die(mysqli_error());
$t4=0;
while($brrow=mysqli_fetch_array($br))
{
 $sq="select sum(bal) from debt1 where brcode='$brrow[0]'";
  $res=mysqli_query($db,$sq) or die('err'.mysqli_error());
  print "<tr><td rowspan=2 align=center><b>$brrow[1]</B><br>As on ".$brrow[2]."</td>";
   while($row=mysqli_fetch_array($res))
    {
      print "<tr><td align=right>$row[0]</td>";
      $t4+=$row[0];
    }
}
  print "<tr bgcolor='c1d799'><td align=center><b>Total</td><td align=RIGHT>".putcoma($t4)."</td></tr>";
  print "<tr bgcolor='c1d799'><td align=center><b>Grant Total</td><td align=RIGHT>".putcoma($t1+$t2+$t3+$t4)."</td></tr>";

?>
</table>







</td><td align=center valign=top>
<form action="<?print $PHP_SELF?>">
<table class=dontprintme bgcolor='c1d799' width=99%>
<tr><td align=center colspan=2><h3>DEBTORS LIST</TD></tr>
<tr bgcolor=white><td align=center>Select Branch </td><td> <select name='brn'>
<?
$br=mysqli_query($db,"select distinct a.brcode,b.brname,date_format(a.upddate,'%d %M %Y') from debt1 a, branch b where a.brcode=b.brcode") or die(mysqli_error());
while($brrow=mysqli_fetch_array($br))
{
 print "<option value='$brrow[0]'>$brrow[1]</option>";
}
?>
</select></td></tr>
<tr><td colspan=2 align=center><input type=submit value='Show' name='show'>
<input type=button value='Close this window' onClick=window.close()>
</td></tr>
</table>
</td></tr>
</table>
</form>
<br>
<?
if($show)
{
$res=mysqli_query($db,"select cname,bal,date_format(upddate,'%W %d %M %Y at %r'),brcode from debt1 where brcode='$brn'");
if(mysqli_num_rows($res)>0)
{
$row=mysqli_fetch_array($res);
 $bres=mysqli_query($db,"select brname from branch where brcode='$row[brcode]'");
  $b_row=mysqli_fetch_row($bres);
?>
<table width=100% bgcolor='lightsteelblue'>
<tr><td align=center><h3>DEBTORS LIST OF <?print $b_row[0]?></TD></tr>
<tr><td align=center> As on <?print $row[2]?>
</table>
<br>
<?
 print "<table cellpadding=3> <th> No. <th>Debtors <th>Balance Amt.";
 $i=1;
 $t=0;
do
{
 if($i%2==0)
   print "<tr bgcolor=edf6dd> ";
 else
   print "<tr>";
 print "<td align=center>".$i++."</td><td>$row[0]</td><td align=right>$row[1]</td>";
 $t+=$row[1];
}while($row=mysqli_fetch_row($res));
print "<tr bgcolor=c1d799><td colspan=2 align=center><b>Total </td><td align=right>$t</td>";
print "</table>";
}
} // end if show
?>
