<?php

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



$exportexcell = $_GET['exportexcell'];

if($exportexcell ==1){
	$filename = "Exp_SorderExport" . date('Ymd') . ".xls";
	header("Content-Disposition: attachment; filename=\"$filename\"");
	header("Content-Type: application/vnd.ms-excel");
} 

if($exportexcell ==1){
}

if($exp ==1){
	$filename = "SalesOrder_" . date('Ymd') . ".xls";
	header("Content-Disposition: attachment; filename=\"$filename\"");
	header("Content-Type: application/vnd.ms-excel");
} 



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> Export Proforma Invoice </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;
                 }				 
				 
				
				 

</style>


</HEAD>

<BODY>
HERE;

include("config.inc.php");


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

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

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

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

	

$TotalQty = 0;
$STotalVolume = 0;
$STotalBoxes = 0;
$STotalGrossWeight = 0;
$STotalNetWeight = 0;


if($_GET['SorderId'] != '')
{
	$query = " Select Sorder_ID, Sorder_Reference, Date_Format(Sorder_Date,'%d/%m/%Y') As Sorder_Date, Sorder_Date As InvoiceDate, ".
	         " Date_Format(Sorder_MaturityDate,'%d/%m/%Y') As MaturityDate, ifNull(Sorder.Journal_ID,0) As JournalID, ".
			 " Sorder.Client_code As ClientCode, Clients.Ledger_name As ClientName, Client_VAT, Client_Telephone1 as Client_Telephone1, Client_CreditLimit, ".
			 " Client_Address As ClientAddress, Clients.Ledger_nameF As ClientNameF,   ".
			 " Client_VATNo,Client_financialno as clientfinancialno, Sorder.Currency_code As CurrencyCode, Client_Address, Sorder_DeliveryPlace, ".
			 " Sorder_Remark, Sorder.Project_Code As ProjectCode, Project_Name, Sorder.Costcent_code As Costcentcode, Costcent_name,  ".
			 " Round(ifNull(Sorder_Taxable,0), ".$DecimalBase.") As SorderTaxable, Round(ifNull(Sorder_nonetaxable,0), ".$DecimalBase.") As SorderNoneTaxable, ".
			 " Round((if($SalesTTC=1,ifNull(Sorder_Taxable,0)*(1+$Setup_VatPercentage/100),ifNull(Sorder_Taxable,0)) + ifNull(Sorder_NoneTaxable,0)   + (ifNull(Sorder_Tax1,0) + ifNull(Sorder_Tax2,0) + ifNull(Sorder_Tax3,0) + ifNull(Sorder_Tax4,0) + ifNull(Sorder_Tax5,0))), ".$DecimalBase.") As Sorder_GrossTotal,  ".
			 " Round(ifNull(Sorder_DiscountPer,0), ".$DecimalBase.") As SorderDiscountPer, Round(ifNull(Sorder_DiscountAmount,0), ".$DecimalBase.") As SorderDiscountAmount,  ".
			 " Round(ifNull(Sorder_VAT,0), ".$DecimalBase.") As Sorder_vat, Round(ifNull(Sorder_VATBase1,0), ".$DecimalBase.") As Sorder_VATBase1, ".
			 " Round((if($SalesTTC=1,ifNull(Sorder_Taxable,0)*(1+$Setup_VatPercentage/100),ifNull(Sorder_Taxable,0)) + ifNull(Sorder_NoneTaxable,0) + if($SalesTTC=1,0,IfNull(Sorder_VAT,0)) + ifNull(Sorder_TransportAmount,0) - IfNull(Sorder_DiscountAmount,0) + (ifNull(Sorder_Tax1,0) + ifNull(Sorder_Tax2,0) + ifNull(Sorder_Tax3,0) + ifNull(Sorder_Tax4,0) + ifNull(Sorder_Tax5,0))), ".$DecimalBase.") As Sorder_NetTotal,  ".
			 " Round((ifNull(Sorder_Tax1,0) + ifNull(Sorder_Tax2,0) + ifNull(Sorder_Tax3,0) + ifNull(Sorder_Tax4,0) + ifNull(Sorder_Tax5,0)), ".$DecimalBase.") As Sorder_TotalTaxes,  ".
			 " Round(ifNull(Sorder_TransportAmount,0), ".$DecimalBase.") As SorderTransportAmt, ifNull(Sorder_VATChckbx,0) Sorder_VATChckbx ".
			 " From Sorder ".
			 " Left Join Clients on Sorder.Client_code = Clients.Ledger_Number ".
			 " Left Join Project on Sorder.Project_Code = Project.Project_Code ".
			 " Left Join CostCent on Sorder.Costcent_code = CostCent.CostCent_Code ".
	         " Where Sorder_ID = ".$_GET['SorderId'];
//echo $query;
//exit;
	$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."','','".$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 - $Sorder_NetTotal;
	
	$title = 'Invoice No '.$Sorder_Reference;
		
	echo "<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td align='left'>";
	
	echo "<table border='0' width='1000px' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=300>
		  <col width=400>
		  <col width=300>";
	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='3' style='height:20px;'>&nbsp;</td></tr>";
	
	echo "<tr>";
	echo "	<td align='left' colspan='2' style='font-size:14px; font-weight:bold; border-bottom:3px solid black;'>VAT NUMBER: ".$Setup_vatno."</td>";
	echo "	<td align='center' style='font-size:14px; font-style:italic;'>EXPORT PROFORMA ORDER</td>";
	echo "</tr>";
	
	echo "</table></td></tr>";
	
	
	echo "<tr><td align='left'>";
	echo "<table border='0' width='1000px' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=1000>";
		
	echo "<tr><td COLSPAN=2><table width=980 style='table-layout:fixed;' ><tr>";
	echo "	<td>";
	echo "			<table border='0' width='550px' cellspacing=0 cellpadding=0 style='table-layout:fixed;' >
					<col width=550>";
	#echo "			<tr><td align='left' style='font-size:13px; font-weight:bold;'>Client #: ".$ClientCode."</td></tr>";	
	echo "			<tr><td align='left' style='font-size:13px; font-weight:bold;'>Client :  ".$ClientName." &nbsp;&nbsp;&nbsp; ".$ClientCode." </td></tr>";
    echo "			<tr><td align='left' style='font-size:12px; font-weight:bold;'> TIN#:      ".$ClientFINANCIALNO."</td></tr>";	
	#echo "			<tr><td align='left' style='font-size:13px; font-weight:bold;'>".$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;'>TEL: ".$Client_Telephone1."</td></tr>";	
	echo "			</table>";
	echo "		";
		echo"</td><td width=180>&nbsp;</td><td align=right>";
	echo "		";
	echo "			<table border='0' width='250px' cellspacing=0 cellpadding=0 style='table-layout:fixed;' >
					<col width=70>
					<col width=80>
					<col width=100>";
	echo "			<tr><td align='left' style='font-size:13px; font-weight:bold;'>Proforma #:</td>";
	echo "				<td align='left' colspan='2' style='font-size:13px; font-weight:bold; padding-left:3px;'>".$Sorder_Reference."</td></tr>";
	echo "			<tr><td align='left' style='font-size:13px; font-weight:bold;'>Date:</td>";	
	echo "				<td align='left' colspan='2' style='font-size:13px; font-weight:bold; padding-left:3px;'>".$Sorder_Date."</td>";	
	echo "			</tr>";
	echo "			<tr><td align='left' style='font-size:13px; font-weight:bold;'>Currency:</td>";
	echo "				<td align='left' colspan='2' style='font-size:13px; font-weight:bold; padding-left:3px;'>".$CurrencyCode."</td>";
	echo"			</tr>";
	echo "			</table>";
	echo "		";
	echo "	</td></tr></table></td>";
	echo "</tr>";
		
	echo "</table></td></tr>";
	

	
		$querydt = " Select SorderDt.SorderDt_id, SorderDt.Item_CodePacked, if($chkhs=1,Items.item_codeexport,SorderDt.Item_code) as ItemCode
				,  if(sorderDt_Remark='' or sorderDt_Remark is null ,if('$plang'='EN',item_descriptionexporten,if('$plang'='AR',item_descriptionexportar,item_descriptionexportfr) ),sorderDt_Remark) As ItemDescription, sorderdt_itemunit as ItemUnit, ".
			   " Item_DescriptionF1 As ItemDescriptionF1, Item_DescriptionF2 As ItemDescriptionF2, if(ifNull(SorderDt.Item_Vat,0) > 0, '*', ' ') as ItemIsVat, ".
			   " SorderDt_Quantity as SorderDtQuantity, Round((ifNull(SorderDt_Price,0)), ".$DecimalBase.") As SorderDtPrice, ".
			   " Round((ifNull(SorderDt_PriceTTC,0)*ifNull(sorderdt_itemunit,1)), ".$DecimalBase.") As SorderDtPriceTTC, ".
			   " Round(((SorderDt_Quantity*SorderDt_Price)-((SorderDt_Quantity*SorderDt_Price*ifNull(SorderDt_Discount,0))/100)), ".$DecimalBase.") As SorderDtTotal, ".
			   " Round(((SorderDt_Quantity*SorderDt_PriceTTC)-((SorderDt_Quantity*SorderDt_PriceTTC*ifNull(SorderDt_Discount,0))/100)), ".$DecimalBase.") As SorderDtTotalTTC, ".
			   " sorderdt_itempackage as ItemPackage, ".
			   //" if(sorderdt_quantitybox=0,0,(ifNull(SorderDt_Quantity,0)/ifNull(sorderdt_itemunit,1))) as QtyPerUnit, ".
			   " sorderdt_quantitybox  as QtyPerUnit, ".
			   " (ifNull(Item_NetWeight,0)*ifNull(sorderdt_itemunit,1)) as ItemNetWeight,  ".
			   " (ifNull(Item_NetWeight,0)*ifNull(SorderDt_Quantity,0)) as TotalNetWeight, (ifNull(Item_Weight,0)*ifNull(sorderdt_itemunit,1)) as ItemGrossWeight,   ".
			   " (ifNull(Item_Weight,0)*ifNull(SorderDt_Quantity,0)) as TotalGrossWeight, ifNull(Item_Volume,0) as ItemVolume,  ".
			   " (ifNull(Item_Volume,0)*ifNull(SorderDt_Quantity,0)/ifNull(sorderdt_itemunit,1)) as TotalVolume, SorderDt_Remark,ifNull(SorderDt_Discount,0) as SorderDt_Discount ".
			   " ,(SorderDt_Quantity*SorderDt_Price*ifNull(SorderDt_Discount,0)/100) as SorderDt_DiscountAmount ".
			   " From SorderDt Left Join Items on SorderDt.Item_code = Items.Item_code ".
			   " Where SorderDt.Sorder_ID = ".$_GET['SorderId'].
			   " Order By SorderDt.SorderDt_id Asc  ";
		   
	//echo $querydt;
	echo "<tr><td align='left'>";
	echo "<table border='0' width='1910px' cellspacing='0' class='detail' style='table-layout:fixed; margin-top:5px;' >
		  <col width=85>
		  <col width=250>
		  <col width=40>
		  <col width=75>
		  <col width=75>
		  <col width=80>
		  <col width=30>
		  <col width=70>
		  <col width=70>
		  <col width=70>
		  <col width=70>
		  <col width=70>
		  <col width=70>
		  <col width=70>
		  <col width=70>
		  <col width=70>
		  <col width=90>
		  <col width=80>
		  <col width=80>
		  <col width=200>";
	echo "<tr>";
	if($chkhs==1)
	echo "   <th align='left' style='padding-left:3px;'>HS Code</th>";
	else
	echo "   <th align='left' style='padding-left:3px;'>Item Code</th>";
	echo "   <th align='center'>Description</th>";
	//echo "   <th align='center'>Brand</th>";
	//echo "   <th align='center'>Origin</th>";
	//echo "   <th align='center'>Material</th>";
	echo "	 <th align='center'>Qty of Boxes</th>";
	echo "   <th align='center'>Pack/Box</th>";
	echo "	 <th align='center'>Qty of Packs</th>";
	
	//echo "	 <th align='center'>Net Weight /Box (KG)</th>";
	echo "	 <th align='center'>Unit Price($CurrencyCode)</th>";
	echo "	 <th align='center'>Disc %</th>";
	echo "	 <th align='center'  style=''>Total Price($CurrencyCode)</th>";
	echo "	 <th align='center'>Total Net Weight /Item (KG)</th>";
	//echo "	 <th align='center'>G. Weight /Box (KG)</th>";

	echo "	 <th align='center'>Total G. Weight /Item (KG)</th>";
	//echo "	 <th align='center'>Volume /Box (m3)</th>";
	echo "	 <th align='center' style='border-right:1px solid black;'>Total Volume /Item (m3)</th>";

	/*echo "   <th align='center'>Prod.Date</th>";
	echo "   <th align='center'>Expire Date</th>";
	echo "	 <th align='center' style='border-right:1px solid black;'>Remark</th>";*/
	echo "</tr>";			
	 
	$i=0;
	$resultdt=mysql_query($querydt,$connection) or die(mysql_error());
	$TotalDiscountAmount=0;
	
	while($rows=mysql_fetch_array($resultdt)){
		extract($rows);
		
			if ($i%2==0)
        		$Color="#ABD4F1";
			else
				$Color="#EFF1F3";
				
				
		$TotalQty = $TotalQty + $SorderDtQuantity;
		
		$STotalVolume = $STotalVolume + $TotalVolume;
		$STotalBoxes = $STotalBoxes + $QtyPerUnit;
		$STotalGrossWeight = $STotalGrossWeight + $TotalGrossWeight;
		$STotalNetWeight = $STotalNetWeight + $TotalNetWeight;
		
		$sub_query1  = " Select ItemDt_Value From ItemsDt Where upper(ItemParam_Code) = 'ORIGIN' And Item_Code = '".$ItemCode."' Limit 1 ";
		$sub_result1 = mysql_query($sub_query1,$connection);
		$sub_rows1 = mysql_fetch_row($sub_result1);
		$ItemOrigin = $sub_rows1[0];
		
		$sub_query2  = " Select ItemDt_Value From ItemsDt Where upper(ItemParam_Code) = 'MATERIAL' And Item_Code = '".$ItemCode."' Limit 1 ";
		$sub_result2 = mysql_query($sub_query2,$connection);
		$sub_rows2 = mysql_fetch_row($sub_result2);
		$ItemMaterial = $sub_rows2[0];

		$sub_query3  = " Select ItemDt_Value From ItemsDt Where upper(ItemParam_Code) = 'BRAND' And Item_Code = '".$ItemCode."' Limit 1 ";
		$sub_result3 = mysql_query($sub_query3,$connection);
		$sub_rows3 = mysql_fetch_row($sub_result3);
		$Itembrand = $sub_rows3[0];
		   
		echo "<tr bgcolor=$Color>";
		echo "  <td align='left' valign='top'>$ItemCode</td>";
		echo "  <td align='left' valign='top'>$ItemIsVat&nbsp;$ItemDescription</td>";
		/*echo "  <td align='left' valign='top'>&nbsp;$Itembrand</td>";
		echo "  <td align='left' valign='top'>&nbsp;$ItemOrigin</td>";
		echo "  <td align='left' valign='top'>&nbsp;$ItemMaterial</td>";*/
		echo "	<td align='right' valign='top'>".number_format($QtyPerUnit, 0)."</td>";
		echo "  <td align='center' valign='top'>&nbsp;$ItemPackage</td>";
		echo "	<td align='right' valign='top'>".number_format($SorderDtQuantity, $DecimalBase)."</td>";
	
		//echo "	<td align='right' valign='top'>".number_format($ItemNetWeight, $DecimalBase)."</td>";
		
			if ($SalesTTC == 1) {
			echo "	<td align='right' valign='top'>".number_format($SorderDtPriceTTC, $DecimalBase)."</td>";
			echo "	<td align='right' valign='top' style='border-right:1px solid black;'>".number_format($SorderDtTotalTTC, $DecimalBase)."</td>";
		}	
		else {
			echo "	<td align='right' valign='top'>".number_format($SorderDtPrice, $DecimalBase)."</td>";
			echo "	<td align='right' valign='top'>".number_format($SorderDt_Discount, $DecimalBase)."</td>";		
			echo "	<td align='right' valign='top' >".number_format($SorderDtTotal, $DecimalBase)."</td>";
		}
		
		echo "	<td align='right' valign='top'>".number_format($TotalNetWeight, $DecimalBase)."</td>";
		//echo "	<td align='right' valign='top'>".number_format($ItemGrossWeight, $DecimalBase)."</td>";
		
				
	
		
		
		echo "	<td align='right' valign='top'>".number_format($TotalGrossWeight, $DecimalBase)."</td>";
		//echo "	<td align='right' valign='top'>".number_format($ItemVolume, $DecimalBase)."</td>";
		echo "	<td align='right' valign='top'  style='border-right:1px solid black;border-right:1px solid black;'>".number_format($TotalVolume, $DecimalBase)."</td>";
	
			
		/*echo "  <td align='center' valign='top'>&nbsp;$SorderDtExpireDate</td>";
		echo "  <td align='center' valign='top'>&nbsp;$SorderDtProductionDate</td>";
		echo "  <td align='left' valign='top' style='border-right:1px solid black;'>&nbsp;$SorderDt_Remark</td>";*/
				
		echo "</tr>";
		
		$TotalDiscountAmount=$TotalDiscountAmount+$SorderDt_DiscountAmount;
		$i+=1;
	}
	
	if (empty($Sorder_NetTotal)) $Sorder_NetTotal = 0;
	$queryalpha  = " SELECT Alpha_Num ('ENGLISH', '".$CurrencyCode."', ".$Sorder_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='7' align='right' style='border-top:1px solid black;border-left:0px solid black;'>&nbsp;</td>
			<td align='right' style='border-top:1px solid black;border-left:0px solid black;'>&nbsp;</td>
			<td align='right' style='border-top:1px solid black;border-left:0px solid black;'>&nbsp;</td>
			<td align='right' style='border-top:1px solid black;border-left:0px solid black;'>&nbsp;</td>
			<td align='right' style='border-top:1px solid black;border-left:0px solid black;'>&nbsp;</td>
			
		 </tr>";
	/*echo "<tr>
			<td colspan='7' align='right' style='border-bottom:1px solid black; font-weight: bold; padding-right:10px;'>TOTAL</td>
			<td align='right' style='border-bottom:1px solid black;border-right:1px solid black; font-weight:bold;'>".number_format($GrossTotal, $DecimalBase)."</td>
			 
		 </tr>";
		*/	 
	
	if($Sorder_VATChckbx==0)
      $Setup_VatPercentage=0;
   if ($TotalDiscountAmount>0)  { 
	echo "<tr>
			<td colspan='11' align='left' style='border-right:1px solid black; border-top:1px solid black; font-weight: bold; padding-right:10px;'>TOTAL DISCOUNT: ".number_format($TotalDiscountAmount, $DecimalBase)." ".$CurrencyCode."</td>
		</tr>";			
	echo "<tr>
			<td colspan='11' align='left' style='border-right:1px solid black; border-top:0px solid black; font-weight: bold; padding-right:10px;'>AMOUNT VAT @ $Setup_VatPercentage%: ".number_format($Sorder_vat, $DecimalBase)."</td>
		</tr>";
	} else {
		
		echo "<tr>
			<td colspan='11' align='left' style='border-right:1px solid black; border-top:1px solid black; font-weight: bold; padding-right:10px;'>AMOUNT VAT @ $Setup_VatPercentage%: ".number_format($Sorder_vat, $DecimalBase)."</td>
			</tr>";
	}
	
	/*echo "<tr>
			<td colspan='11' align='left' style='border-right:1px solid black; font-weight: bold; padding-right:10px;'>AMOUNT WITHOUT VAT: ".number_format($SorderNoneTaxable, $DecimalBase)." ".$CurrencyCode."&nbsp;&nbsp;&nbsp;AMOUNT WITH VAT: ".number_format($SorderTaxable, $DecimalBase)." ".$CurrencyCode."</td>
		</tr>
			 
	*/
	if ($Sorder_vat>0) {
	  echo "	<tr>
			<td colspan='11' align='left' style='border-right:1px solid black; font-weight: bold; padding-right:10px;'>TOTAL BEFORE VAT: ".number_format($Sorder_GrossTotal, $DecimalBase)." ".$CurrencyCode."&nbsp;&nbsp;&nbsp;TOTAL AFTER VAT: ".number_format($Sorder_NetTotal, $DecimalBase)." ".$CurrencyCode."</td>
		</tr>";
	} else {
		 echo "	<tr>
			<td colspan='11' align='left' style='border-right:1px solid black; font-weight: bold; padding-right:10px;'>NET TOTAL: ".number_format($Sorder_NetTotal, $DecimalBase)." ".$CurrencyCode."</td>
		</tr>";
	}
	echo "<tr>
			<td colspan='11' align='left' style='border-right:1px solid black; border-bottom:1px solid black; font-weight: bold; padding-right:10px;'>".$TotalAmountLetter."</td>
		</tr>";
	
	echo "</table></td></tr>";
	
		

	echo "<tr><td align='left'>";
	echo "<table border='0' width='635px' cellspacing=0 cellpadding=1  style='table-layout:fixed; margin-top:20px;' >
		  <col width=150>
		  <col width=130>
		  <col width=80>
		  <col width=260>";
		
	echo "<tr>";
	echo "	 <td align='left' style='border-left:1px solid black; border-top:1px solid black; font-size:14px; font-weight:bold;'>Total Price ($CurrencyCode)</td>";
	#echo "	 <td align='right' style='border-left:1px solid black; border-top:1px solid black; font-size:14px; padding-right:5px;'>".format_currency($GrossTotal, $DecimalBase)."</td>";
	echo "	 <td align='right' style='border-left:1px solid black; border-top:1px solid black; font-size:14px; padding-right:5px;'>".format_currency($Sorder_NetTotal, $DecimalBase)."</td>";
	echo "	 <td align='center' style='border-right:1px solid black; border-left:1px solid black; border-top:1px solid black; font-size:14px;'>".$CurrencyCode."</td>";
	echo "	 <td align='left'>&nbsp;</td>";
	echo "</tr>";

	echo "<tr>";	echo "<tr>";
	echo "	 <td align='left' style='border-left:1px solid black; border-top:1px solid black; font-size:14px; font-weight:bold;'>Total Volume</td>";
	echo "	 <td align='right' style='border-left:1px solid black; border-top:1px solid black; font-size:14px; padding-right:5px;'>".format_currency($STotalVolume, $DecimalBase)."</td>";
	echo "	 <td align='center' style='border-right:1px solid black; border-left:1px solid black; border-top:1px solid black; font-size:14px;'>m3</td>";
	echo "	 <td align='left'>&nbsp;</td>";
	echo "</tr>";
	echo "<tr>";
	echo "	 <td align='left' style='border-left:1px solid black; border-top:1px solid black; font-size:14px; font-weight:bold;'>Total Gross Weight</td>";
	echo "	 <td align='right' style='border-left:1px solid black; border-top:1px solid black; font-size:14px; padding-right:5px;'>".format_currency($STotalGrossWeight, $DecimalBase)."</td>";
	echo "	 <td align='center' style='border-right:1px solid black; border-left:1px solid black; border-top:1px solid black; font-size:14px;'>KG</td>";
	echo "	 <td align='left'>&nbsp;</td>";
	echo "</tr>";
	echo "<tr>";
	echo "	 <td align='left' style='border-left:1px solid black; border-top:1px solid black; font-size:14px; font-weight:bold;'>Total Net Weight</td>";
	echo "	 <td align='right' style='border-left:1px solid black; border-top:1px solid black; font-size:14px; padding-right:5px;'>".format_currency($STotalNetWeight, $DecimalBase)."</td>";
	echo "	 <td align='center' style='border-right:1px solid black; border-left:1px solid black; border-top:1px solid black; font-size:14px;'>KG</td>";
	echo "	 <td align='left'>&nbsp;</td>";
	echo "</tr>";
	echo "<tr>";
	echo "	 <td align='left' style='border-left:1px solid black; border-top:1px solid black; border-bottom:1px solid black; font-size:14px; font-weight:bold;'>Total No of Boxes</td>";
	echo "	 <td align='right' style='border-left:1px solid black; border-top:1px solid black; border-bottom:1px solid black; font-size:14px; padding-right:5px;'>".format_currency($STotalBoxes, $DecimalBase)."</td>";
	echo "	 <td align='center' style='border-right:1px solid black; border-left:1px solid black; border-top:1px solid black; border-bottom:1px solid black; font-size:14px;'></td>";
	echo "	 <td align='left'>&nbsp;</td>";
	echo "</tr>";
	echo "</table></td></tr>";
	
	
	echo "</table>";
	echo "<br>";
	

}



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

