<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
include_once("../opendb.php");
if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); parent.document.location='../ind.php' </script>";
  return;
}
include_once("../test.php");
$bnk=$_REQUEST['bnk'];
 $y=$_REQUEST['y'];
$m=$_REQUEST['m'];
$d=$_REQUEST['d'];
$y1=$_REQUEST['y1'];
$m1=$_REQUEST['m1'];
$d1=$_REQUEST['d1'];

 $df=$y."-".$m."-".$d;
 $dt=$y1."-".$m1."-".$d1;
 
?>


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

<script>
function show()
{
window.open("prnbbook.php?df=<?print$df?>&dt=<?print$dt?>&bnk=<?print $bnk?>");
}
</script>
<title>Xpress</title>
    <script type="text/javascript" src="../scripts/jquery-latest.js"></script>
	<link type="text/css" href="../includes/style1.css" rel="stylesheet" />
	<link type="text/css" href="../css/form.css" rel="stylesheet" />
    <script type="text/javascript">
        $(function() {
          if ($.browser.msie && $.browser.version.substr(0,1)<7)
          {
			$('li').has('ul').mouseover(function(){
				$(this).children('ul').show();
				}).mouseout(function(){
				$(this).children('ul').hide();
				})
          }
        });        
    </script>
<body background="../back_ground.jpg">



<table class="container"><tr><td>
<center>
<table width=100%>
<tr><td align=right><a href='javascript:show()'><img src='../images/printer.jpg' 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  ".$df." to ".$dt?>
<table  cellpadding=1 cellspacing=1 class='report'>
<tr ><th width=90><center><b>Date</th> <th width=80><center><b>Voucher </th> <th><center><b>Ledger</th> <th width=100><center><b>Receipt</th><th width=100><center><b> Payment</th> <th><center><b>Narration</th>

<?

$tr=0.00;
$tp=0.00;

//print putcoma(abs($tr-$tp));

$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='';

if($opbal>0 && strtoupper($opb['drcr'])=="DR")
{
print "<tr ><td></td><td colspan=2><font color=green>To Opening Balance</td><td align=right><font color=green>".number_format(abs($opbal),2,".",",")."</td><td></td><td></td></tr>";
$tr+=$opbal;
$actype='DR';
}
else if($opbal>0 && strtoupper($opb['drcr'])=="CR")
{
print "<tr ><td></td><td colspan=2><font color=green>To Opening Balance</td>";
print "<td></td><td align=right><font color=red>".number_format(abs($opbal),2,".",",")."</td><td></td><td></td></tr>";
$tp+=$opbal;
$actype='CR';
}
else if($opbal<0 && strtoupper($opb['drcr'])=="CR")
{
print "<tr ><td></td><td colspan=2><font color=green>To Opening Balance</td>";
print "<td align=right><font color=red>".number_format(abs($opbal),2,".",",")."</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=2><font color=green>To Opening Balance</td><td></td><td align=right><font color=red>".number_format(abs($opbal),2,".",",")."</td><td></td><td></td></tr>";
$tp+=abs($opbal);
$actype='DR';
}
$opbal=0;
while($row1=mysqli_fetch_array($result1))
{
//echo $bnk;
$sq="select distinct jvno from journal2 where date='$row1[date]' and partic='$bnk' and brcode='$brcode'";
//echo $sq;
if($opt=='dr') $tr+=$opbal;
else $tp+=$opbal;
     $se=mysqli_query($db,$sq)or die(mysqli_error($db));
   while($prt_row=mysqli_fetch_array($se))
    {
        $sq11="select narr from journal2 where date='$row1[date]' and jvno = '$prt_row[0]' and partic='$bnk' and brcode='$brcode'";
        $narr2=mysqli_query($db,$sq11)or die(mysqli_error($db));
        $narr1=mysqli_fetch_row($narr2);
        $mnarr = $narr1[0];
          $fnl=mysqli_query($db,"select * from journal2 where jvno='$prt_row[0]' and brcode='$brcode'")or die(mysqli_error($db));
          //$sql="select * from journal2 where jvno='$prt_row[0]' and brcode='$brcode'";
          //echo $sql;
          while($fnl_row=mysqli_fetch_array($fnl))
          {
              
               if(strtoupper($fnl_row['partic'])!=strtoupper($bnk)) // && $fnl_row[type]=='Dr'
		       {
		           
		           //echo strtoupper($fnl_row[partic])." ".$prt_row[0];
                    //echo "<br>";
                    //echo strtoupper($bnk);
                    //echo "<br>";
		           $date1=implode('-', array_reverse(explode('-', $fnl_row['date'])));
  
                    print "\n<tr bgcolor=white><td>".$date1."</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>".$fnl_row['dramt']."</d>";
		            print "\n<td>$fnl_row[narr]</td></tr>";
		           // print "\n<td>$mnarr 11</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($db));
$cres=mysqli_fetch_row($rs1);

//print "<tr ><td colspan=3 align=center><font color=green><b>by Closing Balance</td><td align=right><font color=green>".number_format(abs($tr-$tp),2,".",",")."</td><td></td><td></td>";

print "<tr ><td colspan=3 align=center><font color=green><b>by Closing Balance</td><td align=right><font color=green>".number_format(abs($cres[0]),2,".",",")."</td><td></td><td></td>";

print "<tr ><td colspan=3><td align=right><font color=green>".number_format(abs($tr))."<br><hr></td><td align=right> <font color=green>".number_format(abs($tp),2,".",",")."<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($db));
$cres=mysqli_fetch_row($rs);

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

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

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

$tr=0;
$tp=0;


//$opbal=$closebal;


}
?>

