<?php

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


include("config.inc.php");
include("function.inc.php");
include("Functions_Trans.php");

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


$querylog = " select ifNull(FI_GrnDel,0) As FI_GrnDel, ifNull(FI_GrnMod,0) As FI_GrnMod, ifNull(FI_GrnApp,0) As FI_GrnApp, " .
    " ifNull(FI_RmGrnDel,0) As FI_RmGrnDel, ifNull(FI_RmGrnMod,0) As FI_RmGrnMod, ifNull(FI_RmGrnApp,0) As FI_RmGrnApp, " .
    " ifNull(login_belongstogrp,0) As FI_BelongsToGrp, ifNull(ShowTransaction,0) As ShowTransaction " .
    " from " . $_SESSION['dbLabel'] . ".login where user_account='" . $_SESSION['useraccount'] . "' and Upper(Login_Schema) = Upper('" . $_SESSION['dbName'] . "')  ";
$resultlog = mysql_query($querylog, $connection) or die(mysql_error());
while ($rows = mysql_fetch_array($resultlog)) {
    extract($rows);
}

//Read From Module Table
$querymodule = " Select ifNull(Module_Project,0) As Module_Project, ifNull(Module_Costcenter,0) As Module_CostCenter, " .
    " ifNull(Module_ProjectParent,0) As Module_ProjectParent, ifNull(Module_CostCenterParent,0) As Module_CostCenterParent " .
    " From Module";
$resultmodule = mysql_query($querymodule, $connection) or die(mysql_error());
while ($rowmodule = mysql_fetch_array($resultmodule)) {
    extract($rowmodule);
}




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

$MainCompanyName = explode('_', $_SESSION['dbName']);

$queryperiod = " Select ifNull(LockViewPeriod,0) As LockViewPeriod, ViewPeriodDate, ifNull(ViewLockDaily,0) As ViewLockDaily, ViewNumberOfDays, ifNull(AllowLockedPeriod,0) As AllowLockedPeriod, " .
    " LockedPeriod As LockedPeriodDate, LockedPeriodDays As LockedPeriodDays " .
    " From " . $_SESSION['dbLabel'] . ".ListUserDir  " .
    " Where Usercode='" . $_SESSION['useraccount'] . "' " .
    " And Upper(CompanyName) = Upper('" . $MainCompanyName[0] . "') " .
    " And Upper(Diryear) = Upper('" . $MainCompanyName[1] . "') ";

$resultperiod = mysql_query($queryperiod, $connection) or die(mysql_error());
$rowsperiod   = mysql_num_rows($resultperiod);

while ($rows = mysql_fetch_array($resultperiod)) {
    extract($rows);
}

//echo $queryperiod;	

$queryproject = " select distinct project.project_code, project_name " .
    " from project left join " . $_SESSION['dbLabel'] . ".loginstk on (($Module_ProjectParent=1 and project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%')) or ($Module_ProjectParent=0 and project.project_code = loginstk.project_code )) " .
    " where user_account='" . $_SESSION['useraccount'] . "' " .
    " and (loginstk.project_code is not null and loginstk.project_code != '') " .
    " order by project.project_code";
$projectresult = mysql_query($queryproject, $connection) or die(mysql_error());
$projectexist  = mysql_num_rows($projectresult);


$querycostcent = " select distinct costcent.costcent_code, costcent_name " .
    " from costcent left join " . $_SESSION['dbLabel'] . ".loginstk on (($Module_CostCenterParent=1 and costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%')) or ($Module_CostCenterParent=0 and costcent.costcent_code = loginstk.costcent_code )) " .
    " where user_account='" . $_SESSION['useraccount'] . "' " .
    " and (loginstk.costcent_code is not null and loginstk.costcent_code != '') " .
    " order by costcent.costcent_code";
$costcentresult = mysql_query($querycostcent, $connection) or die(mysql_error());
$costcentexist  = mysql_num_rows($costcentresult);

$queryledger = " select distinct ledger_number, ledger_include " .
    " from " . $_SESSION['dbLabel'] . ".ledgerstk  " .
    " where user_account='" . $_SESSION['useraccount'] . "' " .
    " and (ledger_number is not null and ledger_number != '') " .
    " order by ledger_number";
$ledgerresult = mysql_query($queryledger, $connection) or die(mysql_error());
$ledgerexist  = mysql_num_rows($ledgerresult);
$rows = mysql_fetch_array($ledgerresult);
$ledgerinclude = $rows[1];

$querylog = "select ifNull(FI_PurchaseCategory,0) As FI_PurchaseCategory from " . $_SESSION['dbLabel'] . ".login where user_account='" . $_SESSION['useraccount'] . "' and Upper(Login_Schema) = Upper('" . $_SESSION['dbName'] . "')  ";
$resultlog = mysql_query($querylog, $connection) or die(mysql_error());
while ($rows = mysql_fetch_array($resultlog)) {
    extract($rows);
}

//Get Default Project
$querydflproject = "select project_code as defaultproject from " . $_SESSION['dbLabel'] . ".loginstk where user_account='" . $_SESSION['useraccount'] . "' and default_project=1 ";
$resultdflproject = mysql_query($querydflproject, $connection) or die(mysql_error());
while ($rows = mysql_fetch_array($resultdflproject)) {
    extract($rows);
}

//Get Default Coscenter
$querydflcostcent = "select costcent_code as defaultcostcent from " . $_SESSION['dbLabel'] . ".loginstk where user_account='" . $_SESSION['useraccount'] . "' and default_costcenter=1 ";
$resultdflcostcent = mysql_query($querydflcostcent, $connection) or die(mysql_error());
while ($rows = mysql_fetch_array($resultdflcostcent)) {
    extract($rows);
}


$PurType     = $_POST['purtype'];
if ($PurType == "") {
    $PurType = $_GET["purtype"];
}
$purid        = $_POST['purid'];
if ($purid == "") {
    $purid = $_GET["purid"];
}
if ($PurType == 'GRN')      $Title = 'List of Items Goods Receive Note';
else if ($PurType == 'RM')  $Title = 'List of Items Goods Receive Note - RM';


print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" media="all" href="css/style.css">
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>
<title>Search Goods Receive Note</title>
<script type="text/javascript" language="javascript">
function PrintItemBarcode(pid){
	win = window.open('Form_GRNBarcode.php?pid='+pid, 'Form_GRNBarcode', 'toolbar=no,titlebar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=no,width=260,height=180,top=100,left=200');
	win.creator = self;
	win.focus();
}


</script>

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}

.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}

.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}

.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #000000 ;font-size: 10px;}

</style>

<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>


</head>
<body class='bodybackground'>
<form name='grnitems' method='post' action=''>
HERE;

$PRef = GetSQLValueString($keyword, 'text');

if (empty($ViewPeriodDate))
    $ViewPeriodDate = 'Null';
if (empty($ViewNumberOfDays))
    $ViewNumberOfDays = 1;
$querysearch = " Select GrnPurchasesDt.PurchaseDt_ID as PurchaseDt_ID, GrnPurchasesDt.Item_CodePacked as Item_codePacked, 
   GrnPurchasesDt.Item_code as Item_code, Item_Description as Item_desc " .
    " , PurchaseDt_Quantity as purchasedt_qty, Date_Format(Purchase_Date,'%d-%m-%Y') as Purchase_Date
    From GrnPurchasesDt Left Join Items on GrnPurchasesDt.Item_code = Items.Item_code " .
    " Where GrnPurchasesDt.Purchase_ID = " . $purid;

$resultsearch = mysql_query($querysearch, $connection) or die(mysql_error());
$totalrows = mysql_num_rows($resultsearch);


echo "<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed'  width='700px'>";
echo "<tr>
		<td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
	  	<td id='search-top2' style='height:34px;' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>$Title
			</span></td></tr>
			</table>
		</td>
		<td id='search-top3' style='height:34px; width:31px;'>&nbsp;</td>
	</tr>";

echo "<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		 <td id='search-content2' >";
echo"		</td>
		<td id='search-content3' style='width:31px;'>&nbsp;</td>";
echo " </tr>";


echo "<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		<td id='search-content2'>
			<div style='overflow:scroll;height:400px;width:700px;'>
			<table border='0'  cellpadding='0' cellspacing='1'>
			
			  <col width=50>
			  <col width=130>
              <col width=170>
			  <col width=150>
			  <col width=100>
			  <col width=100>
            
			 ";



echo " <tr class='Search-Label-bg'>
	    <td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style4'>Item Code </span></td>
        <td  height='25' align='center'><span class='style4'>Item Decription </span></td>
		<td  height='25' align='center'><span class='style4'>Date </span></td>
		<td  height='25' align='center'><span class='style4'>Quantity</span></td>
        
	
	  </tr>";



while ($rows = mysql_fetch_array($resultsearch)) {

    extract($rows);
    $ct += 1;

    if ($ct % 2 == 0)
        $Color = "style='background-color:" . $grid_color1 . "'";
    else
        $Color = "style='background-color:" . $grid_color2 . "'";


    //Access Mode On Insert/Edit/Delete

    //echo '<tr ' . $Color . '>';
    echo '<tr ' . $Color . ' >';


    echo '<td align="center"><span class="style5"><img src="img/mag.jpg" onMouseOver="style.cursor=\'hand\'"  onclick="PrintItemBarcode(' . $PurchaseDt_ID . ');"  title="' . $PrevHint . '"  style="width: 15px; height: 15px; "></span></td>';

    echo '	<td align="left"><span class="style5">' . $Item_code . '</span></td>
			<td align="center"><span class="style5">' . $Item_desc . '</span></td>
			<td align="center"><span class="style5">' . $Purchase_Date . '</span></td>
			<td align="center"><span class="style5">' . $purchasedt_qty . '</span></td>
		
		</tr>';
}

//<td height="20" align="left"><a href="#" onClick="SelectRef('.$Purchase_ID.');" class="style5"><span class="style5 style17" style="text-decoration:underline;">'.$Purchase_Reference.'</span></a></td>			


echo "</table></div></td><td id='search-content3' style='width:31px;'>&nbsp;</td></tr>
";

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







print <<<HERE
</form>
</body>

HERE;

function explodeword($date)
{

    $trim = explode('/', $date);
    $trim = "$trim[2]" . "-" . "$trim[1]" . "-" . "$trim[0]";
    return $trim;
}
