<?
 session_start();
 include("test.php");
?>
<style>
 body,td{font-family:arial;font-size:13;color:black;}
 body{background-color:white;}
 select
  {border-style:ridge;
   border-color:black;
   height:25px;
   border-size=1;
   font-family:fixed;
   color:black;
  }
 input
  {border-style:ridge;
   border-color:#22437f;
   background-color:white;
   height:25px;
   font-weight:bold;
   border-size=1;
   font-family:sans-serif;
   color:BLACK;
  }
</style>
<center>
 

<?print $branch?><br>
Bank Book (<?print $bnk?>) <br>for the period of <?print datetoHtml($df)." To ".datetoHtml($dt)?>
<table  cellpadding=1 cellspacing=3 width=100% border=0>
<tr bgcolor='white'><td width=90><center><b>Date</td> <td width=90><center><b>Voucher </td> <td width=170><center><b>Ledger</td><td></td> <td width=90><center><b>Receipt</td><td></td><td></td><td width=90><center><b> Payment</td> <td><center><b>Narration</td></tr>
<hr>
</table>
<hr>
<table  cellpadding=1 cellspacing=3 width=100%>

<?
 include("opendb.php");
$sq="select op,drcr from ledger where brcode='$brcode' and led='$bnk'";
$re=mysqli_query($db,$sq) or die(mysqli_error());
$opb=mysqli_fetch_array($re) or die(mysqli_error());
$opbval=$opb[0];
$drorcr=$opb[1];

$res=mysqli_query($db,"select acfrom from acperiod");
$r1=mysqli_fetch_row($res);

$ssq="select sum(dramt)-sum(cramt) from journal2 where brcode='$brcode' and date>='$r1[0]' and date<'$df' and (partic='$bnk')";

$res2=mysqli_query($db,$ssq)or die(mysqli_error());
$r2=mysqli_fetch_row($res2);

if($drorcr=='Cr' and $r2[0]<0)
 $opbal=($opbval+abs($r2[0]));
else if($drorcr=='Cr' and $r2[0]>0)
 $opbal=($opbval-$r2[0]);
else if($drorcr=='Dr' and $r2[0]>0)
 $opbal=($opbval+$r2[0]);
else if($drorcr=='Dr' and $r2[0]<0)
 $opbal=($opbval+$r2[0]);
else
 $opbal=$opbval;
$sq="select distinct date from journal2 where brcode='$brcode' and date>='$df' and date<='$dt' and partic='$bnk' order by date";

$result1=mysqli_query($db,$sq)or die(mysqli_error());

$head=1;
$actype='';


while($row1=mysqli_fetch_array($result1))
{
//print '<br>opbal='.$opbal.'drcr:'.$opb[drcr];
if($opbal>0 && strtoupper($opb[drcr])=="DR")
{
print "<tr ><td></td><td></td><td></td><td></td><td colspan=3><b>To Opening Balance</td><td align=right><font size=1><b>".putComa(abs($opbal))."</td><td>|</td><td></td></tr>";
$tr+=$opbal;
$actype='DR';
}
else if($opbal>0 && strtoupper($opb[drcr])=="CR")
{
print "<tr ><td></td><td></td><td></td><td></td><td colspan=3><font size=1><b>To Opening Balance</td><td></td><td align=right><font size=1><b>".putComa(abs($opbal))."</td><td>|</td><td></td></tr>";
$tp+=$opbal;
$actype='CR';
}
else if($opbal<0 && strtoupper($opb[drcr])=="CR")
{
print "<tr ><td></td><td></td><td></td><td></td><td colspan=3><font size=1><b>To Opening Balance</td><td></td><td align=right><font size=1><b>".putComa(abs($opbal))."</td><td>|</td><td></td></tr>";
$tr+=$opbal;
$actype='CR';
}
else if($opbal<0 && strtoupper($opb[drcr])=="DR")
{
print "<tr ><td></td><td></td><td></td><td></td><td colspan=3><font size=1><b>To Opening Balance</td><td></td><td align=right><font size=1><b>".putComa(abs($opbal))."</td><td>|</td><td></td></tr>";
$tp+=abs($opbal);
$actype='DR';
}


$sq="select distinct jvno from journal2 where date='$row1[date]' and partic='$bnk' and brcode='$brcode'";

     $se=mysqli_query($db,$sq)or die(mysqli_error());
//$tr=0;
//$tp=0;
   while($prt_row=mysqli_fetch_array($se))
    {

  	$fnl=mysqli_query($db,"select * from journal2 where jvno='$prt_row[0]' and brcode='$brcode'")or die(mysqli_error());
          while($fnl_row=mysqli_fetch_array($fnl))
          {
               if(strtoupper($fnl_row[partic])!=strtoupper($bnk)) // && $fnl_row[type]=='Dr'
		{
                 print "\n<tr bgcolor=white><td width=1 align=right>|</td><td width=60>".datetohtml($fnl_row[date])."</td><td width=2>|</td><td width=65 align=left><font size=1>$fnl_row[against]:$fnl_row[no]</td><td width=2 align=left>|</td><td width=220 align=left><font size=1>$fnl_row[partic]</td>";
                print "\n<td>|</td><td width=80 align=right><font size=1>".nozero($fnl_row[cramt])."</td><td width=2>|</td><td width=80 align=right><font size=1>".nozero($fnl_row[dramt])."</td>";
		print "\n<td width=10 align=center>|</td><td><font size=1>$fnl_row[narr]</td><td>|</td></tr>";
			if($fnl_row[type]=='Dr') $tp+=$fnl_row[dramt]; else $tr+=$fnl_row[cramt];


/*if($actype=='DR')
$closebal=($tr+$opbal)-$tp;
else
$closebal=($tp+$opbal)-$tr;
*/
                 }
          }

    }
//print "\n<tr><td> x tr=".($tr ).' tp='.($tp )."</td></tr>";

//<a href='multientry/viewtrans.php?code=$fnl_row[jvno]'>

if($tr>$tp)
{
print "<tr ><td></td><td></td><td></td><td colspan=3 align=center><b>by Closing Balance</td><td>|</td><td align=right><font size=1><b>".putComa(abs($tr-$tp))."</td><td>|</td><td></td>";

print "<tr ><td></td><td></td><td></td><td></td><td colspan=3><td align=right><font size=1><b>".putComa(abs($tr))."<br><hr></td><td>|</td><td align=right> <font size=1><b>".putComa(abs($tp))."<br><hr></td><td>|</td></tr>";
$opbal=$tr-$tp;
}
else
{
print "<tr ><td></td><td></td><td></td><td colspan=3 align=center><b>by Closing Balance</td><td>|</td><td align=right><font size=1><b>".putComa(abs($tp-$tr))."</td><td>|</td>";

print "<tr ><td></td><td></td><td></td><td></td><td colspan=3><td align=right><font size=1><b>".putComa(abs($tp+abs($opbal)))."<br><hr></td><td>|</td><td align=right><font size=1><b> ".putComa(abs($tp+abs($opbal)))."<br><hr></td><td>|</td></tr>";

$opbal=$tr-$tp;
}
$tr=0;
$tp=0;


//$opbal=$closebal;


}
?>

