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



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

include("config.inc.php");
require_once('Spreadsheet/Excel/Writer.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);
}

	
 
//Creating a workbook
$workbook = new Spreadsheet_Excel_Writer();
$workbook->setVersion(8);
 
//Sending HTTP headers
$workbook->send('Exp_ExportInvoice.xls');
 
//Creating a worksheet
$worksheet=&$workbook->addWorksheet('Invoice Export');
$worksheet->setInputEncoding('windows-1256'); 
$worksheet->setLandscape();
 
//set all columns same width
$columnWidth = 10;


//Title Format 
$sheetTitleFormat =& $workbook->addFormat(array('bold'=>1,
  'size'=>10));

//Column Title Format  
$columnTitleFormat =& $workbook->addFormat(array('bold'=>1,
  'top'=>1,
  'bottom'=>1 ,
  'size'=>15,
  'color'=>0x0A,
  ));
  
 
  
// Regular Format
$regularFormat =& $workbook->addFormat(array('size'=>9,
  'align'=>'left' 
  ));

// Bold Title Format  
$TitleFormat =& $workbook->addFormat(array('size'=>18,
  'align'=>'left', 
  'bold'=>'1', 
  'italic'=>'1'
  ));

$TitleFormat1 =& $workbook->addFormat(array('size'=>10,
  'align'=>'left', 
  'bold'=>'0'
  ));
$TitleFormat1->setTextWrap();    
  
$regularleftFormat =& $workbook->addFormat(array('size'=>10,
  'align'=>'left'  ));
$regularrightFormat =& $workbook->addFormat(array('size'=>10,
  'align'=>'right'  ));  
  
$boldleftFormat =& $workbook->addFormat(array('size'=>10,
  'align'=>'left'  ,
  'valign'=>'top'  ,
  'bold'=>1
  ));  
$boldrightFormat =& $workbook->addFormat(array('size'=>10,
  'align'=>'right'  ,
  'bold'=>1
  )); 
$boldcenterFormat =& $workbook->addFormat(array('size'=>10,
  'align'=>'center'  ,
  'valign'=>'top'  ,
  'bold'=>1
  )); 

$headerFormat =& $workbook->addFormat(array('size'=>10,
  'align'=>'center'  ,
  'valign'=>'bottom'  ,
  'bold'=>1
  ));  
$headerFormat->setTextWrap();  
 

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


if($_GET['SalesId'] != '')
{
	$query = " Select Delivery_ID, Delivery_Reference, Date_Format(Delivery_Date,'%d/%m/%Y') As Delivery_Date, Delivery_Date 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, 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  ".
			 " From Deliveries ".
			 " Left Join Clients on Deliveries.Client_code = Clients.Ledger_Number ".
			 " Left Join Project on Deliveries.Project_Code = Project.Project_Code ".
			 " Left Join CostCent on Deliveries.Costcent_code = CostCent.CostCent_Code ".
	         " Where Delivery_ID = ".$_GET['SalesId'];

	$result=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
	}
	
	if (empty($JournalID)) $JournalID = 0;
	//Get Client Balance
	$querybalance = "SELECT IFNULL(GetClientBalance('".$ClientCode."','".$InvoiceDate."','','".$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;
		
	$worksheet->setMerge(1, 0, 1, 3);
	$worksheet->write(1, 0, $Company_Name, $TitleFormat);
	$worksheet->setMerge(2, 0, 2, 3);
	$worksheet->write(2, 0, $Company_Capital, $TitleFormat1);
	$worksheet->setMerge(3, 0, 3, 3);
	$worksheet->setRow (3, 60);
	$worksheet->write(3, 0, $Company_Memo, $TitleFormat1);
	
	$worksheet->write(5, 0, 'VAT NUMBER: '.$Setup_vatno, $boldleftFormat);
	$worksheet->write(5, 10, 'EXPORT INVOICE', $boldleftFormat);
	

	$worksheet->write(7, 0, 'Client #: '.$ClientCode, $boldleftFormat);
	$worksheet->write(8, 0, $ClientName, $boldleftFormat);
	$worksheet->write(9, 0, $ClientAddress, $boldleftFormat);
	$worksheet->write(10, 0, 'TEL: '.$Client_Telephone1, $boldleftFormat);
	
	$worksheet->write(7, 10, 'Invoice #: '.$Delivery_Reference, $boldleftFormat);
	$worksheet->write(8, 10, 'Date: '.$Delivery_Date, $boldleftFormat);
	$worksheet->write(9, 10, 'Currency: '.$CurrencyCode, $boldleftFormat);

	  
		  
	$RowNum    = 12;
	$ColumnNum = 0;	  

	$worksheet->setColumn ($ColumnNum, $ColumnNum, 15);
	$worksheet->write($RowNum,$ColumnNum,"Item Code", $headerFormat);
	$ColumnNum += 1;
	
	$worksheet->setColumn ($ColumnNum, $ColumnNum, 40);
	$worksheet->write($RowNum,$ColumnNum,"Description", $headerFormat);
	$ColumnNum += 1;
	
	
	/*$worksheet->setColumn ($ColumnNum, $ColumnNum, 12);
	$worksheet->write($RowNum,$ColumnNum,"Brand", $headerFormat);
	$ColumnNum += 1;
	
	$worksheet->setColumn ($ColumnNum, $ColumnNum, 12);
	$worksheet->write($RowNum,$ColumnNum,"Origin", $headerFormat);
	$ColumnNum += 1;
	
	$worksheet->setColumn ($ColumnNum, $ColumnNum, 12);
	$worksheet->write($RowNum,$ColumnNum,"Material", $headerFormat);
	$ColumnNum += 1;*/
	
	$worksheet->setColumn ($ColumnNum, $ColumnNum, 10);
	$worksheet->write($RowNum,$ColumnNum,"Qty of Boxes", $headerFormat);
	$ColumnNum += 1;
	
	
	$worksheet->setColumn ($ColumnNum, $ColumnNum, 12);
	$worksheet->write($RowNum,$ColumnNum,"Pack/Box", $headerFormat);
	$ColumnNum += 1;
	
	
	$worksheet->setColumn ($ColumnNum, $ColumnNum, 10);
	$worksheet->write($RowNum,$ColumnNum,"Qty of Packs", $headerFormat);
	$ColumnNum += 1;
	
	
	

	
	/*$worksheet->setColumn ($ColumnNum, $ColumnNum, 10);
	$worksheet->write($RowNum,$ColumnNum,"Net Weight /Box (KG)", $headerFormat);
	$ColumnNum += 1;*/
	
	$worksheet->setColumn ($ColumnNum, $ColumnNum, 10);
	$worksheet->write($RowNum,$ColumnNum,"Total Net Weight /Item (KG)", $headerFormat);
	$ColumnNum += 1;
	
	/*$worksheet->setColumn ($ColumnNum, $ColumnNum, 10);
	$worksheet->write($RowNum,$ColumnNum,"G. Weight /Box (KG)", $headerFormat);
	$ColumnNum += 1;*/
	
	$worksheet->setColumn ($ColumnNum, $ColumnNum, 10);
	$worksheet->write($RowNum,$ColumnNum,"Total G. Weight /Item (KG)", $headerFormat);
	$ColumnNum += 1;
	
	/*$worksheet->setColumn ($ColumnNum, $ColumnNum, 10);
	$worksheet->write($RowNum,$ColumnNum,"Volume /Box (m3)", $headerFormat);
	$ColumnNum += 1;*/
	
	$worksheet->setColumn ($ColumnNum, $ColumnNum, 10);
	$worksheet->write($RowNum,$ColumnNum,"Total Volume /Item (m3)", $headerFormat);
	$ColumnNum += 1;
	
	$worksheet->setColumn ($ColumnNum, $ColumnNum, 12);
	$worksheet->write($RowNum,$ColumnNum,"Price/ Box ($CurrencyCode)", $headerFormat);
	$ColumnNum += 1;
	
	$worksheet->setColumn ($ColumnNum, $ColumnNum, 12);
	$worksheet->write($RowNum,$ColumnNum,"Total Price/ Item ($CurrencyCode)", $headerFormat);
	$ColumnNum += 1;
	
	/*$worksheet->setColumn ($ColumnNum, $ColumnNum, 12);
	$worksheet->write($RowNum,$ColumnNum,"Prod.Date", $headerFormat);
	$ColumnNum += 1;
	
	$worksheet->setColumn ($ColumnNum, $ColumnNum, 12);
	$worksheet->write($RowNum,$ColumnNum,"Expire Date", $headerFormat);
	$ColumnNum += 1;
	
	$worksheet->setColumn ($ColumnNum, $ColumnNum, 30);
	$worksheet->write($RowNum,$ColumnNum,"Remark", $headerFormat);
	$ColumnNum += 1;*/
	
	
	$querydt = " Select DeliveryDt.DeliveryDt_id, DeliveryDt.Item_CodePacked, if($chkhs=1,Items.item_codeexport,DeliveryDt.Item_code)   as ItemCode, ".
			   "  if(DeliveryDt_Remark='' or DeliveryDt_Remark is null ,if('$xplang'='EN',item_descriptionexporten,if('$xplang'='AR',item_descriptionexportar,item_descriptionexportfr) ),DeliveryDt_Remark)  As ItemDescription, Deliverydt_itemunit as ItemUnit, ".
			   " Item_DescriptionF1 As ItemDescriptionF1, Item_DescriptionF2 As ItemDescriptionF2, if(ifNull(DeliveryDt.Item_Vat,0) > 0, '*', ' ') as ItemIsVat, ".
			   " DeliveryDt_Quantity as DeliveryDtQuantity, Round((ifNull(DeliveryDt_Price,0)), ".$DecimalBase.") As DeliveryDtPrice, ".
			   " Round((ifNull(DeliveryDt_PriceTTC,0)*ifNull(Deliverydt_itemunit,1)), ".$DecimalBase.") As DeliveryDtPriceTTC, ".
			   " Round(((DeliveryDt_Quantity*DeliveryDt_Price)-((DeliveryDt_Quantity*DeliveryDt_Price*ifNull(DeliveryDt_DiscountPer,0))/100)), ".$DecimalBase.") As DeliveryDtTotal, ".
			   " Round(((DeliveryDt_Quantity*DeliveryDt_PriceTTC)-((DeliveryDt_Quantity*DeliveryDt_PriceTTC*ifNull(DeliveryDt_DiscountPer,0))/100)), ".$DecimalBase.") As DeliveryDtTotalTTC, ".
			   " Deliverydt_itempackage as ItemPackage, (ifNull(DeliveryDt_Quantity,0)/ifNull(Deliverydt_itemunit,1)) as QtyPerUnit, (ifNull(Item_NetWeight,0)*ifNull(Deliverydt_itemunit,1)) as ItemNetWeight,  ".
			   " (ifNull(Item_NetWeight,0)*ifNull(DeliveryDt_Quantity,0)) as TotalNetWeight, (ifNull(Item_Weight,0)*ifNull(Deliverydt_itemunit,1)) as ItemGrossWeight,   ".
			   " (ifNull(Item_Weight,0)*ifNull(DeliveryDt_Quantity,0)) as TotalGrossWeight, ifNull(Item_Volume,0) as ItemVolume,  ".
			   " (ifNull(Item_Volume,0)*ifNull(DeliveryDt_Quantity,0)/ifNull(Deliverydt_itemunit,1)) as TotalVolume, DeliveryDt_Remark, Date_Format(DeliveryDt_ExpireDate,'%d/%m/%Y') As DeliveryDtExpireDate, ".
			   " Date_Format(DeliveryDt_ProductionDate,'%d/%m/%Y') As DeliveryDtProductionDate ".
			   " 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;
	 
	$RowNum    = 13;
	$ColumnNum = 0;	 
	
	$resultdt=mysql_query($querydt,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultdt)){
		extract($rows);
		
		$TotalQty = $TotalQty + $DeliveryDtQuantity;
		
		$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];

		
		$worksheet->write($RowNum, $ColumnNum, $ItemCode, $regularleftFormat);
		$ColumnNum += 1;
		$worksheet->write($RowNum, $ColumnNum, $ItemIsVat.' '.$ItemDescription, $regularleftFormat);
		$ColumnNum += 1;
		/*$worksheet->write($RowNum, $ColumnNum, $ItemBrand, $regularleftFormat);
		$ColumnNum += 1;
		$worksheet->write($RowNum, $ColumnNum, $ItemOrigin, $regularleftFormat);
		$ColumnNum += 1;
		$worksheet->write($RowNum, $ColumnNum, $ItemMaterial, $regularleftFormat);
		$ColumnNum += 1;*/
		$worksheet->write($RowNum, $ColumnNum, number_format($QtyPerUnit, $DecimalBase), $regularrightFormat);
		$ColumnNum += 1;	
		
		
		$worksheet->write($RowNum, $ColumnNum, $ItemPackage, $regularrightFormat);
		$ColumnNum += 1;
		
		$worksheet->write($RowNum, $ColumnNum, $DeliveryDtQuantity, $regularleftFormat);
		$ColumnNum += 1;
		
		if ($SalesTTC == 1) {
			$worksheet->write($RowNum, $ColumnNum, number_format($DeliveryDtPriceTTC, $DecimalBase), $regularrightFormat);
			$ColumnNum += 1;
			$worksheet->write($RowNum, $ColumnNum, number_format($DeliveryDtTotalTTC, $DecimalBase), $regularrightFormat);
			$ColumnNum += 1;
		}	
		else {
			$worksheet->write($RowNum, $ColumnNum, number_format($DeliveryDtPrice, $DecimalBase), $regularrightFormat);
			$ColumnNum += 1;
			$worksheet->write($RowNum, $ColumnNum, number_format($DeliveryDtTotal, $DecimalBase), $regularrightFormat);
			$ColumnNum += 1;
		}
		
		 
		$worksheet->write($RowNum, $ColumnNum, number_format($TotalNetWeight, $DecimalBase), $regularrightFormat);
		/*$ColumnNum += 1;
		$worksheet->write($RowNum, $ColumnNum, number_format($ItemNetWeight, $DecimalBase), $regularrightFormat);*/
		$ColumnNum += 1;
		$worksheet->write($RowNum, $ColumnNum, number_format($TotalGrossWeight, $DecimalBase), $regularrightFormat);
		/*$ColumnNum += 1;
		$worksheet->write($RowNum, $ColumnNum, number_format($ItemGrossWeight, $DecimalBase), $regularrightFormat);*/
		$ColumnNum += 1;
		$worksheet->write($RowNum, $ColumnNum, number_format($TotalVolume, $DecimalBase), $regularrightFormat);
		/*$ColumnNum += 1;
		$worksheet->write($RowNum, $ColumnNum, number_format($ItemVolume, $DecimalBase), $regularrightFormat);*/
		/*$ColumnNum += 1;
		$worksheet->write($RowNum, $ColumnNum, number_format($TotalVolume, $DecimalBase), $regularrightFormat);
		$ColumnNum += 1;*/

			
		/*
			
		$worksheet->write($RowNum, $ColumnNum, $DeliveryDtExpireDate, $regularleftFormat);
		$ColumnNum += 1;
		$worksheet->write($RowNum, $ColumnNum, $DeliveryDtProductionDate, $regularleftFormat);
		$ColumnNum += 1;
		$worksheet->write($RowNum, $ColumnNum, $DeliveryDt_Remark, $regularleftFormat);
		$ColumnNum += 1;*/
		
				
		$RowNum  += 1;
		$ColumnNum = 0;	
	}
	
	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];
	
	
	$RowNum  += 1;
	$ColumnNum = 10;
	
	$worksheet->write($RowNum, $ColumnNum, 'TOTAL', $boldleftFormat);
	$ColumnNum += 1;
	$worksheet->write($RowNum, $ColumnNum, number_format($GrossTotal, $DecimalBase), $boldrightFormat);
	$ColumnNum += 1;
	
	$RowNum  += 2;
	$ColumnNum = 0;
		
	$worksheet->write($RowNum, $ColumnNum, "AMOUNT VAT @ $Setup_VatPercentage%: ".number_format($Delivery_VAT, $DecimalBase), $boldleftFormat);
	$ColumnNum += 1;
	
	$RowNum  += 1;
	$ColumnNum = 0;
	
	$worksheet->write($RowNum, $ColumnNum, "AMOUNT WITHOUT VAT: ".number_format($GrossTotal, $DecimalBase)." ".$CurrencyCode."   AMOUNT WITH VAT: ".number_format($Delivery_NetTotal, $DecimalBase)." ".$CurrencyCode, $boldleftFormat);
	$ColumnNum += 1;
	
	$RowNum  += 1;
	$ColumnNum = 0;
	
	$worksheet->write($RowNum, $ColumnNum, $TotalAmountLetter, $boldleftFormat);
	$ColumnNum += 1;
		 
		 
	$RowNum  += 2;
	$ColumnNum = 0;
		
	$worksheet->write($RowNum, $ColumnNum, "Total Price ($CurrencyCode)", $boldleftFormat);
	$ColumnNum += 1;
	$worksheet->write($RowNum, $ColumnNum, format_currency($GrossTotal, $DecimalBase), $boldrightFormat);
	$ColumnNum += 1;
	$worksheet->write($RowNum, $ColumnNum, $CurrencyCode, $boldcenterFormat);
	$ColumnNum += 1;
	
	$RowNum  += 1;
	$ColumnNum = 0;
		
	$worksheet->write($RowNum, $ColumnNum, "Total Volume", $boldleftFormat);
	$ColumnNum += 1;
	$worksheet->write($RowNum, $ColumnNum, format_currency($STotalVolume, $DecimalBase), $boldrightFormat);
	$ColumnNum += 1;
	$worksheet->write($RowNum, $ColumnNum, "m3", $boldcenterFormat);
	$ColumnNum += 1;
	
	$RowNum  += 1;
	$ColumnNum = 0;
		
	$worksheet->write($RowNum, $ColumnNum, "Total Gross Weight", $boldleftFormat);
	$ColumnNum += 1;
	$worksheet->write($RowNum, $ColumnNum, format_currency($STotalGrossWeight, $DecimalBase), $boldrightFormat);
	$ColumnNum += 1;
	$worksheet->write($RowNum, $ColumnNum, "KG", $boldcenterFormat);
	$ColumnNum += 1;
	
	$RowNum  += 1;
	$ColumnNum = 0;
		
	$worksheet->write($RowNum, $ColumnNum, "Total Net Weight", $boldleftFormat);
	$ColumnNum += 1;
	$worksheet->write($RowNum, $ColumnNum, format_currency($STotalNetWeight, $DecimalBase), $boldrightFormat);
	$ColumnNum += 1;
	$worksheet->write($RowNum, $ColumnNum, "KG", $boldcenterFormat);
	$ColumnNum += 1;

	$RowNum  += 1;
	$ColumnNum = 0;
	$worksheet->write($RowNum, $ColumnNum, "Total No of Boxes", $boldleftFormat);
	$ColumnNum += 1;
	$worksheet->write($RowNum, $ColumnNum, format_currency($STotalBoxes, $DecimalBase), $boldrightFormat);
	$ColumnNum += 1;
	$worksheet->write($RowNum, $ColumnNum, "", $boldcenterFormat);
	$ColumnNum += 1;

}



function format_currency($amount, $DecimalBase) { 

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

$workbook->close();
?>