<?php
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($HTTP_SESSION_VARS['dbName']==""){
header("location:../index.php");
}
$success = include "config.inc.php"; if (!$success) {include "../config.inc.php";}

echo "<title>Delivery Report</title>";
echo "<style>
body,html{width:99.8%; height:99%;  }
#container {
   min-height:99%; 
   position:relative;
}


a {outline:none ; text-decoration:none;}

area {outline:none}
p{padding:0px; margin:0px;}
</style>";
$connection   = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");

$SalesId      = $_GET['SalesId'];
$query_setup = "select DecimalBase, Setup_vatpercentage from setup";
$result_setup = mysql_query($query_setup, $connection);
$row_setup = mysql_fetch_array($result_setup);
extract($row_setup);
$query_delivery  = " 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_fax, ".
			 " Client_Address As ClientAddress, Client_Address2 As ClientAddressF, Clients.Ledger_nameF As ClientNameF, Client_Remark,   ".
			 " Client_VATNo, Deliveries.Currency_code As CurrencyCode, Client_Address, Delivery_ShippedTo, Delivery_Marks, Delivery_ShippedVia, ".
			 " Delivery_Remark, Deliveries.Project_Code As ProjectCode, Project_Name, Deliveries.Costcent_code As Costcentcode, Costcent_name,  ".
			 " Round(ifNull(Delivery_Taxable,0), ".$DecimalBase.") As DeliveryTaxable, Round(ifNull(Delivery_NoneTaxable,0), ".$DecimalBase.") As DeliveryNoneTaxable, ".
			 " Round(ifNull(Delivery_Total,0), ".$DecimalBase.") As GrossTotal, ".
			 " Round(ifNull(Delivery_DiscountPer,0), ".$DecimalBase.") As DeliveryDiscountPer, Round(ifNull(Delivery_DiscountAmount,0), ".$DecimalBase.") As DeliveryDiscountAmount,  ".
			 " Round(ifNull(Delivery_VAT,0), ".$DecimalBase.") As Delivery_VAT, Round(ifNull(Delivery_VATBase1,0), ".$DecimalBase.") As Delivery_VATBase1, ".
			 " Round((ifNull(Delivery_Total,0) + IfNull(Delivery_VAT,0) - IfNull(Delivery_DiscountAmount,0)), ".$DecimalBase.") As Delivery_NetTotal,  ".
			 " Round((ifNull(Delivery_TotalBase1,0) + IfNull(Delivery_VATBase1,0) - IfNull(Delivery_DiscountBase1,0)), ".$DecimalBase.") As Delivery_NetTotalBase1,  ".
			 " Round((ifNull(Delivery_Tax1,0) + ifNull(Delivery_Tax2,0) + ifNull(Delivery_Tax3,0) + ifNull(Delivery_Tax4,0) + ifNull(Delivery_Tax5,0)), ".$DecimalBase.") As Delivery_TotalTaxes,  ".
			 " Round(ifNull(Delivery_TransportAmount,0), ".$DecimalBase.") As DeliveryTransportAmt , Round(ifNull(Delivery_DiscountTTC,0), ".$DecimalBase.") As DeliveryDiscountTTC,   ".
			 " Round((ifNull(Delivery_TaxableTTC,0) + ifNull(Delivery_NoneTaxable,0) - ifNull(Delivery_TransportTTC,0) - ifNull(Delivery_BonusTTC,0) - IfNull(Delivery_DiscountTTC,0) + (ifNull(Delivery_Tax1,0) + ifNull(Delivery_Tax2,0) + ifNull(Delivery_Tax3,0) + ifNull(Delivery_Tax4,0) + ifNull(Delivery_Tax5,0))), ".$DecimalBase.") As Delivery_NetTotalTTC,  ".
			 " Deliveries.Salesman_Code As SalesmanCode, Deliveries.User_Account as UserAccount, Deliveries.Delivery_ManualNo as DeliveryManualNo, Client_Telephone2, ".
			 " ifNull(Delivery_CashInBase1,0) as DeliveryCashInBase1, ifNull(Delivery_CashInBase2,0) as DeliveryCashInBase2, Client_PaymentTerms,client_address3,client_discountonInvoice, Delivery_ShipName as DeliveryContact, Delivery_ShippingWay as CardMessage, date_format(Delivery_ClientDeliveryDate, '%d/%m/%Y') as ClientDeliveryDate, clients.client_financialno as ClientMOF, Delivery_ClientPhone as ClientContactPhone".
			 " From Deliveries ".
			 " Left Join Clients on Deliveries.Client_code = Clients.Ledger_Number ".
			 " Left Join Project on Deliveries.Project_Code = Project.Project_Code ".
			 " Left Join CostCent on Deliveries.Costcent_code = CostCent.CostCent_Code ".
	         " Where Delivery_ID = ".$_GET['SalesId'];
//echo $query_delivery;
$result_delivery = mysql_query($query_delivery, $connection); 
$row_delivery    = mysql_fetch_array($result_delivery);
extract($row_delivery);

$query_setup  = "select COMPANY_NAME as CompanyName, Company_memo as CompanyAddress, COMPANY_TELEPHONE as CompanyPhone, Setup_vatno as VATNO, base1 from setup";
$result_setup = mysql_query($query_setup, $connection);
$row_setup    = mysql_fetch_array($result_setup);
extract($row_setup);
$CompanyAddress = str_replace("\n", "<br>", $CompanyAddress);
echo "<div style='min-height: 100%; position: relative;'>
<table width=800>
<tr style='height: 40px'>
<td style='text-align: left; width: 300px; vertical-align: top' colspan='3'><img src='LogoFile.php' width='200px' height='80px' /></td>
<td colspan='2' style='width: 300px'></td>
<td style='font-size: 14px; font-family: Times New Roman; width: 200px; vertical-align: top; text-align: right' colspan='3'><span style='font-size: 18px; font-weight: bold; font-family: Times New Roman; padding-top: 10px'>".$CompanyName."</span><div style='height: 10px'></div>".$CompanyAddress."<br>
</td>
</tr>
</table>
";
echo "<table width=800 style='border-collapse: collapse;'>

<tr>
<td colspan='8' style='text-align: center; font-size: 22px; font-weight: bold; padding-bottom: 10px; border-bottom: solid 2px #000000; font-family: Times New Roman'>Delivery No. ".$Delivery_Reference."</td>
</tr>
<tr>
<td colspan='8' style='border-left: solid 2px #000000; border-right: solid 2px #000000; border-bottom: solid 2px #000000;'>
<table style='border-collapse: collapse'>
<tr>
<td style='font-size: 14px; font-weight: bold; background-color: #a9abac; width: 100px; vertical-align: top'>Client</td>
<td style='font-size: 14px; font-family: Times New Roman; width: 100px; vertical-align: top'>".$ClientCode."</td>
<td style='font-size: 14px; font-family: Times New Roman; width: 100px; vertical-align: top' colspan='2'>".$ClientName."</td>
<td style='width: 100px'></td>
<td style='width: 100px'></td>
<td style='font-size: 14px; font-weight: bold; background-color: #a9abac; width: 100px; vertical-align: top'>Date</td>
<td style='font-size: 14px; width: 100px; vertical-align: top'>".$Delivery_Date."</td>
</tr>
<tr>
<td style='font-size: 14px; font-weight: bold; background-color: #a9abac; width: 100px; vertical-align: top'>Address</td>
<td style='font-size: 14px; width: 100px; font-size: 14px; font-family: Times New Roman; vertical-align: top' colspan='3'>".$ClientAddress."</td>
<td style='width: 100px'></td>
<td style='width: 100px'></td>
<td style='font-size: 14px; font-weight: bold; background-color: #a9abac; width: 100px; vertical-align: top'>Currency</td>
<td style='width: 100px; vertical-align: top'>".$CurrencyCode."</td>
</tr>
<tr>
<td style='font-size: 14px; font-weight: bold; background-color: #a9abac; width: 100px; vertical-align: top'>Telephone</td>
<td style='font-size: 14px; width: 100px; font-size: 14px; font-family: Times New Roman; vertical-align: top' colspan='7'>".$Client_Telephone1."</td>
</tr>
<tr>
<td style='font-size: 14px; font-weight: bold; background-color: #a9abac; width: 100px; vertical-align: top'>MOF #</td>
<td style='font-size: 14px; width: 250px; font-size: 14px; font-family: Times New Roman; vertical-align: top' colspan='7'>".$ClientMOF."</td>
</tr>
<tr>
<td style='font-size: 14px; font-weight: bold; background-color: #a9abac; width: 100px; vertical-align: top'>Remark</td>
<td style='font-size: 14px; width: 100px; font-size: 14px; font-family: Times New Roman; vertical-align: top' colspan='7'>".$Delivery_Remark."</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan='8' style='border: solid 2px #000000'>
<table style='border-collapse: collapse'>
<tr>
<td style='font-size: 14px; font-weight: bold; background-color: #a9abac; width: 100px; vertical-align: top'>Contact Person</td>
<td style='font-size: 14px; font-family: Times New Roman; width: 200px; vertical-align: top' colspan='4'>".$DeliveryContact."</td>
<td style='width: 100px'></td>
<td style='width: 100px'></td>
<td style='font-size: 14px; font-weight: bold; background-color: #a9abac; width: 100px; vertical-align: top'>Delivery Date</td>
<td style='font-size: 14px; width: 100px; vertical-align: top'>".$ClientDeliveryDate."</td>
</tr>
<tr>
<td style='font-size: 14px; font-weight: bold; background-color: #a9abac; width: 100px; vertical-align: top'>Delivery Address</td>
<td style='font-size: 14px; width: 200px; font-size: 14px; font-family: Times New Roman; vertical-align: top' colspan='7'>".$Delivery_ShippedTo."</td>
</tr>
<tr>
<td style='font-size: 14px; font-weight: bold; background-color: #a9abac; width: 100px; vertical-align: top'>Message</td>
<td style='font-size: 14px; width: 200px; font-size: 14px; font-family: Times New Roman; vertical-align: top' colspan='7'>".$CardMessage."</td>
</tr>

</table>
</td>
</tr>
<tr>
<th colspan='2' style='font-size: 14px; font-family: Times New Roman; border-left: solid 2px #000000; border-bottom: solid 2px #000000; width: 100px; border-right: solid 0.5px #000000'>Code</th>
<th colspan='2' style='font-size: 14px; font-family: Times New Roman; border-bottom: solid 2px #000000; border-right: solid 0.5px #000000; width: 300px;'>Description</th>
<th colspan='1' style='font-size: 14px; font-family: Times New Roman; border-bottom: solid 2px #000000; border-right: solid 0.5px #000000; width: 50px; '>Qty</th>
</tr>";
$query_deliverydt = "select items.item_code,item_description,deliverydt_quantity,deliverydt_price,total, DiscountPer as Discount
from (
select deliverydt.item_code,deliverydt_remark,deliverydt_quantity, deliverydt.DeliveryDt_DiscountPer as DiscountPer,
deliverydt_price,(deliverydt_quantity*deliverydt_price*(1-deliverydt.DeliveryDt_DiscountPer/100)) total from deliverydt
where delivery_id='".$SalesId."' and deliverydt_remark not in (
  select t1.deliverydt_remark from (
select items.item_code,deliverydt_remark from items
inner join deliverydt on deliverydt.item_code=items.item_code and delivery_id='".$SalesId."'
where items.item_package = items.Item_Code) as t1
inner join deliverydt on deliverydt.deliverydt_remark=t1.deliverydt_remark
group by deliverydt_remark)
union
select t1.item_code,t1.deliverydt_remark,sum(deliverydt_quantity) deliverydt_quantity, deliverydt.DeliveryDt_DiscountPer as DiscountPer2,
round(sum(deliverydt_quantity*deliverydt_price*(1-deliverydt.DeliveryDt_DiscountPer/100))/sum(deliverydt_quantity),2) deliverydt_price,
sum(deliverydt_quantity*deliverydt_price*(1-deliverydt.DeliveryDt_DiscountPer/100)) total from (
select items.item_code,deliverydt_remark, deliverydt.DeliveryDt_DiscountPer from items
inner join deliverydt on deliverydt.item_code=items.item_code and delivery_id='".$SalesId."'
where items.item_package = items.Item_Code) as t1
inner join deliverydt on deliverydt.deliverydt_remark=t1.deliverydt_remark
group by item_code,deliverydt_remark) as deliverydt
inner join items on items.item_code=deliverydt.item_code";

//echo $query_deliverydt; exit;

$result_deliverydt = mysql_query($query_deliverydt, $connection);
while($row_deliverydt = mysql_fetch_array($result_deliverydt))
{
	extract($row_deliverydt);
	$totalX = $deliverydt_price * $deliverydt_quantity;
	$discountX = (($totalX - $total) * 100) / $totalX;
	echo "<tr>
<td colspan='2' style='font-size: 14px; font-family: Times New Roman width: 100px;'>".$item_code."</td>
<td colspan='2' style='font-size: 14px; font-family: Times New Roman width: 300px;'>".$item_description."</td>
<td colspan='1' style='font-size: 14px; font-family: Times New Roman width: 50px; text-align: right'>".$deliverydt_quantity."</td>
</tr>";
}
$vatAmount = (($Setup_vatpercentage/100)*$Delivery_NetTotal);
$beforeAmount = $Delivery_NetTotal - $vatAmount;
echo "

</table>";
echo "</td>
</tr>";
echo "</table>";

echo "<div style='position: absolute; bottom: 0px; width: 100%'>";
echo "<table width=800>";
echo "<tr>
<td colspan='4' style='font-size: 16px; font-weight: bold'>Date: ____________________________</td>
<td colspan='4' style='text-align: right; font-size: 16px; font-weight: bold'>Signature: ____________________________</td>
</tr>";
echo "</table></div></div>";

print <<< HERE

<script language=javascript>
 
if (typeof(window.print) != 'undefined')
{
    window.print();
} 
</script>
</BODY>
</HTML>
HERE;
function ConvertCurrency($FromCurr, $ToCurr, $FromDate, $Amount, $connection)
{
	$query = "SELECT ifNull(convertcurfunc('".$FromCurr."', '".$ToCurr."', '".$FromDate."', 0, 0, '', '', ifNull($Amount,0)),0) From Dual ";
	$result = selectData($connection, $query);
	//echo $query;
	Return	$result[0];

}
?>