<?php
//header("Content-type: text/html; charset=windows-1256");
//header("Content-type: text/html; charset=utf-8");

//Report Version For Tiba
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");


if ($_GET['PExportExcel'] == 1) {
	header("Content-Disposition: attachment; filename=Form_DeliveryExcel.xls");
    header("Content-Type: application/vnd.ms-excel");
}

if($_GET['SalesId'] != ''){
	$queryU="Update Deliveries set Delivery_Printed = ifNull(Delivery_Printed,0) + 1 Where  Delivery_ID = ".$_GET['SalesId'];
	$resultU=mysql_query($queryU,$connection) or die(mysql_error());
	}






print <<< HERE
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<TITLE> Invoice Report </TITLE>

<style type="text/css">

table.master th { font-size: 14px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                }				 
table.master td { font-size: 14px; 
                  font-family:Times-New-Roman;
                 }
table.detail th { font-size: 13px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
				  border-top:1px solid black;
				  border-bottom:1px solid black;
				  border-left:1px solid black;
                }				 
				 
table.detail td { font-size: 12px; 
                  font-family:Times-New-Roman;
				  border-left:1px solid black;
				  word-wrap: break-word; 
				  word-break:break-all;
                 }				 
				 
table.masterAR th { font-size: 16px; 
					font-weight: bold;
					font-family:Times-New-Roman;
				  }				 
table.masterAR td { font-size: 16px; 
					font-family:Times-New-Roman;
                 }
table.detailAR th { font-size: 14px; 
					font-weight: bold;
					font-family:Times-New-Roman;
					border-top:1px solid black;
					border-bottom:1px solid black;
					border-left:1px solid black;
                }				 
				 
table.detailAR td { font-size: 14px; 
					font-family:Times-New-Roman;
					border-left:1px solid black;
					word-wrap: break-word; 
					word-break:break-all;
                 }
				 

</style>


</HEAD>

<BODY>
HERE;



//mysql_set_charset('utf8',$connection);

//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, Setup_vatno, ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage, ".
			 " Company_Name, Company_NameF, Company_Memo, Company_MemoF, Company_Capital, Company_CapitalF ".
			 " from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

//Read From SalesSetup Table
$querySalesSetup="Select SalesTTC From SalesSetup";
$resultSalesSetup=mysql_query($querySalesSetup,$connection) or die(mysql_error());
while($rowSalesSetup=mysql_fetch_array($resultSalesSetup)){
	extract($rowSalesSetup);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_AdditionalTaxes,0) As Module_AdditionalTaxes, ifNull(Module_ShowInvoiceAging,0) As Module_ShowInvoiceAging, ".
             " ifNull(Module_ShowLayoutProj,0) As Module_ShowLayoutProj, ifNull(Module_ShowLayoutCost,0) As Module_ShowLayoutCost ".
			 " From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}



$TotalQty = 0;
$TotalGrossWeight = 0;

if (empty($_GET['PLang'])) $_GET['PLang'] = 'AR';

if ($_GET['PLang'] == 'AR') {
	$MasterClass='masterAR';
	$DetailClass='detailAR';	
} else {
	$MasterClass='master';
	$DetailClass='detail';	
}	

if($_GET['SalesId'] != '')
{
	
	
	$query = " Select Delivery_ID, Delivery_Reference, Date_Format(Delivery_Date,'%d/%m/%Y') As Delivery_Date, concat(Date_Format(Delivery_Date,'%Y'), '-12-31 23:59:59') As InvoiceDate, ".
	         " Date_Format(JournalDt_ValueDate,'%d/%m/%Y') As MaturityDate, ifNull(Deliveries.Journal_ID,0) As JournalID, ".
			 " Deliveries.Client_code As ClientCode, Clients.Ledger_name As ClientName, Client_VAT, Client_Telephone1 as Client_Telephone1, Client_CreditLimit, ".
			 " Client_Address As ClientAddress, Client_Address2 As ClientAddressF, Clients.Ledger_nameF As ClientNameF, Client_Remark,   ".
			 " Client_VATNo, Deliveries.Currency_code As CurrencyCode, Client_Address, Delivery_ShippedTo, Delivery_Marks, Delivery_ShippedVia, ".
			 " Delivery_Remark, Deliveries.Project_Code As ProjectCode, Project_Name, Deliveries.Costcent_code As Costcentcode, Costcent_name,  ".
			 " Round(ifNull(Delivery_Taxable,0), ".$DecimalBase.") As DeliveryTaxable, Round(ifNull(Delivery_NoneTaxable,0), ".$DecimalBase.") As DeliveryNoneTaxable, ".
			 " Round(ifNull(Delivery_Total,0), ".$DecimalBase.") As GrossTotal, ".
			 " Round(ifNull(Delivery_DiscountPer,0), ".$DecimalBase.") As DeliveryDiscountPer, Round(ifNull(Delivery_DiscountAmount,0), ".$DecimalBase.") As DeliveryDiscountAmount,  ".
			 " Round(ifNull(Delivery_VAT,0), ".$DecimalBase.") As Delivery_VAT, Round(ifNull(Delivery_VATBase1,0), ".$DecimalBase.") As Delivery_VATBase1, ".
			 " Round((ifNull(Delivery_Total,0) + IfNull(Delivery_VAT,0) - ifNull(Delivery_TransportAmount,0) - IfNull(Delivery_DiscountAmount,0) - IfNull(Delivery_BonusAmount,0) + (ifNull(Delivery_Tax1,0) + ifNull(Delivery_Tax2,0) + ifNull(Delivery_Tax3,0) + ifNull(Delivery_Tax4,0) + ifNull(Delivery_Tax5,0))), ".$DecimalBase.") As Delivery_NetTotal,  ".
			 " Round((ifNull(Delivery_Tax1,0) + ifNull(Delivery_Tax2,0) + ifNull(Delivery_Tax3,0) + ifNull(Delivery_Tax4,0) + ifNull(Delivery_Tax5,0)), ".$DecimalBase.") As Delivery_TotalTaxes,  ".
			 " Round(ifNull(Delivery_TransportAmount,0), ".$DecimalBase.") As DeliveryTransportAmt , Round(ifNull(Delivery_DiscountTTC,0), ".$DecimalBase.") As DeliveryDiscountTTC,   ".
			 " Round((ifNull(Delivery_TaxableTTC,0) + ifNull(Delivery_NoneTaxable,0) - ifNull(Delivery_TransportTTC,0) - ifNull(Delivery_BonusTTC,0) - IfNull(Delivery_DiscountTTC,0) + (ifNull(Delivery_Tax1,0) + ifNull(Delivery_Tax2,0) + ifNull(Delivery_Tax3,0) + ifNull(Delivery_Tax4,0) + ifNull(Delivery_Tax5,0))), ".$DecimalBase.") As Delivery_NetTotalTTC,  ".
			 " Deliveries.Salesman_Code As SalesmanCode, Deliveries.User_Account as UserAccount, Deliveries.Delivery_ManualNo as DeliveryManualNo, Client_Telephone2, ".
			 " ifNull(Delivery_CashInBase1,0) as DeliveryCashInBase1, ifNull(Delivery_CashInBase2,0) as DeliveryCashInBase2, Client_PaymentTerms,client_address3,client_discountonInvoice ".
			 " From Deliveries ".
			 " Left Join Clients on Deliveries.Client_code = Clients.Ledger_Number ".
			 " Left Join Project on Deliveries.Project_Code = Project.Project_Code ".
			 " Left Join CostCent on Deliveries.Costcent_code = CostCent.CostCent_Code ".
	         " Where Delivery_ID = ".$_GET['SalesId'];

	$result=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
	}
	
	if (empty($JournalID)) $JournalID = 0;
	//Get Client Balance
	$querybalance = "SELECT IFNULL(GetClientBalance('".$ClientCode."','".$InvoiceDate."','".$Costcentcode."','".$CurrencyCode."','".$base1."','".$base2."',1) ,0) ";
	//echo "<br><br>".$queryprevbalance;
	$resultbalance=mysql_query($querybalance,$connection) or die(mysql_error());
	$rowbalance=mysql_fetch_array($resultbalance);
	$ClientCurrBalance = $rowbalance[0];
	$ClientPrevBalance = $ClientCurrBalance - $Delivery_NetTotal;
	
	$title = 'Invoice No '.$Delivery_Reference;
		
	echo "<table border=0 width=760 cellspacing=0 cellpadding=0><tr><td align='left'>";
	
	echo "<table border='0' width='750' cellspacing=0 cellpadding=0 class='$MasterClass' style='table-layout:fixed;' >
		  <col width=230>
		  <col width=210>
		  <col width=110>
		  <col width=180>
		  
		  ";
	if($printlogo==0)	  
	echo "<tr height=120>";
	else
	echo "<tr>";
	
	if($printlogo==1)
	echo "	<td align='left' colspan='1' valign='top' style='font-size:12px; font-style:italic; font-weight:bold;'>".nl2br($Company_Name)."</td>";
	else
	echo "	<td align='left' colspan='1' valign='top' style='font-size:12px; font-style:italic; font-weight:bold;'></td>";
	
	if($printlogo==1)
	echo "	<td align='center' valign=center rowspan='3'><img src='LogoFile.php' width='200px' height='80px' /></td>
								<td  valign=center rowspan='3'><img src='IsoFile.php' width='130px' height='80px' />
								</td>";
	else echo"<td align='center' valign=center rowspan='3'>&nbsp;</td><td  valign=center rowspan='3'></td>";		
	
	echo"<td>&nbsp;</td>";	
	
	echo "</tr>";
	echo "<tr>";
	
	if($printlogo==1)
	echo "	<td align='left' colspan='2' style='font-size:11px;'>".nl2br($Company_Capital)."</td>";
	else echo"<td>&nbsp;</td>";	
	
	echo "</tr>";

	echo "<tr><td align='left' colspan='2' style='height:20px;'>&nbsp;</td></tr>";
	
	
		echo "<tr>";
		//echo "	<td align='left' colspan='2' style='font-size:14px; font-weight:bold; border-bottom:3px solid black;'>&nbsp;</td>";
		echo "	<td align='center' colspan='4' style='font-size:32px; font-weight:Bold;'>CASH INVOICE</td>";
		echo "</tr>";
	echo "<tr height='20'><td></td></tr>";
	
	echo "</table></td></tr>";
	
	
		echo "<tr><td align='left'>";
		echo "<table border='0' width='750' cellspacing=0 cellpadding=0 class='$MasterClass' style='table-layout:fixed;' >
			  <col width=350><col width=400>";
		
		echo "<tr>";
		echo "	<td><div style='position:relative; float:left; border:1px solid darkgray; width:350px; margin-top:5px; height:130;padding:5px 5px;'>";
		echo "			<table border='0' width='340px' cellspacing=0 cellpadding=0 style='table-layout:fixed;' >
						<col width=90>
						<col width=250>";
		echo "			<tr><td colspan='1' align='left' style='font-size:13px; font-weight:bold;'>Customer : </td>
							<td colspan='0' valign='top' > $ClientName</td>
						</tr>";	
		/* echo "			<tr><td colspan=1  style='font-size:13px; font-weight:bold;' align=left>Code : </td>
							<td colspan='0' valign='top' > $ClientCode</td>
						</tr>"; */
		echo "			<tr><td colspan='1' align='left' style='font-size:12px; font-weight:bold;'>Address :</td>
							<td colspan='0' valign='top' > $ClientAddress</td>
						</tr>";	
		echo "			<tr height='20'><td colspan=1  style='font-size:13px; font-weight:bold;' align=left></td>
							<td colspan='0' valign='top' ></td>
						</tr>";				
		/* echo "			<tr><td colspan='1' align='left' style='font-size:12px; font-weight:bold;'>LPO No : </td>
							<td colspan='0' valign='top' > </td>
						</tr>";	 */
		echo "			<tr><td colspan='1' align='left' style='font-size:13px; font-weight:bold;'>Telephone :</td>
							<td colspan='0' valign='top' >$Client_Telephone1</td>
						</tr>";	
		echo "			<tr><td colspan='1' align='left' style='font-size:13px; font-weight:bold;'>Vehicle No :</td>
							<td colspan='0' valign='top' > </td>
						</tr>";	
		echo "			</table>";
		echo "		</div></td>";
		
		$border_bottom="border-bottom:darkgray solid 1px;";
		$border_top="border-top: darkgray solid 1px;";
		$border_right="border-right: darkgray solid 1px;";
		$border_left="border-left: darkgray solid 1px;";
		
		echo "<td>		<div style='position:relative; margin: 0 15px;float:left; width:400px; margin-top:20px;'>";
		echo "			<table border='0' width='400px' cellspacing=0 cellpadding=0 style='table-layout:fixed;' >
						<col width=90>
						<col width=130>
						<col width=80>
						<col width=100>";
		echo "			<tr><td align='center' colspan='2' style='font-size:13px; font-weight:bold;'>
								VAT Registration No:</td>";
		echo "				<td align='left' colspan='2' style='font-size:13px; font-weight:bold; padding-left:3px;'>
							LKV2620s2sa05s6a</td>
						</tr>";
		echo "			<tr height='20'><td align='left' style='font-size:13px; font-weight:bold;$border_top;$border_left;'>Number:</td>";
		echo "				<td align='left' colspan='1' style='$border_top;font-size:13px; font-weight:bold; padding-left:3px;'>
							$Delivery_Reference</td>
							<td align='left' style='font-size:13px; font-weight:bold; $border_top;'>Date:</td>";
		echo "				<td align='left' colspan='1' style='$border_top;$border_right;font-size:13px; font-weight:bold; padding-left:3px;'>
							$Delivery_Date</td>
						</tr>";
		echo "			<tr height='20'><td align='left' style='$border_top;$border_left;font-size:13px; font-weight:bold;'>Location:</td>";
		echo "				<td align='left' colspan='1' style='$border_bottom; $border_top;font-size:13px; font-weight:bold; padding-left:3px;'>
							OREGUN ,msmn</td>
							<td align='left' style='font-size:13px; font-weight:bold; $border_bottom; $border_top; '>Point:</td>";
		echo "				<td align='left' colspan='1' style='$border_bottom; $border_top;$border_right;font-size:13px; font-weight:bold; padding-left:3px;'>
							ACC15</td>
						</tr>";
		echo "			<tr height='20'><td align='left' style=' $border_bottom; $border_top;$border_left;font-size:13px; font-weight:bold;'>
								Detail:</td>";
		echo "				<td align='left' colspan='3' style='$border_right;$border_bottom;font-size:13px; font-weight:bold; padding-left:3px;'>
							LKV2620s2sa05s6a</td>
						</tr>";				
		echo "			</table>";
		echo "			</div>";
		echo "	</td>";
		echo "</tr>";
		
		echo "</table></td></tr>";
	 

	
	$querydt = " Select DeliveryDt.DeliveryDt_id, DeliveryDt.Item_CodePacked, DeliveryDt.Item_code as ItemCode, Item_Description As ItemDescription, ifNull(Items.Item_Unit,1) as ItemUnit, ".
			   " Item_DescriptionF1 As ItemDescriptionF1, Item_DescriptionF2 As ItemDescriptionF2, if(ifNull(DeliveryDt.Item_Vat,0) > 0, '*', ' ') as ItemIsVat, ".
			   " DeliveryDt_Quantity as DeliveryDtQuantity,Round(DeliveryDt_Price, ".$DecimalBase.") As DeliveryDtPrice, Round(DeliveryDt_Quantity*DeliveryDt_Price) As DeliveryDttotalPrice, ".
			   " Round(DeliveryDt_DiscountPer, ".$DecimalBase.") As DeliveryDtDiscountPer,  ".
			   " Round(((DeliveryDt_Quantity*DeliveryDt_Price)-((DeliveryDt_Quantity*DeliveryDt_Price*ifNull(deliverydt_itemdiscount,0))/100)), ".$DecimalBase.") As DeliveryDtTotal, ".
			   " Round(DeliveryDt_PriceTTC, ".$DecimalBase.") As DeliveryDtPriceTTC, ".
			   " Round(((DeliveryDt_Quantity*DeliveryDt_PriceTTC)-((DeliveryDt_Quantity*DeliveryDt_PriceTTC*ifNull(deliverydt_itemdiscount,0))/100)), ".$DecimalBase.") As DeliveryDtTotalTTC, ".
			   " (DeliveryDt_Quantity*ifNull(Items.Item_Weight,0)) As GrossWeight,ifNull(deliverydt_clientdiscount,0) deliverydt_clientdiscount,ifNull(deliverydt_itemdiscount,0) deliverydt_itemdiscount ".
				
			   " From DeliveryDt Left Join Items on DeliveryDt.Item_code = Items.Item_code ".
			   " Where DeliveryDt.Delivery_ID = ".$_GET['SalesId'].
			   " Order By DeliveryDt.DeliveryDt_id Asc  ";
		//  echo $querydt;
	
		echo "<tr><td align='left'>";
		echo "<table border='0' width='760' cellspacing='0' cellpadding='2' style='table-layout:fixed; margin-top:5px;' >
			  <col width=80>
			  <col width=130>
			  <col width=250>
			  <col width=100>
			  <col width=100>
			  <col width=100>";
		echo "<tr height='25'>";
		echo "   <th align='center' style='$border_top;$border_right;$border_left;$border_bottom;font-size:14px;'>Quantity</th>";
		echo "   <th align='left' style='$border_top;$border_right;$border_bottom;font-size:14px;'>Item Code</th>";
		echo "	 <th align='left' style='$border_top;$border_right;$border_bottom;font-size:14px;' colspan='2'>Description</th>";
		echo "	 <th align='right' style='$border_top;$border_right;$border_bottom;font-size:14px;'>Rate</th>";
		echo "	 <th align='right' style='$border_top;$border_right;$border_bottom;font-size:14px;'>Amount</th>";
		echo "</tr>";			
	
	$TotalPriceQty=0;
	$total_discountamount=0;
	$total_discountamount_oninvoice=0;
	$resultdt=mysql_query($querydt,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultdt)){
		extract($rows);
		
		
		if ($SalesTTC == 1) 
			$total_discountamount = $total_discountamount + ($DeliveryDtQuantity*$DeliveryDtPriceTTC * $deliverydt_itemdiscount / 100);
		else
			$total_discountamount = $total_discountamount + ($DeliveryDtQuantity*$DeliveryDtPrice * $deliverydt_itemdiscount / 100);
			
			if ($SalesTTC == 1) 
			$total_discountamount_oninvoice = $total_discountamount_oninvoice + ($DeliveryDtQuantity*$DeliveryDtPriceTTC * $deliverydt_clientdiscount / 100);
		else
			$total_discountamount_oninvoice = $total_discountamount_oninvoice + ($DeliveryDtQuantity*$DeliveryDtPrice * $deliverydt_clientdiscount / 100);

		
		$TotalPriceQty = $TotalPriceQty + $DeliveryDttotalPrice;
		
		echo "<tr height=19>";
			echo "  <td align='left' valign='top' style='$border_right;$border_left;font-size:14px;'>$DeliveryDtQuantity</td>";
			echo "  <td align='left' valign='top' style='$border_right;font-size:14px;'>$ItemCode </td>";
			echo "  <td align='left' valign='top' style='$border_right;font-size:14px;'>$ItemDescription </td>";
			echo "	<td align='right' valign='top' style='$border_right;font-size:14px;'></td>";
			echo "	<td align='right' valign='top' style='$border_right;font-size:14px;'></td>";
			echo "	<td align='right' valign='top' style='$border_right;font-size:14px;'>".number_format($DeliveryDttotalPrice, $DecimalBase)."</td>";
			

		echo "</tr>";

	}
	echo "<tr><td style='$border_right;$border_left;$border_bottom;'></td><td style='$border_right;$border_bottom;'></td>
	<td style='$border_right;$border_bottom;'></td><td style='$border_right;$border_bottom;'></td><td style='$border_right;$border_bottom;'></td>
	<td style='$border_right;$border_bottom;'></td></tr>";
	
	
	
	echo "</table></td></tr>";
	
	echo "<tr><td align='left'>";
		echo "<table border='0' width='750' cellspacing=0 cellpadding=2  style='table-layout:fixed; ' >
			  <col width=80>
			  <col width=230>
			  <col width=250>
			  <col width=100>
			  <col width=100>";
			  $total_Amount = $TotalPriceQty + $total_Vatamount - $total_discountamount;
			  
			  $queryalpha  = " SELECT Alpha_Num ('ENGLISH', '".$CurrencyCode."', ".$total_Amount.") As AlphaAmount FROM Dual ";
		$resultalpha = mysql_query($queryalpha,$connection) or die(mysql_error());
		$rowalpha=mysql_fetch_array($resultalpha);
		$TotalAmountLetter = $rowalpha[0];
	
			echo "<tr>";
				echo "<tr>";
					echo "   <th  colspan='2' align='left'>
								
								</th>";
					echo "	 <th align='right' style='$border_right;font-size:14px;'>SUB TOTAL</th>";
					echo "	 <th align='right' colspan='2'  style='$border_right;' >".number_format($TotalPriceQty, $DecimalBase)."</th>";
				echo "</tr>";	
			echo "</tr>";
			echo "<tr>";
				echo "<tr>";
					echo "   <th  colspan='2' align='left' style='border-bottom:0;font-size:14px;'>Amount in words:</th>";
					echo "	 <th align='right' style='$border_right;font-size:14px;'>VAT </th>";
					echo "	 <th align='right'  colspan='2' style='$border_right;' >".number_format($total_Vatamount, $DecimalBase)."</th>";
				echo "</tr>";	
			echo "</tr>";
			echo "<tr>";
				echo "<tr>";
					echo "   <th  colspan='2' align='left' style='font-size:14px;' >$TotalAmountLetter</th>";
					echo "	 <th align='right' style='$border_right;font-size:14px;'>DISCOUNT </th>";
					echo "	 <th align='right' colspan='2' style='$border_right;'>".number_format($total_discountamount, $DecimalBase)."</th>";
				echo "</tr>";
			echo "<tr>";
			
				echo "<tr>";
					echo "   <th  colspan='2' align='left' style='padding-left:3px;'></th>";
					echo "	 <th align='right' style='$border_right;font-size:14px;'>TOTAL </th>";
					echo "	 <th align='center' colspan='2' style='$border_bottom;$border_right;' ><span style='float:left;font-size:13px;'>NAIRA</span> 
					<span style='float:right;'>".number_format($total_Amount, $DecimalBase)."</span></th>";
				echo "</tr>";	
			echo "</tr>	";			
			echo "</tr>
	</table></td></tr>";
	echo "<tr height='40'><td></td></tr>";
	echo "<tr><td align='left'>";
		echo "<table border='0' width='750' cellspacing=0 cellpadding=1  style='table-layout:fixed; ' >
			  <col width=375>
			  <col width=375>";
		
			echo "<tr><td align='center' style='color:darkgray;'>_______________________</td>" ;
			
			echo "<td align='center' style='color:darkgray;'>_______________________</td></tr>";
		echo "<tr><td align='center' align='top'  style='color:darkgray;'>Sales Manager</td>" ;
			
			echo "<td align='center' align='top' style='color:darkgray;'>Customer</td></tr>";
		 
		
	
		echo "</table></td></tr>";
	echo "<table border=0 width='750' cellspacing=0 cellpadding=0  class='totals' style='table-layout:fixed;position:fixed;bottom:0;' >
				  <col width=400>
				  <col width=150>
				  <col width=150>";
				  
			echo "<tr><td style='color: darkgray;font-size: 14px;' colspan='3' align='center'>Conditions: (1) Once sold the money cannot be refunded</td></tr>";
			
			
						
			

			
	echo "</table></td></tr>";
	
	echo "</table>";
 	

}
//echo $copies;
echo "</td>	</tr></table>";
echo "&nbsp;";

echo "</body></html>";


function format_currency($amount, $DecimalBase) { 

    if($amount < 0) 
        return "(".number_format(abs($amount),$DecimalBase).")"; 
 
    else return number_format($amount, $DecimalBase); 
} 

function Number2Text($numberValue){

	$textResult = ''; // so i can use .=
	$numberValue = "$numberValue";
	
	if($numberValue[0] == '-'){
		$textResult .= 'ليرة ';
		$numberValue = substr($numberValue,1);
	}
	
	$numberValue = (int) $numberValue;	
	
	$def = array(	"0" => 'صفر',
					"1" => 'واحد',
					"2" => 'اثنان',
					"3" => 'ثلاث',
					"4" => 'اربع',
					"5" => 'خمس',
					"6" => 'ست',
					"7" => 'سبع',
					"8" => 'ثمان',
					"9" => 'تسع',
					"10" => 'عشر',
					"11" => 'أحد عشر',
					"12" => 'اثنا عشر',
					"100" => 'مائة',
					"200" => 'مئتان',
					"1000" => 'ألف',
					"2000" => 'ألفين',
					"1000000" => 'مليون',
					"2000000" => 'مليونان',
					"1000000000" => 'مليار',
					"2000000000" => 'ملياران'
					);

	// check for defind values	
	if(isset($def[$numberValue])) { 
		// checking for numbers from 2 to 10 :reson = 2 to 10 uses 'ة' at the end
		if($numberValue < 11 && $numberValue > 2){
			$textResult .= $def[$numberValue].'ة';
		}
		else{
			// the rest of the defined numbers
			$textResult .= $def[$numberValue];
			//echo " ".$numberValue." "; 
		}
	}
	else{ 
		$tensCheck = $numberValue%10;
		$numberValue = "$numberValue";
		
		for($x = strlen($numberValue); $x > 0; $x--){
			$places[$x] = $numberValue[strlen($numberValue)-$x];
		}

		switch(count($places)){
			case 2: // 2 numbers
			case 1: // or 1 number
			{
				$textResult .= ($places[1] != 0) ? $def[$places[1]].(($places[1] > 2 || $places[2] == 1) ? 'ة' : '').(($places[2] != 1) ? ' و' : ' ') : '';
				$textResult .= (($places[2] > 2) ? $def[$places[2]].'ون' : $def[10].(($places[2] != 2) ? '' : 'ون'));				
			}
			break;
			case 3: // 3 numbers
			{
				$lastTwo = (int) $places[2].$places[1];
				$textResult .= ($places[3] > 2) ? $def[$places[3]].' '.$def[100] : $def[(int) $places[3]."00"];
				if($lastTwo != 0){
					$textResult .= ' و'.Number2Text($lastTwo);
				}
			}
			break; 
			case 4: // 4 numbrs
			{
				$lastThree = (int) $places[3].$places[2].$places[1];
				$textResult .= ($places[4] > 2) ? $def[$places[4]].'ة الاف' : $def[(int) $places[4]."000"];
				if($lastThree != 0){
					$textResult .= ' و'.Number2Text($lastThree);
				}
			}
			break;
			case 5: // 5 numbers
			{	
				$lastThree = (int) $places[3].$places[2].$places[1];
				$textResult .= Number2Text((int) $places[5].$places[4]).((((int) $places[5].$places[4]) != 10) ? ' الفاً' : ' الاف');
				if($lastThree != 0){
					$textResult .= ' و'.Number2Text($lastThree);
				}
			}
			break;
			case 6: // 6 numbers
			{	
				$lastThree = (int) $places[3].$places[2].$places[1];
				$textResult .= Number2Text((int) $places[6].$places[5].$places[4]).((((int) $places[5].$places[4]) != 10) ? ' الفاً' : ' الاف');
				if($lastThree != 0){
					$textResult .= ' و'.Number2Text($lastThree);
				}
			}
			break;
			case 7: // 7 numbers 1 mill
			{	
				$textResult .= ($places[7] > 2) ? $def[$places[7]].' ملايين' : $def[(int) $places[7]."000000"];
				$textResult .= ' و';
				$textResult .= Number2Text((int) $places[6].$places[5].$places[4].$places[3].$places[2].$places[1]);
			}
			break;
			case 8: // 8 numbers 10 mill
			case 9: // 9 numbers 100 mill
			{	
				$places[9] = (isset($places[9])) ? $places[9] : '';
				$firstThree = (int) $places[9].$places[8].$places[7];
				$textResult .= 	Number2Text($firstThree);
				$textResult .=	($firstThree < 11) ? ' ملايين ' : ' مليون ';
				if(((int) $places[6].$places[5].$places[4].$places[3].$places[2].$places[1]) != 0){
					$textResult .= ' و';
					$textResult .=	Number2Text((int) $places[6].$places[5].$places[4].$places[3].$places[2].$places[1]);
				}
			}
			
			break;
			case 10: // 10 numbers 1 billion
			{	
				$textResult .= ($places[10] > 2) ? $def[$places[10]].' مليارات' : $def[(int) $places[10]."000000000"];
				$textResult .= ' و';
				$textResult .= Number2Text((int) $places[9].$places[8].$places[7].$places[6].$places[5].$places[4].$places[3].$places[2].$places[1]);
			}
			break;
			case 11: // 11 numbers 10 bill
			case 12: // 12 numbers 100 bill
			{	
				$places[12] = (isset($places[12])) ? $places[12] : '';
				$firstThree = (int) $places[12].$places[11].$places[10];
				$textResult .= 	Number2Text($firstThree);
				$textResult .=	($firstThree < 11) ? ' مليارات ' : ' مليار ';
				if(((int) $places[9].$places[8].$places[7].$places[6].$places[5].$places[4].$places[3].$places[2].$places[1]) != 0){
					$textResult .= ' و';
					$textResult .=	Number2Text((int) $places[9].$places[8].$places[7].$places[6].$places[5].$places[4].$places[3].$places[2].$places[1]);
				}
			}
			
			break;
			default:
			{
				$textResult = 'هذا رقم كبير .. ';
			}
		}

	}
	return $textResult;
}

print <<< HERE

<script language=javascript>
 
if (typeof(window.print) != 'undefined')
{
    window.print();
} 
</script>
</BODY>
</HTML>
HERE;
?>

