<?php
session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
//header('Content-Type: text/html; charset=UTF-8');
//echo $_SESSION['encodingmode'];
if($_SESSION['dbName']==""){
header("location:../index.php");
}
include("config.inc.php");
//include("Functions_Trans.php");
echo "<title>Invoice Report</title>";
echo "<style>
body,html{width:99.8%; height:99%;  }


h4 { border-top-style: double; 
	 border-top-color: black; 
	 border-top-width: 1px;
   }
#container {
   min-height:99%; 
   position:relative;
}

#footer{
 position:fixed;
 bottom:0;
 left:0;
}

#footertd{
	position:absolute;
 bottom:0;
 left:0;
}
#footer #footerdiv{
	bottom:0;
	position relative;
	//border:solid 1px black;
}
#header{
	 position:fixed;
top:0;

 left:0;
 border:solid 1px black;
}
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($_SESSION['dbName'],$connection) or die("Could not find DB");

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}


$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'];

$result_delivery = mysql_query($query_delivery, $connection); 
$row_delivery    = mysql_fetch_array($result_delivery);
extract($row_delivery);
//echo $Delivery_Remark;
//echo mb_detect_encoding ( $Delivery_Remark )."dgd";
$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 id='container'>";
/*echo "<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>
";*/
$now=current_server_time($connection);
$dt=split(" ",$now[0]);
$printdate=split("-",$dt[0]);
//echo "<table><tr ><td><div id='header'><table><tr><td>fgd</td><td>fdfsdgd</td><td>fgdzs</td></tr></table></div><td></tr>";
//echo "</table>";

echo "<table border=0 width=100%><col width=200><col width=300><col width=200><col width=300>
<tr>
<td rowspan=3><img src='LogoFile.php' width='200px' height='80px' /></td>
<td ><table border=0><tr><td align=left><i>  $CompanyName</i></td></tr>
			<tr height=20>&nbsp;<td></td></tr>
			<tr>&nbsp;<td align=left><i> $CompanyAddress</i></td></tr>
			</table></td>
<td ></td>
<td ><table><tr><td><i>Date &nbsp;&nbsp;&nbsp;". $printdate[2]."/".$printdate[1]."/".$printdate[0] .
								"&nbsp;&nbsp;&nbsp; Time&nbsp;&nbsp;&nbsp;".$dt[1]." ".$dt[2];
						echo"</i></td></tr>
			<tr><td><i>Invoice nb</i></td></tr>
			<tr><td><i>File number</i></td></tr>
			</table></td>
</tr>
<tr height=40px></tr>
</table>";
//echo "<table><tr height=180></tr></table>";
echo "<table width=100% >

<tr>
<td colspan='8' style='text-align: center; font-size: 22px; font-weight: bold; padding-bottom: 10px; font-family: Times New Roman'>Invoice No. ".$Delivery_Reference."</td>
</tr>
<tr height='20'><td colspan=8><h4></h4></td></tr>
<tr>
<td colspan='8' style=''>
<table border=0 style='border-collapse: collapse' width=100%>
<tr height=10></tr>
<tr>
<td style='font-size: 14px; font-weight: bold; background-color: #a9abac; width: 100px; vertical-align: top'>Customer</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'>Carrier</td>
<td style='font-size: 14px; width: 100px; vertical-align: top'></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='4'>".$ClientAddress."</td>
<td style='width: 100px'></td>

<td style='font-size: 14px; font-weight: bold; background-color: #a9abac; width: 100px; vertical-align: top'>Bill of Lading</td>
<td style='width: 100px; vertical-align: top'></td>
</tr>


<tr><td style='width: 100px'></td>
<td style='width: 100px'></td><td style='width: 100px'></td>
<td style='width: 100px'></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'>Reference</td>
<td style='font-size: 14px; width: 100px; font-size: 14px; font-family: Times New Roman; vertical-align: top' colspan='7'></td>
</tr>
<tr height='3'><td colspan=8><h4></h4></td></tr>
</table>
</td>
</tr>
<tr><td><table width=100% border=0><tr>
<th colspan='2' style='font-size: 14px; font-family: Times New Roman;   width: 100px; '>Code</th>
<th colspan='2' style='font-size: 14px; font-family: Times New Roman;  width: 250px;'>Description</th>
<th colspan='1' style='font-size: 14px; font-family: Times New Roman;   width: 100px; '>Qty</th>
<th colspan='1' style='font-size: 14px; font-family: Times New Roman;  width: 100px; '>Unit Price</th>

<th colspan='1' style='font-size: 14px; font-family: Times New Roman;   width: 100px; '>Total Price</th>
<th colspan='1' style='font-size: 14px; font-family: Times New Roman;   width: 100px; '>Currency</th>
</tr>";
echo "<tr height='3'><td colspan=8><h4></h4></td></tr>";
$query_deliverydt = " 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, ".
			   " Round(DeliveryDt_DiscountPer, ".$DecimalBase.") As DeliveryDtDiscountPer,  ".
			   " Round(((DeliveryDt_Quantity*DeliveryDt_Price)-((DeliveryDt_Quantity*DeliveryDt_Price*ifNull(DeliveryDt_DiscountPer,0))/100)), ".$DecimalBase.") As DeliveryDtTotal, ".
			   " Round(DeliveryDt_PriceTTC, ".$DecimalBase.") As DeliveryDtPriceTTC, ".
			   " Round(((DeliveryDt_Quantity*DeliveryDt_PriceTTC)-((DeliveryDt_Quantity*DeliveryDt_PriceTTC*ifNull(DeliveryDt_DiscountPer,0))/100)), ".$DecimalBase.") As DeliveryDtTotalTTC, ".
			   " (DeliveryDt_Quantity*ifNull(Items.Item_Weight,0)) As GrossWeight, DeliveryDt_Remark,DeliveryDt.currency_code ". 
			   " From DeliveryDt Left Join Items on DeliveryDt.Item_code = Items.Item_code ".
			   " Where DeliveryDt.Delivery_ID = ".$_GET['SalesId'].
			   " Order By DeliveryDt.DeliveryDt_id Asc  ";


$result_deliverydt = mysql_query($query_deliverydt, $connection);
//echo $query_deliverydt;
/*$nbline=20;$page=0;
$i=0;
$j=0;*/
//while(($row_deliverydt = mysql_fetch_array($result_deliverydt))||($j<=50))
	while($row_deliverydt = mysql_fetch_array($result_deliverydt))
{
	extract($row_deliverydt);
	$totalX = $deliverydt_price * $deliverydt_quantity;
	$discountX = (($totalX - $total) * 100) / $totalX;
	echo "<tr height='30'>
<td colspan='2' align='center' style='font-size: 14px; font-family: Times New Roman width: 100px;'>".$ItemCode."</td>
<td colspan='2' align='left' style='font-size: 14px; font-family: Times New Roman width: 300px;'>&nbsp;&nbsp;&nbsp;".$ItemDescription."</td>
<td colspan='1' align='center'  style='font-size: 14px; font-family: Times New Roman width: 50px; '>".$DeliveryDtQuantity."</td>
<td colspan='1' align='center'  style='font-size: 14px; font-family: Times New Roman width: 100px; '>".number_format($DeliveryDtPrice, $DecimalBase)."</td>

<td colspan='1' align='center'  style='font-size: 14px; font-family: Times New Roman width: 100px; '>".number_format($DeliveryDtTotal, $DecimalBase)."</td>
<td colspan='1' align='center'  style='font-size: 14px; font-family: Times New Roman width: 100px;'>".$currency_code."</td>

</tr>";
if ($DeliveryDt_Remark!='')
echo "<tr height='30'><td align='right' colspan=2><b>Remark:</b></td><td colspan=6 align=left>&nbsp;&nbsp;&nbsp;$DeliveryDt_Remark  ad</td></tr>";
/*if ($page!=0){$nbline=25;}
	
	if ($i==$nbline){
	$page++;
	$i=0;*/
	//echo "<div id='footertd'><tr><td><table><tr><td >footer</td></table></td></tr></div>";
	//echo "<tr><td>&nbsp;<p style='page-break-before: always'></p>
//</td></tr>";
//echo "<tr height=40><td></td></tr>";
////////////////////////////////header////////////////////////////////
//echo "<tr><td><table><tr><td><br><br><br><br>header</td></tr></table></td></tr>";
////////////////////////////////header////////////////////////////////
//}
//else echo "<tr><td>dfg</td></tr>";
$j++;
$i++;
}
////////////////////////////////footer////////////////////////////////
//echo "<tr><td><div id='footer'><div id='footerdiv'><table><tr><td>ff</td><td>ff</td><td>ff</td><td>ff</td><td>ff</td></tr>
//</table></div></div></td></tr>";
////////////////////////////////footer////////////////////////////////
$vatAmount = (($Setup_vatpercentage/100)*$Delivery_NetTotal);
$beforeAmount = $Delivery_NetTotal - $vatAmount;
echo "<tr height='60'></tr>
<tr><td colspan='6' align='right'>Total</td><td colspan='1' style=' border-top: none; font-size: 14px; text-align: right; width: 200px; font-family: Times New Roman'>".number_format($GrossTotal, $DecimalBase)."</td>
 </tr>
<tr><td colspan='6' align='right'>Vat";
$query_setup  = "select Setup_vatpercentage from setup";
$result_setup = mysql_query($query_setup, $connection);
$row_setup    = mysql_fetch_array($result_setup);

extract($row_setup); 
echo $Setup_vatpercentage." %</td> <td  style=' border-top: none; font-size: 14px; text-align: right; width: 200px; font-family: Times New Roman'>$Delivery_VAT</td></tr>
<tr><td colspan='6' align='right'>total due</td><td colspan='1' style=';font-size: 14px; text-align: right; width: 200px; font-family: Times New Roman'>".number_format($Delivery_NetTotal, $DecimalBase)."</td>";
echo "</tr>

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


function current_server_time($connection){

  $string="Select date_format(NOW(),'%Y-%m-%d %h:%i %p') ";
  $result=mysql_query($string,$connection) or die(mysql_error());
  $now=mysql_fetch_row($result);

  return $now;
}

print <<< HERE

<script language=javascript>
 
if (typeof(window.print) != 'undefined')
{
    window.print();
} 
</script>
HERE;
echo "</div></div>";
echo "</BODY>
</HTML>";


?>