<?php

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


include("config.inc.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");


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

$item	   	= $_GET['item']; if ($item == ""){$item = $_POST["item"];}
$index		= $_GET['index']; if ($index == "") $index = $_POST['index'];
$frm		= $_GET['frm']; if ($frm == "") $frm = $_POST['frm'];
$perpage   	= $_GET['perpage']; if ($perpage == ""){$perpage = $_POST["perpage"];}
$startrange = $_GET['startrange']; if ($startrange == ""){$startrange = $_POST["startrange"];}
$PSearch  	= $_GET['PSearch']; if ($PSearch == ""){$PSearch = $_POST["PSearch"];}
$PItem_Code = $_GET['PItem_Code']; if ($PItem_Code == ""){$PItem_Code = $_POST["PItem_Code"];}

		
print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />


<title>Search Expire Date</title>
<script type="text/javascript" language="javascript">

function SelectRecord(value1){

	var vFrm   = document.search.frm.value;
	var vItem  = document.search.item.value;
	var vIndex = document.search.index.value;
	
	if(vFrm=='1'){
		opener.document.getElementById('expiredate['+vIndex+']').value = value1;
	}
	
	self.close();
}


function ClearForm()
{
	vFrm   = document.search.frm.value;
	vItem  = document.search.item.value;
	vindex  = document.search.index.value;
	vItem_Code  = document.search.PItem_Code.value;
	vWarehouseCode  = document.search.PWarehouseCode.value;
	
	document.search.PSearch.value='';
	document.search.action='List_ExpireDate.php?frm='+vFrm+'&item='+vItem+'&index='+vindex+'&PItem_Code='+vItem_Code+'&PWarehouseCode='+vWarehouseCode;
	document.search.submit();

}

function RefreshPage()
{
	document.search.submit();

}

function LoadForm() 
{
	x=document.search;
	x.PSearch.focus();

}

function SetNextPage(startrange,perpage){
	vFrm   = document.search.frm.value;
	vItem  = document.search.item.value;
	vItem_Code  = document.search.PItem_Code.value;
	vindex  = document.search.index.value;
	vWarehouseCode  = document.search.PWarehouseCode.value;
	
	document.search.action='List_ExpireDate.php?PSearch=&frm='+vFrm+'&item='+vItem+'&index='+vindex+'&PItem_Code='+vItem_Code+'&PWarehouseCode='+vWarehouseCode+'&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function SetPrvPage(startrange,perpage){
    vFrm   = document.search.frm.value;
	vItem  = document.search.item.value;
	vItem_Code  = document.search.PItem_Code.value;
	vindex  = document.search.index.value;
	vWarehouseCode  = document.search.PWarehouseCode.value;
	
	document.search.action='List_ExpireDate.php?PSearch=&frm='+vFrm+'&item='+vItem+'&index='+vindex+'&PItem_Code='+vItem_Code+'&PWarehouseCode='+vWarehouseCode+'&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}




</script>

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 12px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}

a:visited {
	text-decoration: none;
	color: #0000FF;
}


.style1 {color: #999999; font-weight: bold; font-size: 11px;}
.style2 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style3 {color: #999999; font-weight: bold; font-size: 11px; }
.style4 {color: #3333FF; }

</style>

</head>
<body onLoad='LoadForm();'>
<form name='search' method='post' action=''>
<input type='hidden' name='item'  value='$item'>
<input type='hidden' name='frm' value='$frm'>
<input type='hidden' name='index' value='$index'>
<input type='hidden' name='PItem_Code' value='$PItem_Code'>
<input type='hidden' name='PWarehouseCode' value='$PWarehouseCode'>

HERE;

echo"<table border='0' cellspacing='1' style='table-layout:fixed' align='center'>

	  <col width=150>
	  <col width=280>
	  <col width=110>
	
		     
	  <tr bgcolor='#DDDDDD'>
	    <td align='center' colspan='3'><span class='style2'>List of Expire Date</span></td></tr>
	  <tr bgcolor='#DDDDDD' height=30px>
	    <td align='left'><span class='style1'>Search by Date </span></td>
		<td align='left'><span class='style1'><input type='text' name='PSearch' id='PSearch' value='$PSearch' style='width:150px;' maxlength='100'/></span>
		    <input type='submit' name='btnSubmit' value='Find' style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
			<input type='button' name='btnCancel' value='Cancel' onclick='ClearForm();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
		</td>
		<td align='left' valign='center'><span class='style1'>Per Page </span><span class='style1'><select name='perpage' id='perpage' value='$perpage' onchange='RefreshPage();'>";
              

        if ($perpage==20)
			$s20 = 'selected';
		else if ($perpage==30)
			$s30 = 'selected';
		else if ($perpage==40)
			$s40 = 'selected';
		else if ($perpage==50)
			$s50 = 'selected';
			
				
        echo'<option '.$s20.' value="20">20</option>
			 <option '.$s30.' value="30">30</option>
			 <option '.$s40.' value="40">40</option>
			 <option '.$s50.' value="50">50</option>
			 </select></span></td>';
echo" </tr>
	  <tr bgcolor='#DDDDDD'>
		<td height='15' align='center' colspan='3'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td>
	  </tr>	
	  <tr bgcolor='#CCD8E6'>
	  	<td  height='25' align='center'><span class='style3'>Expire Date </span></td>
		<td  height='25' align='center'><span class='style3'>Qty Exists </span></td>
	  </tr>";
		
/*            if($PWarehouseCode != ''){
	$Qry_QtyExists = " SELECT ifnull(QtyExists('".$PItem_Code."', '".$PWarehouseCode."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'DI', '', ''),0) As QtyExists ";	
				
}*/
$querysearch = " Select Date_Format(Item_ExpireDate,'%d/%m/%Y') As Item_ExpireDate, ".
			   " cast(Sum(QuantityProduceIn - QuantityProduceOut + QuantityEntered - QuantityDelivered) as decimal(20,2)) As QtyExists ".
			   " From StkTransaction ".
			   " Where StkTransaction.Item_ExpireDate Like ('%".$PSearch."%') ".
			  " And StkTransaction.Item_Code = '".$PItem_Code."' ";
if($PWarehouseCode != '')
	$querysearch = $querysearch." and Warehouse_code = '".$PWarehouseCode."' ";
			   //" And Transaction_Type In ('PI','PD','AI') ".
$querysearch = $querysearch." Group By Date_Format(Item_ExpireDate,'%d/%m/%Y')  ".
							" having cast(Sum(QuantityProduceIn - QuantityProduceOut + QuantityEntered - QuantityDelivered) as decimal(20,2)) > 0 ".
							" Order By StkTransaction.Item_ExpireDate Asc ";

//echo $querysearch;
$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());
$totalrows = mysql_num_rows($resultsearch);
	
if ($startrange=='')
	$startrange = 0;
if ($perpage=='')
	$perpage = 20;
		
$querysearch = $querysearch." limit ".$startrange.",".$perpage;
$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());		


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."'";	
			


	
	echo"<tr $Color>";
	echo"  <td align='left'><a onclick=\"SelectRecord('$Item_ExpireDate');\" href='#'><span class='style4'>$Item_ExpireDate</span></a></td>";
	echo"  <td align='right' style='padding-right:5px;'><span class='style4'>$QtyExists</span></td>";
    echo"</tr>";
	
}
							
	
		
echo"   <tr><td colspan='3'>&nbsp;</td></tr>
		<tr><td colspan='3'><table width='100%' border=0><tr><td width='145'>&nbsp;</td><td width='125' align='center'>";
		
		$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.jpg' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td width='125' align='center'>";
		
		
		if ($startrange <= $totalrows){
			$startrange = $startrange + $perpage;
			if  ($startrange <= $totalrows)
				echo"<img src='img/arrowright.jpg' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange."','".$perpage."')>";
		} else
			echo "&nbsp;";
			
		echo"</td><td width='145'>&nbsp;</td></tr>";
		echo"</table>";

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


          

		  

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

HERE;

?>