<?php
session_start();
 
if($_SESSION['dbName']==""){
header("location:../index.php");
}

 
include("config.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

$query_setup  = "select company_name, company_memo, DecimalBase, Setup_VatPercentage, setup_invoiceremarks from setup";
$result_setup = mysql_query($query_setup, $connection); 
$row_setup    = mysql_fetch_array($result_setup);
extract($row_setup);
print <<<HERE
<html>
<head>
<title>INVOICE REPORT</title>
</head>
<body>
HERE;

$query = "Select Transferrequest_Remark , transferrequest_id, Transferrequest_Reference as Delivery_Reference,
Date_Format(Transferrequest_Date,'%d/%m/%Y') As Delivery_Date ,
transferrequest.client_code As ClientCode, Clients.Ledger_name As ClientName,
Client_VAT,Client_VATNo, Client_Address As ClientAddress, Client_Telephone1 as Client_Telephone1,Client_Telephone2 as Client_Telephone2, Client_CreditLimit, client_fax
 ,total as GrossTotal , discount as  DeliveryDiscountAmount ,vat as Delivery_VAT ,
 (total +vat -discount) as nettotal , currencycode As CurrencyCode ,
 Date_Format(datefrom,'%d/%m/%Y') as rentfromdate , Date_Format(dateto,'%d/%m/%Y') as renttodate
 
From transferrequest
Left Join Clients on transferrequest.Client_code = Clients.Ledger_Number Where Transferrequest_Id = ".$_GET['transferrequest_id'];
			 
			 
//echo $query;exit;
	$result=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
	}
echo "<table width=800>
<tr>
<td width=50%><img src='LogoFile.php' width=180 /></td>
<td width=50% style='text-align: center; font-family: Arial; font-size: 40px;  color: #a6a6a6;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Invoice</td>
</tr>
</table>
<table width=800>
<tr style='height: 20px'>
<td></td>
</tr>
</table>
<table width=800>
<tr>
<td width=500 style='font-family: Arial; font-size: 14px'><i><b><u>$company_name</u></b></i>
<br><b>
<pre>$company_memo</pre>
</td>
<td style='width: 300px; vertical-align: top;'>
<table width=100%>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>DATE:</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 13px'>$Delivery_Date</td>
</tr>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>Reference #:</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 13px'>$Delivery_Reference</td>
</tr>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>Customer ID:</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 13px'>$ClientCode</td>
</tr>


<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>Currency Code:</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 13px'>$CurrencyCode</td>
</tr>



</table>
</td>
</tr>
<tr style='height: 15px'>
<td></td>
</tr>
</table>
<table width=800>
<tr>
<td style='font-weight: bold; font-size: 13px; font-family: Arial'>Rent Invoice:</td>
</tr>
</table>
<table width=800>
<tr>
<td style='font-size: 13px; font-family: Arial' width=55>Name</td>
<td style='font-size: 13px; font-family: Arial' width=745>: $ClientName</td>
</tr>
<tr>
<td style='font-size: 13px; font-family: Arial' width=55>Address</td>
<td style='font-size: 13px; font-family: Arial' width=745>: $ClientAddress</td>
</tr>
<tr>
<td style='font-size: 13px; font-family: Arial' width=55>Phone</td>
<td style='font-size: 13px; font-family: Arial' width=745>: $Client_Telephone1 $Client_Telephone2</td>
</tr>";
if($Client_VAT == 1){
echo"<tr>
<td style='font-size: 13px; font-family: Arial' width=55>VAT #</td>
<td style='font-size: 13px; font-family: Arial' width=745>: $Client_VATNo</td>
</tr>";
}


echo "

<tr>
<td style='font-size: 13px; font-family: Arial' width=90>From Date</td>
<td style='font-size: 13px; font-family: Arial' width=300>: $rentfromdate</td>

<td style='font-size: 13px; font-family: Arial' width=90>To Date</td>
<td style='font-size: 13px; font-family: Arial' width=300>: $renttodate</td>


</tr>

";
 

echo "<tr style='height: 15px'>
<td colspan=2></td>
</tr>";
echo "<tr>
<td colspan=2 style='font-size: 13px; font-family: Arial;' ><b>Comments or special instructions:</b><br>&nbsp;$Transferrequest_Remark</td>
</tr>";
echo "<tr style='height: 15px'>
<td colspan=2></td>
</tr>";
echo"</table>";
echo "<table width=800 style='border-collapse:collapse'>";
echo "<tr >";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; background: #d0d0d0'>Code</th>";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 285px; background: #d0d0d0 '>Description</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 50px; background: #d0d0d0 '>Qty</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 100px; background: #d0d0d0 '>Unit Price</th>";
	// echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; background: #d0d0d0 '>Disc.</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 100px; background: #d0d0d0 '>Total Price ".$CurrencyCode."</th>";
	echo "</tr>";
	$querydt = " Select transferrequestdt.TransferrequestDt_id,   transferrequestdt.Item_code as ItemCode, Item_Description As ItemDescription, ifNull(Items.Item_Unit,1) as ItemUnit, ".
			   " Item_DescriptionF1 As ItemDescriptionF1, Item_DescriptionF2 As ItemDescriptionF2,   ".
			   " TransferrequestDt_Quantity as DeliveryDtQuantity, Round(transferrequestdt_price, ".$DecimalBase.") As DeliveryDtPrice ".
		    ", Round(((TransferrequestDt_Quantity*transferrequestdt_price)-((TransferrequestDt_Quantity*transferrequestdt_price*ifNull(null,0))/100)), ".$DecimalBase.") As DeliveryDtTotal ".
			   
				
			   " From transferrequestdt Left Join Items on transferrequestdt.Item_code = Items.Item_code ".
			   " Where transferrequestdt.Transferrequest_Id = ".$_GET['transferrequest_id'].
			   " Order By transferrequestdt.TransferrequestDt_id Asc  ";
			   
	 
	 //echo $querydt;exit;
	$resultdt = mysql_query($querydt, $connection);
	$total_beforediscount=0;
	$total_discountamount=0;
	$total_discountamount_oninvoice=0;
	while($rowdt = mysql_fetch_array($resultdt)){
		// echo "<script> alert('hi'); </script>";
		extract($rowdt);
		$total_beforediscount = $total_beforediscount + ($DeliveryDtQuantity*$DeliveryDtPrice);
		$total_discountamount = $total_discountamount + ($DeliveryDtQuantity*$DeliveryDtPrice * $deliverydt_itemdiscount / 100);
		$total_discountamount_oninvoice = $total_discountamount_oninvoice + ($DeliveryDtQuantity*$DeliveryDtPrice * $deliverydt_clientdiscount / 100);
		
		echo "<tr >";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; vertical-align: top; '>$ItemCode</th>";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 285px; vertical-align: top;'>$ItemDescription</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 50px; vertical-align: middle; '>$DeliveryDtQuantity</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 100px; vertical-align: middle; text-align: right;'>".number_format($DeliveryDtPrice, $DecimalBase)."</th>";
	// echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; vertical-align: middle; text-align: right;'>$DeliveryDtDiscountPer %</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 100px; vertical-align: middle; text-align: right;'>".number_format($DeliveryDtTotal, $DecimalBase)."</th>";
	echo "</tr>";
	}
	//echo $total_beforediscount;
	$total_afterdiscount=$total_beforediscount-$total_discountamount-$total_discountamount_oninvoice;
	if (empty($nettotal)) $nettotal = 0;
	$queryalpha  = " SELECT Alpha_Num ('ENGLISH', '".$CurrencyCode."', ".$nettotal.") As AlphaAmount FROM Dual ";
		
	$resultalpha = mysql_query($queryalpha,$connection) or die(mysql_error());
	$rowalpha=mysql_fetch_array($resultalpha);
	echo "<tr><td colspan=3 rowspan=4 style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; background: #d0d0d0; text-align: left; font-weight: bold'>".$rowalpha[0]."</td>
	<td colspan=1 style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; background: #d0d0d0; text-align: right; border-bottom: none; font-weight: bold'>Gross Total </td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 100px; vertical-align: middle; text-align: right; border-bottom: none'>".number_format($GrossTotal, $DecimalBase)."</td>
	</tr>
	<tr>
	<td colspan=1 style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; background: #d0d0d0; text-align: right; border-bottom: none; font-weight: bold; border-top: none'>Discount</td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 100px; vertical-align: middle; text-align: right; border-bottom: none; border-top: none'>".number_format($DeliveryDiscountAmount, $DecimalBase)."</td>
	</tr>
	<tr>
	<td colspan=1 style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; background: #d0d0d0; text-align: right; border-bottom: none; font-weight: bold; border-top: none'>VAT $Setup_VatPercentage%</td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 100px; vertical-align: middle; text-align: right; border-bottom: none; border-top: none'>".number_format($Delivery_VAT, $DecimalBase)."</td>
	</tr>
	<tr>
	<td colspan=1 style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; background: #d0d0d0; text-align: right; font-weight: bold; border-top: none'>NET Total</td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 100px; vertical-align: middle; text-align: right;  border-top: none'>".number_format($nettotal, $DecimalBase)."</td>
	</tr>
	<tr style='height: 35px'>
	<th colspan=6></th>
	</tr>
	</tr>
	<tr>
	<td colspan=6 style='font-size: 13px; font-weight: none; font-family: Arial'>$setup_invoiceremarks</td>
	</tr>
	<tr style='height: 45px'>
	<th colspan=6></th>
	</tr>
	<tr>
	<th colspan=6 style='font-size: 14px; font-family: Arial'>THANK YOU FOR YOUR BUSINESS!</th>
	</tr>
	";
echo"</table>";
print <<<HERE
<script>
window.print();
</script>
</body>
</html>
HERE;
?>