<link rel="stylesheet" href="css/allcss.css" type="text/css" />
<script type="text/javascript" src="js/jquery.tools.min.js"></script>

<?php
session_start();
require_once 'includes/database.php';
require_once 'includes/funcs.php';
	
														// reprint
if (isset($_GET['r'])){
	$db=new MySQLDatabase();
	$db->open_connection();
    $payno =  $_GET['p'];
    $query="SELECT * FROM paytrans WHERE payno = '$payno'"; 
    $sql = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
    if ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)){
            $payamount = $row['amount']; 
            $bacc = $row['bacc'];
            $rmks = $row['rmks']; 
			$rtype = $row['rtype']; 
            $ts = $row['ts'];
            $supp = $row['supp'];
			$pmode = $row['pmode'];
            $chk = $row['chqno'].' - '.$row['bacc']; 
            $pd = substr($row['pdate'],8,2)."-".substr($row['pdate'],5,2)."-".substr($row['pdate'] ,0,4); 
            $user = $row['us'];
    }
	$db->close_connection();
}														// direct print after voucher
else {
    $payno = $_GET['p'];
    $user = $_SESSION['reusername'];
    $supp = $_GET['supp'];
    $payamount = $_GET['am'];
	$pd = $_GET['pd'];
    $rmks = $_GET['rmks'];
	$pmode = $_GET['pmode'];
    $chk = $_GET['chk'];
	$repono = $_GET['repono'];
	$ecat = $_GET['ecat'];
	$rtype = $_GET['rtype'];
    $ts = date("d-m-Y H:i:s");
}	
							
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 class="printable" style="width:650px;border: solid 1px #056fc4;font-family:Trebuchet MS, Arial;font-size:14px;"><div id="printheader">
      <tr><td colspan="3" style="width:650px;font-weight:bold class="trline"><center><span class="invoice">PAYMENT VOUCHER</span></center></td></tr>
      <tr><td align"left" colspan="3" class="trline">
			<table> <tr><td align"left" colspan="2"></td> 
				<td><span class="invoice"></span></td></tr></table>
           </div>';      
echo '<tr><td colspan="3"></td></tr>'; echo '<tr><td colspan="3"></td></tr>';		   
echo '<tr><td colpan="2"><span id="invoiceno">Payment Voucher No. &nbsp;'.$payno.'</span></td> <td></td> <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date &nbsp;'.$pd.'</td></tr>';


echo '<tr><td colspan="2" style="border: 1px solid #666; padding:10px;"><b>Payment To: &nbsp;<br/></b>'.(($ecat ==='') ? getsupplier($supp): 'EXPENSES').'</b>'
        . '</td> <td></td></tr><br/>';
echo '<tr><td colspan="3"></td></tr>';
echo '<tr><td colspan="3"></td></tr>';
echo '<tr><td colspan="3">Being Payment For:'.$rtype.'</td></tr>';

echo '<tr><td colspan="3" align="center"> <table cellspacing="6px" width="80%" cellpadding="5px" style="font-family:Trebuchet MS, Arial;font-size:12px;border-collapse:collapse;line-height:14px;">';
echo '<th style="width:60px;">Doc. No.</th> <th>Date</th> <th> Naration</th>  <th>Amount</th>';
echo '<tr><td colspan="4"><hr/></td></tr>';
$db=new MySQLDatabase();
$db->open_connection(); 
if($ecat ==='') {
	$sql = mysqli_query($db->connection, "SELECT insbills.billno, insbills.idate, insbills.amount, CONCAT('Policy No.',insrecs.pno,' - ',insrecs.regno,'-', insrecs.vehmod, '-', insrecs.cname) as nar FROM insbills, insrecs WHERE insbills.billno = insrecs.billno AND insbills.payno = '$payno'") or die (mysqli_error($db->connection) );
	$tot = 0;  
	while ($rowa = mysqli_fetch_array($sql, MYSQLI_ASSOC)){ 
		$idate = substr($rowa['idate'],8,2)."-".substr($rowa['idate'],5,2)."-".substr($rowa['idate'] ,0,4);
		echo '<tr><td align="left">'.$rowa['billno'].'</td> <td align="left">'.$idate.'</td> <td align="left">'.$rowa['nar'].'</td> <td align="right">'.number_format($rowa['amount'],2).'</td></tr>';      
	}
}
else {
	$sql = mysqli_query($db->connection, "SELECT CONCAT(regno,' - ', vehmod, ' - ', cname) as nar FROM reporders WHERE repono = '$repono'") or die (mysqli_error($db->connection) ); 
	if ($rowa = mysqli_fetch_array($sql, MYSQLI_ASSOC)){ 
		echo '<tr> <td colspan="3" align="left">'.$rowa['nar'].' - '.$ecat.'</td> <td align="right">'.number_format(floatval($payamount),2).'</td></tr>';
	  }	
}
$db->close_connection();
echo '<tr><td colspan="4"><hr/></td></tr>';	
echo '<tr><td align="right" colspan="4">Total Amount:&nbsp;&nbsp;'.number_format(floatval($payamount),2).'</td></tr>';
echo '</table></td></tr>';
echo '<tr><td colspan="3"></td></tr>';
	  
echo '<tr><td style="width:250px"><b></td><td style="width:100px"></td><td style="width:250px"><b><br></td></tr>';
echo '<tr><td colspan="3"></td></tr>';
echo '<tr><td colspan="2">Mode:&nbsp;'.$pmode.' - '.$chk.'</td><td align="left">Prepared By:&nbsp;'.$user.' - '.$ts.'</td></tr>';
echo '<tr><td align="right" colspan="3">&nbsp;&nbsp;</td></tr>';
echo '<tr><td colspan="2"></td><td align="left">Authorised By:____________</td></tr>';
echo '<tr><td colspan="3"></td></tr>'; 	
echo '<tr><td colspan="3"></td></tr>';
echo '<tr><td colspan="2"></td><td align="left">Collected By:____________</td></tr>';
echo '<tr><td colspan="2">Total Kshs:&nbsp; '.convert_number_to_words($payamount).' Only </td></tr>';
echo '<tr><td colspan="3"></td></tr>';
echo '<tr><td align="right" colspan="3"></td></tr></table>';
echo '</div>';

?>
<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>
