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

<style>
td{font-family:verdana;font-size:14}
body{background-color:ivory}
</style>
<center>

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

<table border =1 width=100%>
<tr><td align=right><input type=button value='Print' onClick=window.open("prntb.php")> </td></tr>
</table>
<br>
<!-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-->
<table border=1>
<TR bgcolor='#5b5858'><TD COLSPAN=3 ALIGN=CENTER><font color=white>
<b>TRIAL BALANCE AS ON <?print date("d-M-Y",time())?> </TD></TR>
<tr><td>&nbsp;</td><td align=center><b>Debit</td><td align=center><b>Credit</td></tr>
<?
 $result=mysqli_query($db,"select distinct grp from ledger where brcode='$brcode'")or die(mysqli_error());
while($row=mysqli_fetch_row($result))
  { 
$tr=0.0;$tp=0.0;$bal=0.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")
	{
	$tr+=$row2[op]; 
	}
	else 
	{
	$tp+=$row2[op];
	}
       print "<tr onMouseOver=bgColor='alice-blue'; onMouseOut=bgColor='ivory'><td> <blockquote> $row2[led]</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=($tr-$tp); $bal=number_format($bal,2,'','');
//******************************************************

if($bal>0)
{
 print "<td align=right>".putComa($bal)."</td><td>&nbsp;</td>";
 $totl+=$bal;
}
else 
{
 print "<td>&nbsp;</td><td align=right>".putcoma($bal*-1)."</td>";
 $totr+=$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])."</td></tr>";

$difftb=abs($totl)-abs($totr);
$difftb=number_format($difftb,'2','','');

if($totl>$totr)
{
print "<tr><td>Trial Balance Difference</td><td></td><td align=right>".putComa($difftb)."</td></tr>";
print "<tr bgcolor='#5b5858'><td><font color=white><b><center>Total</td>";
print "<td align=right> <font color=white>".putComa($totl)."</td>";
print "<td align=right> <font color=white>".putComa(abs($totl))."</td></tr>";
}
else if($totl<$totr)
{
print "<tr><td>Trial Balance Difference</td><td align=right>".putComa($difftb)."</td><td></td></tr>";
print "<tr bgcolor='#5b5858'><td><font color=white><b><center>Total</td>";
print "<td align=right> <font color=white>".putComa($totr)."</td>";
print "<td align=right> <font color=white>".putComa(abs($totr))."</td></tr>";
}

//if($difftb>0)
//print "<tr><td align=right>Difference: ".putComa($difftb);
?>
</table>
<br>

<?
 $ops=mysqli_query($db,"select closestock from plopstock");
 $r=mysqli_fetch_row($ops);
// print "<b>Closing Stock : $r[0]</b>";
?>

