<?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'];

$cus = $_REQUEST['c'];	
if($cus === ""){ $cust = "All Customers";}	
else {$cust = $_REQUEST['c'];}	 
$ed = substr($_REQUEST['e'],6,4)."-".substr($_REQUEST['e'],3,2)."-".substr($_REQUEST['e'],0,2);
$sd = substr($_REQUEST['s'],6,4)."-".substr($_REQUEST['s'],3,2)."-".substr($_REQUEST['s'],0,2);

    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;">
    <tr><td colspan="7"><center><span style="font-size:18px;font-weight:normal;float:right;"> <b>REPOSESSION EXPENSES LIST</b></span><span style="font-size:22px;float:left;font-weight:bold"> QOOLPOINT LTD</span></center></td></tr>
    <tr><td colspan="7"><span style="font-size:16px;font-weight:bold;"><center>'. $cust.'</center></span><br/>
	<br/><span style="font-size:16px;font-weight:bold;"><center>'. str_repeat('&nbsp;',15).'Details From: &nbsp;'.$_REQUEST['s'].' &nbsp; To: &nbsp;'.$_REQUEST['e'].'</center></span></td></tr>';
    echo'<tr><u><th>S/no</th> <th>Date</th> <th>Doc. No.</th> <th>Naration</th> <th>Type</th> <th>Amount</th></u></tr>'; 
    echo '<tr><td colspan="7"><hr/></td></tr>'; 

	$db=new MySQLDatabase();
	$db->open_connection();
															// get invoices & receipts
	if($cus === ""){ $query = "SELECT paytrans.payno, reporders.repono, paytrans.pdate, CONCAT(reporders.regno, ' - ', reporders.vehmod, ' - ', paytrans.rmks) as nar, paytrans.ecat, paytrans.amount FROM paytrans, reporders WHERE reporders.repono = paytrans.repono AND paytrans.pdate >= '$sd' AND paytrans.pdate <= '$ed' ORDER BY reporders.cname, reporders.repono, paytrans.pdate"; }
	else { $query = "SELECT paytrans.payno, reporders.repono, paytrans.pdate, CONCAT(reporders.regno, ' - ', reporders.vehmod, ' - ', paytrans.rmks) as nar, paytrans.ecat, paytrans.amount FROM paytrans, reporders WHERE reporders.repono = paytrans.repono AND paytrans.pdate >= '$sd' AND paytrans.pdate <= '$ed' AND reporders.cname = '$cus' ORDER BY reporders.repono, paytrans.pdate"; }			
	$tot = 0;  	$otot = 0;	 $i = 1; 	$orepo = 0;
    $result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
	   if((intval($row['repono']) !== $orepo) && ($orepo!==0)){ 
										echo '<tr><td colspan="6"><hr/></td></tr>'; 
										echo '<tr><td colspan="5">Order No. Total</td> <td align="right">'.number_format($otot,0).'</td></tr>'; 
										echo '<tr><td colspan="6"><hr/></td></tr>'; 
										$otot = 0;	$i = 1; 
									}
		$otot = $otot + floatval($row['amount']);							
        $tot = $tot + floatval($row['amount']);
		$orepo = intval($row['repono']);
        $pdate = substr($row['pdate'],8,2)."-".substr($row['pdate'],5,2)."-".substr($row['pdate'] ,0,4);
        echo '<tr><td>'.$i.'</td> <td>'.$pdate.'</td> <td>'.$row['payno'].'</td> <td>'.$row['nar'].'</td> <td>'.$row['ecat'].'</td> <td align="right">'.number_format($row['amount'],0).'</td></tr>';      
        $i++;
    }
	echo '<tr><td colspan="6"><hr/></td></tr>';   
    echo '<tr><td colspan="5">Order No. Total</td> <td align="right">'.number_format($otot,0).'</td></tr>'; 
    echo '<tr><td colspan="6"><hr/></td></tr>';   
    echo '<tr><td colspan="5">Grand Total</td> <td align="right">'.number_format($tot,0).'</td></tr>';  
    echo '<tr><td colspan="6"><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> </div> <br />';
    echo '<div style="page-break-after:always"> </div>';

$db->close_connection();
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>