<?php

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


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

							
$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 Login Table
$querylog=" select ifNull(Login_ModifyAllPending,0) As Login_ModifyAllPending ".
          " from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}


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

$queryperiod= " Select ifNull(LockViewPeriod,0) As LockViewPeriod, ViewPeriodDate, ifNull(ViewLockDaily,0) As ViewLockDaily, ViewNumberOfDays ".
              " From ".$HTTP_SESSION_VARS['dbLabel'].".ListUserDir  ".
			  " Where Usercode='".$HTTP_SESSION_VARS['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;	


if (empty($searchby)) $searchby = 2;

if ($startrange=='')	$startrange = 0;
if ($perpage=='')   	$perpage = 20;

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">
<title>Search Production Requests Pending</title>
<script type="text/javascript" language="javascript">

function ClearForm()
{
  document.search.keyword.value='';
  document.search.SelectedID.value = '';
 
  document.search.action='Form_ProductionReqSearchPending.php';
  document.search.submit();

}

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

function SetNextPage(startrange,perpage){

	document.search.action='Form_ProductionReqSearchPending.php?keyword=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function SetPrvPage(startrange,perpage){

	document.search.action='Form_ProductionReqSearchPending.php?keyword=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}


function DeleteRecord(PrdId, PrdRef){

	if (PrdId != '') {
		if (confirm ( "Are you sure you want to Delete this record with reference = "+PrdRef+" ?") )
		{
			document.search.action="Form_ProductionReq_Actions.php?mainmode=deletepending&SPrdId="+PrdId;
			document.search.submit();
		}
	}
	
}

function UpdateRecord(PrdId){
		
	opener.document.production.txtPrdId.value=PrdId;
	opener.document.production.mainaction.value='edit';
	opener.document.production.actionmode.value='';
	opener.document.production.action='Form_ProductionRequest.php';
	opener.document.production.submit();
	self.close();
	
}


function ShowHideDetails(PrdReqId, Option) {
	if (Option == 'S') {
		document.search.SelectedID.value = PrdReqId;
	}
	else if (Option == 'H') {
		document.search.SelectedID.value = '';
	}	
	document.search.action='Form_ProductionReqSearchPending.php?SelectedID='+PrdReqId;
	document.search.submit();
}

function SelectSearchBy()
{	

  //document.search.keyword.value='';
  document.search.action='Form_ProductionReqSearchPending.php?startrange=0';
  document.search.submit();

}

function SubmitForm()
{	

  document.search.SelectedID.value = '';
  //document.search.keyword.value='';
  document.search.action='Form_ProductionReqSearchPending.php?startrange=0';
  document.search.submit();

}

function RefreshPage()
{
  document.search.keyword.value='';

  document.search.action='Form_ProductionReqSearchPending.php?startrange=0';
  document.search.submit();

}


function SetFirstPage(startrange,perpage){
	document.search.action='Form_ProductionTemplateSearch.php?startrange=0&perpage='+perpage;
	document.search.submit();
}

function SetLastPage(perpage,totalrows){
	var result=totalrows % perpage;
	if (result == 0) result = perpage;
	startrange=totalrows-result;
	document.search.action='Form_ProductionTemplateSearch.php?startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}
</script>

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 0px;
	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 {color: #5151FF; text-decoration:underline}

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

</style>

</head>
<body OnLoad='LoadForm();' class='bodybackground'>
<form name='search' method='post' action=''>
<input type='hidden' name='SelectedID' id='SelectedID' value='$SelectedID'>
HERE;


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

if (empty($ViewPeriodDate))
   $ViewPeriodDate = 'Null';
if (empty($ViewNumberOfDays))   
   $ViewNumberOfDays = 1;

$querysearch=" Select Distinct ProductionRequest.ProductionReq_ID, ProductionRequest.ProductionReq_Reference, Date_Format(ProductionRequest.ProductionReq_Date,'%d-%m-%Y') As ProductionReq_Date, ".
             " ProductionRequest.ProductionReq_Remark, ProductionRequest.ProductionReq_Manager, ProductionRequest.ProductionReq_Shift, ".
			 " ProductionRequest.Logged_Code As UserAccount ".
			 " From Temp_ProductionRequest As ProductionRequest ";
							 
$querysearch= $querysearch.											 
			 " Where (($searchby = 2 and ifNull(ProductionReq_Reference,-1) Like Concat(ifNull($PRef, ifNull(ProductionReq_Reference,-1)),'%')) Or ".
			 "        ($searchby = 1 and Date_Format(ProductionRequest.ProductionReq_Date,'%Y-%m-%d') = ifNull(Date_Format($PRef,'%Y-%m-%d'), Date_Format(ProductionRequest.ProductionReq_Date,'%Y-%m-%d'))) Or  ".
			 "        ($searchby = 3 and ifNull(ProductionRequest.ProductionReq_Manager, -1) Like Concat(ifNull($PRef, ifNull(ProductionRequest.ProductionReq_Manager, -1)),'%') ) Or  ".
			 "        ($searchby = 4 and ifNull(ProductionRequest.ProductionReq_Shift, -1) Like Concat(ifNull($PRef, ifNull(ProductionRequest.ProductionReq_Shift, -1)),'%') ) Or  ".
			 "        ($searchby = 5 and ProductionRequest.Logged_Code Like Concat(ifNull($PRef, ProductionRequest.Logged_Code),'%')) ) ".
			 " And (($Login_ModifyAllPending = 0 and ProductionRequest.Logged_Code = '".$HTTP_SESSION_VARS['useraccount']."') or ($Login_ModifyAllPending = 1 and 1 = 1)) ";

$querysearch= $querysearch." Order By ProductionReq_Date Desc, ProductionReq_Reference ";
			 
//echo $querysearch;			 

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

echo"<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed' width='860px'>";
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'>List of Pending Production Requests
			</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"<table border='0' cellspacing='1' style='table-layout:fixed'>
	  <col width=30>
	  <col width=30>
	  <col width=30>
	  <col width=90>
	  <col width=80>
	  <col width=130>
	  <col width=130>
	  <col width=250>
	  <col width=120>

	  <tr class='Search-header-bg'>
	    <td align='left' colspan='3'><span class='style4'>Search by </span></td>
		<td align='left' colspan='6'><span class='style4'><input type='text' name='keyword' id='keyword' value='$keyword' size='15' maxlength='30'/></span>
	
		<select name='searchby' id='searchby' value='$searchby' onchange='SelectSearchBy();'>";
		
			if ($searchby==1)
				$s21 = 'selected';
			else if ($searchby==2)
				$s22 = 'selected';
			else if ($searchby==3)
				$s23 = 'selected';
			else if ($searchby==4)
				$s24 = 'selected';		
			else if ($searchby==5)
				$s25 = 'selected';
				
			echo"<option ".$s21." value='1'>Date (yyyy-mm-dd)</option>
				 <option ".$s22." value='2'>Reference</option>
				 <option ".$s23." value='3'>Manager</option>
				 <option ".$s24." value='4'>Shift</option>
				 <option ".$s25." value='5'>User</option>
				 </select>";	
				 
echo"	     <button type='button' id='btnSubmit' name='btnSubmit' onclick='SubmitForm(); return false;' title='Find'><span><span>Find</span></span></button>
			<button type='button' id='btnCancel' name='btnCancel' onclick='ClearForm();return false;' title='Cancel'><span><span>Cancel</span></span></button></td>";
		
echo" </tr>
</table>
		</td>
		<td id='search-content3' style='width:31px;'>&nbsp;</td>"; 
echo" </tr>";


	 echo"<tr>
		<td  id='search-content1' style='height:34px;width:31px;'>&nbsp;</td>
		<td  id='search-content2' style='height:34px;width:1025px;'>
		<table width='100%' border=0>
		<tr>
			<td align='right' style='width:380px;'><span class='style4'>Per Page </span><span class='style4'><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>
			<td  style="width:74px;border-right:1px solid #000000;text-align:right;">';
			
			if($startrange > 0)
				echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetFirstPage('".$startrange."','".$perpage."')>";

			$diff= $startrange - $perpage;
				
			if (($diff) >= 0 ){
				$prvstartrange = $startrange - $perpage;
				echo"&nbsp;&nbsp;<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
			}
			else
				echo "&nbsp;";
		
echo"		</td><td  align='left' style='width:74px;'>";
		
		
			if ($startrange <= $totalrows){
				$startrange1 = $startrange + $perpage;
				if  ($startrange1 < $totalrows){
					echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange1."','".$perpage."')>";
					echo"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."')>";	
				}
			} else
				echo "&nbsp;";
				
			
echo"		</td><td style='width:380px;'>&nbsp;</td>
		</tr>";
echo"	</table></td>
		<td id='search-content3' style='width:31px;'>&nbsp;</td>";
echo"</tr>";			

	if($message!=''){
		echo"	  <tr class='Search-header-bg'>
		<td height='15' align='center' colspan='8' class='searchmessage'>".$message."</td>
		</tr>";
	}
	
	echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		<td id='search-content2'>
		<div style='overflow:scroll;height:400px;width:790px;'>
		<table border='0'  cellpadding='0' cellspacing='1' >
		  <col width=20>
		  <col width=20>
		  <col width=20>
		  <col width=90>
		  <col width=90>
		  <col width=130>
		  <col width=130>
		  <col width=250>
		  <col width=120>
		  
	  <tr class='Search-Label-bg'>
		<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style4'>Reference </span></td>
		<td  height='25' align='center'><span class='style4'>Date </span></td>
		<td  height='25' align='center'><span class='style4'>Manager</span></td>
		<td  height='25' align='center'><span class='style4'>Shift</span></td>
		<td  height='25' align='center'><span class='style4'>Remark</span></td>
		<td  height='25' align='center'><span class='style4'>User</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."'";	
			

	echo'<tr '.$Color.'>
			<td align="center"><span class="style5"><img src="img/edit.png" onMouseOver="style.cursor=\'hand\'" onclick="UpdateRecord('.$ProductionReq_ID.');" title="Edit Record" ></span></td>
			<td align="center"><span class="style5"><img src="img/delete.png" onMouseOver="style.cursor=\'hand\'" onclick="DeleteRecord('.$ProductionReq_ID.', \''.$ProductionReq_Reference.'\');" title="Delete Record" ></span></td>';
			
	if ($SelectedID != "" && $ProductionReq_ID == $SelectedID)		
		echo'<td align="center"><span class="style5"><img src="img/hide.png" onMouseOver="style.cursor=\'hand\'"  onclick="ShowHideDetails('.$ProductionReq_ID.', \''.'H'.'\');" title="Hide Divisions" height="15px" width="15px"></span></td>';
	else	
		echo'<td align="center"><span class="style5"><img src="img/show.png" onMouseOver="style.cursor=\'hand\'"  onclick="ShowHideDetails('.$ProductionReq_ID.', \''.'S'.'\');" title="Show Divisions" height="15px" width="15px"></span></td>';
			
	echo'	<td align="left"><span class="style5">'.$ProductionReq_Reference.'</span></td>
			<td align="center"><span class="style5">'.$ProductionReq_Date.'</span></td>
			<td align="left"><span class="style5">'.$ProductionReq_Manager.'</span></td>
			<td align="left"><span class="style5">'.$ProductionReq_Shift.'</span></td>
			<td align="left"><span class="style5">'.$ProductionReq_Remark.'</span></td>
			<td align="left"><span class="style5">'.$UserAccount.'</span></td>
		</tr>';
		
	if ($SelectedID != "" && $ProductionReq_ID == $SelectedID) {
		$sub_query = " Select ProductionReqFG.Item_Code, Items.Item_Description, ProductionReqFG.Warehouse_Code, ProductionReqFG_Quantity ".
					 " From Temp_ProductionReqFG As ProductionReqFG ".
					 " Inner Join Items on ProductionReqFG.Item_Code = Items.Item_Code ".
					 " Where ProductionReqFG.ProductionReq_Id = '".$SelectedID."' ";
		
		$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());	
		$sub_totalrows = mysql_num_rows($sub_result);
		
		if ($sub_totalrows > 0) {
			echo "<tr ".$Color.">";
			echo "<td colspan='2' valign='top' align='left'>&nbsp;</td>";
			echo "<td colspan='7' valign='top' align='left'><div style='border:1px solid #33CCFF; z-index:9999; background-color:#CCFFFF; padding-left:5px; padding-bottom:5px;'>";
			echo "<table width='95%' border='0' cellpading='0' cellspacing='1'>";
			echo "<tr><td colspan='4' align='center'><span class='style17'>Finish Goods</span></td></tr>";
			echo "<tr><td width='20%'><span class='style17'>Item Code</span></td><td width='50%'><span class='style17'>Description</span></td> <td width='15%' align='center'><span class='style17'>Warehouse</span></td><td width='15%' align='center'><span class='style17'>Quantity</span></td>";
			while($sub_rows=mysql_fetch_array($sub_result)){
				echo "<tr><td>".$sub_rows[0]."</td><td>".$sub_rows[1]."</td><td>".$sub_rows[2]."</td><td align='right'>".$sub_rows[3]."</td></tr>";
			
			}
			
			echo "</tr></table></div></td>";
			echo "</tr>";
		}
		
		$sub_query1= " Select ProductionReqRM.Item_Code, Items.Item_Description, ProductionReqRM.Warehouse_Code, ProductionReqRM_Quantity ".
					 " From Temp_ProductionReqRM As ProductionReqRM ".
					 " Inner Join Items on ProductionReqRM.Item_Code = Items.Item_Code ".
					 " Where ProductionReqRM.ProductionReq_Id = '".$SelectedID."' ";
		$sub_result1=mysql_query($sub_query1,$connection) or die(mysql_error());	
		$sub_totalrows1 = mysql_num_rows($sub_result1);
		
		if ($sub_totalrows1 > 0) {
			echo "<tr ".$Color.">";
			echo "<td colspan='2' valign='top' align='left'>&nbsp;</td>";
			echo "<td colspan='7' valign='top' align='left'><div style='border:1px solid #33CCFF; z-index:9999; background-color:#CCFFFF; padding-left:5px; padding-bottom:5px;'>";
			echo "<table width='95%' border='0' cellpading='0' cellspacing='1'>";
			echo "<tr><td colspan='4' align='center'><span class='style17'>Raw Materials</span></td></tr>";
			echo "<tr><td width='20%'><span class='style17'>Item Code</span></td><td width='50%'><span class='style17'>Description</span></td> <td width='15%' align='center'><span class='style17'>Warehouse</span></td><td width='15%' align='center'><span class='style17'>Quantity</span></td>";
			while($sub_rows1=mysql_fetch_array($sub_result1)){
				echo "<tr><td valign='top'>".$sub_rows1[0]."</td><td valign='top'>".$sub_rows1[1]."</td><td valign='top'>".$sub_rows1[2]."</td><td align='right' valign='top'>".$sub_rows1[3]."</td></tr>";
			
			}
			
			echo "</tr></table></div></td>";
			echo "</tr>";
		}
	}
}

			
		
	echo"</table></div></td><td id='search-content3' style='width:31px;'>&nbsp;</td></tr>";		
		
echo"	<tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; width:920px;' valign='top'><table width='100%' border=0 height=28px>
		<tr><td style='width:280px;'>&nbsp;</td><td align='right' style='width:74px;border-right:1px solid #000000;'>";
		
			if($startrange > 0)
				echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetFirstPage('".$startrange."','".$perpage."')>";
				
		//$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			//$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td width='150' align='left'>";
		
		
		if ($startrange <= $totalrows){
			//$startrange = $startrange + $perpage;
			if  ($startrange1 < $totalrows){
				echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange1."','".$perpage."')>";
				echo"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."')>";	
			}	
		} else
			echo "&nbsp;";
			
		echo"</td><td width='170'>&nbsp;</td></tr>";
		echo"</table></td><td id='search-bottom3' style='height:34px; width:31px;'>&nbsp;</td>";


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

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

HERE;

?>