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

//Report Version For Tiba
session_start();
 
/////////////////////////////////////////////////////// 
//BLOCK USED TO REDIRECT TO COMPANY SPECIFIC REPORTS//
/////////////////////////////////////////////////////
 $curfolder = substr(getcwd(), strrpos(getcwd(), '\\') + 1);
 $curfolder = strtolower($curfolder);
 if ($curfolder == "contract")
 {
$pagename = basename($_SERVER['REQUEST_URI']);
$pagenamewithoutparams =  basename($_SERVER['PHP_SELF']);
$CompYear = explode('_',$_SESSION["dbName"]);
$companyname = strtoupper($CompYear[0]);
$clientrelatedpage = getcwd()  . "\\" .$companyname . "\\".$$pagenamewithoutparams  ;

 
if (file_exists($clientrelatedpage.$pagenamewithoutparams )) {
header("location:$companyname/$pagename");
} 

if (!file_exists($clientrelatedpage)) {
    mkdir($clientrelatedpage, 0777, true);
}
	
}
////////////////////////////////////////////////////////////////////////////////////////// 
///adjust include,css, javascript library etc... in cloned report ex ////////////////////
// $success = include "config.inc.php"; if (!$success) {include "../config.inc.php";}///
//js exemplage <script src = '../scriptname.js' >  </script>     //////////////////////
//////////////////////////////////////////////////////////////////////////////////////


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

/*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> Return 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;
*/

print <<< HERE
<HTML>
<HEAD>
<TITLE> Delivery Report </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">

<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: 14px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                }				 
				 
table.detail td { font-size: 12px; 
                  font-family:Times-New-Roman;
                 }				 
				 
table.detail td.sr  { font-size: 10px; 
					  font-style: italic; 
                      font-family:Times-New-Roman;
                    }
				 
table.totals td.labels { font-size: 14px; 
                         font-weight: bold;
                         font-family:Times-New-Roman;
						 border-width: 1px;
						 border-style: none solid solid solid;
  						 border-color: #666666;
                       }					  
table.totals td.amounts { font-size: 14px; 
                          font-family:Times-New-Roman;
						  border-width: 1px;
						  border-style: none none solid none;
  						  border-color: #666666;
				        }
table.totals td.totalborder {
								font-size: 14px; 
								font-family:Times-New-Roman;
								border-width: 1px;
								border-style: none solid solid none;
								border-color: #666666;
							}	
table.totals td.labels1 { font-size: 14px; 
                          font-weight: bold;
                          font-family:Times-New-Roman;
					   	  border-width: 1px;
						  border-style: none none solid solid;
  						  border-color: #666666;
                        }							
				 
h4 { border-top-style: double; 
	 border-top-color: black; 
	 border-top-width: 1px;
   }

h3 { border-top-style: double; 
	 border-top-color: black; 
	 border-top-width: 2px;
	 width: 180px;
   } 
   
   
</style>


</HEAD>

<BODY>
HERE;

 
$success = include "config.inc.php"; if (!$success) {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_Delivery,0) As Module_Delivery, ifNull(Module_AdditionalTaxes,0) As Module_AdditionalTaxes, ".
			" ifNull(Module_ShowLayoutProj,0) As Module_ShowLayoutProj, ifNull(Module_ShowLayoutCost,0) As Module_ShowLayoutCost, ".
			" ifNull(Module_ShowInvoiceAging,0) As Module_ShowInvoiceAging ".
			" From Module";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

//Read From Login Table
$querylog=" select ifNull(FI_DeliverySalesPrice,0) As FI_DeliverySalesPrice ".
          " from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}	

$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['ReturnSalesId'] != '')
{
	$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_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,Sorder_Reference ".
			 " 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['ReturnSalesId'];

	$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=100% cellspacing=0 cellpadding=0><tr><td align='left'>";
	echo "<table border='0' width='640' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=140>
		  <col width=300>
		  <col width=200>";
	echo "<tr>";
	echo "	<td align='left' valign='top' rowspan='3' style=' padding-bottom: 7px'><img src='LogoFile.php' width='150px' height='130px' /></td>";
	echo "	<td align='left' colspan='2' valign='top' style='font-size:20px; font-weight:bold;'>".nl2br($Company_Name)."</td>";
	echo "</tr>
	
	";
	echo "<tr>";
	echo "	<td align='left' colspan='2' style='font-size:13px;'>"
	.nl2br($Company_Memo). "<hr style=\"visibility:hidden;\"><span style=\"font-size:14px;font-weight: bold;\">Tax Registration: " .$Setup_vatno . "</span></td>";
	echo "</tr>";
	echo "<tr>
			<td align='center' style='font-size:20px; font-weight:bold; height:50px;  vertical-align: middle; padding-bottom: 7px' colspan=2>"."TAX CREDIT NOTE"."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
			<td align='center' style='height:50px; '></td>
		  </tr>";
	echo "<tr>
		  <td> &nbsp;</td> <td> &nbsp;</td>
		<td colspan=1 style='border-bottom:0px solid black; width: 640px; text-align:right;font-weight: bold; font-size: 16px; padding-bottom: 7px'>" .str_replace("Invoice","Invoice No:",$title). "</td>
		</tr>";
	echo "</table></td></tr>";
	echo "<tr><td align='left'>";
	echo "<table border=0 width='640' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=110> <!-- old 80 -->
		  <col width=320>
		  <col width=140>
		  <col width=100>";
	echo "<tr>";
	echo "  <th align='left' style='padding-top: 10px;'>Client</th>";
	echo "  <td align='left' style='padding-top: 10px;'>$ClientCode&nbsp;-&nbsp;$ClientName</td>";
	echo "  <th align='left' style='padding-top: 10px;'>Date</th>";
	echo "  <td align='left' style='padding-top: 10px;'>$Delivery_Date</td>";
	echo "</tr>";
	
	if(($Client_ShowCreditLimit ==1 or $Client_ShowMaturityDate ==1) and ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1) ){
	
	echo "<tr>";
	
	if($Client_ShowCreditLimit ==1){
		echo "  <th align='left'>Credit limit</th>";
		echo "  <td align='left' >".number_format($Client_CreditLimit, $DecimalBase)."</td>";
	}
	
	if($Client_ShowMaturityDate ==1){
		echo "  <th align='left'>Maturity</th>";
		echo "  <td align='left' >".$MaturityDate."</td>";
	}
	
	echo "</tr>";
	
	}
	
	
	echo "<tr>";
	echo "  <th align='left' valign='top'>Address</th>";
	echo "  <td align='left' valign='top' >$ClientAddress</td>";
	echo "  <th align='left'>Order Ref:</th>";
	echo "  <td align='left' >$Sorder_Reference</td>";
	echo "</tr>";
	
	 

	echo "<tr>";
	echo "  <th align='left' valign='top'>Telephone</th>";
	echo "  <td align='left' valign='top' >$Client_Telephone1 - $Client_Telephone2</td>";
	echo "  <th align='left'>Currency</th>";
	echo "  <td align='left' >$CurrencyCode</td>";
	echo "</tr>";
	
		 
	echo "<tr>";
	echo "<th align='left' valign='top'>Prepared By:</th>";
	echo "  <td align='left' valign='top' >$UserAccount</td>";
	echo "  <th align='left'>Date Due</th>";
	echo "  <td align='left' >$MaturityDate</td>";
	echo "</tr>"; 
	 
	
	
	echo "<tr>";
	
	echo "  <th align='left' valign='top' style='border-bottom:1px solid black; padding-bottom: 7px;'>Salesman</th>";
	echo "  <td align='left' valign='top' style='border-bottom:1px solid black; padding-bottom: 7px;'>$SalesmanCode</td>";
	
	if ($SupplyDate != "")
	{
			echo "  <th align='left' style='border-bottom:1px solid black; padding-bottom: 10px;'>Date of supply</th>";
	echo "  <td align='left' style='border-bottom:1px solid black; padding-bottom: 10px;'>$SupplyDate</td>";
	}
	else
	{
			echo "  <th align='left' style='border-bottom:1px solid black; padding-bottom: 10px;'>&nbsp; </th>";
	echo "  <td align='left' style='border-bottom:1px solid black; padding-bottom: 10px;'>&nbsp;</td>";
	}

	
	echo "</tr>";
		
	/*echo "<tr>";

	if ($Module_ShowLayoutProj	== 1) {
		echo "  <th align='left' bgcolor='#D8D8D8'>Project</th>";
		echo "  <td align='left' >$ProjectCode &nbsp;&nbsp;$Project_Name</td>";
	} else {
		echo "  <td align='left' colspan='2'>&nbsp;</td>";
	}

	echo "  <th align='left' valign='top' bgcolor='#D8D8D8'>Driver's Name</th>";
	echo "  <td align='left' >$Delivery_ShippedVia</td>";
	echo "</tr>";
	
	if ($Module_ShowLayoutCost == 1) {	
		echo "<tr>";
		echo "  <th align='left' bgcolor='#D8D8D8'>Cost Center</th>";
		echo "  <td align='left' colspan='3'>$Costcentcode&nbsp;&nbsp;$Costcent_name</td>";
		echo "</tr>";
	}*/
	
	/*echo "<tr>";
	echo "<td>";
		
	echo "</td>";
	echo "<td></td><td></td>";
	echo "</tr>";*/
	
	echo "</table>";

	echo "<table border='0' width='640' cellspacing=0 cellpadding=0;' class='master' style='table-layout:fixed;'>";
		echo "	<tr>";
		echo "  	<th align='left' valign='top' style='padding-top: 10px; padding-bottom: 7px;'>Ship To:</th>";
		echo "	</tr>";
		echo "	<tr rowspan='3'>";
		echo " 		<td align='left' valign='top' style='padding-bottom: 10px;' colspan='2'>$Delivery_ShippedTo</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 ". 
				" From DeliveryDt Left Join Items on DeliveryDt.Item_code = Items.Item_code ".
				" Where DeliveryDt.Delivery_ID = ".$_GET['ReturnSalesId'].
				" Order By DeliveryDt.DeliveryDt_id Asc  ";
					   
	//echo $querydt;
	echo "<tr><td align='left'>";
	echo "<table border=0 width='640' cellspacing=0 cellpadding=0 class='detail' style='table-layout:fixed;' >
		  <col width=30>
		  <col width=120>
		  <col width=200>
		  <col width=60>
		  <col width=80>
		  <col width=50>
		  <col width=100>";
		  
	echo "<tr bgcolor=#D8D8D8>";
	echo "   <th align='center' style='border-bottom:1px solid black;'>No</th>";
	echo "   <th align='left' style='border-bottom:1px solid black;'>Code</th>";
	
	if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1) {
		echo "   <th align='left' style='border-bottom:1px solid black;'>Description</th>";
		echo "	 <th align='center' style='border-bottom:1px solid black;'>Qty.</th>";
		echo "	 <th align='center' style='border-bottom:1px solid black;'>Unit Price</th>";
		echo "	 <th align='center' style='border-bottom:1px solid black;'>Disc.%</th>";
		echo "	 <th align='center' style='border-bottom:1px solid black;'>Total Price</th>";
	} else {
		echo "   <th align='left' colspan='4' style='border-bottom:1px solid black;'>Description</th>";
		echo "	 <th align='center' style='border-bottom:1px solid black;'>Qty.</th>";
	}
	
	echo "</tr>";			
	
	$i = 1;
	$resultdt=mysql_query($querydt,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultdt)){
		extract($rows);
		
		$TotalQty = $TotalQty + $DeliveryDtQuantity;
		
		echo "<tr>";
		echo "  <td align='center' valign='top'>$i</td>";
		echo "  <td align='left' valign='top'>$ItemCode</td>";
		
		if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1) {
			echo "  <td align='left' valign='top'>$ItemDescription</td>";
			echo "	<td align='right' valign='top'>$DeliveryDtQuantity</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'>$DeliveryDtDiscountPer</td>";
			
			if ($SalesTTC == 1) 
				echo "	<td align='right' valign='top'>".number_format($DeliveryDtTotalTTC, $DecimalBase)."</td>";
			else
				echo "	<td align='right' valign='top'>".number_format($DeliveryDtTotal, $DecimalBase)."</td>";
		} else {
			echo "  <td align='left' valign='top' colspan='4'>$ItemDescription</td>";
			echo "	<td align='right' valign='top'>$DeliveryDtQuantity&nbsp;&nbsp;&nbsp;&nbsp;</td>";
		}	
			
				
		echo "</tr>";
		
		if ($Delivery_Remark != '') {
			echo "<tr>";
			echo "  <td align='left' valign='top'>&nbsp;</td>";
			echo "  <td align='left' valign='top' colspan='6'>$Delivery_Remark</td>";
			echo "</tr>";

		}
		
		$i += 1;
		
	}
	

	echo "<tr height='5'><td colspan=7><h4></td></tr>";
	echo "</table></td></tr>";
	
	if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1) {
		
		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);
		
		echo "<tr height='30'><td colspan=7 valign='top' style='font-size:12px; font-family:Times-New-Roman;'>".$rowalpha[0]."</td></tr>";
		
		echo "<tr><td align='left'>";
		echo "<table border=0 width='635' cellspacing=0 cellpadding=1  class='totals' style='table-layout:fixed;' >
			  <col width=110>
			  <col width=130>
			  <col width=140>
			  <col width=120>
			  <col width=100>
			  <col width=20>";
		echo "<tr>";
		echo "   <td align='left' colspan='2'>&nbsp;</td>";
		echo "   <td align='left' class='labels1'>Total Qty. &nbsp;&nbsp;  ".number_format($TotalQty, $DecimalBase)."</td>";
		echo "	 <td align='left' class='labels'>Gross Total</td>";
		
		if ($SalesTTC == 1) 
			echo "	 <td align='right' class='amounts'>".number_format($GrossTotalTTC, $DecimalBase)."</td>";
		else
			echo "	 <td align='right' class='amounts'>".number_format($GrossTotal, $DecimalBase)."</td>";
			
		echo "	 <td align='center' class='totalborder'>&nbsp;</td>";
		echo "</tr>";	

		
		echo "<tr>";
		echo "   <td align='left' colspan='2'>&nbsp;</td>";
		echo "   <td align='left' class='labels1'>&nbsp;</td>";
		echo "	 <td align='left' class='labels'>Discount</td>";
		
		if ($SalesTTC == 1) 
			echo "	 <td align='right' class='amounts'>".number_format($DeliveryDiscountTTC, $DecimalBase)."</td>";
		else
			echo "	 <td align='right' class='amounts'>".number_format($DeliveryDiscountAmount, $DecimalBase)."</td>";
			
		echo "	 <td align='center' class='totalborder'>&nbsp;</td>";
		echo "</tr>";
		
		if ($ExportTransportToJV == 1) {
			echo "<tr>";
			echo "   <td align='left' colspan='2'>&nbsp;</td>";
			echo "   <td align='left' class='labels1'>&nbsp;</td>";
			echo "	 <td align='left' class='labels'>Transport</td>";
			
			if ($SalesTTC == 1) 
				echo "	 <td align='right' class='amounts'>".number_format($DeliveryTransportAmtTTC, $DecimalBase)."</td>";
			else
				echo "	 <td align='right' class='amounts'>".number_format($DeliveryTransportAmt, $DecimalBase)."</td>";
				
			echo "	 <td align='center' class='totalborder'>&nbsp;</td>";
			echo "</tr>";
		}
		
		if ($ExportBonusToJV == 1) {
			echo "<tr>";
			echo "   <td align='left' colspan='2'>&nbsp;</td>";
			echo "   <td align='left' class='labels1'>&nbsp;</td>";
			echo "	 <td align='left' class='labels'>Bonus</td>";
			
			if ($SalesTTC == 1) 
				echo "	 <td align='right' class='amounts'>".number_format($DeliveryBonusAmtTTC, $DecimalBase)."</td>";
			else
				echo "	 <td align='right' class='amounts'>".number_format($DeliveryBonusAmt, $DecimalBase)."</td>";
				
			echo "	 <td align='center' class='totalborder'>&nbsp;</td>";
			echo "</tr>";
		}
		
		echo "<tr>";
		echo "   <td align='left' colspan='2'>&nbsp;</td>";
		echo "   <td align='left' class='labels1'>&nbsp;</td>";
		echo "	 <td align='left' class='labels'>Tax Invoices $Setup_VatPercentage%</td>";
		echo "	 <td align='right' class='amounts'>".number_format($Delivery_VAT, $DecimalBase)."</td>";
		echo "	 <td align='center' class='totalborder'>&nbsp;</td>";
		echo "</tr>";
		
		if ($Module_AdditionalTaxes == 1) {
			echo "<tr>";
			echo "   <td align='left' colspan='2'>&nbsp;</td>";
			echo "   <td align='left' class='labels1'>&nbsp;</td>";
			echo "	 <td align='left' class='labels'>Taxes</td>";
			echo "	 <td align='right' class='amounts'>".number_format($Delivery_TotalTaxes, $DecimalBase)."</td>";
			echo "	 <td align='center' class='totalborder'>&nbsp;</td>";
			echo "</tr>";
		}
		
		
		
		echo "<tr>";
		echo "   <td align='left' colspan='2'>&nbsp;</td>";
		echo "   <td align='left' class='labels1'>&nbsp;</td>";
		echo "	 <td align='left' class='labels'>Net Total $CurrencyCode</td>";
		
		if ($SalesTTC == 1) 
			echo "	 <td align='right' class='amounts'>".number_format($Delivery_NetTotalTTC, $DecimalBase)."</td>";
		else
			echo "	 <td align='right' class='amounts'>".number_format($Delivery_NetTotal, $DecimalBase)."</td>";
			
		echo "	 <td align='center' class='totalborder'>&nbsp;</td>";
		echo "</tr>";
	
		echo "</table></td></tr>";
	} else {
		echo "<tr><td align='left'>";
		echo "<table border=0 width='635' cellspacing=0 cellpadding=1  class='totals' style='table-layout:fixed;' >
			  <col width=450>
			  <col width=70>
			  <col width=80>
			  <col width=20>";
		echo "<tr>";
		echo "   <td align='left' >&nbsp;</td>";
		echo "   <td align='left' class='labels1'>Total Qty. </td>";
		echo "   <td align='right' class='labels1'>".number_format($TotalQty, $DecimalBase)."</td>";
		echo "	 <td align='center' class='totalborder'>&nbsp;</td>";
		echo "</tr>";
		echo "</table></td></tr>";
	}
		
	echo "<tr><td align='left' style='padding-top:20px;'>";
	echo "<table border=0 width='640' cellspacing=0 cellpadding=1  class='master' style='table-layout:fixed;' >
		  <col width=120>
		  <col width=120>
		  <col width=120>
		  <col width=120>
		  <col width=160>";
	
//	echo "<tr height='30'>";
//	echo "  <td align='center' style='border-top:1px solid black;'>&nbsp;</td>";
//	echo "  <td align='center' style='border-top:1px solid black;'>Date&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;</td>";
//	echo "  <td align='center' style='border-top:1px solid black;'>Date&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;</td>";
//	echo "  <td align='center' style='border-top:1px solid black;'>Date&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;</td>";
//	echo "  <td align='center' style='border-top:1px solid black;'>&nbsp;</td>";
//	echo "</tr>";	
	echo "</table></td></tr>";
	echo "<tr style='height: 20px'>
	<td></td>
	</tr>";
	echo "<tr><td align='left' style='padding-top:30px;'>";
	echo "<table border=0 width='640' cellspacing=5 cellpadding=0  class='master' style='table-layout:fixed;' >
		  <col width=100>
		  <col width=120>
		  <col width=120>
		  <col width=100>
		  <col width=100>
		  <col width=100>";
	
	echo "<tr>";
	echo "  <td align='center' colspan='3' style='font-weight: bold; font-size: 16px;'>Customer Signature<br><br><br>
	__________________________________</td>";
	echo "  <td align='center' colspan='3' style='font-weight: bold; font-size: 16px;'><span style='text-transform: uppercase'>$StmtMessage</span> Signature<br><br><br>
	__________________________________</td>";
	echo "</tr>";
	echo "<tr style='height: 30px;'>
	<td colspan=6></td>
	</tr>";
	echo "<tr>";
	echo "  <td align='center' colspan='5'>&nbsp;</td>";
	echo "  <td align='center'>$UserAccount</td>";
	echo "</tr>";
	
//	echo "<tr height='30'>";
//	echo "  <td align='center' style='border-top:2px solid black;'>Controled by</td>";
//	echo "  <td align='center' style='border-top:2px solid black;'>Warehouse Manager</td>";
//	echo "  <td align='center' style='border-top:2px solid black;'>Received by</td>";
//	echo "  <td align='center' style='border-top:2px solid black;'>Client</td>";
//	echo "  <td align='center' style='border-top:2px solid black;'>Accounting</td>";
//	echo "  <td align='center' style='border-top:2px solid black;'>Prepared by</td>";
//	echo "</tr>";	
//	echo "<tr height='5'><td colspan=6><h4></td></tr>";
	echo "</table></td></tr>";
	
	
	echo "<tr><td align='left' style='padding-top:20px;'>";
	echo "<table border=0 width='640' cellspacing=0 cellpadding=0  class='master' style='table-layout:fixed;' >
		  <col width=120>
		  <col width=200>
		  <col width=120>
		  <col width=200>";
		  
//	echo "<tr>";
//	echo "  <td align='center'><img src='./img/chigo.JPG' width='100px' height='100px' /></td>";
//	echo "  <td align='center'><img src='./img/chigo.JPG' width='100px' height='100px' /></td>";
//	echo "  <td align='center'><img src='./img/chigo.JPG' width='100px' height='100px' /></td>";
//	echo "  <td align='center'><img src='./img/chigo.JPG' width='100px' height='100px' /></td>";
//	echo "</tr>";	
	echo "</table></td></tr>";	  
		  
		  
	echo "</table>";



	/*
	echo "<table border=0 width=100% 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=280>
		  <col width=190>
		  <col width=280>";
	echo "<tr>";
	echo "	<td align='left' colspan='2' valign='top' style='font-size:20px; font-style:italic; font-weight:bold;'>".nl2br($Company_Name)."</td>";
	echo "	<td align='center' rowspan='3'><img src='LogoFile.php' width='180px' height='100px' /></td>";
	echo "</tr>";
	echo "<tr>";
	echo "	<td align='left' colspan='2' style='font-size:11px;'>".nl2br($Company_Capital)."</td>";
	echo "</tr>";
	echo "<tr>";
	echo "	<td align='left' colspan='2' style='font-size:11px;'>".nl2br($Company_Memo)."</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;'>VAT NUMBER: ".$Setup_vatno."</td>";
		echo "	<td align='center' style='font-size:14px; font-style:italic;'>RETURN 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;'>VAT NUMBER: ".$Setup_vatno."</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 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 align='right' style='font-size:15px; font-weight:bold;'>".$ClientAddressF."</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_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 ". 
			   " From DeliveryDt Left Join Items on DeliveryDt.Item_code = Items.Item_code ".
			   " Where DeliveryDt.Delivery_ID = ".$_GET['ReturnSalesId'].
			   " 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=280>
			  <col width=70>
			  <col width=70>
			  <col width=70>
			  <col width=50>
			  <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=310>
			  <col width=70>
			  <col width=70>
			  <col width=70>
			  <col width=50>
			  <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());
	while($rows=mysql_fetch_array($resultdt)){
		extract($rows);
		
		$TotalQty = $TotalQty + $DeliveryDtQuantity;
		$TotalGrossWeight = $TotalGrossWeight + $GrossWeight;
		
		$ItemBoxes  = floor($DeliveryDtQuantity/$ItemUnit);
		$ItemPieces = ($DeliveryDtQuantity%$ItemUnit);
		$UnitPack = $ItemBoxes . 'B ';
		if ($ItemPieces > 0) $UnitPack .= $ItemPieces . 'P';
		
		echo "<tr>";
		
		if($_GET['PLang'] != 'AR') {
			echo "  <td align='left' valign='top'>$ItemCode</td>";
			echo "  <td align='left' valign='top'>$ItemIsVat&nbsp;$ItemDescription</td>";
		} else {
			echo "  <td align='right' valign='top' style='border-right:1px solid black;'>$ItemCode</td>";
			echo "  <td align='right' valign='top'>$ItemIsVat&nbsp;$ItemDescriptionF1</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($DeliveryDtDiscountPer, $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($DeliveryDtTotal, $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='6' 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='font-weight: bold; padding-left:20px; border-right:0px;'>Total Weight(KG): ".number_format($TotalGrossWeight, $DecimalBase)."</td>
				<td colspan='4' align='right' style='font-weight: bold; padding-right:10px; border-left:0px;'>TOTAL</td>
				<td align='right' style='border-right:1px solid black;'>".number_format($GrossTotal, $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='6' align='right' style='font-weight: bold; padding-right:10px;'>$base1&nbsp;&nbsp;".number_format($Delivery_VATBase1, $DecimalBase)."&nbsp;&nbsp;&nbsp;Tax Invoices @ $Setup_VatPercentage%</td>
				<td align='right' style='border-right:1px solid black; border-bottom:1px solid black;'>".number_format($Delivery_VAT, $DecimalBase)."</td>
			 </tr>";
		echo "<tr>
				<td colspan='3' align='left' style='font-weight: bold; border-bottom:1px solid black;'>".$TotalAmountLetter."</td>
				<td colspan='3' align='right' style='font-weight: bold; padding-right:10px; border-bottom:1px solid black;'>TOTAL WITH VAT</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>";
	} 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);
		//$TotalAmountLetter = $rowalpha[0];
	
		$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='6' 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>";
		echo "<tr>
				<td colspan='2' align='right' style='font-weight: bold; padding-right:20px; border-right:1px solid black; border-left:0px;'> ����� ����� (��): ".number_format($TotalGrossWeight, $DecimalBase)."</td>
				<td colspan='4' align='left' style='font-weight: bold; padding-left:10px; border-right:0px;'>�������</td>
				<td align='right'>".number_format($GrossTotal, $DecimalBase)."</td>
			 </tr>";

		if ($DeliveryDiscountPer != 0) {	 
			if ($SalesTTC == 1)	{
				echo "<tr>
					<td colspan='6' 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='6' 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='6' align='left' style='border-right:1px solid black; font-weight: bold; padding-left:10px;'>Tax Invoices @ $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='3' align='left' style='font-weight: bold; padding-left:10px; border-bottom:1px solid black;'>������� �� VAT</td>
				<td align='right' style='border-bottom:1px solid black; font-size:14px; font-weight:bold;'>".number_format($Delivery_NetTotal, $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>";
		
		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='left' colspan='2'>&nbsp;</td>";
		
		if ($DeliveryCashInBase1 != 0 || $DeliveryCashInBase2 != 0) {
			echo "<td align='center' style='font-size:15px; font-weight:bold;'>Cash</td>";
			echo "<td align='left'>&nbsp;</td>";
		} else {
			echo "<td align='left' colspan='2'>&nbsp;</td>";
		}
		echo "</tr>";
			
		echo "<tr>";
		echo "	 <td align='right' style='font-size:14px; font-weight:bold;'>New Balance:</td>";
		echo "	 <td align='right' style='font-size:14px; padding-right:5px;'>".format_currency($ClientCurrBalance, $DecimalBase)." ".$CurrencyCode."</td>";
		echo "	 <td align='right' style='font-size:12px;'>Signature & Stamps:</td>";
		echo "	 <td align='center' style='border-bottom:1px solid black;'>&nbsp;</td>";
		echo "</tr>";
		
		echo "<tr style='height:30px;'>";
		echo "	 <td align='center' style='font-size:12px;'>".$UserAccount."</td>";
		echo "	 <td align='left' colspan='3'>&nbsp;</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>";
		
		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;'>".format_currency($ClientPrevBalance, $DecimalBase). " ".$CurrencyCode."</td>";
		//echo "	 <td align='left' colspan='2'>&nbsp;</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>";
			
		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;'>".format_currency($ClientCurrBalance, $DecimalBase)." ".$CurrencyCode."</td>";
		echo "	 <td align='left' style='font-size:12px;'>�������:</td>";
		echo "	 <td align='center' style='border-bottom:1px solid black;'>&nbsp;</td>";
		echo "</tr>";
		
		echo "<tr style='height:30px;'>";
		echo "	 <td align='center' style='font-size:12px;'>".$UserAccount."</td>";
		echo "	 <td align='left' colspan='3'>&nbsp;</td>";
		echo "</tr>";
		
		echo "</table></td></tr>";
	}
	
	echo "</table>";
	echo "<br>";
	
*/
}

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


function format_currency($amount, $DecimalBase) { 

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

print <<< HERE

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

