<?
 session_start();
 $brcode = $_SESSION['brcode'];
 $user = $_SESSION['user'];
$df=$_REQUEST['df'];
$dt=$_REQUEST['dt'];
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 $unit.' '.$branch?></td></tr>
<tr><td align=center>Trial Balance from <?print datetohtml($df).' to '.datetohtml($dt)?></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>
<th>Ledger(Group) <th>Closing Dr<th>Closing Cr<th>
<?
$br=str_replace(' ','',$branch);
$br=str_replace('(','',$br);
$br=str_replace(')','',$br);
$tbl='tbcls1'.$brcode.$user;
$tblop='tbclsop1'.$brcode.$user;
$tbl1='tbclssum'.$brcode.$user;
$i=1;
$mopdrtot = 0;
$mopcrtot = 0; 
$mdrtot = 0;
$mcrtot = 0;
$mcldrtot =0;
$mclcrtot =0;
$result = mysqli_query($db,"select * from ledger where brcode='$brcode' order by grp,led") or die("ERR:".mysqli_error());
$mgrp = '';
$grp_tot = 0;
while($row=mysqli_fetch_row($result))
{ 
    if ($i==1)
    {
       $mgrp = $row[1];
       ++$i;
    }
    $reop=mysqli_query($db,"select * from $tblop where partic='$row[0]'") or die("ERR:".mysqli_error());
    $roop1=mysqli_fetch_row($reop);
    $re=mysqli_query($db,"select * from $tbl where partic='$row[0]'") or die("ERR:".mysqli_error());
    $ro=mysqli_fetch_row($re);
    $mopdr = 0;
    $mopcr = 0; 
    if($row[3]=='Dr')
    {
         if ($row[2]>=0)
	      $mopdr = $row[2];
         else
             $mopcr = $row[2] * -1;
    }
    if($row[3]=='Cr')
    {
         if ($row[2]>=0)
	      $mopcr = $row[2];
         else
             $mopdr = $row[2] * -1;
    }
    $mdr = 0;
    $mcr = 0;
    $mopt_trans = 0;
    if($roop1[1]>0) 
        $mopt_trans = $roop1[1];
    if($roop1[2]>0)
	 $mopt_trans = $mopt_trans - $roop1[2];
    $mopdr = $mopdr - $mopcr + $mopt_trans;
    $mopcr = 0;
    if($mopdr < 0)
        {
        $mopcr = $mopdr * -1;
        $mopdr = 0;
        }
    if ($ro[1]>0)
        $mdr = $ro[1];
    if ($ro[2]>0)
        $mcr = $ro[2];    
    $mopdrtot +=$mopdr;
    $mopcrtot +=$mopcr; 
    $mdrtot += $mdr;
    $mcrtot += $mcr;
    $mclbal = $mopdr - $mopcr + $mdr - $mcr;
    $mcldr = 0;
    $mclcr = 0;
    if ($mclbal>=0)
        $mcldr = $mclbal;
    else
        $mclcr = $mclbal * -1;
    if($mgrp!=$row[1])
    {
          if ($grp_tot != 0)
		print "<tr><td> <a href='viewdet3.php?cd=$mgrp&df=$df&dt=$dt' title='Click to view ledger' target='m'><b>$mgrp</td>";
	   if ($grp_tot >0)
	   {
	          $mcldrtot +=$grp_tot; 		   
		   print "<td align='right'>".putComa($grp_tot)."</td><td align='right'>".putComa(0)."</td></tr>";
	   }
	   else
	   {
		   $mclcrtot +=($grp_tot * -1);
		   if ($grp_tot<0)
                    print "<td align='right'>".putComa(0)."</td><td align='right'>".putComa($grp_tot*-1)."</td></tr>";
	   }
	   $grp_tot = 0;
          $mgrp=$row[1];
          $grp_dr = 0;
          $grp_cr = 0; 
     }
    $grp_tot += $mclbal;
}
if ($grp_tot != 0)
	print "<tr><td> <a href='viewdet3.php?cd=$mgrp&df=$df&dt=$dt' title='Click to view ledger' target='m'><b>$mgrp</td>";
if ($grp_tot >0)
{
     $mcldrtot +=$grp_tot; 
     print "<td align='right'>".putComa($grp_tot)."</td><td align='right'>".putComa(0)."</td></tr>";
}
else
{
     $mclcrtot +=($grp_tot * -1);
     if ($grp_tot<0)
        print "<td align='right'>".putComa(0)."</td><td align='right'>".putComa($grp_tot*-1)."</td></tr>";
}
$mcldrtot=number_format($mcldrtot,2,".","");
$mclcrtot=number_format($mclcrtot,2,".","");
if($mcldrtot>$mclcrtot)
{
	print "<tr><td>Opening Contra</td><td align='right'>".putComa(0)."</td><td align='right'>".putComa($mcldrtot-$mclcrtot)."</td></tr>";
	print "<tr><td>T O T A L</td><td align='right'>".putComa($mcldrtot)."</td><td align='right'>".putComa($mcldrtot)."</td></tr>";
}
elseif($mcldrtot<$mclcrtot)
{
	print "<tr><td>Opening Contra</td><td align='right'>".putComa($mclcrtot-$mcldrtot)."</td><td align='right'>".putComa(0)."</td></tr>";
	print "<tr><td>T O T A L</td><td align='right'>".putComa($mclcrtot)."</td><td align='right'>".putComa($mclcrtot)."</td></tr>";
}
else
	print "<tr><td>T O T A L</td><td align='right'>".putComa($mcldrtot)."</td><td align='right'>".putComa($mclcrtot)."</td></tr>";
print "</table>";
?>
