<?
 session_start();
if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); parent.document.location='ind.php' </script>";
  return;
}
include("../test.php");
?>
<style>
 td{font-family:arial narrow;font-size:14;color:black;}
 th{font-family:arial narrow;font-size:14;color:white;background-color:#5b5858}
 body{background-color:ivory;}
 select
  {border-style:ridge;
   border-color:black;
   height:25px;
   border-size=1;
   font-family:verdana;
   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>
<?
 $df=$y."-".$m."-".$d;
 $dt=$y1."-".$m1."-".$d1;
?>
<script>
function show()
{
window.open("prnbbook.php?df=<?print$df?>&dt=<?print$dt?>&bnk=<?print $bnk?>");
}
</script>
<center>
<table width=100%>
<tr><td align=right><a href='javascript:show()'><img src='Type001.gif' border=0><br>Print</a></td></tr>
</table>

<font face=arial narrow size=3>
Bank Book of<font color=blue> <?print $bnk. " </font><br>from  ".datetohtml($df)." to ".datetohtml($dt)?>
<table bgcolor=ivory cellpadding=1 cellspacing=1 width=100%>
<tr bgcolor='silver'><td width=90><center><b>Date</td> <td width=80><center><b>Voucher </td> <td><center><b>Ledger</td> <td width=100><center><b>Receipt</td><td width=100><center><b> Payment</td> <td><center><b>Narration</td>

<?
 include("../opendb.php");
$sq="select op,drcr from ledger where brcode='$brcode' and led='$bnk'";
$re=mysqli_query($db,$sq) or die(mysqli_error($db));
$opb=mysqli_fetch_array($re) or die(mysqli_error($db));
$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($db));
$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($db));

$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 bgcolor=white><td></td><td colspan=2><font color=green>To Opening Balance</td><td align=right><font color=green>".putComa(abs($opbal))."</td><td></td><td></td></tr>";
$tr+=$opbal;
$actype='DR';
}
else if($opbal>0 && strtoupper($opb[drcr])=="CR")
{
print "<tr bgcolor=white><td></td><td colspan=2><font color=green>To Opening Balance</td><td></td><td align=right><font color=red>".putComa(abs($opbal))."</td><td></td><td></td></tr>";
$tp+=$opbal;
$actype='CR';
}
else if($opbal<0 && strtoupper($opb[drcr])=="CR")
{
print "<tr bgcolor=white><td></td><td colspan=2><font color=green>To Opening Balance</td><td></td><td align=right><font color=red>".putComa(abs($opbal))."</td><td></td><td></td></tr>";
$tr+=$opbal;
$actype='CR';
}
else if($opbal<0 && strtoupper($opb[drcr])=="DR")
{
print "<tr bgcolor=white><td></td><td colspan=2><font color=green>To Opening Balance</td><td></td><td align=right><font color=red>".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($db));
//$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($db));
          while($fnl_row=mysqli_fetch_array($fnl))
          {
               if(strtoupper($fnl_row[partic])!=strtoupper($bnk)) // && $fnl_row[type]=='Dr'
		{
                 print "\n<tr bgcolor=white><td>".datetohtml($fnl_row[date])."</td><td>$fnl_row[against]:$fnl_row[no]</td><td><a href='multientry/viewtrans.php?code=$fnl_row[jvno]'>$fnl_row[partic]</a></td>";
                print "\n<td align=right>".nozero($fnl_row[cramt])."</td><td align=right>".nozero($fnl_row[dramt])."</d>";
		print "\n<td>$fnl_row[narr]</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>";


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

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

print "<tr bgcolor=silver><td colspan=3><td align=right><font color=green>".putComa(abs($tp+abs($opbal)))."<br><hr></td><td align=right> <font color=green>".putComa(abs($tp+abs($opbal)))."<br><hr></td><td></td></tr>";

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


//$opbal=$closebal;


}
?>

