<?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;">
<tr><td colspan="10"><center><span style="font-size:18px;font-weight:normal;float:left;"> <b>INSURANCE STATUS 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>Booking No.</th> <th>Date</th> <th>Customer</th> <th>Provider</th> <th>Details</th> <th>Type</th> <th>Value</th> <th>Premium</th> <th>Cus. Receipt</th> <th>Pro.Payment</th> </u></tr>';
echo '<tr><td colspan="10"><hr/></td></tr>'; 
$sql = mysqli_query($db->connection, "SELECT insno, idate, cname, supp, CONCAT(regno,'-',vehmod,'-',insurer) as nar, icat, inam, insprem, invno, billno FROM insrecs WHERE idate >='$sd' AND idate <='$ed' ORDER BY insno;") or die (mysqli_error($db->connection) );
while ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)){
    $idate = substr($row['idate'],8,2)."-".substr($row['idate'],5,2)."-".substr($row['idate'],0,4);
	$invno = $row['invno']; 	$billno = $row['billno'];
	$recp = 0;
	$asql = mysqli_query($db->connection, "SELECT paidamount FROM insinvoices WHERE invno = '$invno';") or die (mysqli_error($db->connection) );
	if ($rowa = mysqli_fetch_array($asql, MYSQLI_ASSOC)){ $recp = $rowa['paidamount'];	 }
	$paym = 0;
	$asql = mysqli_query($db->connection, "SELECT paidamount FROM insbills WHERE billno = '$billno';") or die (mysqli_error($db->connection) );
	if ($rowb = mysqli_fetch_array($asql, MYSQLI_ASSOC)){ $paym = $rowb['paidamount'];	 } 
    echo "<tr><td>".$row['insno']."</td> <td>".$idate."</td> </td> <td>".$row['cname']."</td> <td>".getsupplier($row['supp'])."</td> <td>".$row['nar']."</td> <td>".$row['icat']."</td> <td align='right'>".number_format($row['inam'],0)."</td> <td align='right'>".number_format($row['insprem'],0)."</td>  <td align='right'>".number_format($recp,0)."</td>  <td align='right'>".number_format($paym,0)."</td></tr>"; 
    }
echo '<tr><td colspan="10"><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 '</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>