<?php

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

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

if($PExportExcel ==1){
}

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 Invoice Report </TITLE>

<style type="text/css">
.styleh {
	font-size: 16px;
    font-weight: bold;
}

.styled {
	font-size: 16px;
}			 

.styledd {
	font-size: 12px;
}
</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);
}

$SalesId  = $_GET['SalesId'];



if( $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 As ClientVATNO, client_financialno AS ClientFINANCIALNO, Deliveries.Currency_code As CurrencyCode, Client_Address, Delivery_ShippedTo, Delivery_Marks, Delivery_ShippedVia, ".
			 " Deliveries.Delivery_Remark AS DeliveryRemark, 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, ifNull(Delivery_VatChkbx,0) Delivery_VatChkbx  ".
			 " 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 = $SalesId ;";

	$result=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
	}
	$param = array();
	$param['date'] = $Delivery_Date;
	$param['reference'] = $Delivery_Reference;
	$param['currency'] = $CurrencyCode;
	$param['ClientName'] = $ClientName;
	$param['ClientCode'] = $ClientCode;
	$param['ClientAddress'] = $ClientAddress;
	$param['Client_Telephone1'] = $Client_Telephone1;
	
	headerPrint($param, $connection);
	
	//Details
	echo "<table width='800px' cellpadding=0 cellspacing=0><col width='500px'><col width='300px'>";
	
	echo "</table>";
	echo "<div style='page-break-after: always;'></div>";
	
	//Second page
	headerPrint($param, $connection);
	

}


function headerPrint($param, $connection){
	$btop = "border-top: 1px solid black;";
	$bbottom = "border-bottom: 1px solid black;";
	$bright = "border-right: 1px solid black;";
	$bleft = "border-left: 1px solid black;";
	
	$Delivery_Date 		= $param['date'];
	$Delivery_Reference = $param['reference'];
	$CurrencyCode 		= $param['currency'];
	$ClientName 		= $param['ClientName'];
	$ClientCode 		= $param['ClientCode'];
	$ClientAddress 		= $param['ClientAddress'];
	$Client_Telephone1 	= $param['Client_Telephone1'];
	
	echo "<table width='800px' cellpadding=0 cellspacing=0><col width='500px'><col width='300px'>";
	echo "<tr>
			<td align='left' valign='top'><table width='300px' cellpadding=0 cellspacing=0><col width='500px'>
				<tr><td style='font-size: 26px;font-family: sans-serif;'>GEORGES HOBEIKA</td></tr>
				<tr><td style='padding-left: 90px;font-size: 12px;font-family: sans-serif;'>ATELIER S.A.L.</td></tr>
				<tr HEIGHT='20px'><td ></td></tr>
				<tr><td style='font-size: 12px;font-family: sans-serif;'>FISCAL NUMBER: 1293002</td></tr>
				<tr><td style='font-size: 12px;font-family: sans-serif;'>RC: 1134</td></tr>
			</table></td>";
	echo "  <td><table width='300px' cellpadding=0 cellspacing=0><col width='150px'><col width='20px'><col width='130px'>";
	echo "		<tr><td style='$btop;$bleft;padding: 5px 0px 0 10px;'><span class='styleh'>Date</span></td><td  style='$btop;'><span class='styleh'>:</span></td><td  style='$btop;$bright;'><span class='styled'>$Delivery_Date</span></td></tr>";
	echo "		<tr><td style='$bleft;padding: 0 0 0 10px;'><span class='styleh'>Invoice #</span></td><td><span class='styleh'>:</span></td><td style='$bright;'><span class='styled'>$Delivery_Reference</span></td></tr>";
	echo "		<tr><td style='$bleft;padding: 0 0 0 10px;'><span class='styleh'>Currency</span></td><td><span class='styleh'>:</span></td><td style='$bright;'><span class='styled'>$CurrencyCode</span></td></tr>";
	echo "		<tr><td style='$bleft;padding: 0 0 0 10px;'><span class='styleh'>Type</span></td><td><span class='styleh'>:</span></td><td style='$bright;'><span class='styled'>G</span></td></tr>";
	echo "		<tr><td style='$bbottom;$bleft;padding: 0 0 5px 10px;'><span class='styleh'>Shop / Whse</span></td><td style='$bbottom;'><span class='styleh'>:</span></td><td style='$bbottom;$bright;'><span class='styled'>000</span></td></tr>";
	echo "  </table></td>";	
	echo "</tr>";
	echo "<tr height='20px'><td></td></tr>";
	echo "</table>";
	
	//client header
	echo "<table width='800px' cellpadding=0 cellspacing=0><col width='150px'><col width='20px'><col width='280px'><col width='350px'>";
	echo "<tr><td style='$btop;$bleft;padding: 5px 0px 0 10px;'><span class='styleh'>Client Name</span></td><td style='$btop;'><span class='styleh'>:</span></td><td style='$btop;$bright;'><span class='styled'>$ClientName</span></td><td></td></tr>";
	echo "<tr><td style='$bleft;padding: 0 0 0 10px;'><span class='styleh'>Client #</span></td><td ><span class='styleh'>:</span></td><td style='$bright;'><span class='styled'>$ClientCode</span></td><td></td></tr>";
	echo "<tr height='100px'><td colspan='3' align='left' valign='top' style='$bbottom;$bleft;$bright; padding: 0 0 5px 10px;'><span class='styledd'>$ClientAddress";
	if($Client_Telephone1 != '') echo " Telephone: $Client_Telephone1";
	echo "	</span></td><td></td></tr>";
	echo "</table>";
	
}

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

