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


if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

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

}

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

	$query1="select Ledger_number, Ledger_name from ledger where ledger_number='".$_GET['lcode']."'";
    $result1=mysql_query($query1,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result1)){
    	extract($rows);
		
	}

    $post="1";
}

?>

<!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=utf-8" />
<title>Select Ledgers</title>
<script type="text/javascript" language="javascript">

function clearform()
{
  document.form1.PLedger_Name.value='';
  document.form1.PLedger_Number.value='';

}
</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}
-->
</style></head>

<body>
<form id="form1" name="form1" method="post" action="">
  <table width="750" border="0" align="center">
    <tr>
      <th colspan="5" bgcolor="#DDDDDD"><span class="style1"> Ledgers </span></th>
    </tr>
    <tr>
      <td bgcolor="#DDDDDD" width="136"> <span style="font-size: 11px; color: #999999; font-weight: bold;">Search for Ledger  <br> Number / Name</span></td>
      <td bgcolor="#DDDDDD" width="100"><input type="text" name="PLedger_Number" value="<?php echo $PLedger_Number; ?>"  autofocus/>
        <input name="code1" type="hidden" id="code1"  value="<?php echo $Ledger_number; ?>">
		<!--my code I have added the hidden input code2
		<input name="code2" type="hidden" id="code2"  value="<?php echo $Ledger_number; ?>">--></td>
      <td bgcolor="#DDDDDD" width="450"><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; ?>" />
        <input name="item1" type="hidden" id="item1" value="<?php echo $_GET['item']; ?>" />
		<input name="index" type="hidden" id="index" value="<?php echo $_GET['index']; ?>" /></td>
    </tr>
<?php if($post=="1") { ?>
<script type="text/javascript" language="javascript">
x=document.form1;
item1 = x.item1.value;
index = x.index.value;

if(item1=="1"){
opener.document.form1.fromledger.value     =document.form1.code1.value;
opener.document.form1.fromledgername.value =document.form1.desc1.value;
opener.document.form1.toledger.value       =document.form1.code1.value;
opener.document.form1.toledgername.value   =document.form1.desc1.value;
self.close();
}else if(item1=="2"){
opener.document.form1.toledger.value     =document.form1.code1.value;
opener.document.form1.toledgername.value =document.form1.desc1.value;
self.close();
}
else if(item1=="3"){
opener.document.form1.fromledger.value     =document.form1.code1.value;
opener.document.form1.fromledgername.value =document.form1.desc1.value;
self.close();
}
else if(item1=="4"){
opener.document.Setup.fromledger.value     =document.form1.code1.value;
opener.document.Setup.fromledgername.value =document.form1.desc1.value;
self.close();
}
else if(item1=="5"){
opener.document.Setup.toledger.value       =document.form1.code1.value;
opener.document.Setup.toledgername.value   =document.form1.desc1.value;
self.close();
}

else if(item1=="6"){
opener.document.Setup.fromledgerdoe.value     =document.form1.code1.value;
self.close();
}
else if(item1=="7"){
opener.document.Setup.toledgerdoe.value       =document.form1.code1.value;
self.close();
}
else if(item1=="8"){
opener.document.Setup.fromledgerex.value       =document.form1.code1.value;
opener.document.Setup.fromledgernameex.value   =document.form1.desc1.value;
self.close();
}
else if(item1=="9"){
opener.document.Setup.fromledgeria.value       =document.form1.code1.value;
opener.document.Setup.fromledgernameia.value   =document.form1.desc1.value;
self.close();
}
else if(item1=="10"){
opener.document.Setup.fromledgercode.value   = document.form1.code1.value;
opener.document.Setup.fromledgerdesc.value   = document.form1.desc1.value;
self.close();
}
else if(item1=="11"){
opener.document.Setup.toledgercode.value	= document.form1.code1.value;
opener.document.Setup.toledgerdesc.value    = document.form1.desc1.value;
self.close();
}
else if(item1=="12"){
opener.document.budget.fromledger.value		   = document.form1.code1.value;
opener.document.budget.fromledgername.value    = document.form1.desc1.value;
if (opener.document.budget.toledger.value == ''){
	opener.document.budget.toledger.value		 = document.form1.code1.value;
	opener.document.budget.toledgername.value    = document.form1.desc1.value;
}
self.close();
}
else if(item1=="13"){
opener.document.budget.toledger.value		 = document.form1.code1.value;
opener.document.budget.toledgername.value    = document.form1.desc1.value;
self.close();
}
else if(item1=="14"){
opener.document.getElementById('ledgercode['+index+']').value = document.form1.code1.value;
self.close();
}
else if(item1=="15"){
opener.document.search.fromledger.value		 = document.form1.code1.value;
opener.document.search.toledger.value		 = document.form1.code1.value;
self.close();
}
else if(item1=="16"){
opener.document.search.toledger.value		 = document.form1.code1.value;
self.close();
}
/* else if(item1=="17"){
opener.document.transfer.txtChargesAccountDb.value		 = document.form1.code1.value;
self.close();
}
else if(item1=="18"){
opener.document.transfer.txtChargesAccountCd.value		 = document.form1.code1.value;
self.close();
} */

else if(item1=="19"){
opener.document.getElementById('ledgernumber['+index+']').value = document.form1.code1.value;
opener.document.getElementById('ledgername['+index+']').value   = document.form1.desc1.value;
self.close();
}

else if(item1=="20"){
	opener.document.getElementById('assetcrdt').value	= document.form1.code1.value;
	self.close();
}
else if(item1=="21"){
	opener.document.getElementById('assetdbt').value	= document.form1.code1.value;
	self.close();
}
else if(item1=="22"){
	opener.document.getElementById('fromledger').value	= document.form1.code1.value;
	opener.document.getElementById('fromledgername').value	= document.form1.desc1.value;
	opener.document.getElementById('toledger').value	= document.form1.code1.value;
	opener.document.getElementById('toledgername').value	= document.form1.desc1.value;
	self.close();
}
else if(item1=="23"){
	opener.document.getElementById('toledger').value	= document.form1.code1.value;
	opener.document.getElementById('toledgername').value	= document.form1.desc1.value;

	self.close();
}
else if(item1=="24"){
opener.document.getElementById('expledger'+index).value = document.form1.code1.value;
self.close();
}
else if(item1=="25"){
opener.document.getElementById('incledger'+index).value = document.form1.code1.value;
self.close();
}
else if(item1=="26"){
opener.document.getElementById('cliledger'+index).value = document.form1.code1.value;
self.close();
}
else if(item1=="27"){
opener.document.getElementById('txtGitLedger').value = document.form1.code1.value;
window.opener.document.getElementById('txtGitLedger').onchange();

self.close();
}
else if(item1=="28"){
opener.document.getElementById('txtExpLedger').value = document.form1.code1.value;
self.close();
}
else if(item1=="29"){
opener.document.getElementById('fromclient').value = document.form1.code1.value;
opener.document.getElementById('fromclientname').value = document.form1.desc1.value;
self.close();
}
else if(item1=="30"){
opener.document.getElementById('txtledgernocl['+index+']').value = document.form1.code1.value;
self.close();
}
else if(item1=="31"){
opener.document.getElementById('txtledgernoexp['+index+']').value = document.form1.code1.value;
self.close();
}


</script>
<?php } ?>
    <tr bgcolor="#E5E5E5">
      <td><span class="style2">Ledger Number </span></td>
      <td colspan="2"><span class="style2">Ledger Name </span></td>
    </tr>
<?php


 

$querylog=" select ifNull(login_belongstogrp,0) As FI_BelongsToGrp ".
          " from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."') ";
$resultlog=mysql_query($querylog) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

 

//Read From Module Table
$querymodule=" Select ifNull(Module_BelongsToGroup,0) As Module_BelongsToGroup From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

$queryledger= " select distinct ledger_number, ledger_include ".
              " from ".$_SESSION['dbLabel'].".ledgerrepstk  ".
			  " 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];

//maroun
if ($accountoption==4)
{
$querymasterproject=" select distinct Masterproject from ".$_SESSION['dbLabel'].".loginstk ".
					" where loginstk.user_account='".$_SESSION['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 ".$_SESSION['dbLabel'].".loginstk ".
					   " where loginstk.user_account='".$_SESSION['useraccount']."' ".
					   " and (Mastercostcenter is not null and Mastercostcenter != '') ";
$costcentermasterresult = mysql_query($querymastercostcenter,$connection) or die(mysql_error());
$costcentermasterexist   = mysql_num_rows($costcentermasterresult);
}

$querysalesmen=" select distinct clientsalesmen 
			from ".$_SESSION['dbLabel'].".loginstk ".
			  " where user_account='".$_SESSION['useraccount']."' 
			  and clientsalesmen<>'' 
			  or clientsalesmen <> null
			  "  ;

$salesmenresult = mysql_query($querysalesmen,$connection) or die(mysql_error());
$salesmenexist   = mysql_num_rows($salesmenresult);

if ($salesmenexist==0)
{
$query1=" select Ledger.Ledger_number, Ledger.Ledger_name from ledger ";

	if (($costcentermasterexist > 0 or $projectmasterexist > 0  ) and $accountoption==4)
	$query1 = $query1 . "inner join clients on ledger.ledger_number = clients.ledger_number ";
	//maroun
	if ($costcentermasterexist > 0 and $accountoption==4) {	
			$query1 = $query1 ."  and clients.CostCenter_Code in ( select distinct Mastercostcenter from ".$_SESSION['dbLabel'].".loginstk ".
			  				   "			  where loginstk.user_account='".$_SESSION['useraccount']."' ".
							   "              and (Mastercostcenter is not null and Mastercostcenter != '') ) ";
									}
	//maroun								
	if ($projectmasterexist > 0 and $accountoption==4) {	
			$query1= $query1 ."   and clients.project_code in ( select distinct Masterproject from ".$_SESSION['dbLabel'].".loginstk ".
														 "			   where loginstk.user_account='".$_SESSION['useraccount']."' ".
														 "             and (Masterproject is not null and Masterproject != '')) ";
								}
								
/*								
	if ($ledgerexist > 0) {
		$query1 = $query1." Inner join ( select distinct ledger_number from ".$_SESSION['dbLabel'].".ledgerrepstk ".
						  "			     where user_account='".$_SESSION['useraccount']."' ".
						  "              and (ledger_number is not null and ledger_number != '') ".
						  "			   )  Q3 on (($ledgerinclude = 1 and Ledger.Ledger_Number like concat(Q3.ledger_number,'%') ) or ($ledgerinclude = 0 and Ledger.Ledger_Number Not like concat(Q3.ledger_number,'%') )) ";	
	}
*/

	

							  
    $query1= $query1." where Ledger.Ledger_number like '".$PLedger_Number."%'";
	$query1= $query1." and Ledger.Ledger_name like '%".$PLedger_Name."%' ";
	$query1= $query1." and Ledger.ledger_grpdet=0 ";
	//echo $query1;exit;
	if($item==24 || $item==28)
	$query1= $query1." and Ledger.ledger_number like '6%' ";
	if($item==25)
	$query1= $query1." and Ledger.ledger_number like '7%' ";
	if($item==26 || $item==29)
	$query1= $query1." and Ledger.ledger_number in (select distinct ledger_number from clients) ";
	
if($ledgerinclude== 1)
	$query1=$query1." And Exists ( select 1 from ".$_SESSION['dbLabel'].".ledgerrepstk ".
							  "              where ledgerrepstk.user_account='".$_SESSION['useraccount']."' ".
							  "              and ledger.ledger_number like concat(ledgerrepstk.ledger_number,'%') ".
							  "            ) ";

if($ledgerinclude== 0)
	$query1=$query1." And Not Exists ( select 1 from ".$_SESSION['dbLabel'].".ledgerrepstk ".
							  "                  where ledgerrepstk.user_account='".$_SESSION['useraccount']."' ".
							  "                  and ledger.ledger_number like concat(ledgerrepstk.ledger_number,'%') ".
							  "                ) ";
							  
	if ($Module_BelongsToGroup == 1)				
		$query1= $query1." and ifNull(ledger.Ledger_BelongsToGroup,0) >= ifNull($FI_BelongsToGrp,0) ";
							   
	$query1= $query1." order by Ledger.ledger_number ";
}		
else
//maroun
{
	$query1=" select ledger.Ledger_number, ledger.Ledger_name from ledger inner join clients on ledger.ledger_number = clients.ledger_number".
        " where ledger.Ledger_number like '".$PLedger_Number."%' ";
	$query1 = $query1 . " and clients.Salesmen_Code in 
		(	select distinct clientsalesmen from ".$_SESSION['dbLabel'].".loginstk  where user_account = '".$_SESSION['useraccount']."'
			and (clientsalesmen <>'' and clientsalesmen is not null)
		)";	
				//maroun
	if ($costcentermasterexist > 0  and $accountoption==4) {	
			$query1 = $query1 ." and clients.CostCenter_Code in ( select distinct Mastercostcenter from ".$_SESSION['dbLabel'].".loginstk ".
			  				   "			  where loginstk.user_account='".$_SESSION['useraccount']."' ".
							   "              and (Mastercostcenter is not null and Mastercostcenter != '') ) ";
									}
	//maroun								
	if ($projectmasterexist > 0  and $accountoption==4) {	
			$query1= $query1."  and clients.project_code in 		( select distinct Masterproject from ".$_SESSION['dbLabel'].".loginstk ".
														 "			   where loginstk.user_account='".$_SESSION['useraccount']."' ".
														 "             and (Masterproject is not null and Masterproject != '')) ";
														 
								}
	/*							
	if ($ledgerexist > 0) {
		$query1 = $query1." Inner join ( select distinct ledger_number from ".$_SESSION['dbLabel'].".ledgerrepstk ".
						  "			     where user_account='".$_SESSION['useraccount']."' ".
						  "              and (ledger_number is not null and ledger_number != '') ".
						  "			   )  Q3 on (($ledgerinclude = 1 and Ledger.Ledger_Number like concat(Q3.ledger_number,'%') ) or ($ledgerinclude = 0 and Ledger.Ledger_Number Not like concat(Q3.ledger_number,'%') )) ";	
	}
	*/
	
	
if($ledgerinclude== 1)
	$query1=$query1." And Exists ( select 1 from ".$_SESSION['dbLabel'].".ledgerrepstk ".
							  "              where ledgerrepstk.user_account='".$_SESSION['useraccount']."' ".
							  "              and ledger.ledger_number like concat(ledgerrepstk.ledger_number,'%') ".
							  "            ) ";

if($ledgerinclude== 0)
	$query1=$query1." And Not Exists ( select 1 from ".$_SESSION['dbLabel'].".ledgerrepstk ".
							  "                  where ledgerrepstk.user_account='".$_SESSION['useraccount']."' ".
							  "                  and ledger.ledger_number like concat(ledgerrepstk.ledger_number,'%') ".
							  "                ) ";
							  
	$query1 = $query1 .	" and ledger.Ledger_name like '%".$PLedger_Name."%' and ifnull(Ledger_grpdet,0) = 0 ";
	
	if ($Module_BelongsToGroup == 1)				
		$query1= $query1." and ifNull(ledger.Ledger_BelongsToGroup,0) >= ifNull($FI_BelongsToGrp,0) ";
		
	$query1 = $query1 . " order by ledger.ledger_number ";
	
	
}		

		
	//echo $query1;	exit;
	
$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."'";	
}

?>
    <tr <?php echo $Color; ?>>
      <td align="left"><a href="select_ledger.php?lcode=<?php echo $Ledger_number; ?>&item=<?php echo $_GET['item']; ?>&index=<?php echo $_GET['index']; ?>"><?php echo $Ledger_number; ?></a></td>
      <td align="left" colspan="2"><a href="select_ledger.php?lcode=<?php echo $Ledger_number; ?>&item=<?php echo $_GET['item']; ?>&index=<?php echo $_GET['index']; ?>"><?php echo $Ledger_name; ?></a></td>
    </tr>
<?php
}
?>
  </table>
</form>
</body>
</html>
