<?
session_start();

if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); document.location='ind.php' </script>";
  return;
}

include("test.php");
include ("opendb.php");
print "<center>$branch";
print " </b><center>Debtors Transactions from 1-4-2011 to 31-3-2012</center>";

?>
<style>
 td{font-family:'arial narrow';font-size:10;color:black;}
// th{font-family:'arial narrow';font-size:14;color:white;background-color:#5b5858}
// body{background-color:ivory}
</style>
<!-- <link rel="stylesheet" href="style.css" type="text/css" /> -->
<?
$res_led=mysqli_query($db,"select * from ledger where grp like 'sundry deb%' and brcode='$brcode' order by led");
while($led_row=mysqli_fetch_array($res_led))
{
$tr=0;
$tp=0;
$sql="select * from ledger where led='$led_row[led]' and brcode='$brcode'";
//print $sql;
?>


<?
$re=mysqli_query($db,$sql);
$opb=mysqli_fetch_array($re)or die('Err1'.mysqli_error());

 $sq="select sum(dramt),sum(cramt) from journal2 where partic='$led_row[led]' and brcode='$brcode'";
// print $sq;
  $chktr=mysqli_query($db,$sq);
  $chktrrow=mysqli_fetch_row($chktr);
  if($chktrrow[0]==0 && $chktrrow[1]==0 && $opb[op]==0)
    {print "</table><br>"; continue;}

?>
<br>
<table  cellspacing=0 cellpadding=2 width=100%>
<th colspan=12><font face='arial narrow' size=2 ><b><? print $led_row[led]?> </font>
</table><hr>
<table  cellspacing=0 cellpadding=2 width=100%>

<tr>
<th>|<th width><font size=2 >Date <th>|<th width><font size=2 >Vouch/Rec. No. <th>|<th><font size=2 >Debit <th>|<th><font size=2 >Credit <th>|<th><font size=2 >Ref.<th>| 
<?


if(strtoupper($opb[drcr])=="DR")
{
if($opb[op]>=0)
{
print "\n<tr style={background-color:silver}><td>|</td><td></td> <td colspan=2>| Opening balance</td><td>|</td><td align=right>".putComa(abs($opb[op]))."</td><td>|</td><td></td><td>|</td><td colspan=6></td></tr>";
$tr+=$opb[op];
}
else
{
print "\n<tr style={background-color:silver}><td>|</td><td></td> <td colspan=2>| Opening balance</td><td>|</td><td></td><td>|</td><td align=right>".putComa(abs($opb[op]))."</td><td>|</td><td colspan=3></td></tr>";
$tp+=abs($opb[op]);
}

}
else if(strtoupper($opb[drcr])=="CR")
{
if($opb[op]>=0)
{
print "\n<tr style={background-color:silver}><td>|</td><td></td><td colspan=2>Opening balance</td><td>|</td><td></td><td>|</td><td align=right>$opb[op]</td><td>|</td><td colspan=4></td></tr>";
$tp+=$opb[op];
}
else
{
print "\n<tr style={background-color:silver}><td>|</td><td></td> <td colspan=2>Opening balance</td><td>|</td><td align=right>".putComa(abs($opb[op]))."</td><td>|</td><td colspan=3></td><td>|</td></tr>";
$tr+=abs($opb[op]);
}

}

$sql="select * from journal2 where brcode='$brcode' and partic='$led_row[led]' order by date,jvno ";
$result=mysqli_query($db,$sql) or die ('Error'.mysqli_error());
while($row=mysqli_fetch_array($result))
{
   if($row[type]=='Cr') //$row[cr]==$cd)
    {
 print "\n<tr bgcolor='#ffffff' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>|</td><td>".datetohtml($row[date])."</td><td>|</td><td> &nbsp;&nbsp;  To $row[narr]-($row[j_type])</td><td>|</td><td align=right></td><td>|</td><td align=right>".putComa($row[cramt])."</td><td>|</td> <td>$row[against]:$row[no]</td><td>|</td>";
//<td align=center><input type=button value='Del' onClick=document.location='deldat.php?code=$row[0]'></td>
print "</tr>";
$tp+=$row[cramt];

  }
 else //if($row[dr]==$cd)
  {
 print "\n<tr bgcolor='#ffffff' onMouseOver=bgColor='#f7dbd4'; onMouseOut=bgColor='#fffcd6'><td>|</td><td>".datetohtml($row[date])."</td><td>|</td><td> By $row[narr]-($row[j_type])</td><td>|</td><td align=right>".putComa($row[dramt])."</td><td>|</td><td align=right></td> <td>|</td><td>$row[against]:$row[no]</td><td>|</td>";
//<td align=center><input type=button value='Del' onClick=document.location='deldat.php?code=$row[0]'></td>
print "</tr>";
$tr+=$row[dramt];

 }

}
print "<tr style={background-color:silver}><td>|</td><td colspan=3 align=center><font color=black><B>Total</td><td>|</td>";
print "<td align=right><font color=black>".putComa($tr)."</td><td>|</td>";
print "<td align=right><font color=black>".putComa(abs($tp))."</td><td colspan=2></td><td>|</td></tr>";

$bal=diff($tr,$tp);

//print "$tr-$tp = bal=".$bal." <br>- ".($tr-$tp);
//*******************************************************
// if(strtoupper($opb[drcr])=="DR" && $opb[op]<0)
// 	$bal*=-1;
  //$bal=number_format($bal,2,'','');
//$bal=number_format($bal,2,'.',','); //,2,'','');
//********************************************************
if($bal>0)
print "\n<tr><td>|</td><td colspan=3 align=center><b><font color=black>Balance</td><td>|</td><td align=right><font color=black><b>".putcoma(abs($bal))."</td><td>|</td><td><font color=black><b> Debit </td><td>|</td><td colspan=1></td><td>|</td></tr>";
else
print "\n<tr><td>|</td><td colspan=3 align=center><b><font color=black>Balance</td><td>|</td><td align=right><font color=black><b>".putcoma(abs($bal))."</td><td>|</td><td><font color=black> <b>Credit </td><td>|</td><td colspan=1></td><td>|</td></tr>";

print "</table><hr>";

}


//**************************************************************8888888888888888888888888


function diff($x,$y)
{
 $difres=mysqli_query($db,"select ($x-$y)");
 $difrow=mysqli_fetch_row($difres);
 return $difrow[0];
}
?>
-END-
