<?php
session_start();

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


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

}


?>

<!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=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>List of Ledgers</title>
<script type="text/javascript" language="javascript">

function clearform()
{
  document.form1.PLedger_number.value='';
  document.form1.PLedger_name.value=''

}
function fillfield(value1,value2){
	x = document.form1;
	item1 = x.item1.value;
	

	if(item1=="1"){
		opener.document.form1.fromledger.value = value1;
		opener.document.form1.fromledgername.value = value2;
		opener.document.form1.toledger.value = value1;
		opener.document.form1.toledgername.value = value2;
	}
	else if(item1=="2"){
		opener.document.form1.toledger.value = value1;
		opener.document.form1.toledgername.value = value2;
	}
	else if(item1=="3"){
		opener.document.invoices.fromledger.value = value1;
		opener.document.invoices.fromledgername.value = value2;
	}
	else if(item1=="4"){
		opener.document.invoices.toledger.value = value1;
		opener.document.invoices.toledgername.value = value2;
	}
	else if(item1=="5"){
		opener.document.invoices.fromledger.value = value1;
		opener.document.invoices.fromledgername.value = value2;
	}

	self.close();
}


</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;
	font-size: 13px;
}
.style3 {color: #999999}
-->
</style></head>

<body>
<form id="form1" name="form1" method="post" action="">
  <table width="680" border="0" align="center" cellpadding=0 cellspacing=1>
    <tr>
      <th colspan="5" bgcolor="#DDDDDD"><span class="style1"> LEDGERS </span></th>
    </tr>
    <tr>
      <td bgcolor="#DDDDDD"> <span style="font-size: 11px; color: #999999; font-weight: bold;">Search for Ledger  <br> Number / Name</span></td>
      <td bgcolor="#DDDDDD"><input type="text" name="PLedger_number" value="<?php echo $PLedger_number; ?>"  />
        <input name="code1" type="hidden" id="code1"  value="<?php echo $Ledger_number; ?>">
		<input name="item1" type="hidden" id="item1" value="<?php echo $_GET['item']; ?>" />
		<input name="accountoption" type="hidden" id="accountoption" value="<?php echo $_GET['accountoption']; ?>" />
	  </td>
		
      <td bgcolor="#DDDDDD"><input type="text" name="PLedger_name"  value="<?php echo $PLedger_name; ?>" size='35'/>
	    <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;'/>
        <input name="desc1" type="hidden" id="desc1" value="<?php echo $Ledger_name; ?>" />
	  </td>
    </tr>
    <tr bgcolor="#E5E5E5">
      <td width="136"><span class="style2">Ledger Number </span></td>
      <td width="350" colspan="2"><span class="style2">Ledger Name </span></td>
    </tr>
<?php

//maroun
if ($accountoption==2 or $accountoption==3)
{
	$querysalesmen= " select distinct clientsalesmen ".
				    " from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ". 
				    " where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
				    " and (clientsalesmen<>''  or clientsalesmen is not null) "  ;
	$salesmenresult = mysql_query($querysalesmen,$connection) or die(mysql_error());
	$salesmenexist  = mysql_num_rows($salesmenresult);
	
	
 
}
//maroun
if ($accountoption==2)
{
	$querymasterproject=" select distinct Masterproject from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
						" where loginstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
						" and (Masterproject is not null and Masterproject != '') ";													 
	$projectmasterresult = mysql_query($querymasterproject,$connection) or die(mysql_error());
	$projectmasterexist   = mysql_num_rows($projectmasterresult);

	$querymastercostcenter=" select distinct Mastercostcenter from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
						   " where loginstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
						   " and (Mastercostcenter is not null and Mastercostcenter != '') ";
	$costcentermasterresult = mysql_query($querymastercostcenter,$connection) or die(mysql_error());
	$costcentermasterexist   = mysql_num_rows($costcentermasterresult);
}


$query1=" select Ledger_number, Ledger_name from suppliers ".
        " where Ledger_number like '".$PLedger_number."%' ".
		" and Ledger_name like '%".$PLedger_name."%' ".
		" and $accountoption = 1 ".
		" union ".
		" select Ledger_number, Ledger_name from clients ".
        " where Ledger_number like '".$PLedger_number."%' ".
		" and Ledger_name like '%".$PLedger_name."%' ".
		" and $accountoption = 2 ";
		
		//maroun	
		/*if ($salesmenexist > 0 and $accountoption==2)
			$query1 = $query1 . " and clients.Salesmen_Code in 
								   ( select distinct clientsalesmen from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk  where user_account = '".$HTTP_SESSION_VARS['useraccount']."'
								     and (clientsalesmen <>'' and clientsalesmen is not null)
								   )";	*/
		//maroun
		if ($costcentermasterexist > 0) {	
				$query1 = $query1 ." and clients.CostCenter_Code in ( select distinct Mastercostcenter from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
								   "			  where loginstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
								   "              and (Mastercostcenter is not null and Mastercostcenter != '') ) ";
										}
		//maroun								
		if ($projectmasterexist > 0) {	
				$query1= $query1."  and clients.project_code in 		( select distinct Masterproject from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
															 "			   where loginstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
															 "             and (Masterproject is not null and Masterproject != '')) ";
															 
									}
										

$query1 = $query1 ." union ".
		" select Ledger_number, Ledger_name from salesmen ".
        " where Ledger_number like '".$PLedger_number."%' ".
		" and Ledger_name like '%".$PLedger_name."%' ".
		" and $accountoption = 3  ";
		
		//maroun	
		if ($salesmenexist > 0 and $accountoption==3)
			$query1 = $query1 . " and salesmen.ledger_number in 
								  ( select distinct clientsalesmen from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk  where user_account = '".$HTTP_SESSION_VARS['useraccount']."'
								    and (clientsalesmen<>'' and clientsalesmen is not null))";

$query1 = $query1 ." union ".
		" select Ledger_number, Ledger_name from Ledger ".
        " where Ledger_number like '".$PLedger_number."%' ".
		" and Ledger_name like '%".$PLedger_name."%' ".
		" and $accountoption = 0 and ifnull(Ledger_grpdet,0) = 0 ";
		
$query1 = $query1 ." order by Ledger_number ";

 
		

$result1=mysql_query($query1,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($result1)){
	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."'";	
		
	
	$desc=ereg_replace("'","%27",$Ledger_name);
	$desc=ereg_replace("\"","%27",$desc);
	
	echo"<tr $Color>";
    echo"  <td align='left'><a href='#' onclick=\"fillfield('".$Ledger_number."','".$desc."');\" >".$Ledger_number."</a></td>
	       <td align='left' colspan='2'><a href='#' onclick=\"fillfield('".$Ledger_number."','".$desc."');\" >".$Ledger_name."</a></td>
        </tr>";	

}		
?>

  </table>
</form>
</body>
</html>
