<?php
session_start();
if (!isset($_SESSION['reusername'])) { echo 'Sorry You Dont Have permissions to View this Page!'; exit();}
require_once 'includes/database.php';
require_once 'includes/display.php';
require_once 'includes/funcs.php';
echo $htmlheaders;  echo '<head>';
echo $js;
echo $recss;
echo '<style>body{overflow-y:scroll;}</style>';
echo '</head><body>';
$user = $_SESSION['reusername'];
$db=new MySQLDatabase();
$db->open_connection();
$bacc = mysqli_real_escape_string($db->connection, $_GET['bacc']);
$sd = substr($_REQUEST['sbldate'],6,4)."-".substr($_REQUEST['sbldate'],3,2)."-".substr($_REQUEST['sbldate'],0,2);
$ed = substr($_REQUEST['ebldate'],6,4)."-".substr($_REQUEST['ebldate'],3,2)."-".substr($_REQUEST['ebldate'],0,2);
if ($bacc !=='') {
    echo '<p><a $href="#" id="printnow" class="printnow" onclick="myprint();" style="float:left;">Print</a>';
    echo '<a href="#" id="closenow" class="closenow" onclick="window.close();">Close</a></p>';
	echo '<div id="printblk">';
    echo '<table id="treport" style="width:90%;margin-left:30px;font-family:Trebuchet MS, Arial;font-size:12px;"';
    echo '<tr><td colspan="6"><center><span style="font-size:18px;font-weight:normal;float:left;"> <b>BANK LEDGER </b></span><span style="font-size:22px;font-weight:bold"></span><center>';
    echo '<br/><span style="font-size:16px;font-weight:bold;"><center>'. str_repeat('&nbsp;',25).$bacc.'</center><br/><span style="font-size:16px;font-weight:bold;"><center>'. str_repeat('&nbsp;',25).'From: &nbsp;'.$_REQUEST['sbldate'].' &nbsp; To: &nbsp;'.$_REQUEST['ebldate'].'</center></span></td></tr>';
    echo'<tr><u> <th>Date</th><th>Doc. No.</th><th>Naration</th> <th>Debit</th> <th>Credit</th> <th>Balance</th></u></tr>';
    echo '<tr><td colspan="7"><hr/></td></tr>'; 

                                                                    // get BF invoices & receipts 
    $query = "SELECT SUM(amount) as am FROM recptrans WHERE bacc = '$bacc' AND rdate < '$sd' 
              UNION SELECT SUM(-1*amount) FROM paytrans WHERE bacc = '$bacc' AND pdate < '$sd'
			  UNION SELECT SUM(-1*amount) FROM dexptrans WHERE bacc = '$bacc' AND ddate < '$sd'
			  UNION SELECT SUM(amount) FROM bktrans WHERE dacc = '$bacc' AND jdate < '$sd'  
			  UNION SELECT SUM(-1*amount) FROM bktrans WHERE cacc = '$bacc' AND jdate < '$sd'";
	$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
	$tot = 0;
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC )){
			$tot = $tot + floatval($row['am']);
	}
    $que = "SELECT ob FROM banks WHERE bacc = '$bacc'";
    $res = mysqli_query($db->connection, $que) or die(mysqli_error($db->connection) );
    if ($rowo = mysqli_fetch_array($res, MYSQLI_ASSOC)){ $tot = $tot + floatval($rowo['ob']);  }
	echo '<tr><td align="right" colspan="3">B/F &nbsp;</td> <td></td> <td></td> <td align="right">'.number_format($tot,0).'</td>  </tr>'; 
																			// get invoices & receipts 
    $query = "SELECT recpno, rdate, CONCAT(pmode, '-', IFNULL(chqno,' '), ' - ', ' RECEIPT -', rmks) as nar, IF(amount>0,amount,0) as dam, IF(amount<0,-1*amount,0) as cam FROM recptrans WHERE bacc = '$bacc' AND rdate >= '$sd' AND rdate <= '$ed' 
              UNION SELECT payno AS recpno, pdate as rdate, CONCAT(pmode, '-', IFNULL(chqno,' '), ' - ', ' PAYMENT -', rmks), IF(amount<0,-1*amount,0), IF(amount<0,0,amount) FROM paytrans WHERE bacc = '$bacc' AND pdate >= '$sd' AND pdate <= '$ed' 
			  UNION SELECT dexpno AS recpno, ddate as rdate, CONCAT(pmode, '-', IFNULL(chqno,' '), ' - ', expacc,' - ', rmks), IF(amount<0,-1*amount,0), IF(amount<0,0,amount) FROM dexptrans WHERE bacc = '$bacc' AND ddate >= '$sd' AND ddate <= '$ed'
			  UNION SELECT jvno AS recpno,  jdate AS rdate, CONCAT('JV-',rmks), amount, 0 FROM bktrans WHERE dacc = '$bacc' AND jdate >= '$sd' AND jdate <= '$ed' 
			  UNION SELECT jvno AS recpno, jdate AS rdate, CONCAT('JV-',rmks), 0, amount FROM bktrans WHERE cacc = '$bacc' AND jdate >= '$sd' AND jdate <= '$ed' ORDER BY rdate, recpno";
      $dtot = 0;  $ctot = 0;  $i = 1;
    $result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC )){
        $dtot = $dtot + floatval($row['dam']);
        $ctot = $ctot + floatval($row['cam']);
        $tot = $tot + floatval($row['dam']) - floatval($row['cam']);
        $rdate = substr($row['rdate'],8,2)."-".substr($row['rdate'],5,2)."-".substr($row['rdate'] ,0,4);
        echo '<tr><td>'.$rdate.'</td><td>'.$row['recpno'].'</td><td>'.$row['nar'].'</td><td align="right">'.number_format($row['dam'],0).'</td><td align="right">'.number_format($row['cam'],0).'</td><td align="right">'.number_format($tot,0).'</td></tr>';      
        $i++;
    }
    echo '<tr><td colspan="7"><hr/></td></tr>';   
    echo '<tr><td colspan="3"></td><td align="right">'.number_format($dtot,0).'</td><td align="right">'.number_format($ctot,0).'</td><td align="right">'.number_format($tot,0).'</td></tr>';  
    echo '<tr><td colspan="7"><hr/></td></tr>';  
    echo '</table>'; 

    echo '<span align="left" style="font-size:x-small">Printed by: '.$user.' &nbsp;&nbsp; '.date("d-m-Y H:i:s"). '</span> <br />';
    echo '<div style="page-break-after:always"> </div>';
	echo '</div>';
}
echo '</body></html>';
?>
<script type="text/javascript">
function myprint(){
	var w = window.open('', '', 'width=800,height=600,resizeable,scrollbars');
	 w.document.write($("#printblk").html());
	
	 w.document.close(); 										// needed for chrome and safari
	 javascript:w.print();
	 w.close();
};	
</script>