<?php


session_start();
if($_SESSION['dbName']==""){
header("location:../index.php");
}
if($exp ==1){
	$filename = "GRN_" . date('Ymd') . ".xls";
	header("Content-Disposition: attachment; filename=\"$filename\"");
	header("Content-Type: application/vnd.ms-excel");
} 

print <<< HERE
<HTML>
<HEAD>
<TITLE> Goods Receive Note Report </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">

<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: 14px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                }				 
				 
table.detail td { font-size: 12px; 
                  font-family:Times-New-Roman;
                 }				 
				 
table.detail td.sr  { font-size: 10px; 
					  font-style: italic; 
                      font-family:Times-New-Roman;
                    }
				 
table.totals td.labels { font-size: 14px; 
                         font-weight: bold;
                         font-family:Times-New-Roman;
						 border-width: 1px;
						 border-style: none none solid solid;
  						 border-color: #666666;
                       }					  
table.totals td.curr { font-size: 14px; 
                       font-weight: bold;
					   font-style: italic;
                       font-family:Times-New-Roman;
					   border-width: 1px;
					   border-style: none none solid solid;
					   border-color: #666666;
                     }					   
table.totals td.amounts { font-size: 14px; 
                          font-family:Times-New-Roman;
						  border-width: 1px;
						  border-style: none none solid none;
  						  border-color: #666666;
				        }
table.totals td.totalborder {
								font-size: 14px; 
								font-family:Times-New-Roman;
								border-width: 1px;
								border-style: none solid solid solid;
								border-color: #666666;
							}						
h4 { border-top-style: double; 
	 border-top-color: black; 
	 border-top-width: 1px;
   }				 

</style>


</HEAD>

<BODY>
HERE;

include("config.inc.php");


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

//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2  from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_ShowLayoutProj,0) As Module_ShowLayoutProj, ifNull(Module_ShowLayoutCost,0) As Module_ShowLayoutCost, ifNull(module_locationGrn,0) As module_locationGrn ".
			 " From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

	
$now=current_server_time($connection);
$dt=split(" ",$now[0]);
$printdate=split("-",$dt[0]);

$TotalQty = 0;

if($PurchaseId != '')
{
	$query = " Select Purchase_ID, Purchase_Reference, Date_Format(Purchase_Date,'%d/%m/%Y') As Purchase_Date, ".
			 " Date_Format(Journaldt_valuedate,'%d/%m/%Y') As MaturityDate, GrnPurchases.Currency_code As CurrencyCode, ".
			 " Purchase_Remark, Purchase_InvoiceNo, GrnPurchases.Project_Code As ProjectCode, GrnPurchases.Costcent_code As CostcentCode,  ".
			 " GrnPurchases.Supplier_code As SupplierCode,	Suppliers.Ledger_name As SupplierName, Supplier_PaymentTerms,  ".
			 " Supplier_Representative As SupplierContact, Supplier_Address As SupplierAddress, Supplier_VAT, Supplier_VatNo As SupplierVatNo, ".
			 " Purchase_DebitAccount As PurchaseDebitAccount, ifNull(Purchase_Type, 'GRN') As Purchase_Type, Purchase_PackingListRef, Purchase_FormMNo  ".
			 " From GrnPurchases ".
			 " Left Join Suppliers on GrnPurchases.Supplier_code = Suppliers.Ledger_Number ".
			 " Where Purchase_ID = ".$PurchaseId;
			 
	$result=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
	}
	
	$Title = 'GRN No. '. $Purchase_Reference;
	if ($Purchase_Type == 'RM')  $Title = 'GRN-RM No. '.$Purchase_Reference;
	
	echo "<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td align='left'>";
	echo "<table border=0 width='635' cellspacing=2 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=115>
		  <col width=250>
		  <col width=80>
		  <col width=195>";
	echo "<tr><td height='60'><img src='LogoFile.php' width='115' height='50' /></td><td colspan=2 align='center'><b><font size=5><u>$Title</u></font></td><td align='right' ><font size='2'><b>Printed&nbsp;Date&nbsp;&amp;&nbsp;Time:</b>&nbsp;&nbsp;";
	echo $printdate[2]."/".$printdate[1]."/".$printdate[0];
	echo "<br>".$dt[1]." ".$dt[2];
	echo "</font></td></tr>";
	
	//echo "<tr height='10'><td colspan=4><hr></td></tr>";
	echo "<tr height='5'><td colspan=4><h4></td></tr>";
	echo "<tr>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Supplier</th>";
	echo "  <td align='left'>$SupplierCode &nbsp;&nbsp;$SupplierName</td>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Date</th>";
	echo "  <td align='left'>$Purchase_Date</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Payment Terms</th>";
	echo "  <td align='left'>$Supplier_PaymentTerms</td>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Invoice No.</th>";
	echo "  <td align='left' >$Purchase_InvoiceNo</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Contact</th>";
	
	if ($Purchase_Type == 'RM') {
		echo "  <td align='left' >$SupplierContact</td>";
		echo "  <th align='left' bgcolor='#D8D8D8'>Form M No.</th>";
		echo "  <td align='left' >$Purchase_FormMNo</td>";
	} else
		echo "  <td align='left' colspan='3'>$SupplierContact</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Address</th>";
	
	if ($Purchase_Type == 'RM') {
		echo "  <td align='left' >$SupplierAddress</td>";
		echo "  <th align='left' bgcolor='#D8D8D8'>Pack Ref.</th>";
		echo "  <td align='left' >$Purchase_PackingListRef</td>";
	} else
		echo "  <td align='left' colspan='3'>$SupplierAddress</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "  <th align='left' valign='top' bgcolor='#D8D8D8'>Remark</th>";
	echo "  <td align='left' colspan='3'>$Purchase_Remark</td>";
	echo "</tr>";
	
	echo "<tr>";
	
	if ($Module_ShowLayoutProj	== 1) {
		echo "  <th align='left' bgcolor='#D8D8D8'>Project</th>";
		echo "  <td align='left' >$ProjectCode</td>";
	} else {
		echo "  <td align='left' colspan='2'>&nbsp;</td>";
	}
	
	echo "  <th align='left' bgcolor='#D8D8D8'>Maturity</th>";
	echo "  <td align='left' >$MaturityDate</td>";
	echo "</tr>";
	
	echo "<tr>";
	
	if ($Module_ShowLayoutCost == 1) {	
		echo "  <th align='left' bgcolor='#D8D8D8'>Costcenter</th>";
		echo "  <td align='left' >$CostcentCode</td>";
	} else {
		echo "  <td align='left' colspan='2'>&nbsp;</td>";
	}
	
	echo "  <th align='left' bgcolor='#D8D8D8'>VAT No.</th>";
	echo "  <td align='left' >$SupplierVatNo</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Account Payable</th>";
	echo "  <td align='left' colspan='3'>$PurchaseDebitAccount</td>";
	echo "</tr>";
	
	//echo "<tr height='10'><td colspan=4><hr></td></tr>";
	echo "<tr height='5'><td colspan=4><h4></td></tr>";
	echo "</table></td></tr>";

	if ($ShowDetail == 1) {
		$querydt = " Select GrnPurchasesDt.PurchaseDt_ID  As PurchaseDtID, GrnPurchasesDt.Item_code As ItemCode, if(Items.Item_NonStock=0, Item_Description, PurchaseDt_Remark) As ItemDescription, ".
				   " GrnPurchasesDt.Warehouse_Code As WarehouseCode, PurchaseDt_Quantity As PurchaseDtQuantity, location_code as location_code ".
				   " From GrnPurchasesDt Inner Join Items on GrnPurchasesDt.Item_code = Items.Item_code ".
				   " Where GrnPurchasesDt.Purchase_ID = ".$PurchaseId;
		
	} else {
		$querydt = " Select GrnPurchasesDt.PurchaseDt_ID As PurchaseDtID, GrnPurchasesDt.Item_CodeFinished As ItemCode, Items.Item_Description As ItemDescription, ".
				   " GrnPurchasesDt.Warehouse_Code As WarehouseCode, ifNull(Item_qtyPacked,0) As PurchaseDtQuantity, Count(*) As ItemsCount ".
				   " From GrnPurchasesDt Inner Join Items on GrnPurchasesDt.Item_CodeFinished = Items.Item_code ".
				   " Where GrnPurchasesDt.Purchase_ID = '".$PurchaseId."' ".
				   " And GrnPurchasesDt.Item_CodeFinished Is Not Null ".
				   " Group By GrnPurchasesDt.Item_CodeFinished, GrnPurchasesDt.Warehouse_Code ".
				   " Union ".
				   " Select GrnPurchasesDt.PurchaseDt_ID As PurchaseDtID, GrnPurchasesDt.Item_CodeSemiFinished As ItemCode, Items.Item_Description As ItemDescription, ".
				   " GrnPurchasesDt.Warehouse_Code As WarehouseCode, ifNull(Item_qtyPacked,0) As PurchaseDtQuantity, Count(*) As ItemsCount ".
				   " From GrnPurchasesDt Inner Join Items on GrnPurchasesDt.Item_CodeSemiFinished = Items.Item_code ".
				   " Where GrnPurchasesDt.Purchase_ID = '".$PurchaseId."' ".
				   " And GrnPurchasesDt.Item_CodeSemiFinished Is Not Null ".
				   " Group By GrnPurchasesDt.Item_CodeSemiFinished, GrnPurchasesDt.Warehouse_Code ".
				   " Union ".
				   " Select GrnPurchasesDt.PurchaseDt_ID As PurchaseDtID, GrnPurchasesDt.Item_code As ItemCode, if(Items.Item_NonStock=0, Item_Description, PurchaseDt_Remark) As ItemDescription, ".
				   " GrnPurchasesDt.Warehouse_Code As WarehouseCode, PurchaseDt_Quantity As PurchaseDtQuantity, 1 As ItemsCount ".
				   " From GrnPurchasesDt Inner Join Items on GrnPurchasesDt.Item_code = Items.Item_code ".
				   " Where GrnPurchasesDt.Purchase_ID = '".$PurchaseId."' ".
				   " And GrnPurchasesDt.Item_CodeFinished Is Null ".
				   " And GrnPurchasesDt.Item_CodeSemiFinished Is Null ";
	}			

	//echo $querydt;
	echo "<tr><td align='left'>";
	echo "<table border=0 width='652' cellspacing=2 class='detail' style='table-layout:fixed;' >
		  <col width=120>
		  <col width=280>
		  <col width=100>
		  <col width=100>";
	echo "<tr bgcolor=#D8D8D8>";
	echo "   <th align='left'>Code</th>";
	echo "   <th align='left'>Description</th>";
	echo "	 <th align='center'>Warehouse</th>";
	if($module_locationGrn == 1) echo "	 <th align='center'>Location</th>";
	echo "	 <th align='center'>Qty.</th>";
	echo "</tr>";			
		  
	//echo "<tr height='10'><td colspan=4><hr></td></tr>";
	echo "<tr height='5'><td colspan=4><h4></td></tr>";
	
	$resultdt=mysql_query($querydt,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultdt)){
		extract($rows);
		
		$TotalQty = $TotalQty + $PurchaseDtQuantity;
		
		echo "<tr>";
		echo "  <td align='left' valign='top'>$ItemCode</td>";
		echo "  <td align='left' valign='top'>$ItemDescription</td>";
		echo "  <td align='left' valign='top'>$WarehouseCode</td>";
		if($module_locationGrn == 1) echo "  <td align='left' valign='top'>$location_code</td>";
		echo "	<td align='right' valign='top'>$PurchaseDtQuantity</td>";
		echo "</tr>";
		
		
		if ($PrtSer == 1 && $ShowDetail == 1) {
			$querysr = " SELECT SerialNo_Code ".
					   " FROM Purchasesserialno ".
					   " WHERE Purchasesserialno.GrnDt_id = ".$PurchaseDtID.
					   " AND Purchasesserialno.Journal_Type = 'PI' ";
			
			$resultsr=mysql_query($querysr,$connection) or die(mysql_error());
			$num=mysql_num_rows($resultsr);
			$SerialCode='';
			if ($num != 0)
			{
				while($rowsr=mysql_fetch_array($resultsr)){
					$SerialCode = $SerialCode." , ".$rowsr[0];
				}
				
				$SerialCode = substr($SerialCode,2);
				echo "<tr><td align=center valign=top class='sr'>Serial No :</td><td colspan='3' valign=top class='sr'>";
				echo $SerialCode;
				echo "</td></tr>";
			}
		}
		
		//echo "<tr height='10'><td colspan=7><hr></td></tr>";
	}
	
	//echo "<tr height='10'><td colspan='4'><hr></td></tr>";
	echo "<tr height='5'><td colspan=4><h4></td></tr>";
	
	echo "<tr>";
	echo "   <td align='left' colspan='2'>&nbsp;</td>";
	echo "	 <th align='left' class='labels'>Total Qty.</th>";
	echo "	 <th align='right' class='amounts'>".$TotalQty."</th>";
	echo "</tr>";	
	
	echo "</table></td></tr>";
	
	
	
	
	
	echo "</table>";
	echo "<br>";
	

}


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>
</BODY>
</HTML>
HERE;
?>

