<?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 '<script type="text/javascript" src="js/jquery.tools.min.js"></script>
<script type="text/javascript" src="js/jquery.PrintArea.js"></script>';
echo $recss;
echo '</head><body>';
$user = $_SESSION['reusername'];
$db=new MySQLDatabase();
$db->open_connection();
$sd = substr($_REQUEST['s'],6,4)."-".substr($_REQUEST['s'],3,2)."-".substr($_REQUEST['s'],0,2);
$ed = substr($_REQUEST['e'],6,4)."-".substr($_REQUEST['e'],3,2)."-".substr($_REQUEST['e'],0,2);       
$i=1;

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:20px;font-family:Trebuchet MS, Arial;font-size:12px;">
<tr><td colspan="7"><center><span style="font-size:18px;font-weight:normal;float:center;"> <b>PAYMENTS LIST </b></span><span style="font-size:22px;font-weight:bold"></span><center>
<br/><span style="font-size:16px;font-weight:bold;"><center>'. str_repeat('&nbsp;',25).' From: '.$_REQUEST['s'].'&nbsp;&nbsp; To: '.$_REQUEST['e'].'</center></span></td></tr>';
echo'<tr><u><th>S/no</th> <th>Payment No.</th> <th>Date</th> <th>Supplier</th> <th>Bank</th> <th>Naration</th> <th>Amount</th> </u></tr>';
echo '<tr><td colspan="7"><hr/></td></tr>'; 
$sql = mysqli_query($db->connection, "SELECT paytrans.payno, paytrans.pdate, paytrans.bacc, paytrans.supp, paytrans.amount, CONCAT(paytrans.pmode, ' - ', IFNULL(paytrans.chqno,' '),' - ', IFNULL(paytrans.rmks,' ')) as nar, suppliers.sname FROM paytrans, suppliers WHERE paytrans.supp>0 AND paytrans.supp = suppliers.id AND paytrans.pdate >= '$sd' AND paytrans.pdate <= '$ed' 
                    UNION SELECT dexpno as payno, ddate as pdate, bacc, expacc, amount, CONCAT(pmode, ' - ', IFNULL(chqno,' '),' - ', IFNULL(rmks,' ')) as nar, expacc as sname FROM dexptrans WHERE ddate >= '$sd' AND ddate <= '$ed'
					UNION SELECT paytrans.payno, paytrans.pdate, paytrans.bacc, paytrans.supp, paytrans.amount, CONCAT(paytrans.pmode, ' - ', IFNULL(paytrans.chqno,' '),' - ', reporders.regno, ' - ',reporders.vehmod) as nar, paytrans.ecat as sname FROM paytrans, reporders WHERE paytrans.supp=0 AND paytrans.repono = reporders.repono AND paytrans.pdate >= '$sd' AND paytrans.pdate <= '$ed' 
					ORDER BY bacc, pdate, payno;") or die (mysqli_error($db->connection) );
$tot = 0; $bacc = '*';	$gtot = 0;
while ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)){
    $nbacc = $row['bacc'];
    if(( $nbacc !== $bacc) && ($bacc !== '*') ){ 
      echo '<tr><td colspan="7"><hr/></td></tr>'; 
      echo '<tr><td colspan="6">'.$bacc.' TOTAL</td><td align="right">' . number_format($tot, 0). '</td></tr>';  
      echo '<tr><td colspan="7"><hr/></td></tr>';  $tot = 0;   
    }
    $bacc = $row['bacc'];
    echo "<tr>";
    echo "<td>" . $i . "</td>";
    echo "<td>" . $row['payno'] . "</td>";
    echo "<td style='width:110px;'>" . substr($row['pdate'],8,2)."-".substr($row['pdate'],5,2)."-".substr($row['pdate'],0,4)."</td>";
    echo "<td>" . $row['sname'] . "</td>";
    echo "<td>" . $row['bacc'] . "</td>";
    echo "<td>" . $row['nar'] . "</td>";
    echo "<td align='right'>" . number_format($row['amount'],0). "</td>";
    echo "</tr>";
    $i++; $tot = $tot + floatval($row['amount']);	$gtot = $gtot + floatval($row['amount']);
    }
echo '<tr><td colspan="7"><hr/></td></tr>'; 
echo '<tr><td colspan="6">'.$bacc.' TOTAL</td><td align="right">' . number_format($tot, 0). '</td></tr>';  
echo '<tr><td colspan="7"><hr/></td></tr>'; 	echo '<tr><td colspan="7"> </td></tr>'; 
echo '<tr><td colspan="6">GRAND TOTAL</td><td align="right">' . number_format($gtot, 0). '</td></tr>'; 
echo '<tr><td colspan="7"><hr/></td></tr>';  
echo '</table>'; 
echo '<span style="font-size:14px;font-weight:normal;float:left;font-family:Arial;margin-left:30px;">'; 
echo '<i>Printed by:</i> '.$user.'&nbsp;&nbsp;&nbsp;&nbsp;'.date("d-m-Y  H:i:s").'<br/><br/>'; 
echo '<i>Checked by:</i> _____________________<br/><br/>';
echo '<i>Approved by:</i> _____________________<br/></span>'; 
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>