<link href="css/printline.css" type="text/css" rel="stylesheet" media="print,screen"  />


<?
 session_start();
if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); parent.document.location='../ind.php' </script>";
  return;
}
include("test.php");
 $df=$y."-".$m."-".$d;
 $dt=$y1."-".$m1."-".$d1;
$tr=0.00;
$tp=0.00;
?>
<table width=90%" border="0" cellspacing="0" cellpadding="0" class="grid" >

<center>

<font face=arial narrow size=3>
Bank Book of<font color=blue> <?print $bnk. " </font><br>from  ".datetohtml($df)." to ".datetohtml($dt)?>
<tr bgcolor='silver'><td width=70><center><b>Date</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());
$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='';

if($opbal>0 && strtoupper($opb[drcr])=="DR")
{
print "<tr bgcolor=white><td></td><td colspan=1><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=1><font color=green>To Opening Balance</td>";
print "<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=1><font color=green>To Opening Balance</td>";
print "<td align=right><font color=red>".putComa(abs($opbal))."</td><td></td><td></td><td></td></tr>";
$tr+=abs($opbal);
$actype='CR';
}
else if($opbal<0 && strtoupper($opb[drcr])=="DR")
{
print "<tr bgcolor=white><td></td><td colspan=1><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';
}
$opbal=0;
while($row1=mysqli_fetch_array($result1))
{

$sq="select distinct jvno from journal2 where date='$row1[date]' and partic='$bnk' and brcode='$brcode'";
if($opt=='dr') $tr+=$opbal;
else $tp+=$opbal;
     $se=mysqli_query($db,$sq)or die(mysqli_error());
   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>".datetohtml($fnl_row[date])."</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 font=5>".nozero($fnl_row[dramt])."</td>";
		print "\n<td>$fnl_row[narr]- $fnl_row[against]:$fnl_row[no]</td></tr>";
			if($fnl_row[type]=='Dr') $tp+=$fnl_row[dramt]; else $tr+=$fnl_row[cramt];


                 }
          }

    }


if($tr>$tp)
{
$rs1=mysqli_query($db,"select $tr-$tp")or die('Err:'.mysqli_error());
$cres=mysqli_fetch_row($rs1);

//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=2 align=center><font color=green><b>by Closing Balance</td><td></td><td align=right><font color=green>".putComa(abs($cres[0]))."</td><td></td>";

print "<tr bgcolor=silver><td colspan=2><td align=right><font color=green>".putComa(abs($tr))."<br><hr></td><td align=right> <font color=green>".putComa(abs($tp+$cres[0]))."<br><hr></td><td></td></tr>";
//$opbal=($tr-$tp);
$opbal=$cres[0];
$opt='dr';
}
else
{
$rs=mysqli_query($db,"select $tp-$tr")or die('Err:'.mysqli_error());
$cres=mysqli_fetch_row($rs);

print "<tr bgcolor=silver><td colspan=2 align=center><font color=green><b>by Closing Balance</td>";
//print "<td align=right><font color=white>$tp, $tr : ".$cres[0]."</font>".putComa(abs($tp-$tr))."</td><td></td><td></td>";

print "<td align=right>".putComa(abs($cres[0]))."</td><td></td><td></td>";
print "<tr bgcolor=silver><td colspan=2><td align=right><font color=green>".putComa(abs($tp+$cres[0]))."<br><hr></td><td align=right> <font color=green>".putComa(abs($tp))."<br><hr></td><td></td></tr>";

//$opbal=($tp-$tr);
$opbal=$cres[0];
$opt='cr';
}
if($opt=='dr')
{
 print "<tr><td></td><td colspan=1>Opening Balance</td><td align=right><font color=blue>".putComa($opbal)."</td><td></td></tr>";
}
else
{
 print "<tr><td></td><td colspan=1>Opening Balance</td><td></td><td align=right><font color=blue>".putComa($opbal)."</td></tr>";
}

$tr=0;
$tp=0;


//$opbal=$closebal;


}
?>

