<?php
//header("Content-type: text/html; charset=windows-1256");

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

if($exp ==1){
	$filename = "TransferWaybill_" . date('Ymd') . ".xls";
	header("Content-Disposition: attachment; filename=\"$filename\"");
	header("Content-Type: application/vnd.ms-excel");
} 
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> Delivery Report </TITLE>

<style type="text/css">

table.master th { font-size: 14px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                }				 
table.master td { font-size: 14px; 
                  font-family:Times-New-Roman;
                 }
table.detail th { font-size: 13px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
				  border-top:1px solid black;
				  border-bottom:1px solid black;
				  border-left:1px solid black;
                }				 
				 
table.detail td { font-size: 12px; 
                  font-family:Times-New-Roman;
				  border-left:1px solid black;
                 }				 
				 
						
table.masterAR th { font-size: 16px; 
					font-weight: bold;
					font-family:Times-New-Roman;
				  }				 
table.masterAR td { font-size: 16px; 
					font-family:Times-New-Roman;
                 }
table.detailAR th { font-size: 14px; 
					font-weight: bold;
					font-family:Times-New-Roman;
					border-top:1px solid black;
					border-bottom:1px solid black;
					border-left:1px solid black;
                }				 
				 
table.detailAR td { font-size: 14px; 
					font-family:Times-New-Roman;
					border-left:1px solid black;
					word-wrap: break-word; 
					word-break:break-all;
                 }				 

</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,'%d/%m/%Y') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage, ".
			 " Company_Name, Company_NameF, Company_Memo, Company_MemoF, Company_Capital, Company_CapitalF , COMPANY_TELEPHONE ".
			 " 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);
}

$Cambria='font-family:Cambria';	

$TotalQty = 0;

if (empty($_GET['PLang'])) $_GET['PLang'] = 'AR';

if ($_GET['PLang'] == 'AR') {
	$MasterClass='masterAR';
	$DetailClass='detailAR';	
} else {
	$MasterClass='master';
	$DetailClass='detail';	
}	

if($_GET['transfer_id'] != '')
{
			 
	$query = " SELECT TRANSFER_REFERENCE , date_format(transfer.TRANSFER_DATE,'%d/%m/%Y') as TRANSFER_DATE, Transfer_Remark , transfer.Project_code1 , transfer.Project_code2 , 
			transfer.Costcenter_Code, Driver_Name , Vehicle_No ,ToCostcenter_Code ,Document_RefNo,transferdt_warehousefrom,transferdt_warehouseto, 
			if((Transfer_Warehousefrom is null or Transfer_Warehousefrom = ''), TransferDt_WarehouseFrom, Transfer_WarehouseFrom) as TransferWarehouseFrom, 
			if((Transfer_WarehouseTo is null or Transfer_WarehouseTo = ''), TransferDt_WarehouseTo, Transfer_WarehouseTo) as TransferWarehouseTo,
			warehousefrom.Warehouse_Name as TransferWarehouseNameFrom, warehouseto.Warehouse_Name as TransferWarehouseNameTo , 
			clients.ledger_name as clientname , clients.client_telephone1 as clienttel , clients.client_email as clientemail
			from transfer
			left join clients on clients.warehouse_code =  transfer.transfer_warehouseto
			left join transferdt on transferdt.transfer_id = transfer.transfer_id
			left join warehouse as warehousefrom on warehousefrom.warehouse_code = if((Transfer_Warehousefrom is null or Transfer_Warehousefrom = ''), TransferDt_WarehouseFrom, Transfer_WarehouseFrom)
			left join warehouse as warehouseto on warehouseto.warehouse_code = if((Transfer_WarehouseTo is null or Transfer_WarehouseTo = ''), TransferDt_WarehouseTo, Transfer_WarehouseTo)
			where transfer.transfer_id = ".$transfer_id."
			limit 1";
	//echo $query;		 
	$result=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
	}
	
	
		
	echo "<table border=0 width=100% cellspacing=0 cellpadding=0>
		<tr><td align='left'>";
	
	echo "<table border='0' width='635' cellspacing=0 cellpadding=0 class='$MasterClass' style='table-layout:fixed; $Cambria' >
		  <col width=250>
		  <col width=160>
		  <col width=225>";
	echo "<tr height='220px;'>";
	echo "	<td align='center' valign='top' colspan='3'><img src='toolbar/moussa.png' width='1000px' /></td>";
	echo "</tr>";
	echo "<tr>
		  <td colspan='3'><span style='font-weight:bold; font-size:20px'> $Company_Name </span></td></tr>
		  <tr>
		  <td><span style='font-weight:bold;font-size:20px'>Fiscal Number: </span></td>
		  </tr>
		  <tr>
		  <td><span style='font-weight:bold;font-size:20px'>P.O. Box: </span> </td>
		  </tr>
		  <tr>
		  <td><span style='font-weight:bold;font-size:20px'>Tel.: $COMPANY_TELEPHONE </span> </td>
		  </tr>
		  <tr>
		  <td><span style='font-weight:bold;font-size:20px'>info@jabalmoussa.org </span> </td>
		  </tr>";

	

	echo "</table></td></tr>";
	
	echo"<tr height='50px'> 
		<td align='center' colspan='3'><span style='font-size:27px; font-weight:bold;$Cambria'> Delivery Note - $TRANSFER_REFERENCE </span></tr>
		</tr>
		<tr height='30px'>
		
		
		<td align='right'><span style='font-weight:bold;font-size:20px;$Cambria'> Date: $Setup_VatStartDate</span> </td>
		</tr>
		<tr>
		<td><span style='font-weight:bold; font-size:25px;$Cambria'> Delivered to: </span> </td>
		</tr>
		";
	$top = 'border-top:1px solid black';	
	$bottom = 'border-bottom:1px solid black';	
	$left = 'border-left:1px solid black';	
	$right = 'border-right:1px solid black';	

	
	echo "<tr><td align='left'>";
	echo "<table border='0' width=100% cellspacing='0'  style='table-layout:fixed; font-size:14; font-weight:300; $top; $bottom; $right; $left; font-size:22px;$Cambria'>";
		echo " <col width='200px'>
			   <col width='500px'>
		";
		echo "<tr><th align='left' >&nbsp;Company Name:</th> <td> $clientname  </td> </tr>";
		echo "<tr><th align='left'>&nbsp;Phone No:</th> <td> $clienttel</td></tr>";
		echo "<tr><th align='left'>&nbsp;Email:</th> <td> $clientemail </td></tr>";
		echo "<tr><th align='left' colspan='3'>&nbsp;Reference: Delivery Note - $TRANSFER_REFERENCE</th></tr>
		</table> </td> </tr>";
	
		

		
	

	
	if ($detail==0) {
	$querydt = "  select transferdt.Item_CodeFinished as Item_code, transferdt.Item_Barcode Item_Barcode, ifnull(Transferdt.Item_qtyPacked,0) as TransferDt_Quantity, TransferDt_PriceBase1 , Transferdt_remark , Item_description , transferdt_id,  count(*) as itemscount,
				TransferDt_WarehouseTo, Item_DescriptionF1 As ItemDescriptionF1 
				from transferdt , items
				where items.item_code = transferdt.Item_CodeFinished
				And transferdt.Item_CodeFinished Is Not Null
				and transfer_id = ".$transfer_id."
				group by Transferdt.Item_CodeFinished, Transferdt.TransferDt_WarehouseTo 
				Union
				select transferdt.Item_CodeSemiFinished Item_code, transferdt.Item_Barcode Item_Barcode, ifnull(Transferdt.Item_qtyPacked,0) as TransferDt_Quantity, TransferDt_PriceBase1 , Transferdt_remark , Item_description , transferdt_id , count(*) as itemscount,
				TransferDt_WarehouseTo, Item_DescriptionF1 As ItemDescriptionF1
				from transferdt , items
				where items.item_code = transferdt.Item_CodeSemiFinished
				And transferdt.Item_CodeSemiFinished Is Not Null
				and transfer_id = ".$transfer_id."
				group by Transferdt.Item_CodeSemiFinished, Transferdt.TransferDt_WarehouseTo 
				union
				select transferdt.Item_code Item_code, transferdt.Item_Barcode Item_Barcode, TransferDt_Quantity as TransferDt_Quantity, TransferDt_PriceBase1 , Transferdt_remark , Item_description , transferdt_id , 1 as itemscount,
				TransferDt_WarehouseTo, Item_DescriptionF1 As ItemDescriptionF1
				from transferdt , items
				where items.item_code = transferdt.item_code
				And transferdt.Item_CodeSemiFinished Is Null
				And transferdt.Item_CodeFinished Is Null
				and transfer_id = ".$transfer_id;
				
}
else {
	$querydt = " select transferdt.Item_code Item_code, transferdt.Item_Barcode Item_Barcode, TransferDt_Quantity as TransferDt_Quantity, TransferDt_PriceBase1 , Transferdt_remark , Item_description , transferdt_id,
			   TransferDt_WarehouseTo, Item_DescriptionF1 As ItemDescriptionF1 
			   from transferdt , items
			   where items.item_code = transferdt.item_code
			   and transfer_id = ".$transfer_id;
			  // echo $querydt; exit;
}
					   
		echo "<tr height='50px'> <td> </td> </tr>";
		
		echo "<tr><td align='left' style='position:relative; left:10px'>";
		echo" <table border='1' width='850' cellspacing='0'  style='table-layout:fixed; margin-top:5px; font-size:22px; '>
			  <col width=170>
			  <col width=170>
			  <col width=420>
			  <col width=90>
			  ";
		echo"<tr height='30px'>
			 <th> Item # </th>
			 <th> Bar Code / ISBN </th>
			 <th> Description </th>
			 <th> Quantity </th>
			 </tr>";
		
	
	$resultdt=mysql_query($querydt,$connection);
	while($rows=mysql_fetch_array($resultdt)){
		extract($rows);
		
		$TotalQty = $TotalQty + $TransferDt_Quantity;
		
		echo "<tr height='30px'>";
		
		
		echo "  <td align='center' valign='top'>$Item_code</td>";
		echo "	<td align='center' valign='top'> $Item_Barcode</td>";
		echo "  <td align='center' valign='top'>$Item_description</td>";
		
		
		echo "	<td align='center' valign='top'>".number_format($TransferDt_Quantity, $DecimalBase)."</td>";
		
		
			
		
			
		echo "</tr>";

	}
	

		/* echo "<tr height='25px;'>
				<td colspan='2' align='right' style='border-top:1px solid black; border-bottom:1px solid black;'>&nbsp;</td>
				<td align='right' style='border-top:1px solid black; border-bottom:1px solid black; font-size:14px; font-weight:bold;'>".number_format($TotalQty, $DecimalBase)."</td>
				<td colspan='3' align='right' style='border-top:1px solid black; border-bottom:1px solid black; border-right:1px solid black;'>&nbsp;</td>
			 </tr>"; */
	
	
	echo "</table></td></tr>";
	
	
	
		

		echo "<tr><td align='left'>";
		echo "<table border='0' width='635' cellspacing=0 cellpadding=1  style='table-layout:fixed; margin-top:20px;' >
			  <col width=190>
			  <col width=250>
			  <col width=180>
			  <col width=170>";
		
		echo "<tr height='70px'>";
		echo "	 <td align='right' style='font-size:25px; font-weight:bold; $Cambria;'>RECEIVED BY:</td>";
		echo "	 <td>&nbsp;</td>";
		echo "	 <td align='right' style='font-size:25px; font-weight:bold;$Cambria;'>DELIVERED BY:</td>";
		echo "</tr>";
		
		echo "<tr height='35px'>
			  <td align='right' style='font-size:20px; font-weight:bold; $Cambria;'> Name: </td>
			  <td> </td>
			  <td align='right' style='font-size:20px; font-weight:bold; $Cambria;'> Name: </td
			  </tr>";
			  
		echo "<tr height='35px'>
			  <td align='right' style='font-size:20px; font-weight:bold; $Cambria;'> Signature: </td>
			  <td> </td>
			  <td align='right' style='font-size:20px; font-weight:bold; $Cambria;'> Signature: </td
			  </tr>";
		echo "</table></td></tr>";
	
	echo "<tr height='100px'> <td valign='bottom' align='right' style='font-size:20px; font-weight:bold; $Cambria;'><i> Thank You! </i></td>";
	
	echo "<tr height='120px'> <td> </td> </tr></table>";	

	
	echo "<table border='0' width='635' cellspacing=0 cellpadding=0 class='$MasterClass' style='table-layout:fixed; $Cambria' >";
	echo "<tr height='220px;'>";
	echo "	<td align='center' valign='top' colspan='3'><img src='toolbar/moussa1.png' width='1000px'/></td>";
	echo "</tr>";
	
	
	echo "</table>";
	echo "<br>";
	

}



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

