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

$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;



//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, totalWeight ".
						 " 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'];
						// echo$query;

				$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') {
										
									//START INVOICE

echo "							<tr>";
echo "								<td align='left' colspan='1' style='font-size:18px; font-weight:bold'>V A T # $Client_VATNo</td>";
echo "								<td align='right' style='font-size:24px; font-weight:Bold;'>INVOICE</td>";
echo "							</tr>";
									} 
									else {
echo "							<tr>";
echo "								<td align='left' colspan='1' style='font-size:14px;'>V A T # $Client_VATNo</td>";
echo "								<td align='right' style='font-size:20px; font-style:Bold;'>فاتورة</td>";
echo "							</tr>";
									}
									
								//END MY CODE
								
/* 								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;'>INVOICE</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>";
							//Mycode 
							if($_GET['PLang'] != 'AR') {
echo "				<tr>
						<td align='left'>";
echo "						<table border='0' width='1100px' cellspacing=0 cellpadding=0 class='$MasterClass' style='table-layout:fixed;' >
								<col width=750>";
echo "							<tr>";
echo "								<td>";
									//	<div style='position:relative; float:left; border-radius: 12px; border:1px solid black; width:678px; margin-top:5px; box-shadow:3px 2px 0px 0px;'>"; margin-top:5px; box-shadow:3px 2px 0px 0px;  -webkit-print-color-adjust:exact;-webkit-filter:opacity(1);
echo"										<div style='position:relative; float:left; border-radius: 12px; border:1px solid black; width:678px; height:114px; '>";
echo "										<table border='0' width='100px' cellspacing=0 cellpadding=0 style='table-layout:fixed;' >
												<col width=100>
												<col width=200>
												<col width=300>";
echo "											<tr height=25px>
													<td align='left' colspan='3' style='font-size:16px; font-weight:bold;'>&nbsp;".$ClientName."</td>
													<td></td>
												</tr>";	
echo "											<tr height=25px>	
													<td colspan='3'  style='font-size:16px; font-weight:bold;' align=left>&nbsp; $ClientAddress</td>
													<td></td><td></td>
												</tr>";
echo "											<tr height=15px>
													<td align='left' style='font-size:13px; font-weight:bold;'>&nbsp;".$Client_Telephone1."-".$Client_Telephone2."</td>
													<td></td>
												</tr>";	
echo "											<tr height=25px>
													<td align='center' style='font-size:16px; font-weight:bold;'>&nbsp;No.:".$Delivery_Reference."</td>
													<td align='center' style='font-size:16px; font-weight:bold;'>&nbsp;Currency: ".$CurrencyCode."</td>
													<td align='center' style='font-size:16px; font-weight:bold;'>&nbsp;R.C:</td>
												</tr>";	
		echo "								</table>";
		echo "							</div>";
		
		echo "							<div style='position:relative; float:left; height:109px;  border-radius: 12px; border:1px solid black;   margin-left:60px; width:194px; margin-top:5px; padding-left:12px; '>";
		echo "								<table border='0' width='300px' cellspacing=0 cellpadding=0 style='table-layout:fixed;' >
												<col width=70>
												<col width=110>";
		echo "									<tr height=25px> 
													<td align='left' style='font-size:16px; font-weight:bold;'>No:</td>";
		echo "										<td align='left' style='font-size:13px; font-weight:bold; padding-left:3px;'>".$Delivery_Reference."</td>
												</tr>";
		echo "									<tr height=25px>
													<td align='left' style='font-size:16px; font-weight:bold;'>Date:</td>";	
echo "												<td align='left' style='font-size:13px; font-weight:bold; padding-left:3px;'>".$Delivery_Date."</td>";
echo "											</tr>";
echo "											<tr height=25px> 
													<td align='left' style='font-size:16px; font-weight:bold;'>Proforma:</td>";
echo "												<td align='left' style='font-size:13px; font-weight:bold; padding-left:3px;'>".$DeliveryManualNo."</td>
												</tr>";
echo "											<tr >
													<td align='left' style='font-size:16px; font-weight:bold;'>Value Date:</td>";	
echo "												<td align='left' style='font-size:13px; font-weight:bold; padding-left:3px;'>".$Delivery_Date."</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 height=25px>
													<td align='left' colspan='3' style='font-size:13px; font-weight:bold;'>رقم:</td>
													<td></td>
												</tr>";	
echo "											<tr height=25px>	
													<td colspan='3'  style='font-size:13px; font-weight:bold;' align=left>&nbsp;EAS: BLAT JBEIL $ClientAddress</td>
													<td></td><td></td>
												</tr>";
echo "											<tr height=15px>
													<td align='left' style='font-size:12px; font-weight:bold;'>&nbsp;رقم: (09)540016".$Client_Telephone1."-".$Client_Telephone2."</td>
													<td></td>
												</tr>";	
echo "											<tr height=25px>
													<td align='center' style='font-size:13px; font-weight:bold;'>&nbsp رقم:".$Client_VATNo."</td>
													<td align='center' style='font-size:13px; font-weight:bold;'>&nbsp;عملة: ".$CurrencyCode."</td>
													<td align='center' style='font-size:13px; font-weight:bold;'>&nbsp;حساب رقم: ".$Client_VATNo."</td>
												</tr>";	
echo "									</table>";
echo "								</div>";

echo "								<div style='position:relative; float:left;  border-radius: 12px; border:1px solid black;   margin-left:60px; width:194px; margin-top:5px; padding-left:12px; box-shadow:3px 2px 0px 0px;'>";
echo "									<table border='0' width='300px' cellspacing=0 cellpadding=0 style='table-layout:fixed;' >
											<col width=70>
											<col width=110>";
echo "										<tr height=25px> 
												<td align='right' style='font-size:15px; font-weight:bold;'>مطلوب من: ".$ClientNameF."</td>
											</tr>";
echo "										<tr height=25px>
												<td colspan=1  style='font-size:13px; font-weight:bold;' align=left>$Delivery_Remark</td>
											</tr>";
echo "										<tr height=25px>
												<td align='right' style='font-size:15px; font-weight:bold;'>".$client_address3."</td>
											</tr>";	
echo "										<tr height=25px>
												<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;'>TIN #: ".$Client_VATNo."</td>
											</tr>";	
echo "									</table>";
echo "								</div>";
echo "							</td>";
echo "						</tr>";		
echo "					</table>
					</td>
				</tr>";
							}
							//End Mycode
						/* 	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;'>TIN #: ".$Client_VATNo."</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_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 ".
						   " , ifnull(Item_NetWeight,0)*ifnull(SaleDt_Price,0) as totnetweight, ifnull(Item_Volume,0)*DeliveryDt_Quantity as itemvolume, ".
						   " round(ifnull(Item_Unit2Qty,1)/ifNull(Items.Item_Unit,1)) as itemwhole,ifnull(Item_Unit2Qty,1)%ifNull(Items.Item_Unit,1) as itempiece ".
						   " 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;
							
				$totalnetweight = 0;	
				$totalvolume = 0;			
							//My code
							if($_GET['PLang'] != 'AR') {
								$stylefont="style='font-size:16px; font-weight:bold;'";
echo "					<tr>
							<td align='left'>";
echo "							<table border='1' width='500' 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=70>
									<col width=70>
									<col width=70>
									<col width=90>";
echo "								<tr height='50px'>
										<th align='center' $stylefont>Reference</th>";
		echo "   						<th align='center' $stylefont>Description</th>";
		echo "	 						<th align='center' $stylefont>Unit</th>";
		echo "							<th align='center' $stylefont>Whole</th>";
		echo "	 						<th align='center' $stylefont>Pcs.</th>";
		echo "	 						<th align='center' $stylefont>Unit Price</th>";
		echo "	 						<th align='center' $stylefont>Vat</th>";
		echo "	 						<th align='center' $stylefont>Disc%</th>";
echo "	 								<th align='center' $stylefont>TOTAL</th>
									</tr>";
									
									//fetch code
									$total_beforediscount=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_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';
										$totalnetweight = $totalnetweight + $totnetweight;
										$totalvolume = $totalvolume + $itemvolume;	
											if($_GET['PLang'] != 'AR'){
												
echo "								<tr height='50px'>
										<td >$ItemCode</td>
										<td >$ItemDescription</td>
										<td align='right'>".number_format($ItemUnit, $DecimalBase)."</td>
										<td align='right'>$itemwhole</td>";


											if($_GET['PLang'] != 'AR') {
echo"									<td align='right'>$itempiece</td>";
											if ($SalesTTC == 1) 
echo "									<td align='right' >".number_format($DeliveryDtPriceTTC, $DecimalBase)."</td>";
												else
echo "									<td align='right'>".number_format($DeliveryDtPrice, $DecimalBase)."</td>";
echo"									<td></td>
										<td>".number_format($deliverydt_itemdiscount, $DecimalBase)."</td>";
											if ($SalesTTC == 1) 
echo "									<td align='right'  style='border-right:1px solid black;'>".number_format($DeliveryDtTotalTTC, $DecimalBase)."</td>";
												else
echo "									<td align='right'  style='border-right:1px solid black;'>".number_format($DeliveryDtTotal, $DecimalBase)."</td>";

											} 
												else {
													if ($SalesTTC == 1) 
echo "									<td align='right'>".number_format($DeliveryDtTotalTTC, $DecimalBase)."</td>";
														else
echo "									<td align='right'>".number_format($DeliveryDtTotal, $DecimalBase)."</td>";
													}
												
echo"								</tr>";
													}
											}
										}
echo"							</table>
							</td>
						</tr>";
								//End my code
							
echo "<tr height='15px'></tr>";
								//start my code
							
							if($_GET['PLang'] != 'AR') {
echo "					<tr>
							<td align='left'>";
echo "							<table border='0' cellspacing='0' width='800px' class='$DetailClass' style='table-layout:fixed;'>
									<col width=650>
									<col width=28>
									<col width=300>";
									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];
									}
									 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];
									 }
									 $styletd="font-size:17px; border-left:1px solid black; border-right:1px solid black; border-top:1px solid black; border-bottom: 1px solid black; height:45px; border-radius:7px;";
									 $stylebottom=" style='border-bottom:1px solid black; font-size:16px;'";
echo "								<tr height='55px'>
										<td style='font-size:17px; border-left:1px solid black; border-right:1px solid black; border-top:1px solid black; border-bottom: 1px solid black; height:45px; border-radius:7px;'>
										".$TotalAmountLetter."</td>
										<td></td>
										<td>
											<table width='278px' style=' height:195px; border:1px solid black; border-radius:7px; position:absolute; margin-top:-21px' cellspacing='0'>
												<tr style='border-bottom:1px solid black;'>
													<td $stylebottom ><b>GROSS TOTAL</b></td>
													<td align='right' $stylebottom>".number_format($GrossTotal, $DecimalBase)."</td>
												</tr>
												<tr>
													<td $stylebottom><b>DISC.</b></td>
													<td align='right' $stylebottom>".number_format($total_discountamount, $DecimalBase)."</td>
												</tr>
												<tr>
													<td $stylebottom><b> Net BEF.VAT</b></td>
													<td align='right'$stylebottom></td>
												</tr>
												<tr>
													<td $stylebottom><b>VAT.$Setup_VatPercentage %</b></td>
													<td align='right'$stylebottom> ".number_format($Delivery_VAT, $DecimalBase)."</td>
												</tr>
												<tr>
													<td ><b>NET AFT. VAT</b></td>
													<td align='right'>".number_format($Delivery_NetTotal, $DecimalBase)."</td>
												</tr>
											</table>
										</td>
									</tr>
									<tr height='15px' ></tr>
									<tr>
										<table border='0' cellspacing='0' class='$DetailClass' style='table-layout:fixed; width='650px; border:1px solid black; border-radius:7px' >
											<col width=240>
											<col width=150>
											<col width=110>
											<col width=150>
											";
											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];
											}
											 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 height='30px' >
												<td colspan='10' style='font-size:17px; $styletd'>Notes:".$Delivery_Remark."</td>
											</tr>
											<tr height='35px' >
												<td style='font-size:16px;'><b>Gross Weight:</b></td>
												<td style='font-size:14px;' align='left'>$totalWeight Kgs.</td>
												<td style='font-size:16px;'><b>Total Volume:</b></td>
												<td style='font-size:14px;' align='center'>$totalvolume cu. m.</td>
											</tr>
											<tr height='50px' >
												<td style='font-size:16px;'><b>Net Weight:</b></td>
												<td style='font-size:14px;' align='left'>$totalnetweight Kgs.</td>
												<td style='font-size:16px;'><b>Total Cartons:</b></td>
												<td style='font-size:14px;' align='center'></td>
											</tr>
										</table>
									</tr>
								</table>
							</td>
						</tr>";
						
echo "					<tr height='100px'>&nbsp;&nbsp;&nbsp;&nbsp;</tr>

					</td>
				</tr>";
						
								//End my code
					

echo"					</table>
					</td>
				</tr>";
					}
echo "		</table>";
			//}
//echo $copies;
echo "	</td>	
	</tr>
</table>";
echo "&nbsp;";
if  ($copies<3) 
echo "	<div style='page-break-after: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')
{s
    window.print();
} 
</script>
</BODY>
</HTML>
HERE;
?>

