<?php
session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
include("../config.inc.php");
include("../function.inc.php");

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

class TableInfo {
    public $tableHeader; //ex Select Ledger
    public $searchfor; //ex search for ledger <br> Number/Name
	public $column1H; // ex Ledger Number
	public $column2H; // ex Ledger Name
}
///**************************
$obj = new TableInfo();
////////////////////////////////////    ITEMS_TYPE0  


$defaultcolspan = 3; // Default colspan for two td 





if (strpos($_GET['tablename'], 'frmutilities_') !== false) {
	//  noooooooo $defaultcolspan = 2;


$a = explode(	 "_" ,  $_GET['tablename'] );
$text = $a[1];
 

$obj->tableHeader = "Select $text  ";
$obj->searchfor = "Search for $text <br>Code/Description";
$obj->column1H = "$text  Code";
$obj->column2H = "$text  Description";
}



if (strpos($_GET['tablename'], 'ITEMS') !== false) {
	$defaultcolspan = 2;
	
$obj->tableHeader = 'Select ITEMS ';
$obj->searchfor = 'Search for Item<br>Code/Description';
$obj->column1H = 'Item Code';
$obj->column2H = 'Item Description';
}

 


if (strpos($_GET['tablename'], 'LEDGER') !== false) {
$obj->tableHeader = 'Select LEDGERS ';
$obj->searchfor = 'Search for Ledger<br>Number/Name';
$obj->column1H = 'Ledger Number';
$obj->column2H = 'Ledger Name';
}



if (strpos($_GET['tablename'], 'SUPPLIER') !== false) {
$obj->tableHeader = 'Select Suppliers ';
$obj->searchfor = 'Search for Supplier<br>Number/Name';
$obj->column1H = 'Supplier Number';
$obj->column2H = 'Supplier Name';
}



if (strpos($_GET['tablename'], 'CLIENT') !== false) {
$obj->tableHeader = 'Select Clients ';
$obj->searchfor = 'Search for Clients<br>Number/Name';
$obj->column1H = 'Client Number';
$obj->column2H = 'Client Name';
}




if (strpos($_GET['tablename'], 'SALESMEN') !== false) {
$obj->tableHeader = 'Select Salesman ';
$obj->searchfor = 'Search for Salesman<br>Number/Name';
$obj->column1H = 'Salesman Number';
$obj->column2H = 'Salesman Name';
}


 

///***************************

 

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'])
		or die("Could not connect to DB");
		
//	 logHtml( phpinfo() . "" );
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);
}

$perpage   			= $_GET['perpage']; 			if ($perpage == "")			 {	$perpage 			= $_POST["perpage"];			}
$startrange 		= $_GET['startrange']; 			if ($startrange == "")		 {	$startrange 		= $_POST["startrange"];			}


	  


$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=iso-8859-1" />
<title>Select Items</title>
<script type="text/javascript" language="javascript">

 
function getParameterByName(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
function Opener(a,b) // a id , b desc
{  
//http://192.168.0.65/rodolphe_datacom/contract/report/form_report.php?tablename=ledgers&fieldtag=primarykey&term=done&otherterm=&txtid=fromledger&fill=fromledgername&pointto1=toledger&pointto2=toledgername 
    if(getParameterByName('fieldtag') == 'primarykey')
	
	{	


	         var _fname = "#"+ getParameterByName('txtid');
			 $(_fname, window.opener.document).val(a.trim());
			 var _fname2 = "#"+ getParameterByName('fill');
			 $(_fname2, window.opener.document).val(b.trim());
			 
		if(getParameterByName('pointto1') != 'undefined' && getParameterByName('pointto2') != 'undefined')
		{
	

			 
			 var _ffill1 = "#"+ getParameterByName('pointto1');
			 $(_ffill1, window.opener.document).val(a.trim());
			 var _ffill2 = "#"+ getParameterByName('pointto2');
			 $(_ffill2, window.opener.document).val(b.trim());
			 
		}
	}
	
    if(getParameterByName('fieldtag') != 'primarykey') // not equal
	
	{
	         var _fname = "#"+ getParameterByName('fill');
			 $(_fname, window.opener.document).val(a.trim());
			 var _fname2 = "#"+ getParameterByName('txtid');
			 $(_fname2, window.opener.document).val(b.trim());
			 
		if(getParameterByName('pointto1') != 'undefined' && getParameterByName('pointto2') != 'undefined')
		{
	

			 
			 var _ffill1 = "#"+ getParameterByName('pointto1');
			 $(_ffill1, window.opener.document).val(a.trim());
			 var _ffill2 = "#"+ getParameterByName('pointto2');
			 $(_ffill2, window.opener.document).val(b.trim());
			 
		}
	}	
 

     
$('#custom_error',window.opener.document).remove();

self.close();
	 
}



function clearform()
{
	
 
  
  
  document.form1.inp_desc.value='';
  document.form1.inp_code.value='';

}

function SetNextPage(startrange,perpage){
	
	var _tablename =     getParameterByName('tablename');
	var _fieldtag =      getParameterByName('fieldtag');
	
	if(_fieldtag == 'primarykey')
	{
		_term = document.form1.inp_code.value;
		_otherterm = document.form1.inp_desc.value;
	}
	else{
	    _otherterm = document.form1.inp_code.value;
		_term = document.form1.inp_desc.value;
		
	}
	
	//var _term =          getParameterByName('term');
	
	
	//var _otherterm=      getParameterByName('otherterm');	
	var _txtid =         getParameterByName('txtid');
	var _fill =          getParameterByName('fill');
	var _pointto1 =      getParameterByName('pointto1');
	var _pointto2 =      getParameterByName('pointto2');
	
    var vPerPage   = document.form1.perpage.value;
		 
	var _frmAction = 'form_report.php?'; 
	_frmAction = _frmAction + 'tablename=' + _tablename +'&';
    _frmAction = _frmAction + 'fieldtag='  + _fieldtag +'&';
	_frmAction = _frmAction + 'term='      + _term +'&';
    _frmAction = _frmAction + 'otherterm='      + _otherterm +'&';		
	_frmAction = _frmAction + 'txtid='     + _txtid +'&';
	_frmAction = _frmAction + 'fill='      + _fill +'&';
	_frmAction = _frmAction + 'pointto1='  + _pointto1 +'&';
	_frmAction = _frmAction + 'pointto2='  + _pointto2 +'&';
    _frmAction = _frmAction + 'startrange='  + startrange +'&';
	_frmAction = _frmAction + 'perpage='  + perpage +'';
	
	document.form1.action= _frmAction;
	document.form1.submit();

	
	
}


function SetPrvPage(startrange,perpage){
var _tablename =     getParameterByName('tablename');
	var _fieldtag =      getParameterByName('fieldtag');
	
	if(_fieldtag == 'primarykey')
	{
		_term = document.form1.inp_code.value;
		_otherterm = document.form1.inp_desc.value;
	}
	else{
	    _otherterm = document.form1.inp_code.value;
		_term = document.form1.inp_desc.value;
		
	}
	
	//var _term =          getParameterByName('term');
	
	
	//var _otherterm=      getParameterByName('otherterm');	
	var _txtid =         getParameterByName('txtid');
	var _fill =          getParameterByName('fill');
	var _pointto1 =      getParameterByName('pointto1');
	var _pointto2 =      getParameterByName('pointto2');
	
    var vPerPage   = document.form1.perpage.value;
		 
	var _frmAction = 'form_report.php?'; 
	_frmAction = _frmAction + 'tablename=' + _tablename +'&';
    _frmAction = _frmAction + 'fieldtag='  + _fieldtag +'&';
	_frmAction = _frmAction + 'term='      + _term +'&';
    _frmAction = _frmAction + 'otherterm='      + _otherterm +'&';		
	_frmAction = _frmAction + 'txtid='     + _txtid +'&';
	_frmAction = _frmAction + 'fill='      + _fill +'&';
	_frmAction = _frmAction + 'pointto1='  + _pointto1 +'&';
	_frmAction = _frmAction + 'pointto2='  + _pointto2 +'&';
    _frmAction = _frmAction + 'startrange='  + startrange +'&';
	_frmAction = _frmAction + 'perpage='  + perpage +'';
	
	document.form1.action= _frmAction;
	document.form1.submit();

}


/*
http://192.168.0.65/rodolphe_datacom/contract/report/form_report.php?tablename=ledgers&fieldtag=primarykey&term=1&otherterm=&txtid=fromledger&fill=fromledgername&pointto1=toledger&pointto2=toledgername&startrange=&perpage=
*/

function rewriteFrm()
{
	
	 /* tab3 inp_code 
	   if( $_GET['fieldtag'] == "primarykey")
	  {
		    echo $_GET['term']; 
	  }     else echo $_GET['otherterm'];
	  
	  
	 */
	var _tablename =     getParameterByName('tablename');
	var _fieldtag =      getParameterByName('fieldtag');
	
	if(_fieldtag == 'primarykey')
	{
		_term = document.form1.inp_code.value;
		_otherterm = document.form1.inp_desc.value;
	}
	else{
	    _otherterm = document.form1.inp_code.value;
		_term = document.form1.inp_desc.value;
		
	}
	
	//var _term =          getParameterByName('term');
	
	
	//var _otherterm=      getParameterByName('otherterm');	
	var _txtid =         getParameterByName('txtid');
	var _fill =          getParameterByName('fill');
	var _pointto1 =      getParameterByName('pointto1');
	var _pointto2 =      getParameterByName('pointto2');
	
    var vPerPage   = document.form1.perpage.value;
		 
	var _frmAction = 'form_report.php?'; 
	_frmAction = _frmAction + 'tablename=' + _tablename +'&';
    _frmAction = _frmAction + 'fieldtag='  + _fieldtag +'&';
	_frmAction = _frmAction + 'term='      + _term +'&';
    _frmAction = _frmAction + 'otherterm='      + _otherterm +'&';		
	_frmAction = _frmAction + 'txtid='     + _txtid +'&';
	_frmAction = _frmAction + 'fill='      + _fill +'&';
	_frmAction = _frmAction + 'pointto1='  + _pointto1 +'&';
	_frmAction = _frmAction + 'pointto2='  + _pointto2 +'&';
    _frmAction = _frmAction + 'startrange='  + '0' +'&';
	_frmAction = _frmAction + 'perpage='  + vPerPage +'';
	
	//Focus
	 if ($('#inp_code').is(':focus'))
	 {
			_frmAction = _frmAction + '&nextfocus='  + 'inp_code' +'';
	 }
	 	 if ($('#inp_desc').is(':focus'))
	 {
		_frmAction = _frmAction + '&nextfocus='  + 'inp_desc' +'';
	 }
	 // End Focus
	
	document.form1.action= _frmAction;
	document.form1.submit();
	
	
	

  	
}

function RefreshPage(){
	var _tablename =     getParameterByName('tablename');
	var _fieldtag =      getParameterByName('fieldtag');
	var _term =          getParameterByName('term');
	var _otherterm=      getParameterByName('otherterm');	
	var _txtid =         getParameterByName('txtid');
	var _fill =          getParameterByName('fill');
	var _pointto1 =      getParameterByName('pointto1');
	var _pointto2 =      getParameterByName('pointto2');
	
    var vPerPage   = document.form1.perpage.value;
		 
	var _frmAction = 'form_report.php?'; 
	_frmAction = _frmAction + 'tablename=' + _tablename +'&';
    _frmAction = _frmAction + 'fieldtag='  + _fieldtag +'&';
	_frmAction = _frmAction + 'term='      + _term +'&';
    _frmAction = _frmAction + 'otherterm='      + _otherterm +'&';		
	_frmAction = _frmAction + 'txtid='     + _txtid +'&';
	_frmAction = _frmAction + 'fill='      + _fill +'&';
	_frmAction = _frmAction + 'pointto1='  + _pointto1 +'&';
	_frmAction = _frmAction + 'pointto2='  + _pointto2 +'&';
    _frmAction = _frmAction + 'startrange='  + '0' +'&';
	_frmAction = _frmAction + 'perpage='  + vPerPage +'';
	
	document.form1.action= _frmAction;
	document.form1.submit();
}

</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}
-->
 tr{
	 cursor:pointer;
 }
</style>

      <script type="text/javascript" src="js/jquery-1.9.1.js"></script>
      <script type="text/javascript" src="js/jquery-ui.min.js"></script>
      <link type="text/css" href="js/jquery-ui.css" rel="stylesheet">
</head>

<body onload="document.getElementById('inp_code').focus();">

<script type="text/javascript">
function getParameterByName(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

$(document).ready(function()
{
	
//$('#custom_error',window.opener.document).remove();
	
}
);

$(window).load(function() {
var a= ''; var b = '';

//alert(getParameterByName('nextfocus') ===null);

 if (getParameterByName('nextfocus') ===null)
 {
 a =    $('#inp_code').val();
 $('#inp_code').focus();
 $('#inp_code').val(b);
 $('#inp_code').focus();
 $('#inp_code').val(a);
 }
else{
 a =    $('#'+getParameterByName('nextfocus')).val();
 $('#'+getParameterByName('nextfocus')).focus();
 $('#'+getParameterByName('nextfocus')).val(b);
 $('#'+getParameterByName('nextfocus')).focus();
 $('#'+getParameterByName('nextfocus')).val(a);
}



});


$(document).on("keypress", "form", function(event) {
	
 
   if ( event.keyCode ==27)
   {
	   self.close();
   }
    if (event.keyCode ==13)
	{  rewriteFrm();
	 	return false;
	}		
  //  return event.keyCode != 13;
});
</script>
<form id="form1" name="form1"  method="post">
  <table width="850" border="0" align="center">
    <tr>
      <th colspan="4" bgcolor="#DDDDDD"><span class="style1"> <?php  global $obj; echo($obj->tableHeader); ?> </span></th>
    </tr>
    <tr>
      <td bgcolor="#DDDDDD" width="136"> <span style="font-size: 11px; color: #999999; font-weight: bold;"><?php echo($obj->searchfor); ?></span></td>
      <td bgcolor="#DDDDDD" width="100"><input type="text" name="inp_code"  spellcheck="false"  id ="inp_code" value="<?php
	   if( $_GET['fieldtag'] == "primarykey")
	  {
		    echo $_GET['term']; 
	  }     else echo $_GET['otherterm'];
	  
	  
 

	  ?>"  />
	  </td>
      <td bgcolor="#DDDDDD" width="450"><input type="text" name="inp_desc" id ="inp_desc" spellcheck="false"  value="<?php
      if( $_GET['fieldtag'] != "primarykey")
	  {
		    echo $_GET['term']; 
	  }     else echo $_GET['otherterm'];
	
	  ?>" 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;' onclick = "rewriteFrm();return false;"/>
		<input type="button" name="cancelbtn" value="Cancel" onclick='clearform();return false;'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
        </td>
      <td bgcolor="#DDDDDD" width="64"> 
										 
	  </td>
    </tr>
<?php
	echo "<tr bgcolor='#DDDDDD'><td></td>";
	echo "<td>	<span style='font-size: 11px; color: #999999; font-weight: bold;'>Result per page </span><select name='perpage' id='perpage' value='$perpage' onchange='RefreshPage();' >";	
	if ($perpage==20)
		$s20 = 'selected';
	else if ($perpage==30)
		$s30 = 'selected';
	else if ($perpage==40)
		$s40 = 'selected';
	else if ($perpage==50)
		$s50 = 'selected';
	else if ($perpage==100)
		$s100 = 'selected';
	echo"<option ".$s20." value='20'>20</option>
		 <option ".$s30." value='30'>30</option>
		 <option ".$s40." value='40'>40</option>
		 <option ".$s50." value='50'>50</option>
		 <option ".$s100." value='100'>100</option>		 
		 </select></td><td></td><td></td></tr>";
?>




    <tr bgcolor="#E5E5E5">
      <td><span class="style2"><?php echo($obj->column1H); ?> </span></td>
      <td colspan="<?php global $defaultcolspan; echo $defaultcolspan; ?>"><span class="style2"><?php echo($obj->column2H); ?> </span></td>
	  
	  <?php global $defaultcolspan; if($defaultcolspan==2) 
	  {
		  echo " <td><span class=\"style2\">GROUP CODE</span></td>";
	  }
  ?>
   
    </tr>
<?php
 
		
	
 $query1 = queryexecutor($_GET['tablename'],$connection);

 
 $result1=mysql_query($query1,$connection) or die(mysql_error());
 

$totalrows = mysql_num_rows($result1);
	
if ($startrange=='') $startrange = 0;
if ($perpage=='')	 $perpage = 20;
	
	
$query1 = $query1." limit ".$startrange.",".$perpage; 

 

  $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; ?> onclick="Opener( $(this).closest('tr').find('td:eq(0)').text(),$(this).closest('tr').find('td:eq(1)').text());">
   
    
    <td align="left"><?php echo $ac_code; ?>&nbsp;</td>
	
	<td align="left" colspan = "<?php global $defaultcolspan; echo $defaultcolspan; ?>"><?php echo $ac_name; ?>&nbsp;</td>
	
	<?php
	global $defaultcolspan; if($defaultcolspan==2)
	{
		echo "  <td align=\"left\">$ac_group</td> ";
	}
	?>
 
    </tr>	
	
<?php
}

echo"<tr><td colspan='6'>&nbsp;</td></tr>
		 <tr><td colspan='6'><table width='100%' border=0><tr><td width='270'>&nbsp;</td><td width='110' align='center'>";
		
	$diff= $startrange - $perpage;
		
	if (($diff) >= 0 ){
		$prvstartrange = $startrange - $perpage;
		echo"<img src='../img/arrowleft.jpg' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
	}
	else
		echo "&nbsp;";
		
	echo"</td><td width='110' align='center'>";
		
		
	if ($startrange <= $totalrows){
		$startrange = $startrange + $perpage;
		if  ($startrange <= $totalrows)
			echo"<img src='../img/arrowright.jpg' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange."','".$perpage."')>";
	} else
		echo "&nbsp;";
			
	echo"</td><td width='270'>&nbsp;</td></tr>";
		  
?>
  </table>
</form>
</body>
</html>
<?php

		 function queryexecutor($content,$connection){
			 
		
	
	
$fieldtag 		= $_GET['fieldtag']; 		if ($fieldtag == "")		 {	$fieldtag 		= $_POST["fieldtag"];		}
$term 		    = $_GET['term']; 			if ($term == "")		     {	$term 		    = $_POST["term"];			}
$otherterm 		= $_GET['otherterm']; 		if ($otherterm == "")		 {	$otherterm 		= $_POST["otherterm"];		}

	 /*******************/
  $ftag   = $fieldtag; //tag ex primary key or description to filter query according to field tag
  
 // $term   = $_GET["term"];   // string term is generated automalically from javascript and auto fill from query string value
  $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); 
  
 // $otherterm   = $_GET["otherterm"];   // string otherterm is generated automalically from javascript and auto fill from query string value
  $otherterm = trim($otherterm);	
  $otherterm = get_magic_quotes_gpc() ? stripslashes($otherterm) : $otherterm;
  $otherterm = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($otherterm) : mysql_escape_string($otherterm); 
 /*******************/
 /*******************/
 /*******************/
 /*******************/
 
 
 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  ";	
						
					}	
		
 
			
			
			$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']. "') ";
}

 
 
 
 
 

	 
	
	
}	 


//by rod

if ($content == "frmutilities_WareHouse")	

{
          $query = " select warehouse_code as ac_code, warehouse_name as ac_name from warehouse ";
          $query = $query." where warehouse.warehouse_code like '".$term."%' ";
          $query = $query." order by warehouse.warehouse_code, warehouse.warehouse_name  ";	
 	
}


if ($content == "frmutilities_CostCenter")	

{
          $query = " select costcent_code as ac_code, costcent_name as ac_name from costcent ";
          $query = $query." where costcent.costcent_code like '".$term."%' ";
          $query = $query." order by costcent.costcent_code, costcent.costcent_name  ";	
 	
}	


if ($content == "frmutilities_Project")	

{
          $query = " select project_code as ac_code, project_name as ac_name from project ";
          $query = $query." where project.project_code like '".$term."%' ";
          $query = $query." order by project.project_code, project.project_name  ";	
 	
}	


if ($content == "frmutilities_Supplier")	

{
          $query = " select ledger_number as ac_code, ledger_name as ac_name from suppliers ";
          $query = $query." where suppliers.ledger_number like '".$term."%' ";
          $query = $query." order by suppliers.ledger_number, suppliers.ledger_name  ";	
 	
}

if ($content == "frmutilities_Ledger")	

{
          $query = " select ledger_number as ac_code, ledger_name as ac_name from ledger ";
          $query = $query." where ledger.ledger_number like '".$term."%' ";
		  $query = $query. " and ledger_grpdet = 0 ";
          $query = $query." order by ledger.ledger_number, ledger.ledger_name  ";	
 	
}
	
	
// end by rod
 
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 10 ";	
	
 
	
 
	
}	
 
 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  ";	
						
					}
	
	 
	
	 
 
}	
 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  ";	
						
					}
	
 

 
		
	}

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  ";	
						
					}
 
	  
		
	}
 
 
 
 
 
/*******************/
/*******************/
/*******************/
/*******************/	 
 
 
		 
		 
		  return $query;

		  }
?>
