<?
 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:13px}
a{text-decoration:none;color:black}
a:hover{text-decoration:underline}
</style>
<body bgcolor=ivory>
<center>
<table>
<tr><td align=center><b><?print $branch?></td></tr>
<tr><td align=center>Trial Balance for the year ended</td></tr>
<tr><td align=center>[Printed by <?print $user?> on <?print date("d-m-Y  h:i:s a")?>]</td></tr>
</table>
<table border=1>
<tr bgcolor='#5b5858'><td align=center><font color=white width=1100><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;
//print "select distinct grp from ledger where brcode='$brcode'";
 $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]'";

     $re=mysqli_query($db,$sqq) or die(mysqli_error());
     while($row2=mysqli_fetch_array($re))
     {
	if($row2[drcr]=="Dr")
	{
        $totopb+=$tr;
	}
	else 
	{
        $totopb+=$tp;
	}

       print "<tr onMouseOver=bgColor='alice-blue'; onMouseOut=bgColor='ivory'><td> <blockquote> <a href='../vmledger.php?cd=$row2[led]' title='Click to view ledger'>$row2[led]</a></td><td align=right>".putComa($row2[op]).'<font color=darkbrown> '.$row2[drcr]."</td>";

     $sq="select * from journal2 where brcode='$brcode' and partic='$row2[led]'";

         $ree=mysqli_query($db,$sq)or die("ERROR ".mysqli_error());
          while($r1=mysqli_fetch_array($ree))
          {
            if($r1[type]=='Cr')
		{
		$tp+=$r1[cramt];
		}
	    else if($r1[type]=='Dr')
		{
		$tr+=$r1[dramt];
		}
     	}

       $bal=$totopb+($tr-$tp);   //print "<br> bal=$bal";
//*********************************************************************
//	$bal=number_format($bal,2,'','');
//**********************************************************************
if($bal>0)
{
if($row2[drcr]=='Cr')
{
 print "<td align=right>".putComa($bal)."x</td><td></td><td align=right>".
putComa(abs($row2[op]-$bal))."<font color=darkbrown> Cr </font></td>";
}
 else
 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));
?>
