<?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($_SESSION['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");


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

$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");

$query_setup  = "select company_name, company_memo, DecimalBase, Setup_VatPercentage, setup_invoiceremarks from setup";
$result_setup = mysql_query($query_setup, $connection); 
$row_setup    = mysql_fetch_array($result_setup);
extract($row_setup);
print <<<HERE
<html>
<head>
<title> DELIVERY REPORT</title>

<style>
@media print {
    .break {page-break-after: always;}
}
</style>


</head>
<body>
HERE;


// from here 

$i = 0;

$u = 0;


if ( !isset ( $_GET['SalesId'] ))
{

	 
	 $tags = explode(';' , $_SESSION['delivery_id_sess']);
	 $u =   count($tags) -1; 
	 $nxtsalesid = $tags[0];
	 
}
else
{	  
	$nxtsalesid = $_GET['SalesId'];
	$u  = 0;
}

 

/* remove comments then continue the code */

/* end remove comments and continue the code */
while( ($i<$u)   ||   ($i==0 && $u==0))
{
$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_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_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, Client_PaymentTerms,client_address3,client_discountonInvoice, Client_SellingPrice, clients.client_export as ClientExport ".
			 " , deliveries.user_account as myuser".
			 " 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 = $nxtsalesid";
			 
			// echo $query;
		 
		
//echo $query;exit;
	$result=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
	}
	
	 
echo "<table width=800>
<tr>
<td width=50%><img src='LogoFile.php' width=180 /></td>
<td width=50% style='text-align: center; font-family: Arial; font-size: 40px;  color: #a6a6a6;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Delivery Note</td>
</tr>
</table>
<table width=800>
<tr style='height: 20px'>
<td></td>
</tr>
</table>
<table width=800>
<tr>
<td width=500 style='font-family: Arial; font-size: 14px'><i><b><u>$company_name</u></b></i>
<br><b>
<pre>$company_memo</pre>
</td>
<td style='width: 300px; vertical-align: top;'>
<table width=100%>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>DATE:</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 14px'>$Delivery_Date</td>
</tr>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>Invoice #:</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 14px'>$Delivery_Reference</td>
</tr>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>Customer ID:</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 14px'>$ClientCode</td>
</tr>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>User :</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 14px'>$myuser</td>
</tr>
</table>
</td>
</tr>
<tr style='height: 15px'>
<td></td>
</tr>
</table>
<table width=800>
<tr>
<td style='font-weight: bold; font-size: 14px; font-family: Arial'>Bill To:</td>
</tr>
</table>
<table width=800>
<tr>
<td style='font-size: 14px; font-family: Arial' width=55>Name</td>
<td style='font-size: 14px; font-family: Arial' width=745>: $ClientName</td>
</tr>
<tr>
<td style='font-size: 14px; font-family: Arial' width=55>Address</td>
<td style='font-size: 14px; font-family: Arial' width=745>: $ClientAddress</td>
</tr>
<tr>
<td style='font-size: 14px; font-family: Arial' width=55>Phone</td>
<td style='font-size: 14px; font-family: Arial' width=745>: $Client_Telephone1 $Client_Telephone2</td>
</tr>";
if($Client_VAT == 1){
echo"<tr>
<td style='font-size: 14px; font-family: Arial' width=55>VAT #</td>
<td style='font-size: 14px; font-family: Arial' width=745>: $Client_VATNo</td>
</tr>";
}
echo "<tr style='height: 15px'>
<td colspan=2></td>
</tr>";
echo "<tr>
<td colspan=2 style='font-size: 14px; font-family: Arial; font-weight: bold'>Comments or special instructions:</td>
</tr>";
echo "<tr style='height: 15px'>
<td colspan=2></td>
</tr>";
echo"</table>";
echo "<table width=800 style='border-collapse:collapse'>";
echo "<tr >";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 80px; background: #d0d0d0'>Code</th>";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 450px; background: #d0d0d0 '>Description</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 10px; background: #d0d0d0 '>Qty</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 200px; background: #d0d0d0 '>Remark</th>";
	//echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 76px; background: #d0d0d0 '>Disc.</th>";
//	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 100px; background: #d0d0d0 '>Total Price ".$CurrencyCode."</th>";
	echo "</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, ".
			   " DeliveryDt_DiscountPer As DeliveryDtDiscountPer,  ".
			   " Round(((DeliveryDt_Quantity*DeliveryDt_Price)-((DeliveryDt_Quantity*DeliveryDt_Price*ifNull(deliverydt_itemdiscount,0))/100)), ".$DecimalBase.") As DeliveryDtTotal, ".
			   " Round(DeliveryDt_PriceTTC, ".$DecimalBase.") As DeliveryDtPriceTTC, ".
			   " Round(((DeliveryDt_Quantity*DeliveryDt_PriceTTC)-((DeliveryDt_Quantity*DeliveryDt_PriceTTC*ifNull(deliverydt_itemdiscount,0))/100)), ".$DecimalBase.") As DeliveryDtTotalTTC, ".
			   " (DeliveryDt_Quantity*ifNull(Items.Item_Weight,0)) As GrossWeight,ifNull(deliverydt_clientdiscount,0) deliverydt_clientdiscount,ifNull(deliverydt_itemdiscount,0) deliverydt_itemdiscount, Items.Item_Weight ".
				
			   " From DeliveryDt Left Join Items on DeliveryDt.Item_code = Items.Item_code ".
			   " Where DeliveryDt.Delivery_ID = $nxtsalesid".
			   " Order By DeliveryDt.DeliveryDt_id Asc  ";
	$resultdt = mysql_query($querydt, $connection);
	$total_beforediscount=0;
	$total_discountamount=0;
	$total_discountamount_oninvoice=0;
	$totalqty=0;
	while($rowdt = mysql_fetch_array($resultdt)){
		extract($rowdt);
		$total_beforediscount = $total_beforediscount + ($DeliveryDtQuantity*$DeliveryDtPrice);
		$total_discountamount = $total_discountamount + ($DeliveryDtQuantity*$DeliveryDtPrice * $deliverydt_itemdiscount / 100);
		$total_discountamount_oninvoice = $total_discountamount_oninvoice + ($DeliveryDtQuantity*$DeliveryDtPrice * $deliverydt_clientdiscount / 100);
		
		echo "<tr >";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 76px; vertical-align: top;font-weight:100; '>$ItemCode</th>";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 285px; vertical-align: top;font-weight:100;'>$ItemDescription</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 50px; vertical-align: middle;font-weight:100; '>".number_format($DeliveryDtQuantity, $DecimalBase)."</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 100px; vertical-align: middle; text-align: right;font-weight:100;'>$Delivery_remark</th>";
//	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 76px; vertical-align: middle; text-align: right;font-weight:100;'>$DeliveryDtDiscountPer %</th>";
//	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 100px; vertical-align: middle; text-align: right;font-weight:100;'>".number_format($DeliveryDtTotal, $DecimalBase)."</th>";
	echo "</tr>";
	$totalqty=$totalqty+$DeliveryDtQuantity;
	}
	//echo $total_beforediscount;
	$total_afterdiscount=$total_beforediscount-$total_discountamount-$total_discountamount_oninvoice;
	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>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 100px; vertical-align: middle; text-align: right;  border-top: none;border-bottom: none;background: #d0d0d0;'></td>
	<td colspan=1 style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 76px; background: #d0d0d0; text-align: left; border-bottom: none; font-weight: bold'>Total Qty</td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 100px; vertical-align: middle; border-top: none;border-bottom: background: #d0d0d0; none;text-align:center;font-weight: bold;'>".number_format($totalqty,$DecimalBase)."</td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 100px; vertical-align: middle; text-align: right;  border-top: none;border-bottom: none;background: #d0d0d0;'></td>
	
	</tr>
	
	<tr>
	<td colspan=4 style='border: solid 1px #000000; font-family: Arial; font-size: 14px; padding: 5px; width: 76px; background: #d0d0d0; text-align: left; font-weight: bold'></td>
	
	</tr>
	<tr style='height: 35px'>
	<th colspan=6></th>
	</tr>
	</tr>
	<tr>
	<td colspan=6 style='font-size: 14px; font-weight: none; font-family: Arial'>$setup_invoiceremarks</td>
	</tr>
	<tr style='height: 20px'>
	<td colspan='6'><b>Inspected By:</b></td>
	</tr>
	<tr style='height: 20px'>
	</tr>
	<tr style='height: 20px'>
	<td colspan='3' style='text-align:left;'>Manager's Sign:--------------------------------</td>
	<td colspan='3' style='text-align:left;'>Customer's Sign:--------------------------------</td>
	</tr>
	<tr style='height: 45px'>
	<th colspan=6></th>
	</tr>
	<tr>
	<th colspan=6 style='font-size: 14px; font-family: Arial'>THANK YOU FOR YOUR BUSINESS!</th>
	</tr>
	";
echo"</table>";


if ( !isset ( $_GET['SalesId'] ))
{
	echo "<div  class = 'break'></div>";
}
$i++;
 
  
 $nxtsalesid = $tags[$i];
}
function cleanNumber($num){
    $explode = explode('.', $num);
    $count   = strlen(rtrim($explode[1],'0'));
    return bcmul("$num",'1', $count);
}
// to here

print <<<HERE
<script>
window.print();
</script>
</body>
</html>
HERE;
?>