<?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");
}

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


$copies=1;



while ($copies<4){




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;


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");
//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=75>";
	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>";		
	$name='عون فود كومباني ش.م. م';
	if($printlogo==1)
		echo"<td  align='right' colspan='2' style='font-size:15px;'>".nl2br($name)."</td>";	
	else 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>";
	
	if($printlogo==1)
	echo "	<td align='left' colspan='2' style='font-size:11px;'>".nl2br($Company_Memo)."</td>";
	else echo"<td>&nbsp;</td>";	
	
	if($printlogo==1)
	echo "	<td align='right' colspan='2' style='font-size:11px;'><font size=3>
	      		مزرعة يشوع - لبنان
</font><br>

 04-926381<font size=3>تلفون</font>
<br>
<font size=3>س.ت. </font> 67548  <font size=3>بعبدا</font>
<br>
<font size=3>رقم ضريبة القيمة المضافة</font> 601/218432
</td>";
else echo"<td align='right' colspan='2' style='font-size:11px;'>&nbsp;</td>";	

	echo "</tr>";
	echo "<tr><td align='left' colspan='2' style='height:20px;'>&nbsp;</td></tr>";
	*/
	if($_GET['PLang'] != 'AR') {
		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' style='font-size:14px; font-style:italic;'>Delivery Order</td>";
		echo "</tr>";
	} else {
		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' style='font-size:26px;'>أذن تسليم</td>";
		echo "</tr>";
	}
	
	echo "</table></td></tr>";
	
	if($_GET['PLang'] != 'AR') {
		echo "<tr><td align='left'>";
		echo "<table border='0' width='750' cellspacing=0 cellpadding=0 class='$MasterClass' style='table-layout:fixed;' >
			  <col width=750>";
		
		echo "<tr>";
		echo "	<td><div style='position:relative; float:left; border:1px solid black; width:430px; margin-top:5px;'>";
		echo "			<table border='0' width='430px' cellspacing=0 cellpadding=0 style='table-layout:fixed;' >
						<col width=430>";
		echo "			<tr><td align='left' style='font-size:13px; font-weight:bold;'>BILL TO: ".$ClientName."</td></tr>";	
		echo "<tr><td colspan=1  style='font-size:13px; font-weight:bold;' align=left>$Delivery_Remark</td></tr>";
		echo "			<tr><td align='left' style='font-size:12px; font-weight:bold;'>".$ClientAddress."</td></tr>";	
		echo "			<tr><td align='left' style='font-size:12px; font-weight:bold;'>".$Client_Telephone1."-".$Client_Telephone2."</td></tr>";	
		//echo "			<tr><td align='center' style='font-size:13px; font-weight:bold;'>TIN #: ".$Client_VATNo."</td></tr>";	
		echo "			</table>";
		echo "		</div>";
		
		echo "		<div style='position:relative; float:right; border:1px solid black; width:300px; margin-top:5px;'>";
		echo "			<table border='0' width='300px' cellspacing=0 cellpadding=0 style='table-layout:fixed;' >
						<col width=70>
						<col width=120>
						<col width=110>";
		echo "			<tr><td align='right' style='font-size:13px; font-weight:bold;'>Number:</td>";
		echo "				<td align='left' colspan='2' style='font-size:13px; font-weight:bold; padding-left:3px;'>".$Delivery_Reference."</td></tr>";
		echo "			<tr><td align='right' style='font-size:13px; font-weight:bold;'>Date:</td>";	
		echo "				<td align='left' style='font-size:13px; font-weight:bold; padding-left:3px;'>".$Delivery_Date."</td>";	
		echo "				<td align='center' style='font-size:13px; font-weight:bold;'>[".$SalesmanCode."]</td>";	
		echo "			</tr>";
		echo "			<tr><td align='right' style='font-size:13px; font-weight:bold;'>D.O. #:</td>";
		echo "				<td align='left' colspan='2' style='font-size:13px; font-weight:bold; padding-left:3px;'>".$DeliveryManualNo."</td></tr>";
		echo "			<tr><td align='right' style='font-size:13px; font-weight:bold;'>Account #:</td>";	
		echo "				<td align='left' style='font-size:13px; font-weight:bold; padding-left:3px;'>".$ClientCode."</td>";	
		echo "				<td align='center' style='font-size:13px; font-weight:bold;'>Currency: ".$CurrencyCode."</td>";	
		echo "			</tr>";
		echo "			</table>";
		echo "		</div>";
		echo "	</td>";
		echo "</tr>";
		
		echo "</table></td></tr>";
	} else {
		echo "<tr><td align='left'>";
		echo "<table border='0' width='750' cellspacing=0 cellpadding=0 class='$MasterClass' style='table-layout:fixed;' dir='rtl'>
			  <col width=750>";
		
		echo "<tr>";
		echo "	<td><div style='position:relative; float:left; border:1px solid black; width:300px; margin-top:5px;'>";
		echo "			<table border='0' width='300px' cellspacing=0 cellpadding=0 style='table-layout:fixed;' >
						<col width=70>
						<col width=120>
						<col width=110>";
		echo "			<tr><td align='right' style='font-size:15px; font-weight:bold;'>رقم:</td>";
		echo "				<td align='right' colspan='2' style='font-size:15px; font-weight:bold; padding-left:3px;'>".$Delivery_Reference."</td></tr>";
		echo "			<tr><td align='right' style='font-size:15px; font-weight:bold;'>التاريخ:</td>";	
		echo "				<td align='right' style='font-size:15px; font-weight:bold; padding-left:3px;'>".$Delivery_Date."</td>";	
		echo "				<td align='center' style='font-size:15px; font-weight:bold;'>[".$SalesmanCode."]</td>";	
		echo "			</tr>";
		echo "			<tr><td align='right' style='font-size:15px; font-weight:bold;'>عرض رقم:</td>";
		echo "				<td align='right' colspan='2' style='font-size:15px; font-weight:bold; padding-left:3px;'>".$DeliveryManualNo."</td></tr>";
		echo "			<tr><td align='right' style='font-size:15px; font-weight:bold;'>حساب رقم:</td>";	
		echo "				<td align='right' style='font-size:15px; font-weight:bold; padding-left:3px;'>".$ClientCode."</td>";	
		echo "				<td align='center' style='font-size:15px; font-weight:bold;'>عملة: ".$CurrencyCode."</td>";	
		echo "			</tr>";
		echo "			</table>";
		echo "		</div>";
		echo "		<div style='position:relative; float:right; border:1px solid black; width:430px; margin-top:5px;'>";
		echo "			<table border='0' width='430px' cellspacing=0 cellpadding=0 style='table-layout:fixed;' >
						<col width=430>";
		echo "			<tr><td align='right' style='font-size:15px; font-weight:bold;'>مطلوب من: ".$ClientNameF."</td></tr>";
echo "<tr><td colspan=1  style='font-size:13px; font-weight:bold;' align=left>$Delivery_Remark</td></tr>";
		
		echo "			<tr><td align='right' style='font-size:15px; font-weight:bold;'>".$client_address3."</td></tr>";	
		echo "			<tr><td align='right' style='font-size:15px; font-weight:bold;'>".$Client_Telephone1."-".$Client_Telephone2."</td></tr>";	
		echo "			<tr><td align='center' style='font-size:15px; font-weight:bold;'>&nbsp;</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_DiscountPer, ".$DecimalBase.") As DeliveryDtDiscountPer,  ".
			   " Round(((DeliveryDt_Quantity*DeliveryDt_Price)-((DeliveryDt_Quantity*DeliveryDt_Price*ifNull(DeliveryDt_DiscountPer,0))/100)), ".$DecimalBase.") As DeliveryDtTotal, ".
			   " Round(DeliveryDt_PriceTTC, ".$DecimalBase.") As DeliveryDtPriceTTC, ".
			   " Round(((DeliveryDt_Quantity*DeliveryDt_PriceTTC)-((DeliveryDt_Quantity*DeliveryDt_PriceTTC*ifNull(DeliveryDt_DiscountPer,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;
	if($_GET['PLang'] != 'AR') {
		echo "<tr><td align='left'>";
		echo "<table border='0' width='750' cellspacing='0' class='$DetailClass' style='table-layout:fixed; margin-top:5px;' >
			  <col width=120>
			  <col width=320>
			  <col width=70>
			  <col width=70>
			  <col width=70>
			  
			  <col width=90>";
		echo "<tr>";
		echo "   <th align='left' style='padding-left:3px;'>ITEM</th>";
		echo "   <th align='left'>DESCRIPTION</th>";
		//echo "	 <th align='center'>BOX</th>";
		echo "	 <th align='center'>QTY</th>";
		echo "	 <th align='center'>PRICE</th>";
		echo "	 <th align='center'>DISC%</th>";
		echo "	 <th align='center' style='border-right:1px solid black;'>TOTAL</th>";
		echo "</tr>";			
	} else {
		echo "<tr><td align='left'>";
		echo "<table border='0' width='750' cellspacing='0' class='$DetailClass' style='table-layout:fixed; margin-top:5px;' dir='rtl'>
			  <col width=90>
			  <col width=340>
			  <col width=70>
			  <col width=70>
			  <col width=70>
			  <col width=90>";
		echo "<tr>";
		echo "   <th align='right' style='border-right:1px solid black; padding-right:3px;'>الصنف</th>";
		echo "   <th align='right'>الشرح</th>";
		//echo "	 <th align='center'>صندوق</th>";
		echo "	 <th align='center'>الكمية</th>";
		echo "	 <th align='center'>السعر</th>";
		echo "	 <th align='center'>حسم%</th>";
		echo "	 <th align='center'>مجموع</th>";
		echo "</tr>";
	}	
	
	$resultdt=mysql_query($querydt,$connection) or die(mysql_error());

		$total_beforediscount=0;
	$total_discountamount=0;
	$total_discountamount_oninvoice=0;
	while($rows=mysql_fetch_array($resultdt)){
		extract($rows);
		if ($SalesTTC == 1) 
			$total_beforediscount = $total_beforediscount + ($DeliveryDtQuantity*$DeliveryDtPriceTTC);
		else
			$total_beforediscount = $total_beforediscount + ($DeliveryDtQuantity*$DeliveryDtPrice);
	
		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);
			
			
		
		
			$TotalQty = $TotalQty + $DeliveryDtQuantity;
		$TotalGrossWeight = $TotalGrossWeight + $GrossWeight;
		
		$ItemBoxes  = floor($DeliveryDtQuantity/$ItemUnit);
		$ItemPieces = ($DeliveryDtQuantity%$ItemUnit);
		$UnitPack = $ItemBoxes . 'B ';
		if ($ItemPieces > 0) $UnitPack .= $ItemPieces . 'P';
		
		if($_GET['PLang'] != 'AR')
			echo "<tr height=19>";
			else
			echo "<tr height=19>";
			
		if($_GET['PLang'] != 'AR') {
			echo "  <td align='left' valign='top'>$ItemCode</td>";
			echo "  <td align='left' valign='top'><table width=100%><tr><td style='border-left:0px'>$ItemDescription</td><td style='border-left:0px'></td></tr></table></td>";
		} else {
			echo "  <td align='right' valign='top' style='border-right:1px solid black;'>$ItemCode</td>";
			echo "  <td align='right' valign='top'><table  cellpadding=0 cellspacing=0 width=100%><tr><td style='border-left:0px'>$ItemDescriptionF1</td><td style='border-left:0px'></td></tr></table></td>";
		}
		
		//echo "  <td align='center' valign='top'>".$UnitPack."</td>";
		echo "	<td align='right' valign='top'>".number_format($DeliveryDtQuantity, $DecimalBase)."</td>";
			
		if ($SalesTTC == 1) 
			echo "	<td align='right' valign='top'>".number_format($DeliveryDtPriceTTC, $DecimalBase)."</td>";
		else
			echo "	<td align='right' valign='top'>".number_format($DeliveryDtPrice, $DecimalBase)."</td>";
				
		echo "	<td align='right' valign='top'>".number_format($deliverydt_itemdiscount, $DecimalBase)."</td>";
			
			
		if($_GET['PLang'] != 'AR') {
			if ($SalesTTC == 1) 
				echo "	<td align='right' valign='top' style='border-right:1px solid black;'>".number_format($DeliveryDtTotalTTC, $DecimalBase)."</td>";
			else
				echo "	<td align='right' valign='top' style='border-right:1px solid black;'>".number_format($DeliveryDtTotal, $DecimalBase)."</td>";
		} else {
			if ($SalesTTC == 1) 
				echo "	<td align='right' valign='top'>".number_format($DeliveryDtTotalTTC, $DecimalBase)."</td>";
			else
				echo "	<td align='right' valign='top'>".number_format( ($DeliveryDtQuantity*$DeliveryDtPrice)*(1-$deliverydt_itemdiscount/100), $DecimalBase)."</td>";
		}
			
				
		echo "</tr>";

	}
	
	
		 
	if($_GET['PLang'] != 'AR') {
		if (empty($Delivery_NetTotal)) $Delivery_NetTotal = 0;
		$queryalpha  = " SELECT Alpha_Num ('ENGLISH', '".$CurrencyCode."', ".$Delivery_NetTotal.") As AlphaAmount FROM Dual ";
		$resultalpha = mysql_query($queryalpha,$connection) or die(mysql_error());
		$rowalpha=mysql_fetch_array($resultalpha);
		$TotalAmountLetter = $rowalpha[0];
	
	
	/*	echo "<tr>
				<td colspan='5' align='right' style='border-top:1px solid black;'>&nbsp;</td>
				<td align='right' style='border-right:1px solid black; border-top:1px solid black;'>&nbsp;</td>
			 </tr>";*/
		 
echo "<tr>
				<td colspan='2' align='left' style='border-top:1px solid black;font-weight: bold; padding-left:20px; border-right:0px;'>&nbsp;</td>
				<td colspan='3' align='right' style='border-top:1px solid black;font-weight: bold; padding-right:10px; border-left:0px;'>TOTAL BEFORE DISCOUNT</td>
				<td align='right' style='border-top:1px solid black;border-right:1px solid black;'>".number_format($total_beforediscount, $DecimalBase)."</td>
			 </tr>";

			echo "<tr>
				<td colspan='2' align='left' style='border-top:1px solid black;font-weight: bold; padding-left:20px; border-right:0px;'>&nbsp;</td>
				<td colspan='3' align='right' style='border-top:1px solid black;font-weight: bold; padding-right:10px; border-left:0px;'>TOTAL DISCOUNT AMOUNT</td>
				<td align='right' style='border-top:1px solid black;border-right:1px solid black;'>".number_format($total_discountamount, $DecimalBase)."</td>
			 </tr>";
			 if($total_discountamount_oninvoice>0) {
			    $queryalpha  = " SELECT AlphaNumber1 (".$client_discountonInvoice.") As AlphaAmount FROM Dual ";
				$resultalpha = mysql_query($queryalpha,$connection) or die(mysql_error());
				$rowalpha=mysql_fetch_array($resultalpha);
				$TotaldiscountonInvoiceLetter = $rowalpha[0];
			 	echo "<tr>
				<td colspan='2' align='left' style='border-top:1px solid black;font-weight: bold; padding-left:20px; border-right:0px;'>$TotaldiscountonInvoiceLetter PERCENT</td>
				<td colspan='3' align='right' style='border-top:1px solid black;font-weight: bold; padding-right:10px; border-left:0px;'>TOTAL DISCOUNT ON INVOICE</td>
				<td align='right' style='border-top:1px solid black;border-right:1px solid black;'>".number_format($total_discountamount_oninvoice, $DecimalBase)."</td>
			 </tr>";
			 }
			 
			 if($total_discountamount=='') $total_discountamount=0;
			 if($total_discountamount_oninvoice=='') $total_discountamount_oninvoice=0;
			 $total_afterdiscount=$total_beforediscount-$total_discountamount-$total_discountamount_oninvoice;
			 	echo "<tr>
				<td colspan='2' align='left' style='border-top:1px solid black;font-weight: bold; padding-left:20px; border-right:0px;'>&nbsp;</td>
				<td colspan='3' align='right' style='border-top:1px solid black;font-weight: bold; padding-right:10px; border-left:0px;'>TOTAL AFTER DISCOUNT</td>
				<td align='right' style='border-top:1px solid black;border-right:1px solid black;'>".number_format($total_afterdiscount, $DecimalBase)."</td>
			 </tr>";
			 
			 
		if ($DeliveryDiscountPer != 0) {
			if ($SalesTTC == 1)	{
				echo "<tr>
						<td colspan='6' align='right' style='font-weight: bold; padding-right:10px;'>Discount ".number_format($DeliveryDiscountPer, $DecimalBase)."%</td>
						<td align='right' style='border-right:1px solid black; border-bottom:1px solid black;'>".number_format($DeliveryDiscountTTC, $DecimalBase)."</td>
					 </tr>";
			} else {
				echo "<tr>
						<td colspan='6' align='right' style='font-weight: bold; padding-right:10px;'>Discount ".number_format($DeliveryDiscountPer, $DecimalBase)."%</td>
						<td align='right' style='border-right:1px solid black; border-bottom:1px solid black;'>".number_format($DeliveryDiscountAmount, $DecimalBase)."</td>
					 </tr>";
			}
		}
	 
		echo "<tr>
				<td colspan='4' align='left' style='font-weight: bold; border-bottom:1px solid black;'>".$TotalAmountLetter."</td>
				<TD  align='right' style='border-bottom:1px solid black; font-size:14px;'><B>TOTAL</TD>
				<td align='right' style='border-right:1px solid black; border-bottom:1px solid black; font-size:14px; font-weight:bold;'>".number_format($Delivery_NetTotal, $DecimalBase)."</td>
			 </tr>";/*
		echo "<tr>
				<td colspan='7' align='left' style='font-weight:bold; border-left:0px;'>VAT is not refundable after 90 days of invoice date.</td>
			 </tr>";*/
	} else {
		if (empty($Delivery_NetTotal)) $Delivery_NetTotal = 0;
		//$queryalpha  = " SELECT Alpha_Num ('ARABIC', '".$CurrencyCode."', ".$Delivery_NetTotal.") As AlphaAmount FROM Dual ";
		//$resultalpha = mysql_query($queryalpha,$connection) or die(mysql_error());
		//$rowalpha=mysql_fetch_array($resultalpha);
		
		$sub_query = "select currency_namearab from currency where currency_code = '".$CurrencyCode."'";
		$sub_result = mysql_query($sub_query,$connection);
		$sub_row = mysql_fetch_array($sub_result);
		$CurrencyName = $sub_row[0];
		
		$TotalAmount = explode('.', round($Delivery_NetTotal,2));
		$TotalAmountLetter = Number2Text($TotalAmount[0]).' '.$CurrencyName.' و '.$TotalAmount[1].'% '.'فقط لا غير';
	
	
  echo "<tr>
				<td colspan='2' align='left' style='border-top:1px solid black;font-weight: bold; padding-left:20px; border-right:0px;'>&nbsp;</td>
				<td colspan='3' align='left' style='border-top:1px solid black;font-weight: bold; padding-left:10px; border-right:0px;'>المجموع قبل الحسم</td>
				<td align='right' style='border-top:1px solid black;border-right:1px solid black;'>".number_format($total_beforediscount, $DecimalBase)."</td>
			 </tr>";
	echo "<tr>
				<td colspan='2' align='left' style='border-top:1px solid black;font-weight: bold; padding-left:20px; border-right:0px;'>&nbsp;</td>
				<td colspan='3' align='left' style='border-top:1px solid black;font-weight: bold; padding-left:10px; border-right:0px;'>مجموع الحسم</td>
				<td align='right' style='border-top:1px solid black;border-right:1px solid black;'>".number_format($total_discountamount, $DecimalBase)."</td>
			 </tr>";		 
			 
	 if($total_discountamount_oninvoice>0) {
				$TotaldiscountonInvoiceLetter  = Number2Text($client_discountonInvoice);
			 	echo "<tr>
				<td colspan='2' align='left' style='border-top:1px solid black;font-weight: bold; padding-left:20px; border-right:0px;'>$TotaldiscountonInvoiceLetter  بالمئة</td>
				<td colspan='3' align='left' style='border-top:1px solid black;font-weight: bold; padding-right:10px; border-left:0px;'>مجموع الحسم على الفاتورة</td>
				<td align='right' style='border-top:1px solid black;border-right:1px solid black;'>".number_format($total_discountamount_oninvoice, $DecimalBase)."</td>
			 </tr>";
			 }
	 if($total_discountamount=='') $total_discountamount=0;
			 if($total_discountamount_oninvoice=='') $total_discountamount_oninvoice=0;
			 $total_afterdiscount=$total_beforediscount-$total_discountamount-$total_discountamount_oninvoice;
			 	echo "<tr>
				<td colspan='2' align='left' style='border-top:1px solid black;font-weight: bold; padding-left:20px; border-right:0px;'>&nbsp;</td>
				<td colspan='3' align='left' style='border-top:1px solid black;font-weight: bold; padding-right:10px; border-left:0px;'>المجموع بعد الحسم</td>
				<td align='right' style='border-top:1px solid black;border-right:1px solid black;'>".number_format($total_afterdiscount, $DecimalBase)."</td>
			 </tr>";
/*
		echo "<tr>
				<td colspan='5' align='right' style='border-right:1px solid black; border-top:1px solid black;'>&nbsp;</td>
				<td align='right' style='border-top:1px solid black;'>&nbsp;</td>
			 </tr>";
 */

		if ($DeliveryDiscountPer != 0) {	 
			if ($SalesTTC == 1)	{
				echo "<tr>
					<td colspan='5' align='left' style='border-right:1px solid black; font-weight: bold; padding-left:10px;'>حسم ".number_format($DeliveryDiscountPer, $DecimalBase)."%</td>
					<td align='right' style='border-bottom:1px solid black;'>".number_format($DeliveryDiscountTTC, $DecimalBase)."</td>
				 </tr>";
			} else {
				echo "<tr>
					<td colspan='5' align='left' style='border-right:1px solid black; font-weight: bold; padding-left:10px;'>حسم ".number_format($DeliveryDiscountPer, $DecimalBase)."%</td>
					<td align='right' style='border-bottom:1px solid black;'>".number_format($DeliveryDiscountAmount, $DecimalBase)."</td>
				 </tr>";
			}
		}
		/*echo "<tr>
				<td colspan='5' align='left' style='border-right:1px solid black; font-weight: bold; padding-left:10px;'>VAT @ $Setup_VatPercentage%&nbsp;&nbsp;&nbsp;$base1&nbsp;&nbsp;".number_format($Delivery_VATBase1, $DecimalBase)."</td>
				<td align='right' style='border-bottom:1px solid black;'>".number_format($Delivery_VAT, $DecimalBase)."</td>
			 </tr>";*/
		echo "<tr>
				<td colspan='3' align='right' style='font-weight: bold; font-size:15px; border-right:1px solid black; border-bottom:1px solid black;'>".$TotalAmountLetter."</td>
				<td colspan='2' align='left' style='font-weight: bold; padding-left:10px; border-bottom:1px solid black;'>المجموع</td>
				<td align='right' style='border-bottom:1px solid black; font-size:14px; font-weight:bold;'>".number_format($Delivery_NetTotal, $DecimalBase)."</td>
			 </tr>";/*
		echo "<tr>
				<td colspan='7' align='right' style='font-weight:bold; border-left:0px;'>VAT is not refundable after 90 days of invoice date.</td>
			 </tr>";*/
	}
	
	if($_GET['PLang'] != 'AR'){
	echo"<tr><td  style='border-left:0px;border-right:0px' colspan=2 align=left>Total Weight(KG): ".number_format($TotalGrossWeight, $DecimalBase)."</td>
	<td RWOSPAN=2   style='border-left:0px;border-right:0px' colspan=4 align=right><br><img src='test_1D.php?text=$Delivery_Reference' height='50'  width='170' border=0></img></td></tr>";
	
	}
	else{
	echo"<tr><td colspan=2 rowspan=2   style='border-left:0px;border-right:0px'  align=right><br><img src='test_1D.php?text=$Delivery_Reference' height='50'  width='170' border=0></img></td>
	<td   style='border-left:0px;border-right:0px' colspan=4 align=right> مجموع الوزن (كغ): ".number_format($TotalGrossWeight, $DecimalBase)."</td></tr>";
	
	}
		
	
	echo "</table></td></tr>";
	
	

	
	

	if($_GET['PLang'] != 'AR') { 
		echo "<tr><td align='left'>";
		echo "<table border='0' width='750' cellspacing=0 cellpadding=1  style='table-layout:fixed; margin-top:20px;' >
			  <col width=100>
			  <col width=130>
			  <col width=310>
			  <col width=200>";
		if ($DeliveryCashInBase1 != 0 || $DeliveryCashInBase2 != 0) {
		echo "<tr>";
	/*	echo "	 <td align='right' style='font-size:14px; font-weight:bold;'>Old Balance:</td>";
		echo "	 <td align='right' style='font-size:14px; padding-right:5px;'>".format_currency($ClientPrevBalance, $DecimalBase). " ".$CurrencyCode."</td>";
		*/
		echo "	 <td align='right' style='font-size:14px; font-weight:bold;'></td>";
		echo "	 <td align='right' style='font-size:14px; padding-right:5px;'></td>";
		
		if ($DeliveryCashInBase1 != 0 || $DeliveryCashInBase2 != 0) {
			echo "<td align='center' style='font-size:15px; font-weight:bold;'>Paid Cash</td>";
			echo "<td align='left'>&nbsp;</td>";
		} else {
			echo "<td align='left' colspan='2'>&nbsp;</td>";
		}	
		echo "</tr>";
		}
		
		
		if($UserAccount!=''){
		echo "<tr style='height:10px;'>";
		echo "	 <td align='center' style='font-size:12px;'>".$UserAccount."</td>";
		echo "	 <td align='left' colspan='3'>&nbsp;</td>";
		echo "</tr>";
		}
		
	 
		
		
		if ($Client_Remark != "") {
			echo "<tr style='height:30px;'>";
			echo "	 <td align='left' style='font-size:12px;' colspan='4'>".$Client_Remark."</td>";
			echo "</tr>";
		}
		
		echo "</table></td></tr>";
	} else {
		echo "<tr><td align='left'>";
		echo "<table border='0' width='750' cellspacing=0 cellpadding=1  style='table-layout:fixed; margin-top:20px;' dir='rtl'>
			  <col width=100>
			  <col width=130>
			  <col width=310>
			  <col width=200>";
		if ($DeliveryCashInBase1 != 0 || $DeliveryCashInBase2 != 0) {
		echo "<tr>";
		echo "	 <td align='right' style='font-size:14px; font-weight:bold;'></td>";
		echo "	 <td align='right' style='font-size:14px; padding-right:5px;'></td>";
				
		if ($DeliveryCashInBase1 != 0 || $DeliveryCashInBase2 != 0) {
			echo "<td align='center' style='font-size:18px; font-weight:bold;'>سددت نقدا</td>";
			echo "<td align='left'>&nbsp;</td>";
		} else {
			echo "<td align='left' colspan='2'>&nbsp;</td>";
		}
		echo "</tr>";
		}	
 
		if($UserAccount!=''){
		echo "<tr style='height:5px;'>";
		echo "	 <td align='center' style='font-size:12px;'>".$UserAccount."</td>";
		echo "	 <td align='left' colspan='3'>&nbsp;</td>";
		echo "</tr>";
		}
		
		if ($Client_Remark != "") {
			echo "<tr style='height:10px;'>";
			echo "	 <td align='right' style='font-size:12px;' colspan='4'>".$Client_Remark."</td>";
			echo "</tr>";
		}
		 
		
		/*echo"<tr height=140><td colspan=10>يقتضي دفع المبلغ اعلاه خلال مهلة اقصاها ";  
		echo $Client_PaymentTerms." ";
		echo"يوم من ";
echo"تاريخ توقيع المستلم على الفاتورة وفي حال عدم تسديد المبلغ فان الزبون يعتبر اساء للامانة سنداً لاحكام المادة 670 وما يليها من قانون العقوبات اللبناني
<br>
		لا تعاد الضريبة على القيمة المضافة على السلع المرتجعة بعد ثلاثة اشهر من تاريخ هذه الفاتورة
</td></tr>";
*/

	 
		
	}
	
	echo "</table>";
 	

}
echo "</td>	</tr></table>";
echo "&nbsp;";
if  ($copies<3) 
		echo "<div style='page-break-before:always;'></div>";
$copies+=1;
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;
?>

