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

<style>
 td{font-family:verdana;font-size:14;color:black;}
 th{font-family:verdana;font-size:14;color:white;background-color:#5b5858}
 body{background-color:ivory;}
 select
  {border-style:ridge;
   border-color:black;
   height:25px;
   width:170;
   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>
<body onLoad=document.f1.d.focus()>
<center>
<p>&nbsp;<p>&nbsp;<p>&nbsp;
<font face=verdana size=4>
<form name=f1 action='<?print $PHP_SELF?>'>
<table border=0>
<tr><td colspan=2 bgcolor=#5b5858 align=center><font color=white size=4><b>Bank Book for the Period</font></td></tr>
<tr><td> From </td>
    <td>dd<input type=text name=d size=1 value='1'>
	mm<input type=text name=m size=1 value='<?print date("m",time())?>'>
	yyyy<input type=text name=y size=3 value='<?print date("Y",time())?>'>
    </td>
</tr>
<tr><td> To </td>
    <td>dd<input type=text name=d1 size=1 value='<?print date("d",time())?>'>
	mm<input type=text name=m1 size=1 value='<?print date("m",time())?>'>
	yyyy<input type=text name=y1 size=3 value='<?print date("Y",time())?>'>
    </td>
</tr>
<tr><td>Bank</td> 
<td> <select name=bnk>
<?
 include("opendb.php");
 $result=mysqli_query($db,"select * from ledger where brcode='$brcode' and (grp='Bank Accounts' or grp='Bank O/d')");
 while($row=mysqli_fetch_row($result))
 {
  print "<option>$row[0]</option>";
 }
?>
</select>
</td></tr>
  
<tr><td colspan=2 bgcolor=#5b5858 align=center>
  <input type=submit value="Show" name='shw'><input type=submit name='prn' value='Print'></td>
</tr>
</table>
</form>
<?
$prn = $_REQUEST['prn'];
$shw = $_REQUEST['shw'];
 $bnk= $_REQUEST['bnk'];
 $d = $_REQUEST['d'];
 $m = $_REQUEST['m'];
 $y = $_REQUEST['y'];
$d1 = $_REQUEST['d1'];
 $m1 = $_REQUEST['m1'];
 $y1 = $_REQUEST['y1'];
if($prn)
{
   $df=$y.'-'.$m.'-'.$d;
   $dt=$y1.'-'.$m1.'-'.$d1;
       print "<script> window.open('bbook-tp.php?df=$df&dt=$dt&bnk=$bnk');</script>";
}
/*if($shw)
{
   $df=$y.'-'.$m.'-'.$d;
   $dt=$y1.'-'.$m1.'-'.$d1;
       print "<script> window.open('bbook-t.php?df=$df&dt=$dt&bnk=$bnk');</script>";
}*/


if($shw)
{
include("test.php");
 $df=$y."-".$m."-".$d;
 $dt=$y1."-".$m1."-".$d1;
$tr=0.00;
$tp=0.00;

?>
<table width=90%" border="1" 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>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());
$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></td><td colspan=1><font color=green>To Opening Balance</td><td align=right><font color=green><font face=arial narrow size=3>".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></td><td colspan=1><font color=green>To Opening Balance</td>";
print "<td></td><td align=right><font color=red><font face=arial narrow size=3>".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></td><td colspan=1><font color=green>To Opening Balance</td>";
print "<td align=right><font color=red><font face=arial narrow size=3>".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></td><td colspan=1><font color=green>To Opening Balance</td><td></td><td align=right><font color=red><font face=arial narrow size=3>".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><font face=arial narrow size=2>".implode('-', array_reverse(explode('-',$fnl_row['date'])))."</td><td>$fnl_row[against]:$fnl_row[no]</td><td><font face=arial narrow size=3><a href='multientry/viewtrans.php?code=$fnl_row[jvno]'>$fnl_row[partic]</a></td>";
                print "\n<td align=right ><font face=arial narrow size=3>".nozero($fnl_row['cramt'])."</td><td align=right ><font face=arial narrow size=3>".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']; 
			        //print "$tp == $fnl_row[dramt] == $fnl_row[cramt] xxxxx<br>";
			    //}
			    //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></td><td colspan=2 align=center><font color=green><b>by Closing Balance</td><td></td><td align=right><font color=green><font face=arial narrow size=3>".putComa(abs($cres[0]))."</td><td></td><hr>";
    print "<tr bgcolor=silver><td></td><td colspan=2><td align=right><font color=green>".putComa(abs($tr))."<br><hr></td><td align=right> <font color=green><font face=arial narrow size=3>".putComa(abs($tp+$cres[0]))."<br><hr></td><td></td></tr><hr>";
    //$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></td><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><font face=arial narrow size=3>".putComa(abs($cres[0]))."</td><td></td><td></td></tr>";
    print "<tr bgcolor=silver><td></td><td colspan=2><td align=right><font color=green><font face=arial narrow size=3>".putComa(abs($tp+$cres[0]))."<br><hr></td><td align=right> <font color=green><font face=arial narrow size=3>".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></td><td colspan=1>Opening Balance</td><td align=right><font color=blue><font face=arial narrow size=3>".putComa($opbal)."</td><td></td></tr>";
}
else
{
 print "<tr><td><td></td></td><td colspan=1>Opening Balance</td><td></td><td align=right><font color=blue><font face=arial narrow size=3>".putComa($opbal)."</td></tr>";
}

$tr=0;
$tp=0;


//$opbal=$closebal;


}
}
?>

