<?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 '<script type="text/javascript" src="js/jquery-1.10.2.js"></script>
<script type="text/javascript" language="javascript" src="js/jquery.dataTables.js"></script>
<link rel="stylesheet" type="text/css" href="css/jquery.dataTables.css">
<link rel="stylesheet" type="text/css" href="css/allcss.css">';
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;
$a = $_REQUEST['a'];	$f = $_REQUEST['f']; 
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" cellspacing="5px" style="width:90%;margin-left:20px;">
<tr><td colspan="7"><center><span style="font-size:18px;font-weight:normal;float:left;"> <b>REPOSESSION ORDERS 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>Order No.</th> <th>Date</th> <th>Details</th> <th>Customer</th> <th>Auctioneer</th> <th>Charged</th> <th>Expenses</th> <th>GP</th> </u></tr>';
echo '<tr><td colspan="7"><hr/></td></tr>'; 
if($a === ''){
 if($f === 'PENDING'){ 
	$sql = mysqli_query($db->connection, "SELECT tt.repono, tt.odate, tt.cname, tt.auct, tt.nar, tt.ram, SUM(paytrans.amount) as pam, tt.ddate FROM (SELECT reporders.repono, reporders.odate, reporders.auct, reporders.ddate, CONCAT(reporders.regno,'-',reporders.vehmod) as nar, reporders.cname, repoinvoices.amount as ram FROM reporders 
		LEFT OUTER JOIN repoinvoices ON reporders.repono = repoinvoices.repono) as tt LEFT OUTER JOIN paytrans ON paytrans.repono = tt.repono WHERE tt.odate >= '$sd' AND tt.odate <= '$ed' AND tt.ddate = '0000-00-00' GROUP BY tt.repono") or die (mysqli_error($db->connection) );
 }
 else {
	$sql = mysqli_query($db->connection, "SELECT tt.repono, tt.odate, tt.cname, tt.auct, tt.nar, tt.ram, SUM(paytrans.amount) as pam FROM (SELECT reporders.repono, reporders.odate, reporders.auct, CONCAT(reporders.regno,'-',reporders.vehmod) as nar, reporders.cname, repoinvoices.amount as ram FROM reporders 
		LEFT OUTER JOIN repoinvoices ON reporders.repono = repoinvoices.repono) as tt LEFT OUTER JOIN paytrans ON paytrans.repono = tt.repono WHERE tt.odate >= '$sd' AND tt.odate <= '$ed' GROUP BY tt.repono") or die (mysqli_error($db->connection) );
	 }	
}
else{
 if($f === 'PENDING'){ 
	$sql = mysqli_query($db->connection, "SELECT tt.repono, tt.odate, tt.cname, tt.auct, tt.nar, tt.ram, SUM(paytrans.amount) as pam, tt.ddate FROM (SELECT reporders.repono, reporders.odate, reporders.auct, reporders.ddate, CONCAT(reporders.regno,'-',reporders.vehmod) as nar, reporders.cname, repoinvoices.amount as ram FROM reporders 
		LEFT OUTER JOIN repoinvoices ON reporders.repono = repoinvoices.repono) as tt LEFT OUTER JOIN paytrans ON paytrans.repono = tt.repono WHERE tt.odate >= '$sd' AND tt.odate <= '$ed' AND tt.ddate = '0000-00-00' AND tt.auct = '$a' GROUP BY tt.repono") or die (mysqli_error($db->connection) );
	}
 else {
    $sql = mysqli_query($db->connection, "SELECT tt.repono, tt.odate, tt.cname, tt.auct, tt.nar, tt.ram, SUM(paytrans.amount) as pam FROM (SELECT reporders.repono, reporders.odate, reporders.auct, CONCAT(reporders.regno,'-',reporders.vehmod) as nar, reporders.cname, repoinvoices.amount as ram FROM reporders 
		LEFT OUTER JOIN repoinvoices ON reporders.repono = repoinvoices.repono) as tt LEFT OUTER JOIN paytrans ON paytrans.repono = tt.repono WHERE tt.odate >= '$sd' AND tt.odate <= '$ed' AND tt.auct = '$a' GROUP BY tt.repono") or die (mysqli_error($db->connection) );
  } 
}
$gtot = 0;
while ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)){
    $odate = substr($row['odate'],8,2)."-".substr($row['odate'],5,2)."-".substr($row['odate'],0,4);
	$tot = floatval($row['ram']) - floatval($row['pam']);
    echo "<tr><td>".$row['repono']."</td> <td>".$odate."</td> <td>".$row['nar']."</td> <td>".$row['cname']."</td> <td>".$row['auct']."</td> <td align='right'>".number_format($row['ram'],0)."</td> <td align='right'>".number_format($row['pam'],0)."</td> <td align='right'>".number_format($tot,0)."</td></tr>";
    $i++; $gtot = $gtot + $tot;
    }
echo '<tr><td colspan="8"><hr/></td></tr>'; 
echo '<tr><td colspan="7">TOTAL</td><td align="right">' . number_format($gtot, 0). '</td></tr>'; 
echo '<tr><td colspan="8"><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/></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.write('<link rel="stylesheet" type="text/css" href="css/allcss.css">');
	 w.document.close(); 										// needed for chrome and safari
	 javascript:w.print();
	 w.close();
};	
</script>