<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
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");

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}
							
$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
				, ifnull(Module_PurchaseRequestPrice,0) as Module_PurchaseRequestPrice 
				".
             " From Module";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

//Read From Login Table
$querylog=" select ifNull(Login_ModifyAllPending,0) As Login_ModifyAllPending ".
          " 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);
}

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

$queryperiod= " Select ifNull(LockViewPeriod,0) As LockViewPeriod, ViewPeriodDate, ifNull(ViewLockDaily,0) As ViewLockDaily, ViewNumberOfDays ".
              " 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];


$keyword    = $_POST['keyword']; if ($keyword == ""){$keyword = $_GET["keyword"];}
$searchby   = $_POST['searchby']; if ($searchby == ""){$searchby = $_GET["searchby"];}
$category   = $_POST['category']; if ($category == ""){$category = $_GET["category"];}
$perpage    = $_POST['perpage']; if ($perpage == ""){$perpage = $_GET["perpage"];}
$startrange = $_POST['startrange']; if ($startrange == ""){$startrange = $_GET["startrange"];}



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 Purchase Orders</title>
<script type="text/javascript" language="javascript">

function clearform()
{
  document.search.keyword.value='';
 
  document.search.action='Form_PurchaseRequestSearchPending.php';
  document.search.submit();

}

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

function SetNextPage(startrange,perpage){
	
	document.search.action='Form_PurchaseRequestSearchPending.php?keyword=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function SetPrvPage(startrange,perpage){
	
	document.search.action='Form_PurchaseRequestSearchPending.php?keyword=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}


function DeleteRecord(PreqId, PreqRef){

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

function UpdateRecord(PreqId){
		
	opener.document.purchasereq.txtPreqId.value=PreqId;
	opener.document.purchasereq.mainaction.value='edit';
	opener.document.purchasereq.actionmode.value='';
	opener.document.purchasereq.action='Form_PurchaseRequest.php';
	opener.document.purchasereq.submit();
	self.close();
	
}

function SelectSearchBy()
{	
  
  //document.search.keyword.value='';
  document.search.action='Form_PurchaseRequestSearchPending.php?startrange=0';
  document.search.submit();

}

function SubmitForm()
{	
  
  //document.search.keyword.value='';
  document.search.action='Form_PurchaseRequestSearchPending.php?startrange=0';
  document.search.submit();

}

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

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

</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>

</head>
<body OnLoad='LoadForm();' class='bodybackground'>
<form name='search' method='post' action=''>
HERE;

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

if (empty($ViewPeriodDate))
   $ViewPeriodDate = 'Null';
if (empty($ViewNumberOfDays))   
   $ViewNumberOfDays = 1;
		
			 
$querysearch=" Select Distinct Prequest.PRqst_id as PRqst_id, Prequest.PRqst_reference as PRqst_reference, Date_Format(Prequest.PRqst_date,'%d-%m-%Y') As PRqst_date, ".
             " Prequest.Currency_code, PRqst_RequestBy,".
			 " Round((Prequest.PRqst_Total + IfNull(Prequest.PRqst_Vat,0) - IfNull(Prequest.PRqst_DiscountAmount,0) + ".
			 " (ifNull(Prequest.Prqst_Tax1,0) + ifNull(Prequest.Prqst_Tax2,0) + ifNull(Prequest.Prqst_Tax3,0) + ifNull(Prequest.Prqst_Tax4,0) + ifNull(Prequest.Prqst_Tax5,0))), ".$DecimalBase.") As NetTotal,  ".
			 " Prequest.Logged_Code As UserAccount ".
			 " From Temp_Prequest As Prequest Left Join Suppliers on Prequest.Supplier_code = Suppliers.Ledger_Number ";
			 
if ($projectexist >	0)
  $querysearch= $querysearch." Inner join ( select distinct project_code from ".$_SESSION['dbLabel'].".loginstk ".
							 "			   where loginstk.user_account='".$_SESSION['useraccount']."' ".
							 "             and (project_code is not null and project_code != '') ".
							 "			 ) Q1 on (($Module_ProjectParent=1 and Prequest.project_code like concat(Q1.project_code,'%')) or ($Module_ProjectParent=0 and Prequest.project_code = Q1.project_code )) ";
							 
if ($costcentexist > 0)
  $querysearch= $querysearch." Inner join ( select distinct costcent_code from ".$_SESSION['dbLabel'].".loginstk ".
							 "			   where loginstk.user_account='".$_SESSION['useraccount']."' ".
							 "             and (costcent_code is not null and costcent_code != '') ".
							 "			 ) Q2 on (($Module_CostCenterParent=1 and Prequest.costcent_code like concat(Q2.costcent_code,'%')) or ($Module_CostCenterParent=0 and Prequest.costcent_code = Q2.costcent_code )) ";

							 
$querysearch= $querysearch.											 
			 " Where (($searchby = 2 and ifNull(PRqst_reference,-1) Like Concat(ifNull($PRef, ifNull(PRqst_reference,-1)),'%')) Or ".
			 "        ($searchby = 1 and Date_Format(Prequest.PRqst_date,'%Y-%m-%d') = ifNull(Date_Format($PRef,'%Y-%m-%d'), Date_Format(Prequest.PRqst_date,'%Y-%m-%d'))) Or  ".
	 		 "        ($searchby = 4 and Prequest.Logged_Code Like Concat(ifNull($PRef, Prequest.Logged_Code),'%')) ) ".
			 " And (Prequest.prqst_type = 'PQ' or Prequest.prqst_type Is Null or Prequest.prqst_type = '' ) ".
			 " And (($Login_ModifyAllPending = 0 and Logged_Code = '".$_SESSION['useraccount']."') or ($Login_ModifyAllPending = 1 and 1 = 1)) ";

			 

$querysearch= $querysearch." Order By PRqst_date Desc, PRqst_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='750px'>";
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 Purchase Request
			</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=20>
	  <col width=20>
	  <col width=70>
	  <col width=90>
	  <col width=260>
	  <col width=60>
	  <col width=100>
	  <col width=70>
		     

	  <tr class='Search-header-bg'>
	    <td align='left' colspan='3'><span class='style4'>Search by </span></td>
		<td align='left' colspan='5'><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';	
				
			echo"<option ".$s21." value='1'>Date (yyyy-mm-dd)</option>
				 <option ".$s22." value='2'>Reference</option>
				 <option ".$s23." value='3'>Supplier</option>
				 <option ".$s24." value='4'>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>
	  <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='left'><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'>Request by </span></td>";
		
		if ($Module_PurchaseRequestPrice==1) {
		echo"<td  height='25' align='center'><span class='style4'>Currency</span></td>
		<td  height='25' align='center'><span class='style4'>Total</span></td>";
		echo"<td  height='25' align='center'><span class='style4'>User</span></td>";
		}
		else
		echo"<td  colspan=3 height='25' align='center'><span class='style4'>User</span></td>";
		
		
	  echo"</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('.$PRqst_id.');" title="Edit Record" style="width: 20px; height: 20px; "></span></td>
			<td align="center"><span class="style5"><img src="img/delete.png"    onMouseOver="style.cursor=\'hand\'" onclick="DeleteRecord('.$PRqst_id.', \''.$PRqst_reference.'\');" title="Delete Record" style="width: 20px; height: 20px; "></span></td>
			<td align="left"><span class="style5">'.$PRqst_reference.'</span></td>
			<td align="center"><span class="style5">'.$PRqst_date.'</span></td>
			<td align="center"><span class="style5">'.$PRqst_RequestBy.'</span></td>';
			
			if ($Module_PurchaseRequestPrice==1) {
			echo'<td align="center"><span class="style5">'.$Currency_code.'</span></td>
			<td align="right"><span class="style5">'.number_format($NetTotal, $DecimalBase).'</span></td>';
			echo'<td align="left"><span class="style5">'.$UserAccount.'</span></td>';
			}
			else 
			echo'<td colspan=3 align="left"><span class="style5">'.$UserAccount.'</span></td>';
		echo'</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"	<tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; width:800px;' valign='top'><table width='100%' border=0 height=28px>
		<tr><td style='width:380px;'>&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."','".$category."')>";
				
		$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."','".$category."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td width='150' align='center'>";
		
		
		if ($startrange <= $totalrows){
			$startrange = $startrange + $perpage;
			if  ($startrange <= $totalrows){
				echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange."','".$perpage."','".$category."')>";
				echo"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."','".$category."')>";	
			}	
		} 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;

?>