<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="css/allcss.css" type="text/css" />

<script type="text/javascript" src="js/jquery.tools.min.js"></script>
<script type="text/javascript" src="js/jquery.PrintArea.js"></script>
</head>
<body>
<?php
require_once 'includes/database.php';
if (isset($_GET['i'])){
    $invno =  $_GET['i'];
    date_default_timezone_set('Africa/Nairobi'); //time
	$db=new MySQLDatabase();
	$db->open_connection();
    $query="SELECT insrecs.*, insinvoices.invoicecredit, insinvoices.idate, insinvoices.amount, insrecs.amount as cusam 
	FROM insinvoices, insrecs WHERE insinvoices.insno = insrecs.insno AND insinvoices.invno = '$invno'"; 
    $sql = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
    if ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)){
            $amount = $row['amount'];	$cusam = $row['cusam'];
            $ts = date("d-m-Y h:i A"); 
            $insprem = $row['insprem'];		$levy = round(floatval($row['clevy']));		$sduty = round(floatval($row['csduty']));
            $cwtax = round(floatval($row['cwtax']));	$plllab = $row['plllab'];	$pllam = intval($row['pllam']);
			$insrate = $row['insrate'];		$regno = $row['regno'];
			$cexpr = $row['cexpr'];    $cexp = $row['cexp'];    $cpvt = $row['cpvt'];   $cpvtr = $row['cpvtr'];  
			$pno = $row['pno'];		$vehmod = $row['vehmod'];	
			$insam = $row['inam'];	$instel = $row['instel'];	$insaddr = $row['insaddr'];
			$cname = $row['cname'];		$insurer = $row['insurer'];
			$agpno = '';
			if(($row['agpno'] !== NULL) || (!empty($row['agpno']))) { $agpno = $row['agpno']; }
			elseif(($row['agpno3']  !== NULL) || (!empty($row['agpno3']))) { $agpno = $row['agpno3']; }
			elseif(($row['agpno2']  !== NULL) || (!empty($row['agpno']))) { $agpno = $row['agpno2']; }
			elseif(($row['agpno1'] !== NULL) || (!empty($row['agpno']))) { $agpno = $row['agpno1']; }
			$invno1 = $row['invno1'];		$invno2 = $row['invno2'];		$invno3 = $row['invno3'];
			$ic = intval($row['invoicecredit']); 
            $idate = substr($row['idate'],8,2)."-".substr($row['idate'],5,2)."-".substr($row['idate'] ,0,4); 
            $user = $row['us'];
    }
		
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="inv">';	
echo '<table id="trinv" cellpadding="3px" style="font-family:Trebuchet MS, Arial, Helvetica, sans-serif;
font-size:14px;
width:740px;
border-collapse:collapse;
margin-left:10px;
margin-right:5px;
margin-top:30px;
border: 2px solid black; 
padding-right:3px;">
		<tr><td align="center" colspan="3" style="width:760px;border: 2px solid black;">
			<table> <tr><td><img src="images/inslogo.png" style="vertical-align:middle;"></td> 
				<td><span style="font-size:28px;font-weight:bold;">QOOLPOINT INSURANCE AGENCY</span> <br/>
					<span style="font-size:14px;">P.O.Box 82496 - 80100, Mombasa  Tel: 0412228887 / 0722718887 <br> Email: insurance@qool-point.co.ke <br/></span>
				</td></tr>
<tr><td colspan="2" align="right">REGULATED BY THE INSURANCE REGULATORY AUTHORITY</td></tr></table>
		</td></tr>';
echo '<tr><td align="left" style="font-size:14px;">PIN No. &nbsp; A002517691H</td> <td colspan="2" style="width:300px;font-weight:bold;font-family:Arial;font-size:24px;text-align:right; padding-right:30px;">'.(($ic > 0) ? 'INVOICE' : 'CREDIT NOTE').'</td></tr>';   		   
echo '<tr><td colspan="3"></td></tr>';	
echo '<tr><td colpan="2">'.(($ic > 0) ? 'INVOICE' : 'CREDIT NOTE').' No. &nbsp;'.$invno.'</td> <td style="text-align:right;padding-right:10px;">Date : &nbsp;'.$idate.'&nbsp;</td></tr>';
//echo '<tr><td colspan="3"><hr/><td></tr></table>';	   

echo '<table cellpadding="3px" style="border-collapse:collapse; width:740px; font-family:Trebuchet MS, Arial;font-size:14px; margin-left:10px; padding-right:3px;border: 2px solid black;">';	  		   
//echo '<tr><td colspan="3"></td></tr>'; 

echo '<tr><td colspan="3" style="font-size:18px;"><b> <br>To: &nbsp;<br/></b>'.(($cname === "RETAIL CUSTOMERS" ) ? $insurer : $cname.' / '.$insurer) . '<br/>'.$insaddr.'<br/> Tel : '.$instel.'<br/> </td> </tr>';

echo '<tr><td colspan="3" style="border: 2px solid black;"><br/>Policy No.&nbsp; '.$agpno.' </td></tr>';

echo '<tr><td colspan="3">
<tr><td style="width:450px;border: 2px solid black;"><b>DESCRIPTION</b></td> <td style="width:140px;border: 2px solid black;"><b></b></td> <td style="width:140px;border: 2px solid black;"><b>AMOUNT</b></td> </tr>'; 
echo '<tr> <td colspan="3">&nbsp; </td></tr>';
echo '<tr> <td colspan="3">'.(($cname === "RETAIL CUSTOMERS" ) ? $insurer : $cname.' / '.$insurer) .'</td></tr>';
echo '<tr> <td colspan="3"> REG. No.&nbsp;'.$regno.'</td></tr>';
echo '<tr> <td colspan="3"> Value &nbsp;'.number_format($insam,0).'</td></tr>';
echo '<tr> <td colspan="3"> Rate &nbsp;'.$insrate.'%</td></tr>';
echo '<tr><td> Basic Premium </td> <td align="right">'.number_format($insprem,0).'</td> <td></td></tr>';
if ($pllam > 0) { echo '<tr><td>PLL :'.$plllab.' </td> <td align="right">'.number_format($pllam,0).'</td> <td></td></tr>'; }
if($row['cpvt'] != "0") { echo '<tr><td> PVT : '.$cpvtr.' % </td> <td align="right">'.number_format($cpvt,0).'</td> <td></td></tr>'; }
if($row['cexp'] != "0") { echo '<tr><td> Excess Protector : '.$cexpr.'% </td> <td align="right">'.number_format($cexp,0).'</td> <td></td></tr>'; }
echo '<tr><td> Levy</td> <td align="right">'.number_format($levy,0).'</td> <td></td></tr>';
echo '<tr><td> Stamp Duty </td> <td align="right">'.number_format($sduty,0).'</td> <td></td></tr>';

echo '<tr><td> Total Premium </td> <td align="right">'.number_format($cusam,0).'</td> <td></td></tr>';
if ($ic == 0) { $amo = 'Credit Amount'; }
else { 
	if($invno3 !== NULL){ $amo = '3rd Installment';}
	elseif($invno2 !== NULL){ $amo = '2nd Installment';}
	elseif($invno1 !== NULL){ $amo = '1st Installment';}
	else {  $amo = 'Full Annual Premium'; }
}
echo '<tr> <td colspan="3">&nbsp; </td></tr>';
echo '<tr> <td colspan="2" style="border: 2px solid black;" align="left">'.$amo.'&nbsp;&nbsp;</td> <td style="border: 2px solid black;"" align="right">'.number_format($amount,0).'</td></tr>'; 	
echo '<tr><td colspan="3">&nbsp; </td></tr>';
echo '<tr><td colspan="3">&nbsp; </td></tr>';
echo '<tr><td colspan="3">&nbsp; </td></tr>';
echo '<tr><td colspan="3">&nbsp; </td></tr>';
echo '<tr><td colspan="3">&nbsp; </td></tr>';
echo '<tr> <td colspan="2" style="border: 2px solid black;" align="right">Total'.(($ic>0) ? '' : ' Credit').' Amount:&nbsp;&nbsp;</td> <td style="border: 2px solid black;" align="right">'.number_format($amount,0).'</td></tr>'; 
echo '<tr> <td colspan="3" style="border: 2px solid black;" align="left">THANK YOU FOR YOUR BUSINESS&nbsp;&nbsp;</td> </tr>'; 
echo '</table></table>';

echo '<table style="font-family:Trebuchet MS;margin-left:20px;font-size:14px;">';
echo '<tr> <td colspan="2" align="left"><br><br><br>Prepared By:&nbsp;'.$user.' - '.$ts.'</td> <td align="right"><br><br><br> &nbsp;&nbsp;&nbsp;&nbsp;Approved By:_____________</td></tr>';
echo '<tr> <td colspan="3"> &nbsp;</td></tr>';
echo '<tr><td colspan="3"></td>&nbsp;</tr>'; 	echo '<tr><td colspan="3">&nbsp;</td></tr>';
echo '<tr><td colspan="3"></td></tr>';
echo '<tr><td align="right" colspan="3"></td></tr></table>';
echo'</table></div>';
$db->close_connection();
}
?>
</body>

<script type="text/javascript">
function myprint(){
	var w = window.open('', '', 'width=900,height=600,resizeable,scrollbars');
	 w.document.write($("#inv").html());
	 w.document.close(); 										// needed for chrome and safari
	 javascript:w.print();
	 w.close();
};	
</script>
</html>