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

echo "<title>Invoice 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;}

				 
.styleh {
	font-size: 15px;
    font-family: sans-serif;
    font-weight: bold;
}
.styleh2 {
	font-size: 16px;
    font-family: sans-serif;
    font-weight: 100;
}
.styled {
	font-size: 14px;
    font-family: sans-serif;
}
.sig {
    font-family: monospace;
    font-size: 15px;
    color: #383d42;
}
.footer {
	font-size: 10px;
    font-family: sans-serif;
    font-weight: bold;
}
</style>
<body>";
$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, Company_Name, base1, base2 from setup";
$result_setup = mysql_query($query_setup, $connection);
$row_setup = mysql_fetch_array($result_setup);
extract($row_setup);

if($SalesId != ''){
	echo "<table border='0' width='700px'>";
	echo "<tr><td><span style='font-family: sans-serif;font-size: 30px;'>".strtoupper($Company_Name)."</span></td></tr>";
	echo "<tr height='150px'><td></td></tr>";
	echo "</table>";
	
	$query = "select date_format(delivery_date,'%Y.%m.%d'), ledger_name, Delivery_Reference, deliveries.currency_code
				from deliveries
				inner join clients on clients.ledger_number = client_code
				where delivery_id=$SalesId;";
	$res = mysql_query($query,$connection);
	$rw = mysql_fetch_row($res);
	$Delivery_Date = $rw[0];
	$ledger_name = $rw[1];
	$Delivery_Reference = $rw[2];
	$currency_code = $rw[3];
	
	echo "<table border='0' width='700px'><col width='120px'><col width='10px'><col width='200px'><col width='370px'>";
	echo "<tr height='20px'>
				<td ><span class='styleh'>Date</span></td>
				<td ><span class='styleh'>:</span></td>
				<td ><span class='styleh'>$Delivery_Date</span></td>
				<td ><span class='styleh'></span></td>
		  </tr>";
		  
	echo "<tr height='20px'>
				<td ><span class='styleh'>Client Name</span></td>
				<td ><span class='styleh'>:</span></td>
				<td colspan='2'><span class='styleh'>$ledger_name</span></td>
		</tr>";
	echo "<tr height='20px'>
				<td ><span class='styleh'>Invoice #</span></td>
				<td ><span class='styleh'>:</span></td>
				<td ><span class='styleh'>$Delivery_Reference</span></td>
				<td ><span class='styleh'>Adr: &nbsp; Tel:</span></td>
		  </tr>";	
	echo "<tr height='30px'><td colspan='4'></td></tr>";		  
	echo "</table>";
	
	echo "<table border='0' width='700px'><col width='400px'><col width='40px'><col width='130px'><col width='130px'>";
	$query_deliverydt = " Select DeliveryDt.DeliveryDt_id, DeliveryDt.Item_code as ItemCode, Item_Description As ItemDescription, ifNull(Items.Item_Unit,1) as ItemUnit, ".
			   " 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 ". 
			   " From DeliveryDt Left Join Items on DeliveryDt.Item_code = Items.Item_code ".
			   " Where DeliveryDt.Delivery_ID = $SalesId ".
			   " Order By DeliveryDt.DeliveryDt_id Asc  ";
	
	echo "<tr height='30px'>
				<td style='border-left: dotted 1.5px darkgrey; border-right: dotted 1.5px darkgrey; border-top: dotted 1.5px darkgrey; border-bottom: dotted 1.5px darkgrey; padding-left: 10px;'><span class='styleh2'>Description</span></td>
				<td style='border-right: dotted 1.5px darkgrey; border-top: dotted 1.5px darkgrey; border-bottom: dotted 1.5px darkgrey; ' align='center'><span class='styleh2'>Qty</span></td>
				<td style='border-right: dotted 1.5px darkgrey; border-top: dotted 1.5px darkgrey; border-bottom: dotted 1.5px darkgrey;' align='center'><span class='styleh2'>U.Price</span></td>
				<td style='border-right: dotted 1.5px darkgrey; border-top: dotted 1.5px darkgrey; border-bottom: dotted 1.5px darkgrey;' align='center'><span class='styleh2'>Total</span></td>
		  </tr>";
		  
	
	$result_deliverydt = mysql_query($query_deliverydt, $connection);
	$numr = mysql_num_rows($result_deliverydt);
	
	$totaldelivery = 0;
	while($row_deliverydt = mysql_fetch_array($result_deliverydt))
	{
		extract($row_deliverydt);
		
		echo "<tr height='30px'>
				<td style='border-left: dotted 1.5px darkgrey; border-right: dotted 1.5px darkgrey; padding-left: 10px;'><span class='styled'>$ItemCode $ItemDescription</span></td>
				<td style='border-right: dotted 1.5px darkgrey; ' align='center'><span class='styled'>$DeliveryDtQuantity</span></td>
				<td style='border-right: dotted 1.5px darkgrey; ' align='center'><span class='styled'>".number_format($DeliveryDtPrice,$DecimalBase)."</span></td>
				<td style='border-right: dotted 1.5px darkgrey; ' align='center'><span class='styled'>".number_format($DeliveryDtTotal,$DecimalBase)."</span></td>
		  </tr>";
		$totaldelivery = $totaldelivery + $DeliveryDtTotal;
	}	
	
	for($i=0; $i<(12 - $numr); $i++){
		echo "<tr height='30px'>
				<td style='border-left: dotted 1.5px darkgrey; border-right: dotted 1.5px darkgrey; padding-left: 10px;'></td>
				<td style='border-right: dotted 1.5px darkgrey; ' align='center'></td>
				<td style='border-right: dotted 1.5px darkgrey; ' align='center'></td>
				<td style='border-right: dotted 1.5px darkgrey; ' align='center'></td>
		  </tr>";
	}
	echo "<tr height='30px'>
				<td style='border-right: dotted 1.5px darkgrey; border-top: dotted 1.5px darkgrey; padding-left: 10px;'></td>
				<td style='border-top: dotted 1.5px darkgrey; padding-left: 10px;' colspan='2'><span class='styleh'>Sub Total $currency_code</span></td>
				<td style='border-right: dotted 1.5px darkgrey; border-top: dotted 1.5px darkgrey; padding-right: 10px;' align='right'><span class='styled'>".number_format($totaldelivery,$DecimalBase)."</span></td>
		  </tr>";
	
	$queryalpha  = " SELECT Alpha_NumLower ('ENGLISH', '".$currency_code."', ".$totaldelivery.") As AlphaAmount FROM Dual ";
	$resultalpha = mysql_query($queryalpha,$connection) or die(mysql_error());
	$rowalpha=mysql_fetch_row($resultalpha);	
	
	echo "<tr height='30px'>
				<td style='border-right: dotted 1.5px darkgrey; padding-left: 10px;'><span class='styled'>The Sum of $rowalpha[0]</span></td>
				<td style='padding-left: 10px;' colspan='2'><span class='styleh'>VAT 10%</span></td>
				<td style='border-right: dotted 1.5px darkgrey; padding-right: 10px;' align='right'><span class='styled'>".number_format(0,$DecimalBase)."</span></td>
		  </tr>";

	echo "<tr height='30px'>
				<td style='border-right: dotted 1.5px darkgrey; padding-left: 10px;'></td>
				<td style='padding-left: 10px;' colspan='2'><span class='styleh'>TTC $base1</span></td>
				<td style='border-right: dotted 1.5px darkgrey; padding-right: 10px;' align='right'><span class='styled'>".number_format(0,$DecimalBase)."</span></td>
		  </tr>";

	echo "<tr height='30px'>
				<td style='border-right: dotted 1.5px darkgrey; padding-left: 10px;'></td>
				<td style='border-bottom: dotted 1.5px darkgrey; padding-left: 10px;' colspan='2'><span class='styleh'>TTC $base2</span></td>
				<td style='border-right: dotted 1.5px darkgrey; border-bottom: dotted 1.5px darkgrey; padding-right: 10px;' align='right'><span class='styled'>".number_format(0,$DecimalBase)."</span></td>
		  </tr>";	  
	echo "</table>";
	
	echo "<div style='bottom:0; position: fixed;'>";
	echo "<table border=0 width='700px'><col width='250px'><col width='200px'><col width='250px'>";
	echo "<tr>
			<td><span class='footer'>
				Imm. Georges Hobeika <br>
				Blvd Pierre Amine Gemayel<br>
				Sin el Fil, Beyrouth. P.O. Box: 55410<br>
				Tel. +961 1 48 99 69<br>
				Fax. +961 1 51 10 51
			</span></td>
			<td><span class='footer'>
				Georges Hobeika<br>
				15 rue Royale<br>
				75008 Paris<br>
				Tel. +33 1 40 07 57 54<br>
				Fax. +33 1 40 07 01 86
			</span></td>
			<td><span class='footer'>
				info@georgeshobeika.com<br>
				www.georgeshobeika.com
			</span></td>
		</tr>	";
	echo "</table>";
	echo "</div>";
}

print <<< HERE

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

echo "</BODY>
</HTML>";

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

}
?>