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

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

if(!checkdate($m,$d,$y))
{
 print "<script>alert('Invalid Date'); history.back(1);</script>";
 return;
}
if(!checkdate($m1,$d1,$y1))
{
 print "<script>alert('Invalid Date'); history.back(1);</script>";
 return;
}

$df=$y.'-'.$m.'-'.$d;
$dtt=$y1.'-'.$m1.'-'.$d1;
?>
<style>
td{font-family:verdana;font-size:14}
body{background-color:ivory}
</style>
<center>
<!--
<table border=1>
<TR bgcolor='#5b5858'><TD COLSPAN=3 ALIGN=CENTER><font color=white>
<b>TRIAL BALANCE From <?print datetohtml($df)." To ". datetohtml($dtt)?> </TD></TR>
<tr><td>&nbsp;</td><td align=center><b>Debit</td><td align=center><b>Credit</td></tr>
-->
</table>

<!-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-->
<table border=1>
<TR bgcolor='#5b5858'><TD COLSPAN=3 ALIGN=CENTER><font color=white>
<b>TRIAL BALANCE From <?print datetohtml($df)." To ". datetohtml($dtt)?></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 date>='$df' and date<='$dtt' 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>";
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($totr))."</td></tr>";
$difftb=abs($totl)-abs($totr);
$difftb=number_format($difftb,'2','','');
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>";
?>

