<?php
session_start();

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


$query1="select grid_color1,grid_color2 from setup";
$result1=mysql_query($query1,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($result1)){
	extract($rows);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_ProjectParent,0) As Module_ProjectParent,ifNull(Module_RestrictProjectReport,0) As Module_RestrictProjectReport From Module";

$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

$queryExcluded = "select project_code from ".$_SESSION['dbLabel'].".projectstk where Upper(user_account) = Upper('".$_SESSION['useraccount']."') and ifnull(project_include,0) = 0 ";
$resExcluded = mysql_query($queryExcluded, $connection);
$numExcluded = mysql_num_rows($resExcluded);

$item   		= $_GET['item']; 			if ($item == ""){$item = $_POST["item"];}
$frm	   		= $_GET['frm']; 			if ($frm == ""){$frm = $_POST["frm"];}
$index			= $_GET['index']; 			if ($index == "") $index = $_POST['index'];
$chkTrans		= $_GET['chkTrans']; 		if ($chkTrans == "") $chkTrans = $_POST['chkTrans'];
$PProject_Code  = $_POST["PProject_Code"]; 	if ($PProject_Code == ""){$PProject_Code = $_GET["PProject_Code"];}
$PProject_Name  = $_POST["PProject_Name"]; 	if ($PProject_Name == ""){$PProject_Name = $_GET["PProject_Name"];}



if($_GET['lcode']!=""){

	$query1=" select distinct project_code, project_name, project_memo from project ".
			" where project_code ='".$_GET['lcode']."'";
					  
	$result1=mysql_query($query1,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result1)){
		extract($rows);

	}

	$_GET['post']="1";

} 

//User Access
$querylog=" select ifNull(FA_Project,0) As FA_Project, ifNull(FA_ProjectApp,0) As FA_ProjectApp, ifNull(FI_RepRestrictProject,0) as FI_RepRestrictProject ".
          " 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());
		  
//echo $querylog;
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}



?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>List of Projects</title>
<script type="text/javascript" src="js/grid/UI_DataGrid.js"></script>
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>

<script type="text/javascript" language="javascript">

function clearform()
{
	document.form1.PProject_Code.value='';
	document.form1.PProject_Name.value='';
	document.form1.action='List_SorProject.php';
	document.form1.submit();
}

function openprojectform(){

 win = window.open('./Form_Project.php?mode=insert', 'Form_Project', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=1000,height=600,top=100,left=100');		
	win.creator = self;
	win.focus();
}

</script>
<style type="text/css">
<!--
body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 14px;
}
.style1 {
	color: #333333;
	font-weight: bold;
}
.style2 {
	color: #999999;
	font-weight: bold;
}
.style3 {color: #999999; font-size: 13px;}
-->
</style></head>

<body>
<form id="form1" name="form1" method="post" action="">
  <table width="800" border="0" cellspacing="1" align="center">
    <tr>
      <th colspan="5" bgcolor="#DDDDDD"><span class="style1"> PROJECTS </span></th>
    </tr>
    <tr>
      <td bgcolor="#DDDDDD"> <span style="font-size: 11px; color: #999999; font-weight: bold;">Search for Project  <br> Code / Name</span></td>
      <td bgcolor="#DDDDDD"><input type="text" name="PProject_Code" value="<?php echo $PProject_Code; ?>"  />
        <input name="code1" type="hidden" id="code1"  value="<?php echo $project_code; ?>">
      <td bgcolor="#DDDDDD"><input type="text" name="PProject_Name"  value="<?php echo $PProject_Name; ?>" size='35'/>
        <input name="desc1" type="hidden" id="desc1" value="<?php echo $project_name; ?>" />
		<input name="projectmemo" type="hidden" id="projectmemo" value="<?php echo $project_memo; ?>" />
	    <input type="submit" name="Submit" value="Find" style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
		<input type="button" name="cancelbtn" value="Cancel" onclick='clearform();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/></td>
      <td bgcolor="#DDDDDD">
	   
	<?php
	//Gilbert 20-11-2013
	if($FA_ProjectApp==1 && $FA_Project==1){
		echo"<input type='button' name='addbtn' value='Add Project' onclick='openprojectform();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>";
	}
	?>
		<input name="item" type="hidden" id="item" value="<?php echo $item; ?>" />
		<input name="frm" type="hidden" id="frm" value="<?php echo $frm; ?>" />
		<input name="index" type="hidden" value="<?php echo $index; ?>">
		<input name="chkTrans" type="hidden" value="<?php echo $chkTrans; ?>">
	  </td>
    </tr>
<?php if($_GET['post']=="1") { ?>
<script type="text/javascript" language="javascript">
var FormOption = document.form1.frm.value;
var FormItem   = document.form1.item.value;
var vIndex	   = document.form1.index.value; 

if (FormOption == 1) {
	opener.document.salesorder.txtProjectCode.value=document.form1.code1.value;
	opener.document.salesorder.txtIAddress.value=document.form1.projectmemo.value;
}	
else if (FormOption == 2)
	opener.document.sales.txtProjectCode.value=document.form1.code1.value;
else if (FormOption == 3)
	opener.document.sales.txtProjectCode.value=document.form1.code1.value;		
else if (FormOption == 4)
	opener.document.trans.txtProjectCode.value=document.form1.code1.value;
else if (FormOption == 5)
	opener.document.cstjourdt.txtProjectCodeDt.value=document.form1.code1.value;
else if (FormOption == 6)
	opener.document.search.PProjectCode.value=document.form1.code1.value;
else if (FormOption == 7)
	opener.document.production.txtProjectCode.value=document.form1.code1.value;	
else if (FormOption == 8)
	opener.document.adjustment.txtProjectCode.value=document.form1.code1.value;		
else if (FormOption == 9)
	opener.document.returnsales.txtProjectCode.value=document.form1.code1.value;		
else if (FormOption == 10)
	opener.document.returnsales.txtProjectCode.value=document.form1.code1.value;
else if (FormOption == 11){
	opener.document.getElementById('txtProjectCode').value=document.form1.code1.value;
	if(opener.document.getElementById('txtGitLedger'))
	window.opener.document.getElementById('txtGitLedger').onchange();

}
else if (FormOption == 12)
	opener.document.purchasesjv.txtProjectCode.value=document.form1.code1.value;	
else if (FormOption == 13 && FormItem == 1) {
	opener.document.prepayment.txtProjectCode.value=document.form1.code1.value;
	
	if (opener.document.getElementById('txtProjectCodeD'))
		opener.document.prepayment.txtProjectCodeD.value=document.form1.code1.value;
}	
else if (FormOption == 13 && FormItem == 2 && opener.document.getElementById('txtProjectCodeD'))
	opener.document.prepayment.txtProjectCodeD.value=document.form1.code1.value;	
else if (FormOption == 14)
	opener.document.paymentrqst.txtProjectCode.value=document.form1.code1.value;
else if (FormOption == 15)
	opener.document.cstrqst.txtProjectCodeDt.value=document.form1.code1.value;
else if (FormOption == 16)
	opener.document.adjustmentdt.txtProjectCodeDt.value=document.form1.code1.value;	
else if (FormOption == 17) {
	if (opener.document.form1) {
	opener.document.form1.fromproject.value=document.form1.code1.value;	
	if (opener.document.form1.fromprojectname)
		opener.document.form1.fromprojectname.value=document.form1.desc1.value;	
	} else if (opener.document.budget) {
		opener.document.budget.fromproject.value=document.form1.code1.value;			
	}
}	
else if (FormOption == 18) {
	opener.document.form1.clientproject.value=document.form1.code1.value;	
	opener.document.form1.clientprojectname.value=document.form1.desc1.value;	
}	
else if (FormOption == 19)
	opener.document.purchasedt.txtProjectCodeDt.value=document.form1.code1.value;	
else if (FormOption == 20)
	opener.document.salesorderdt.txtProjectCodeDt.value=document.form1.code1.value;
else if (FormOption == 21)
	opener.document.salesdt.txtProjectCodeDt.value=document.form1.code1.value;
else if (FormOption == 22)
	opener.document.returnsalesdt.txtProjectCodeDt.value=document.form1.code1.value;
else if (FormOption == 23)
	opener.document.productiondt.txtProjectCodeDt.value=document.form1.code1.value;
else if (FormOption == 25 && FormItem == 1){
	var idholder = opener.document.getElementById('projectcodedt['+vIndex+']');
	
	opener.document.getElementById('projectcodedt['+vIndex+']').value=document.form1.code1.value;
	opener.document.getElementById('projectcodedt['+vIndex+']').focus();
	fireTrigger(idholder,'change');
}
else if (FormOption == 25 && FormItem == 2){
	var idholder = opener.document.getElementById('projectcodedt2['+vIndex+']');
	
	opener.document.getElementById('projectcodedt2['+vIndex+']').value=document.form1.code1.value;
	opener.document.getElementById('projectcodedt2['+vIndex+']').focus();
	fireTrigger(idholder,'change');
}	
else if (FormOption == 25 && FormItem == 3){
	opener.document.getElementById('journaldtdistcode['+vIndex+']').value=document.form1.code1.value;
	opener.document.getElementById('journaldtdistcode['+vIndex+']').focus();
	
}	
else if (FormOption == 26){
	var idholder = opener.document.getElementById('txtProjectCode');
	opener.document.journal.txtProjectCode.value=document.form1.code1.value;
	fireTrigger(idholder,'change');
}
else if (FormOption == 27)
	opener.document.trans.txtProjectCode.value=document.form1.code1.value;
else if (FormOption == 28 && FormItem == 1){
	var idholder = opener.document.getElementById('projectcodedtfg['+vIndex+']');
	
	opener.document.getElementById('projectcodedtfg['+vIndex+']').value=document.form1.code1.value;
	opener.document.getElementById('projectcodedtfg['+vIndex+']').focus();
	fireTrigger(idholder,'change');
}
else if (FormOption == 28 && FormItem == 2){
	var idholder = opener.document.getElementById('projectcodedtrm['+vIndex+']');
	
	opener.document.getElementById('projectcodedtrm['+vIndex+']').value=document.form1.code1.value;
	opener.document.getElementById('projectcodedtrm['+vIndex+']').focus();
	fireTrigger(idholder,'change');
}	
else if (FormOption == 29 && FormItem == 1){
	var idholder = opener.document.getElementById('fromproject');
	
	opener.document.getElementById('fromproject').value=document.form1.code1.value;
	opener.document.getElementById('fromprojectdesc').value=document.form1.desc1.value;
	opener.document.getElementById('fromproject').focus();
	fireTrigger(idholder,'change');
}		else if (FormOption == 29 && FormItem == 2){
	var idholder = opener.document.getElementById('clientproject');
	
	opener.document.getElementById('clientproject').value=document.form1.code1.value;
	opener.document.getElementById('clientprojectdesc').value=document.form1.desc1.value;
	opener.document.getElementById('clientproject').focus();
	fireTrigger(idholder,'change');
}		else if (FormOption == 29 && FormItem == 3){
	var idholder = opener.document.getElementById('toproject');
	
	opener.document.getElementById('toproject').value=document.form1.code1.value;
	opener.document.getElementById('toprojectdesc').value=document.form1.desc1.value;
	opener.document.getElementById('toproject').focus();
	fireTrigger(idholder,'change');
} else if (FormOption == 30)
	opener.document.porderjv.txtProjectCode.value=document.form1.code1.value;	
else if (FormOption == 31)
	opener.document.form.project_code.value=document.form1.code1.value;	

else if (FormOption == 32) {
	opener.document.production.txtProjectCode.value=document.form1.code1.value;	
} else if (FormOption == 33){
	 if(FormItem == 1)
	opener.document.getElementById('projectcodedt['+vIndex+']').value=document.form1.code1.value;	
	else if(FormItem == 2)
	opener.document.getElementById('projectcodedt2['+vIndex+']').value=document.form1.code1.value;	
}
else if (FormOption == 34) {
	opener.document.FinancialReportDt.projectcode.value=document.form1.code1.value;	
	opener.document.FinancialReportDt.projectdesc.value=document.form1.desc1.value;	
} 
else if (FormOption == 35) {
	opener.document.form1.fromproject.value=document.form1.code1.value;	
	opener.document.form1.fromprojectname.value=document.form1.desc1.value;	
} else if (FormOption == 36) {
	opener.document.vehicle.project_code.value=document.form1.code1.value;	
	opener.document.vehicle.project_name.value=document.form1.desc1.value;	
} else if (FormOption == 37) {
	opener.document.form.project.value=document.form1.code1.value;	
} else if (FormOption == 38){  
	 if(FormItem == 1){
		opener.document.getElementById('fromproject').value=document.form1.code1.value;	
		if (opener.document.getElementById('fromprojectname'))
			opener.document.getElementById('fromprojectname').value = document.form1.desc1.value;	
	 }
	else if(FormItem == 2){
		opener.document.getElementById('toproject').value=document.form1.code1.value;	
		if (opener.document.getElementById('toprojectname'))
			opener.document.getElementById('toprojectname').value = document.form1.desc1.value;	
	}
}
else if (FormOption == 39){
	opener.document.production.mat_project.value=document.form1.code1.value;	
	opener.document.production.mat_projectdesc.value=document.form1.desc1.value;	
 }
 else if (FormOption == 40){
	opener.document.qtybudget.project_code.value=document.form1.code1.value;	
 }else if (FormOption == 41){
	opener.document.getElementById('projectcode['+vIndex+']').value=document.form1.code1.value;	
 }
 else if (FormOption == 42){
	opener.document.getElementById('txtProjectCode').value=document.form1.code1.value;	
	opener.document.getElementById('txtProjectDesc').value=document.form1.desc1.value;	
 } 
 else if (FormOption == 43){
	opener.document.getElementById('tsprojectCode').value=document.form1.code1.value;
	if (opener.document.getElementById('tsprojectDesc'))
		opener.document.getElementById('tsprojectDesc').value=document.form1.desc1.value;	
 }
  else if (FormOption == 44){
	opener.document.getElementById('tvprojectCode').value=document.form1.code1.value;
	if (opener.document.getElementById('tvprojectDesc'))
		opener.document.getElementById('tvprojectDesc').value=document.form1.desc1.value;	
 }
  else if (FormOption == 45){
	if (opener.document.getElementById('olProjectCode')) 
	opener.document.getElementById('olProjectCode').value=document.form1.code1.value;
	if (opener.document.getElementById('olProjectDesc'))
		opener.document.getElementById('olProjectDesc').value=document.form1.desc1.value;
	
	if (opener.document.getElementById('lwProjectCode')) 
	opener.document.getElementById('lwProjectCode').value=document.form1.code1.value;
	if (opener.document.getElementById('lwProjectDesc'))
		opener.document.getElementById('lwProjectDesc').value=document.form1.desc1.value;	
	
	if (opener.document.getElementById('lfProjectCode')) 
	opener.document.getElementById('lfProjectCode').value=document.form1.code1.value;
	if (opener.document.getElementById('lfProjectDesc'))
		opener.document.getElementById('lfProjectDesc').value=document.form1.desc1.value;	
	
	if (FormItem == 1){
		opener.document.getElementById('fromproject').value=document.form1.code1.value;
		opener.document.getElementById('fromprojectdesc').value=document.form1.desc1.value;	
	} 
	if (FormItem == 2){
		opener.document.getElementById('toproject').value=document.form1.code1.value;
		opener.document.getElementById('toprojectdesc').value=document.form1.desc1.value;	
	} 
	
 }
 else if(FormOption == 46){
		opener.document.clientdt.txtProjectCode.value=document.form1.code1.value;
	}
	else if(FormOption == 47){
		opener.document.form1.fromproject.value=document.form1.code1.value;
		opener.document.form1.fromprojectname.value=document.form1.desc1.value;
		opener.document.form1.toproject.value=document.form1.code1.value;
		opener.document.form1.toprojectname.value=document.form1.desc1.value;
	}
	else if(FormOption == 48){
		opener.document.form1.toproject.value=document.form1.code1.value;
		opener.document.form1.toprojectname.value=document.form1.desc1.value;	}
self.close();

</script>
<?php } ?>
    <tr bgcolor="#E5E5E5">
      <td width="136"><span class="style2">Project Code </span></td>
      <td width="411" colspan="2"><span class="style2">Project Name </span></td>
      <td width="57"><span class="style3"></span></td>
    </tr>
<?php
		
//Projects List
$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 != '') ".
			  " and ifnull(project.project_deactivate,0) = 0 ".
			  " and project.project_code like ".GetSQLValueString($PProject_Code.'%','text')." ".
			  " and project_name like ".GetSQLValueString('%'.$PProject_Name.'%','text')." ";
if ($numExcluded > 0)			   
	$queryproject .= " and project.project_code not in (".$queryExcluded.") ";		
// return non blocked projects for production
if ($frm == 7 || $frm == 39) 
	$queryproject .= " and ifnull(project_blocked,0) = 0 ";
	$queryproject .= " order by project.project_code";

//Master/Client Project Query
$queryproject1=" select distinct project.project_code, project_name ".
               " from project left join ".$_SESSION['dbLabel'].".loginstk on project.project_code like concat(ifnull(loginstk.masterproject,''),'%') ".
			   " where user_account='".$_SESSION['useraccount']."' ".
			   " and (loginstk.masterproject is not null and loginstk.masterproject != '') ".
			   " and ifnull(project.project_deactivate,0) = 0 ".
			   " and project.project_code like ".GetSQLValueString($PProject_Code.'%','text')." ".
			   " and project_name like ".GetSQLValueString('%'.$PProject_Name.'%','text')." ";
if ($numExcluded > 0)			   
	$queryproject1 .= " and project.project_code not in (".$queryExcluded.") ";		
	$queryproject1 .= " order by project.project_code";	
	
if ($frm == 29 && $Module_RestrictProjectReport == 1 ){
$queryproject2=" select distinct project.project_code, project_name ".
              " from project left join ".$_SESSION['dbLabel'].".loginstk on project.project_code like concat(loginstk.project_code,'%') ".
			  " where user_account='".$_SESSION['useraccount']."' ".
			  " and (loginstk.project_code is not null and loginstk.project_code != '') ";
if ($numExcluded > 0)			   
	$queryproject2 .= " and project.project_code not in (".$queryExcluded.") ";	
	$queryproject2 .= " order by project.project_code";
			  
$projectresult = mysql_query($queryproject2,$connection) or die(mysql_error());
$projectrows   = mysql_num_rows($projectresult);
if ($projectrows == 0 || $FI_RepRestrictProject == 0)
    $queryproject2=" select distinct project_code, project_name from project ";
	if ($numExcluded > 0)			   
	$queryproject2 .= " and project.project_code not in (".$queryExcluded.") ";
	$queryproject2 .= " order by project_code ";	
}
if ($frm == 18)
	$projectresult = mysql_query($queryproject1,$connection) or die(mysql_error());
if ($frm == 29 && $Module_RestrictProjectReport == 1)
	$projectresult = mysql_query($queryproject2,$connection) or die(mysql_error());
else
	$projectresult = mysql_query($queryproject,$connection) or die(mysql_error());
	
$projectrows   = mysql_num_rows($projectresult);

if ($projectrows == 0) {
    $queryproject=" select distinct project_code, project_name from project ".
	              " where project.project_code like ".GetSQLValueString($PProject_Code.'%','text')." ".
				  " and project_name like ".GetSQLValueString('%'.$PProject_Name.'%','text')." ".
				  " and ifnull(project.project_deactivate,0) = 0 ";
	if ($numExcluded > 0)			   
		$queryproject .= " and project.project_code not in (".$queryExcluded.") ";		  
	// return non blocked projects for production
	if ($frm == 7 || $frm == 39) 
		$queryproject .= " and ifnull(project_blocked,0) = 0 ";			  
		$queryproject .= " order by project_code ";
	
	$projectresult=mysql_query($queryproject,$connection) or die(mysql_error());		
}

if ($frm == 45 && $chkTrans == 1){
	$queryproject=" select distinct project_code, project_name from project ".
				  " inner join logistic_transport on LT_ProjectCode = project_code ".
	              " where project.project_code like ".GetSQLValueString($PProject_Code.'%','text')." ".
				  " and project_name like ".GetSQLValueString('%'.$PProject_Name.'%','text')." ";
	$projectresult=mysql_query($queryproject,$connection) or die(mysql_error());			  
}	
if ($frm == 45 && $chkTrans == 2){
	$queryproject=" select distinct project.project_code as project_code, project_name from project ".
				  " inner join logistic_operations on logistic_operations.project_code = project.project_code ".
	              " where project.project_code like ".GetSQLValueString($PProject_Code.'%','text')." ".
				  " and project_name like ".GetSQLValueString('%'.$PProject_Name.'%','text')." and logistic_operations.client_code = '".$_SESSION['useraccount']."' ";
	$projectresult=mysql_query($queryproject,$connection) or die(mysql_error());			  
}

while($rows=mysql_fetch_array($projectresult)){
	extract($rows);

	$ct+=1;

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

?>
    <tr <?php echo $Color; ?>>
      <td align="left"><span class="style3"><a href="List_SorProject.php?lcode=<?php echo $project_code; ?>&frm=<?php echo $frm; ?>&item=<?php echo $item; ?>&index=<?php echo $index; ?>&chkTrans=<?php echo $chkTrans; ?>"><?php echo $project_code; ?></a></span></td>
      <td colspan="2"><span class="style3"><a href="List_SorProject.php?lcode=<?php echo $project_code; ?>&frm=<?php echo $frm; ?>&item=<?php echo $item; ?>&index=<?php echo $index; ?>&chkTrans=<?php echo $chkTrans; ?>"><?php echo $project_name; ?></a></span></td>
      <td>&nbsp;</td>
    </tr>
<?php
}
?>
  </table>
</form>
</body>
</html>
