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

 include("test.php");
 include ("opendb.php");
?>

<style>
td{font-family:verdana;font-size:14}
</style>
<body bgcolor=ivory>
<table border=1>
<tr bgcolor='#5b5858'><td align=center><font color=white width=1200><b>Name of Ledger</td><td align=center> <font color=white><b>Opening Balance</td><td align=center><font color=white><b> Debit</td><td align=center><font color=white><b> Credit</td><td align=center><font color=white><b>Balance</td></tr>
<?
$totopb=0;$totl=0;$totr=0;
 $result=mysqli_query($db,"select distinct grp from ledger where brcode='$brcode'")or die(mysqli_error());
while($row=mysqli_fetch_row($result))
  { 
$tr=0;$tp=0;$bal=0;$totopb=0;
        print "\n<tr><td colspan=3><b><font color='brown'>$row[0]";
   $sqq="select * from ledger where brcode='$brcode' and  grp='$row[0]'";
//  print "<br>". $sqq;
     $re=mysqli_query($db,$sqq) or die(mysqli_error());
     while($row2=mysqli_fetch_array($re))
     {
//print "<br>- ".$row2[0];
	if($row2[drcr]=="Dr")
	{
//	$tr+=$row2[op]; 
        $totopb+=$tr;
	}
	else 
	{
//	$tp+=$row2[op];
        $totopb+=$tp;
	}
//print "<br>$row2[led] opb = $totopb";
       print "<tr onMouseOver=bgColor='alice-blue'; onMouseOut=bgColor='ivory'><td> <blockquote> $row2[led]</td><td align=right>".putComa($row2[op]).'<font color=darkbrown> '.$row2[drcr]."</td>";

     $sq="select * from actrans where brcode='$brcode' and (ledger='$row2[led]' or dr='$row2[led]' or cr='$row2[led]' or mode='$row2[led]')";

         $ree=mysqli_query($db,$sq)or die("ERROR ".mysqli_error());
          while($r1=mysqli_fetch_array($ree))
          {
            if($r1[cr]==$row2[led])
		{
		$tp+=$r1[amt];
		}
	    else if($r1[dr]==$row2[led])
		{
		$tr+=$r1[amt];
		}
     	}

       $bal=$totopb+($tr-$tp); // print "<br> bal=$bal";
//*********************************************************************
	$bal=number_format($bal,2,'','');
//**********************************************************************
if($bal>0)
{
 print "<td align=right>".putComa($bal)."</td><td></td><td align=right>".
putComa(abs($row2[op]+$bal))."<font color=darkbrown> Dr </font></td>";
 $totl+=$bal;
}
else 
{
 print "<td>&nbsp;</td><td align=right>".putComa(abs($bal))."<td align=right>".putComa(abs($row2[op]-$tp+$tr))."<font color=darkbrown> Cr </font></td>";
 $totr+=abs($bal);
}
  $tr=0;$tp=0; 
 }
}
$rs=mysqli_query($db,"select opstock from plopstock");
$vv=mysqli_fetch_array($rs);
//$totl+=$vv[0];
print "<tr><td><font color=brown><b>Opening Stock</td><td align=right>".putComa($vv[0])." <font color=darkbrown> Dr </td></tr>";

print "<tr bgcolor='#5b5858'><td><font color=white><b><center>Total</td>";
print "<td align=right> <font color=white>".putComa($totopb)."&nbsp;&nbsp;&nbsp;&nbsp;</td>";
print "<td align=right> <font color=white>".putComa(abs($totl))."</td><td align=right> <font color=white>".putComa(abs($totr))."</td></tr>";
//print "<tr><td>".putComa(abs($totl)-abs($totr));
?>
