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

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

}

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

	$queryall="select Document, Ledger_name from ledger where (Document='".$_GET['lcode']."' or Ledger_name='".$_GET['lcode']."'  ) ";
    $result1=mysql_query($queryall,$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=iso-8859-1" />
<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 Document  <br> Number / Name</span></td>
      <td bgcolor="#DDDDDD" width="100"><input type="text" name="PLedger_Number" value="<?php echo $PLedger_Number; ?>"  />
        <input name="code1" type="hidden" id="code1"  value="<?php echo $Document; ?>"></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="form" type="hidden" id="form" value="<?php echo $_GET['form']; ?>" /></td>
    </tr>
<?php if($post=="1" ) {  ?>
<script type="text/javascript" language="javascript">
x=document.form1;
item1=x.item1.value;
if(item1=="1"){
opener.document.form1.fromledger.value     =document.form1.code1.value;
opener.document.form1.fromledgername.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();
}
</script>
<?php } ?>
    <tr bgcolor="#E5E5E5">
      <td><span class="style2">Document <?php if($form == 3) echo "/Ledger No.";?></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
$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($form == 3)
	$query1=" select Document, Ledger_name from ( ";
else $query1 = '';
if ($salesmenexist==0) {
	$query1=$query1." select Document, Ledger_name from ledger ";
	
		
	if($ledgerinclude== 1)
		$querysearch=$querysearch." 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)
		$querysearch=$querysearch." 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." where Document like '".$PLedger_Number."%' ".
					  " and Ledger_name like '%".$PLedger_Name."%' ";
			
	if ($Module_BelongsToGroup == 1)				
		$query1= $query1." and ifNull(ledger.Ledger_BelongsToGroup,0) >= ifNull($FI_BelongsToGrp,0) ";
		
	
	
}	//echo $query1;
else
{
	$query1=$query1." select Document, ledger.Ledger_name as Ledger_name from ledger  inner join clients on ledger.ledger_number = clients.ledger_number ";
	
	if($ledgerinclude== 1)
		$querysearch=$querysearch." 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)
		$querysearch=$querysearch." 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." where Document 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))";	
	$query1 = $query1 . " and ledger.Ledger_name like '%".$PLedger_Name."%' ";
	
	if ($Module_BelongsToGroup == 1)				
		$query1 = $query1." and ifNull(ledger.Ledger_BelongsToGroup,0) >= ifNull($FI_BelongsToGrp,0) ";
		
}

if($form == 3){
	$query1 = $query1 ." union select Ledger_number as Document, Ledger_name from ledger ";
	
		
	if($ledgerinclude== 1)
		$querysearch=$querysearch." 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)
		$querysearch=$querysearch." 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." where Ledger_number like '%".$PLedger_Number."%' ".
					  " and Ledger_name like '%".$PLedger_Name."%' ";
			
	if ($Module_BelongsToGroup == 1)				
		$query1= $query1." and ifNull(ledger.Ledger_BelongsToGroup,0) >= ifNull($FI_BelongsToGrp,0) ";
		
}

if($form == 3)
	$query1=$query1." ) as Q where Document != '' ";



$query1= $query1." order by Document ";
//echo $query1;
$result1=mysql_query($query1,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($result1)){
extract($rows);

if(empty($Document))
  $PDocument = $Ledger_name;
else
  $PDocument = $Document;  
  
$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_document.php?lcode=<?php echo $PDocument; ?>&form=<?php echo $_GET['form'];?>&item=<?php echo $_GET['item']; ?>"><?php echo $Document; ?></a></td>
      <td align="left" colspan="2"><a href="select_document.php?lcode=<?php echo $PDocument; ?>&form=<?php echo $_GET['form'];?>&item=<?php echo $_GET['item']; ?>"><?php echo $Ledger_name; ?></a></td>
    </tr>
<?php
}
?>
  </table>
</form>
</body>
</html>
