<?php
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");

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

$querysalesmen=" select distinct clientsalesmen 
			from ".$_SESSION['dbLabel'].".loginstk ".
			  " where user_account='".$_SESSION['useraccount']."' 
			  and clientsalesmen<>'' 
			  or clientsalesmen <> null
			  "  ;
//echo $querysalesmen;			  
$salesmenresult = mysql_query($querysalesmen,$connection) or die(mysql_error());
$salesmenexist   = mysql_num_rows($salesmenresult);


$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 (!empty($TransDate))
   $TransDate = formatDateNoTime($TransDate);

if (empty($TransID))   
   $TransID = 'null';
   

if (empty($TransDate))   
   $TransDate = 'null';
else
   $TransDate = "'".$TransDate."'"; 
   
if($_GET['lcode']!=""){

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

$post="1";
}

//User Access
$querylog=" select ifNull(FI_Salesmen,0) As FI_Salesmen, ifNull(FI_SalesmenApp,0) As FI_SalesmenApp ".
          " from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());

while($rows=mysql_fetch_array($resultlog)){
	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>List of Salesmen</title>
<script type="text/javascript" language="javascript">

function clearform()
{
	document.form1.PLedger_number.value='';
	document.form1.PLedger_name.value='';
	//document.form1.action='List_SorSalesman.php';
	document.form1.submit();
}

function opensalesmenform(){

 win = window.open('./Form_Salesmen.php?mainmode=insert', 'Form_Salesmen', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=1000,height=600,top=100,left=100');		
	win.creator = self;
	win.focus();
}

function SetNextPage(startrange,perpage){
	vFrm       = document.form1.frm.value;
	vItem 	   = document.form1.item1.value;
	vTransDate = document.form1.TransDate.value;
	
	document.form1.action='List_SorSalesman.php?PLedger_number=&lcode=&frm='+vFrm+'&item1='+vItem+'&TransDate='+vTransDate+'&startrange='+startrange+'&perpage='+perpage;
	document.form1.submit();
}

function SetPrvPage(startrange,perpage){
	vFrm       = document.form1.frm.value;
	vItem 	   = document.form1.item1.value;
	vTransDate = document.form1.TransDate.value;
	
	document.form1.action='List_SorSalesman.php?PLedger_number=&lcode=&frm='+vFrm+'&item1='+vItem+'&TransDate='+vTransDate+'&startrange='+startrange+'&perpage='+perpage;
	document.form1.submit();
}
function RefreshPage(){
	vFrm       = document.form1.frm.value;
	vItem 	   = document.form1.item1.value;
	vTransDate = document.form1.TransDate.value;
	vPerPage   = document.form1.perpage.value;
	
	document.form1.action='List_SorSalesman.php?PLedger_number=&lcode=&frm='+vFrm+'&item1='+vItem+'&TransDate='+vTransDate+'&startrange=0&perpage='+vPerPage;
	document.form1.submit();
}

function SubmitForm() {
	vFrm       = document.form1.frm.value;
	vItem 	   = document.form1.item1.value;
	vTransDate = document.form1.TransDate.value;
	vPerPage   = document.form1.perpage.value;
	
	document.form1.action='List_SorSalesman.php?PLedger_number=&lcode=&frm='+vFrm+'&item1='+vItem+'&TransDate='+vTransDate+'&startrange=0&perpage='+vPerPage;
	document.form1.submit();
}
function chkIsPercentage(){
	if(document.getElementById('chk_is_percentage').checked){
		document.getElementById('is_percentage').value = 1;
	}
	else{
		document.getElementById('is_percentage').value = 0;
	}
}
</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; font-size: 13px;}
-->
</style></head>

<body>
<form id="form1" name="form1" method="post" action="" onsubmit="SubmitForm();">
  <table width="800" border="0" cellspacing="1" align="center">
    <tr>
      <th colspan="4" bgcolor="#DDDDDD"><span class="style1"> SALESMEN </span></th>
    </tr>
    <tr>
      <td bgcolor="#DDDDDD"> <span style="font-size: 11px; color: #999999; font-weight: bold;">Search for Salesman  <br> Number / Name</span></td>
      <td bgcolor="#DDDDDD" colspan="2">
	  <?php
	  if($is_percentage == ""){
		  $is_percentage = 1;
		  
	  }
	  
	  if($is_percentage == 1){
		  $checked = "checked";
	  }
	  else{
		  $checked = "";
	  }
	  ?>
	   <input type="hidden" id="is_percentage" name="is_percentage" value="<?php echo $is_percentage; ?>" />
	   <input type="checkbox" name="chk_is_percentage" id="chk_is_percentage" onclick="chkIsPercentage()" <?php echo $checked; ?> />%
		<input type="text" name="PLedger_number" value="<?php echo $PLedger_number; ?>" style="width:100px;" />
		<input type="text" name="PLedger_name"  value="<?php echo $PLedger_name; ?>" style="width:200px;"/>
        <input name="code1" type="hidden" id="code1"  value="<?php echo $Ledger_number; ?>">
        <input name="desc1" type="hidden" id="desc1" value="<?php echo $Ledger_name; ?>" />
	    <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;'/>
		
<?php

echo" <span style='font-size: 11px; color: #999999; font-weight: bold;'>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';
		
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>
		</select>";
?>
		
		</td>
      <td bgcolor="#DDDDDD">
	
	<?php 
	//Gilbert 20-11-2013
	
	if($FI_SalesmenApp==1 && $FI_Salesmen==1){
		echo"<input type='button' name='addbtn' value='Add Salesmen' onclick='opensalesmenform();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>";
	}
	?>
	
		<input name="item1" type="hidden" id="item1" value="<?php echo $_GET['item']; ?>" />
	    <input name="frm" type="hidden" id="frm" value="<?php echo $_GET['frm']; ?>" />
	    <input name="TransDate" type="hidden" id="TransDate" value="<?php echo $_GET['TransDate']; ?>" /></td>
    </tr>
<?php if($post=="1") { ?>
<script type="text/javascript" language="javascript">
var FormOption = document.form1.frm.value;
var item1 = document.form1.item1.value;
if (FormOption == 1){
	opener.document.salesorder.txtSalesmanCode.value=document.form1.code1.value;
	opener.document.salesorder.txtSalesmanName.value=document.form1.desc1.value;
	opener.document.salesorder.txtSalesmanCode.focus();
	
	opener.document.salesorder.submit();
}
else if (FormOption == 2){
	opener.document.sales.txtSalesmanCode.value=document.form1.code1.value;
	opener.document.sales.txtSalesmanName.value=document.form1.desc1.value;
	opener.document.sales.txtSalesmanCode.focus();
	
	opener.document.sales.submit();
}
else if (FormOption == 3){
	opener.document.sales.txtSalesmanCode.value=document.form1.code1.value;
	opener.document.sales.txtSalesmanName.value=document.form1.desc1.value;
	opener.document.sales.txtSalesmanCode.focus();
	
	opener.document.sales.submit();
}
else if (FormOption == 4){
	opener.document.returnsales.txtSalesmanCode.value=document.form1.code1.value;
	opener.document.returnsales.txtSalesmanName.value=document.form1.desc1.value;
	opener.document.returnsales.txtSalesmanCode.focus();
}
else if (FormOption == 5){
	opener.document.returnsales.txtSalesmanCode.value=document.form1.code1.value;
	opener.document.returnsales.txtSalesmanName.value=document.form1.desc1.value;
	opener.document.returnsales.txtSalesmanCode.focus();
}
else if (FormOption == 6){
	opener.document.salesjv.txtSalesmanCode.value=document.form1.code1.value;
	opener.document.salesjv.txtSalesmanName.value=document.form1.desc1.value;
	opener.document.salesjv.txtSalesmanCode.focus();
}
else if (FormOption == 7){
	opener.document.visit.txtSalesmanCode.value=document.form1.code1.value;
	opener.document.visit.txtSalesmanName.value=document.form1.desc1.value;
	opener.document.visit.txtSalesmanCode.focus();
}
else if (FormOption == 8){
	if(item1 == 1){
	opener.document.form1.fromsalesmancode.value=document.form1.code1.value;
	opener.document.form1.fromsalesmanname.value=document.form1.desc1.value;
	opener.document.form1.fromsalesmancode.focus();
	} else if(item1 == 2){
	opener.document.form1.tosalesmancode.value=document.form1.code1.value;
	opener.document.form1.tosalesmanname.value=document.form1.desc1.value;
	opener.document.form1.tosalesmancode.focus();
	}
}
else if (FormOption == 9){
	opener.document.getElementById('from_salesman_code').value=document.form1.code1.value;
	opener.document.getElementById('from_salesman_name').value=document.form1.desc1.value;
	opener.document.getElementById('from_salesman_name').readOnly = true;
	opener.document.getElementById('to_salesman_code').value=document.form1.code1.value;
	opener.document.getElementById('to_salesman_name').value=document.form1.desc1.value;
	opener.document.getElementById('to_salesman_name').readOnly = true;
}
else if (FormOption == 10){
	opener.document.getElementById('to_salesman_code').value=document.form1.code1.value;
	opener.document.getElementById('to_salesman_name').value=document.form1.desc1.value;
	opener.document.getElementById('to_salesman_name').readOnly = true;
}
else if (FormOption == 11){
	opener.document.salesorderjv.txtSalesmanCode.value=document.form1.code1.value;
	opener.document.salesorderjv.txtSalesmanName.value=document.form1.desc1.value;
	opener.document.salesorderjv.txtSalesmanCode.focus();
}
self.close();

</script>
<?php } ?>
    <tr bgcolor="#E5E5E5">
      <td width="136"><span class="style2">Salesman Number </span></td>
      <td width="411" colspan="2"><span class="style2">Salesman Name </span></td>
      <td width="57"><span class="style3"></span></td>
    </tr>
<?php
/*$query1=" select ledger.Ledger_number, ledger.Ledger_name ".
		" from salesmen inner join ledger on salesmen.ledger_number = ledger.ledger_number ".
        " where ledger.Ledger_number like ".GetSQLValueString($PLedger_number.'%','text')." ".
		" and ledger.Ledger_name like ".GetSQLValueString('%'.$PLedger_name.'%','text')." ".
		" and ((ifNull(ledger.Ledger_inactive,0) = 0 and 1 = 1) Or (ifNull(ledger.Ledger_inactive,0) = 1 and ifNull(ledger.Ledger_inactivedate, $TransDate) >= $TransDate) )  ".
		" order by ledger.Ledger_number ";*/
		
$query1=" select salesmen.Ledger_number, salesmen.Ledger_name ".
		" from salesmen ".
        " where 1=1 ";
if($is_percentage == 1){
$query1 = $query1." and salesmen.Ledger_number like ".GetSQLValueString($PLedger_number.'%','text')." ";
$query1 = $query1." and salesmen.Ledger_name like ".GetSQLValueString('%'.$PLedger_name.'%','text')." ";
}
else{
	if($PLedger_number != ""){
$query1 = $query1." and salesmen.Ledger_number = '".$PLedger_number."' ";
	}
	if($PLedger_name != ""){
$query1 = $query1." and salesmen.Ledger_name = '".$PLedger_name."' ";
	}
}
if ($salesmenexist > 0) {
						$query1 = $query1 . " and salesmen.Ledger_number in 
															(select distinct clientsalesmen from ".$_SESSION['dbLabel'].".loginstk where user_account = '".$_SESSION['useraccount']."'
															 and (clientsalesmen <>'' and clientsalesmen is not null)
															)";	
	}
$query1=$query1." order by salesmen.Ledger_number ";
//echo  $query1;
//exit;
$resultsearch=mysql_query($query1,$connection) or die(mysql_error());
$totalrows = mysql_num_rows($resultsearch);
	
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; ?>>
		  <td align="left"><span class="style3"><a href="List_SorSalesman.php?lcode=<?php echo $Ledger_number; ?>&frm=<?php echo $_GET['frm']; ?>&item=<?php echo $_GET['item']; ?>"><?php echo $Ledger_number; ?></a></span></td>
		  <td colspan="2"><span class="style3"><a href="List_SorSalesman.php?lcode=<?php echo $Ledger_number; ?>&frm=<?php echo $_GET['frm']; ?>&item=<?php echo $_GET['item']; ?>"><?php echo $Ledger_name; ?></a></span></td>
		  <td>&nbsp;</td>
		</tr>
	<?php
}

echo"<tr><td colspan='4'>&nbsp;</td></tr>
	 <tr><td colspan='4'><table width='100%' border=0><tr><td width='270'>&nbsp;</td><td width='130' 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='130' 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>";
echo"</table>";
echo"</td></tr>";
?>
  </table>
</form>
</body>
</html>
