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

 include("../test.php");
 include ("../opendb.php");
$tbl=str_replace('.','',$REMOTE_ADDR).'tb';
?>
<style type="text/css" media="print">
.dontprintme { display: none; }
</style>

<center>
<form action='<?print $PHP_SELF?>'>
<table bgcolor='silver' class=dontprintme>
<tr><td> Date from </td><td>dd<input type=text name='d' size=2 value='<?print date("d",time())?>'>
				mm<input type=text name='m' size=2 value='<?print date("m",time())?>'>
				yy<input type=text name='y' size=4 value='<?print date("Y",time())?>'>
			</td></tr>

<tr><td> To </td><td>dd<input type=text name='d1' size=2 value='<?print date("d",time())?>'>
				mm<input type=text name='m1' size=2 value='<?print date("m",time())?>'>
				yy<input type=text name='y1' size=4 value='<?print date("Y",time())?>'>
			</td>
</tr>
<tr><td colspan=2 align=center><input type=submit name='show' value='Show'></td></tr>
</table>
</form>



<?
if($show)
{
$df=$y.'-'.$m.'-'.$d;
$dt=$y1.'-'.$m1.'-'.$d1;

mysqli_query($db,"drop table $tbl");
$sql="create table $tbl (no int(10), ledger varchar(100),opdr double(14,2) default 0, opcr double(14,2) default 0, dr double(14,2) default 0,cr double(14,2) default 0, cldr double(14,2) default 0, clcr double(14,2) default 0)";
mysqli_query($db,$sql) or die(mysqli_error());

$i=1;

?>

<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 datetohtml($df)?> to <?print 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 <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());
$i=1;
while($row=mysqli_fetch_row($result))
  { 
  mysqli_query($db,"insert into $tbl (no,ledger) values('".$i++."','<b>$row[0]</b>')");
//     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());
     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());
$op4row=mysqli_fetch_row($drcrforop)or die(mysqli_error());



        $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());
           $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[1]-$op4row[0]);}
           }
        else
           {
             $optype='Cr'; 
             if($row2[op]>=0)
               {$crop=$row2[op]+($op4row[1]-$op4row[0]);}
             else
              { $drop=$row2[op]+($op4row[0]-$op4row[1]);}
            }

  //print $row2[led].'- Dr.'.$drop." Cr.".$crop.'<br>';


	     if($drop>0)
              {
//print $row2[led].'- Dr.'.$drop." Cr.".$crop.'<br>';
$bal=(abs($drop)+$drcrrow[0])-$drcrrow[1];
                if($bal>0)$loc='dr';
                  else
                     $loc='cr';
              }
              else if($drop<0)
              {

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

             else if($crop>=0)// && $drop==0)
              {

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


/*	     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')
 {

$sql="insert into $tbl values('".$i++."','$row2[led]','".abs($drop)."','".abs($crop)."','"
.abs($drcrrow[0])."','". abs($drcrrow[1])."','".abs($bal)."','0')";

mysqli_query($db,$sql) or die(mysqli_error());

 }
else if($loc=='cr')
 {

$sql="insert into $tbl values('".$i++."','$row2[led]','".abs($drop)."','".abs($crop)."','".abs($drcrrow[0])."','". abs($drcrrow[1])."','0','".abs($bal)."')";

mysqli_query($db,$sql) or die(mysqli_error());


 }

}

}
$re_tb=mysqli_query($db,"select * from $tbl")or die(mysqli_error());
while($tb_row=mysqli_fetch_array($re_tb))
{
 print "<tr><td>$tb_row[1]</td><td align=right>&nbsp;".nozero($tb_row[2])."</td><td align=right>&nbsp;".nozero($tb_row[3])."</td>";
 print "<td align=right>&nbsp;".nozero($tb_row[4])."</td><td align=right>&nbsp;".nozero($tb_row[5])."</td>";
 print "<td align=right>&nbsp;".nozero($tb_row[6])."</td><td align=right>&nbsp;".nozero($tb_row[7])."</td>";
}

$rsum=mysqli_query($db,"select sum(opdr),sum(opcr),sum(dr),sum(cr), sum(cldr),sum(clcr) from $tbl");
$tbrow=mysqli_fetch_row($rsum);
$totdrop=$tbrow[0];
$totcrop=$tbrow[1];

$totdr=$tbrow[2];
$totcr=$tbrow[3];

$totdrbal=$tbrow[4];
$totcrbal=$tbrow[5];



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


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>";

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

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

 
} // end if           

?>
