<?
 session_start();

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

 include("../test.php");
 include ("../opendb.php");
$df=$y.'-'.$m.'-'.$d;
$dt=$y1.'-'.$m1.'-'.$d1;

?>

<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 from <?print $df.' to '.$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 <th colspan=2>Opening Balance <th colspan=2>Transaction <th colspan=2>Closing
<tr align=center><td>&nbsp;</td><td>Debit </td><td>Credit</td><td>Debit </td><td>Credit</td><td>Debit </td><td>Credit</td></tr>

<?
$result=mysqli_query($db,"select distinct grp from ledger where brcode='$brcode' ")or die(mysqli_error($db));
$i=1;
while($row=mysqli_fetch_row($result))
  { 
     print "<tr><td colspan=3><b>$row[0]</td></tr>";
    $sqq="select * from ledger where brcode='$brcode' and  grp='$row[0]' order by led ";
     $re=mysqli_query($db,$sqq) or die(mysqli_error($db));
     while($row2=mysqli_fetch_array($re))
     {
$drop=0;
$crop=0;
$bal=0;

$drcrforop=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$row2[0]' and brcode='$brcode' and date <'$df'")or die(mysqli_error($db));
$op4row=mysqli_fetch_row($drcrforop)or die(mysqli_error($db));


        $drcrres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$row2[0]' and brcode='$brcode' and date>='$df' and date<='$dt'")or die(mysqli_error($db));
           $drcrrow=mysqli_fetch_row($drcrres);

 
        if($row2[drcr]=='Dr')
           {
             $optype='Dr'; 
              if($row2[op]>=0)
                {$drop=$row2[op]+($op4row[0]-$op4row[1]);}
               else
                 {$crop=$row2[op]+($op4row[0]-$op4row[1]);}
           }
        else
           {
             $optype='Cr'; 
             if($row2[op]>=0)
               {$crop=$row2[op]+($op4row[1]-$op4row[0]);}
             else
              { $drop=$row2[op]+($op4row[0]-$op4row[1]);}
            }

          
             if(abs($drop)>=0 && $crop==0)
              {$bal=(abs($drop)+$drcrrow[0])-$drcrrow[1];
                if($bal>0)$loc='dr';
                  else
                     $loc='cr';
              }

             if(abs($crop)>=0 && $drop==0)
              {
                  $bal=(abs($crop)+$drcrrow[1])-$drcrrow[0];
                    if($bal>0) $loc='cr';
                        else
                            $loc='dr';
               }
//$totdr+=$drcrrow[0];
//$totcr+=$drcrrow[1];



       

 if($loc=='dr')
 {

$totdrbal+=abs($bal);
$totdr+=abs($drcrrow[0]);
$totcr+=abs($drcrrow[1]);

$totdrop+=abs($drop);
$totcrop+=abs($crop);
  print "<tr><td><a href='../vmledger.php?cd=$row2[led]' title='Click to view ledger'>$row2[led]</a></td><td align=right>".putcoma(abs($drop))."</td><td align=right>".putcoma(abs($crop))."</td><td align=right>".putComa(abs($drcrrow[0]))."</td><td align=right>".putcoma(abs($drcrrow[1]))."</td><td align=right>".putComa(abs($bal))."</td>";
 }
else if($loc=='cr')
 {
$totcrbal+=abs($bal);
$totcr+=abs($drcrrow[1]);
$totdr+=abs($drcrrow[0]);

$totcrop+=abs($crop);
$totdrop+=abs($drop);

   print "<tr><td><a href='../vmledger.php?cd=$row2[led]' title='Click to view ledger'>$row2[led]</a></td><td align=right>".putcoma(abs($drop))."</td><td align=right>".putComa(abs($crop))."</td><td align=right>".putComa(abs($drcrrow[0]))."</td><td align=right>".putComa(abs($drcrrow[1]))."</td><td>&nbsp;</td><td align=right> ". putcoma(abs($bal))."</tr>";

 }

}

}
$opdiff=abs($totdrop)-abs($totcrop);

if($opdiff!=0 && $opdiff<0)
 {
   print "<tr><td align=right> <b> Difference</td><td align=right> ".putComa(abs($opdiff))."</td><td></td><td></td>"; ///$totdrop+=abs($opdiff);
 }
else
 {
  print "<tr><td align=right> <b> Difference</td><td>&nbsp;</td><td align=right> ".putcoma(abs($opdiff))."</td><td></td><td></td>";
//  $totcrop+=abs($opdiff);
}
$diff=($totdrbal-$totcrbal);
print "diff=".$diff;

if($dif!=0 && $diff<0)
 print "<td align=right>".putComa(abs($diff))."</td>";
else 
  print "<td>&nbsp;</td><td align=right>".putComa(abs($diff))."</td>";

print "<tr><td align=center><b>Total</td><td align=right>".abs($totdrop)."</td><td align=right>".abs($totcrop)."</td>";

print "<td>".putComa($totdr)."</td><td align=right>".putComa($totcr)."</td>";
print "<td>".putComa($totdrbal)."</td><td align=right>".putComa($totcrbal)."</td>";
print "</table>";

 
           

?>
