<?php

session_start();
if($HTTP_SESSION_VARS['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($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");


if ($TransType=='Tr'){
$querylog=" select ifNull(FI_ITransferDel,0) As FI_ITransferDel, ifNull(FI_TransferMod,0) As FI_TransferMod, 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);
}
//Access Mode On Insert/Edit/Delete
$TrDelMode = '';
$TrEditMode = '';
if ($FI_ITransferDel == 0)
	$TrDelMode = ' disabled ';
if ($FI_TransferMod == 0)
	$TrEditMode = ' disabled ';	
}
else {
$querylog=" select ifNull(FI_IRmTransferDel,0) As FI_IRmTransferDel, ifNull(FI_RmTransferMod,0) As FI_RmTransferMod, 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);
}
//Access Mode On Insert/Edit/Delete
$TrDelMode = '';
$TrEditMode = '';
if ($FI_IRmTransferDel == 0)
	$TrDelMode = ' disabled ';
if ($FI_RmTransferMod == 0)
	$TrEditMode = ' disabled ';	
}



$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('_',$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;	

$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%') ".
			  " where user_account='".$HTTP_SESSION_VARS['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 ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%') ".
			   " where user_account='".$HTTP_SESSION_VARS['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 ".$HTTP_SESSION_VARS['dbLabel'].".ledgerstk  ".
			  " where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
			  " 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];

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

function clearform()
{
  
  document.search.action='form_rentinvoiceSearchpending.php';
  document.search.submit();

}


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

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


function DeleteRecord(TrId,TransType){

	if (TrId != '') {
		if (confirm ( "Are you sure you want to Delete this transfer search pending?") )
		{
			document.search.action="form_rentinvoice_actions.php?actionmode=deletepending&pendingtransferrequest_id="+TrId+'&TransType='+TransType;
			document.search.submit();
		}
	}
	
}

function UpdateRecord(TrId,TransType){
		
	//document.search.action="Form_Transfer_Actions.php";
	opener.document.transferrequest.transferrequest_id.value=TrId;
	opener.document.transferrequest.actionmode.value='edit';
	opener.document.transferrequest.submit();
	self.close();
	}

function PrintPreview(TrId)
{
	var str = '';
	
	str = 'Form_rentinvoicPreview.php?transfer_id='+TrId;
	window.open(str);

}

function ChangeLocation(TransType)
{
    if (document.search.PMode.value == 1) {
		opener.document.transferrequest.action='Form_rentinvoice.php?TransType='+TransType;
		opener.document.transferrequest.submit();
		self.close();
	}	
}

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

function SetLastPage(perpage,totalrows,TransType){
	var result=totalrows % perpage;
	if (result == 0) result = perpage;
	startrange=totalrows-result;
	document.search.action='form_rentinvoicesearchpending.php?startrange='+startrange+'&perpage='+perpage+'&TransType='+TransType;
	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;
}
.style11 {color: #999999; font-weight: bold; }
.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}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #000000 ;font-size: 10px;}

</style>

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

echo"<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed' >";
echo"<tr>
		<td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
	  	<td id='search-top2' style='height:34px; width:950px' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>List of Pending	";
			if ($TransType=='Rm')
			echo "Rm";
		echo" Transfer 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' style='table-layout:fixed' width='950px'>
	  
	  <col width=20>
	  <col width=20>
	  
	 <col width=100> 
	  <col width=100>
	  <col width=100>
	  <col width=145>
	  <col width=140>
	  <col width=140>
	  <col width=140>";

	  
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'>&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'>Project From</span></td>
		<td  height='25' align='center'><span class='style4'>Project To</span></td>
		<td  height='25' align='center'><span class='style4'>Warehouse From</span></td>
		<td  height='25' align='center'><span class='style4'>Warehouse To</span></td>
		<td  height='25' align='center'><span class='style4'>CostCenter</span></td>";
		
		
	  echo"</tr>";
		

	

	
if (empty($ViewPeriodDate))
   $ViewPeriodDate = 'Null';
if (empty($ViewNumberOfDays))   
   $ViewNumberOfDays = 1;
			
					
$querysearch=" select transferrequest_reference,date_format(transferrequest_date,'%d-%m-%Y') as datte1,transferrequest_remark,".
					"project_code1,project_code2,Costcent.Costcent_name ,".
			 " Project1.Project_Name As ProjectName ,
			 Project2.Project_Name As ProjectName2 ,
			 warehouse1.warehouse_name  as warehousefrom, warehouse2.warehouse_name as warehouseto,
			 driver_name,vehicle_no , transferrequest_id from Temp_transferrequest ".
			 " Left Join Project Project1 on Temp_Transferrequest.Project_Code1 = Project1.Project_Code ".
			 " Left Join Project Project2 on Temp_Transferrequest.Project_Code2 = Project2.Project_Code ".
			 " Left Join warehouse warehouse1 on Temp_Transferrequest.Transferrequest_WarehouseFrom = warehouse1.warehouse_Code ".
			 " Left Join warehouse warehouse2 on Temp_Transferrequest.Transferrequest_WarehouseTo = warehouse2.warehouse_Code ".
			 " Left Join Costcent on Temp_Transferrequest.costcenter_code = Costcent.Costcent_code ";
		 
			 
if ($projectexist >	0)
  $querysearch= $querysearch." left join ( select distinct project_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
							 "			   where loginstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
							 "             and (project_code is not null and project_code != '') ".
							 "			 ) Q1 on Temp_Transferrequest.project_code1 like concat(Q1.project_code,'%') ";

if ($projectexist >	0)
  $querysearch= $querysearch." left join ( select distinct project_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
							 "			   where loginstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
							 "             and (project_code is not null and project_code != '') ".
							 "			 ) Q2 on Temp_Transferrequest.project_code2 like concat(Q2.project_code,'%') ";

							 
if ($costcentexist > 0)
  $querysearch= $querysearch." left join ( select distinct costcent_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
							 "			   where loginstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
							 "             and (costcent_code is not null and costcent_code != '') ".
							 "			 ) Q3 on Temp_Transferrequest.costcenter_code like concat(Q3.costcent_code,'%') ";							 

$querysearch= $querysearch.											 
			 //" Where Transfer_Reference = ifNull($PRef, Transfer_Reference) ".
			 " Where 1=1 ".
			 " and Temp_transferrequest.Transferrequest_Type= '".$TransType."'".
			 " And (($Login_ModifyAllPending = 0 and Temp_transferrequest.Logged_Code = '".$HTTP_SESSION_VARS['useraccount']."') or ($Login_ModifyAllPending = 1 and 1 = 1)) ";
			 " And (($rowsperiod = 0 and 1 = 1) Or ($rowsperiod > 0 and (($LockViewPeriod = 1 and  Date_Format(Transferrequest_Date,'%Y-%m-%d') >= Date_Format(ifNull('".$ViewPeriodDate."',Transferrequest_Date),'%Y-%m-%d')) Or ($LockViewPeriod = 0 and 1=1 ))   ".
             "                                                      and (($ViewLockDaily = 1 and DateDiff(Date_Format(Now(),'%Y-%m-%d'),Date_Format(Transferrequest_Date,'%Y-%m-%d')) <= $ViewNumberOfDays) Or ($ViewLockDaily = 0 and  1 = 1 ))) ) ";
//$querysearch= $querysearch." and temp_transfer.logged_code = '".$HTTP_SESSION_VARS['useraccount']."'";
$querysearch= $querysearch." Order By Transferrequest_Reference Desc ";
			 
//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.'>       
			<td align="center"><span class="style5"><img src="img/edit.png" onMouseOver="style.cursor=\'hand\'" style="width: 20px; height: 20px; " onclick="UpdateRecord('.htmlentities($rows[12]).',\''.$TransType.'\');" title="Edit Record"  ></span></td>
			<td align="center"><span class="style5"><img src="img/delete.png" onMouseOver="style.cursor=\'hand\'" style="width: 20px; height: 20px; " onclick="DeleteRecord('.htmlentities($rows[12]).',\''.$TransType.'\');" title="Delete Record" ></span></td>
		
		
			<td align="left"><span class="style5">'.$rows[0].'</span></td>
			<td align="left"><span class="style5">'.$rows[1].'</span></td>
			<td align="left"><span class="style5">'.$rows[6].'</span></td>
			<td align="left"><span class="style5">'.$rows[7].'</span></td>
			<td align="left"><span class="style5">'.$rows[8].'</span></td>
			<td align="left"><span class="style5">'.$rows[9].'</span></td>
			<td align="left"><span class="style5">'.$rows[5].'</span></td>';
			
			
		echo'</tr>';
}
							

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

echo"   <tr ><td id='search-content1'>&nbsp;</td><td id='search-content2'>&nbsp;</td><td id='search-content3'>&nbsp;</td></tr>";
echo"	<tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; width:908px;' 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."','".$TransType."')>";

		$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."','".$TransType."')>";
		}
		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."','".$TransType."')>";
				echo"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."','".$TransType."')>";	
			}
		} else
			echo "&nbsp;";
	
			
		echo"</td><td style='width:380px;'>&nbsp;</td></tr>";
		echo"</table></td><td id='search-bottom3' style='height:34px; width:31px;'>&nbsp;</td>";

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

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

HERE;

?>