<?php

define("DROPDOWNLISTLIMIT", 20);

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


 
 //GetSQLValueString
  function logHtml($content){
     ////////////Debuging purpose //////////////////////////////////////
	 
  echo '<script type="text/javascript">console.log("' . $content . '")</script>';                       
}

 /****
 Variables from query string  
                        ************/
$tbname = $_GET["tablename"];//table used
$term   = $_GET["term"];   // string term is generated automalically from javascript and auto fill from query string value

 //logHtml($term);
// logHtml (function_exists("GetSQLValueString") ); exit; 

  $term = trim($term);	
  $term = get_magic_quotes_gpc() ? stripslashes($term) : $term;

  $term = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($term) : mysql_escape_string($term); 
  
  
// $term = GetSQLValueString($term,"text");


 
                           // also auto generated from system
$ftag   = $_GET["fieldtag"]; //tag ex primary key or description to filter query according to field tag
 /****
 Variables from query string  
                        ************/

 
 
						



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


$result=mysql_query($query,$connection) or die(mysql_error($connection));	
if(mysql_num_rows($result))
{
	
	  global $ftag;
	  
	while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
		//extract($row);
		
			if ($ftag == "primarykey")
					{		//$list .= $ac_code. "||" . $ac_id. "||" . $ac_name."\n";
	     $row_array['value'] = $row['ac_code'];
		 $row_array['id']    = $row['ac_code'];        
		 $row_array['name']  = $row['ac_name'];
		 
		 }
		 
				else{
					//$list .= $ac_code. "||" . $ac_id. "||" . $ac_name."\n";
	     $row_array['value'] = $row['ac_name'];
		 $row_array['id']    = $row['ac_code'];       
		 $row_array['name']  = $row['ac_name'];
		 }

         
        array_push($return_arr,$row_array);

	}

	// send all collected data to the client
	//$list = substr($list,0,-2);
	//echo $list;
	echo json_encode($return_arr);
	
}
else
{
	echo "";
	}
	///**************************
	
	
	 function queryexecutor($content,$connection){
		   global $ftag;global $term; global $otherterm;



 if ($content == "PROJECTS")


 {
 

 }	 
		   
		   
if ($content == "LEDGERSSTRICT")
	
	{
		
$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 != '') ".
			  "";
$ledgerresult = mysql_query($queryledger,$connection) or die(mysql_error());
$ledgerexist  = mysql_num_rows($ledgerresult);	
$rows=mysql_fetch_array($ledgerresult) ;
$ledgerinclude = $rows[1];

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 as ac_code, Ledger.Ledger_name as ac_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,'%') )) ";	
	}
*/

		if ($ftag == "primarykey")
					{
						$query1 = $query1." where Ledger.ledger_number like '".$term."%' ";
								if ($otherterm != "")
							$query1 = $query1 ." and Ledger.Ledger_Name like '%" .$otherterm. "%'";
					}
					else{
						$query1 = $query1." where Ledger.Ledger_Name like '%".$term."%' ";
					    	if ($otherterm != "")
							$query1 = $query1 ." and Ledger.ledger_number like '" .$otherterm. "%'";
					}
					
					
 
	
	
	
	
	
	
	
	
	
	$query1= $query1." and Ledger.ledger_grpdet=0 ";
	//echo $query1;exit;
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."";
}		
else
//maroun
{
	$query1=" select ledger.Ledger_number as ac_code, ledger.Ledger_name as ac_name from ledger inner join clients on ledger.ledger_number = clients.ledger_number";
	
	
       
		
		if ($ftag == "primarykey")
					{
						$query1 = $query1." where Ledger.ledger_number like '".$term."%' ";
								if ($otherterm != "")
							$query1 = $query1 ." and Ledger.Ledger_Name like '%" .$otherterm. "%'";
					}
					else{
						$query1 = $query1." where Ledger.Ledger_Name like '%".$term."%' ";
					    	if ($otherterm != "")
							$query1 = $query1 ." and Ledger.ledger_number like '" .$otherterm. "%'";
					}
					
		
		
		
		
		
	$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) ";
	
	
	
	
		
 
	

					
					
	
	
}	

	if ($ftag == "primarykey")
					{
							$query1 = $query1." order by Ledger.Ledger_Number, Ledger.Ledger_Name  ";	
					}
					else{
							$query1 = $query1." order by Ledger.Ledger_Name, Ledger.Ledger_Number  ";	
						
					}	
		
		
		
		
			$query1 = $query1." limit " .DROPDOWNLISTLIMIT  ;	
			
			
			$query = $query1;
	}
 		   
		 
if ($content == "ITEMS_TYPE0")

{
 
  
$querygroup=" select distinct group_code,group_name ".
			" from grouping where group_code in (select group_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."') ".
			" order by group_code";
 $resultgroup = mysql_query($querygroup,$connection) or die(mysql_error());
$groupexist   = mysql_num_rows($resultgroup); 

$query = "SELECT Item_Code as ac_code,
       Item_Description as ac_name,
       Group_code as ac_group,
       Item_UnitSalesDesc AS unitdesc
FROM items";

if ($ftag == "primarykey")
{
	$query = $query." where Item_Code like '".$term."%' ";
			if ($otherterm != "")
		$query = $query ." and Item_Description like '%" .$otherterm. "%'";
}
else{
	$query = $query." where Item_Description like '%".$term."%' ";
		if ($otherterm != "")
		$query = $query ." and Item_Code  like '" .$otherterm. "%'";
}


if ($groupexist > 0)
{
  $query = $query ."AND items.group_code IN (SELECT group_code FROM "  .$_SESSION['dbLabel'] .".loginstk WHERE user_account='"  .$_SESSION['useraccount']. "') ";
}

 
 $query = $query ." limit " .DROPDOWNLISTLIMIT  ;
 
 
 

	 
	
	
}	

 
		   
if ($content == "LEDGERS")	

{
	
	 
		$query = " select Ledger_number as ac_code, Ledger_name as ac_name from Ledger ";
		
			if ($ftag == "primarykey")
					{
						$query = $query." where Ledger.ledger_number like '".$term."%' ";
								if ($otherterm != "")
							$query = $query ." and Ledger.Ledger_Name like '%" .$otherterm. "%'";
					}
					else{
						$query = $query." where Ledger.Ledger_Name like '%".$term."%' ";
					    	if ($otherterm != "")
							$query = $query ." and Ledger.ledger_number like '" .$otherterm. "%'";
					}
 
		$query = $query .	" and ifnull(Ledger_grpdet,0) = 0 ";
	
		if ($ftag == "primarykey")
					{
							$query = $query." order by Ledger.Ledger_Number, Ledger.Ledger_Name  ";	
					}
					else{
							$query = $query." order by Ledger.Ledger_Name, Ledger.Ledger_Number  ";	
						
					}
	
	$query = $query." limit " .DROPDOWNLISTLIMIT  ;	
	
 
	
 
	
}	

if ($content == "SUPPLIERS")

{
	$query=" select Ledger_number as ac_code, Ledger_name as ac_name from suppliers ";
	
		if ($ftag == "primarykey")
					{
						$query = $query." where suppliers.ledger_number like '".$term."%' ";
								if ($otherterm != "")
							$query = $query ." and suppliers.Ledger_Name like '%" .$otherterm. "%'";
					}
					else{
						$query = $query." where suppliers.Ledger_Name like '%".$term."%' ";
					    	if ($otherterm != "")
							$query = $query ." and suppliers.ledger_number like '" .$otherterm. "%'";
					}
					
		if ($ftag == "primarykey")
					{
							$query = $query." order by suppliers.Ledger_Number, suppliers.Ledger_Name  ";	
					}
					else{
							$query = $query." order by suppliers.Ledger_Name, suppliers.Ledger_Number  ";	
						
					}
	
	$query = $query." limit " .DROPDOWNLISTLIMIT  ;	
	
	 
 
}	

if($content == "SALESMEN")

{
    $querysalesmen= " select distinct clientsalesmen ".
				    " from ".$_SESSION['dbLabel'].".loginstk ". 
				    " where user_account='".$_SESSION['useraccount']."' ".
				    " and (clientsalesmen<>''  or clientsalesmen is not null) "  ;			 
	$salesmenresult = mysql_query($querysalesmen,$connection) or die(mysql_error());
	$salesmenexist  = mysql_num_rows($salesmenresult);
	$query = " select Ledger_number as ac_code, Ledger_name as ac_name from salesmen ";
	
	
		if ($ftag == "primarykey")
					{
						$query = $query." where salesmen.ledger_number like '".$term."%' ";
								if ($otherterm != "")
							$query = $query ." and salesmen.Ledger_Name like '%" .$otherterm. "%'";
					}
					else{
						$query = $query." where salesmen.Ledger_Name like '%".$term."%' ";
					    	if ($otherterm != "")
							$query = $query ." and salesmen.ledger_number like '" .$otherterm. "%'";
					}
					
					
					
					
 			
     		if ($salesmenexist > 0  )
			$query = $query . " and salesmen.ledger_number in 
								  ( select distinct clientsalesmen from ".$_SESSION['dbLabel'].".loginstk  where user_account = '".$_SESSION['useraccount']."'
								    and (clientsalesmen<>'' and clientsalesmen is not null))";									
		if ($ftag == "primarykey")
					{
							$query = $query." order by salesmen.Ledger_Number, salesmen.Ledger_Name  ";	
					}
					else{
							$query = $query." order by salesmen.Ledger_Name, salesmen.Ledger_Number  ";	
						
					}
	
	$query = $query." limit " .DROPDOWNLISTLIMIT  ;	
	
	 
}	


if ($content == "CLIENTS")
	
	{
		$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);
	
$query = 	" select Ledger_number as ac_code, Ledger_name as ac_name from clients ";


if ($ftag == "primarykey")
					{
						$query = $query." where clients.ledger_number like '".$term."%' ";
								if ($otherterm != "")
							$query = $query ." and clients.Ledger_Name like '%" .$otherterm. "%'";
					}
					else{
						$query = $query." where clients.Ledger_Name like '%".$term."%' ";
					    	if ($otherterm != "")
							$query = $query ." and clients.ledger_number like '" .$otherterm. "%'";
					}
		
if ($costcentermasterexist > 0) {	
				$query = $query ." 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) {	
				$query= $query."  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 ($ftag == "primarykey")
					{
							$query = $query." order by clients.Ledger_Number, clients.Ledger_Name  ";	
					}
					else{
							$query = $query." order by clients.Ledger_Name, clients.Ledger_Number  ";	
						
					}
	
	$query = $query." limit " .DROPDOWNLISTLIMIT  ;	

 
		
	}
		   
		   if($content == "sales__OLD")
		   {
			   $query = "";
	
	 /*  select Ledger_number, Ledger_name from salesmen  where Ledger_number like '%'  and Ledger_name like '%%'  and ifnull(salesmen_deactivate,0) = 0  order by Ledger_number */
	 
	 
	$query = $query." Select salesmen.Ledger_Number as ac_code, salesmen.Ledger_Name as ac_name ".
					" From salesmen ";
	
	if ($ftag == "primarykey")
					{
						$query = $query." where salesmen.ledger_number like '".$term."%' ";
					}
					else{
						$query = $query." where salesmen.Ledger_Name like '%".$term."%' ";
					}
					
	
	$query = $query."  and ifnull(salesmen_deactivate,0) = 0 ";
	
/*	if($ledgerinclude== 1)
		$query = $query." And Exists ( select 1 from ".$_SESSION['dbLabel'].".ledgerstk ".
						" 		       where ledgerstk.user_account='".$_SESSION['useraccount']."' ".
						"		       and ledger.ledger_number like concat(ledgerstk.ledger_number,'%') ".
						"   		) ";

	if($ledgerinclude== 0)
		$query = $query." And Not Exists ( select 1 from ".$_SESSION['dbLabel'].".ledgerstk ".
						" 			       where ledgerstk.user_account='".$_SESSION['useraccount']."' ".
						"       		   and ledger.ledger_number like concat(ledgerstk.ledger_number,'%') ".
						"                ) ";
		
	if ($Module_BelongsToGroup == 1)			 
		$query = $query." And ifNull(ledger.Ledger_BelongsToGroup,0) >= ifNull($FI_BelongsToGrp,0) ";
		
	$query = $query." And ((ifNull(ledger.Ledger_inactive,0) = 0 and 1 = 1) Or (ifNull(ledger.Ledger_inactive,0) = 1 and ifNull(Date_Format(ledger.Ledger_inactivedate,'%Y%m%d'), Date_Format(now(),'%Y%m%d')) >= Date_Format(now(),'%Y%m%d')) )  ";
	
	*/
	if ($ftag == "primarykey")
					{
							$query = $query." order by salesmen.Ledger_Number, salesmen.Ledger_Name  ";	
					}
					else{
							$query = $query." order by salesmen.Ledger_Name, salesmen.Ledger_Number  ";	
						
					}
	
	$query = $query." limit " .DROPDOWNLISTLIMIT  ;
	
	 //logHtml($query);
		   }
		 
		 if ($content=="ledgers__OLD")
		 {
			  
			 /* Ledger Include  
			 
			 try {
			
			  $queryledger= " select distinct ledger_number, ledger_include ".
              " from ".$_SESSION['dbLabel'].".ledgerstk  ".
			  " where user_account='".$_SESSION['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];
			 }
			 catch (Exception $e)
			 {
				 
			 }
			*/
			
			 /* End Ledger Include */
		// \\192.168.0.65\htdocs\rodolphe_datacom\contract\Get_AutocompleteQuery.php   ledger include grpdet belong to group not working
			 
			 
 			  $query = "";
			  $query = $query." Select Ledger.Ledger_Number as ac_code, Ledger.Ledger_Name as ac_name ".
					" From Ledger ";
					
					if ($ftag == "primarykey")
					{
						$query = $query." where ledger.ledger_number like '".$term."%' ";
					}
					else{
						$query = $query." where ledger.Ledger_Name like '%".$term."%' ";
					}
			
$query = $query." and ifnull(ledger.ledger_grpdet,0) = 0 ";
	
	if($ledgerinclude== 1 )
		$query = $query." And Exists ( select 1 from ".$_SESSION['dbLabel'].".ledgerstk ".
						" 		       where ledgerstk.user_account='".$_SESSION['useraccount']."' ".
						"		       and ledger.ledger_number like concat(ledgerstk.ledger_number,'%') ".
						"   		) ";

	if($ledgerinclude== 0 )
		$query = $query." And Not Exists ( select 1 from ".$_SESSION['dbLabel'].".ledgerstk ".
						" 			       where ledgerstk.user_account='".$_SESSION['useraccount']."' ".
						"       		   and ledger.ledger_number like concat(ledgerstk.ledger_number,'%') ".
						"                ) ";
		
	
	
	
	if ($Module_BelongsToGroup == 1)
	{		
		$query = $query." And ifNull(ledger.Ledger_BelongsToGroup,0) >= ifNull($FI_BelongsToGrp,0) ";
	}

	
	$query = $query." And ((ifNull(ledger.Ledger_inactive,0) = 0 and 1 = 1) Or (ifNull(ledger.Ledger_inactive,0) = 1 and ifNull(Date_Format(ledger.Ledger_inactivedate,'%Y%m%d'), Date_Format(now(),'%Y%m%d')) >= Date_Format(now(),'%Y%m%d')) )  ";
	
						if ($ftag == "primarykey")
					{
					$query = $query." order by Ledger.Ledger_Number, Ledger.Ledger_Name  ";	}
                          else{
							  $query = $query." order by Ledger.Ledger_Name, Ledger.Ledger_Number  ";
						  }					
	$query = $query." limit " .DROPDOWNLISTLIMIT  ;
			// logHtml($query);
	
	
		 }
		  return $query;
    	
}

?>