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

}

$item   	= $_GET['item']; if ($item == ""){$item = $_POST["item"];}
$frm	   	= $_GET['frm']; if ($frm == ""){$frm = $_POST["frm"];}
$perpage   	= $_GET['perpage']; if ($perpage == ""){$perpage = $_POST["perpage"];}
$startrange = $_GET['startrange']; if ($startrange == ""){$startrange = $_POST["startrange"];}
$TransDate  = $_GET['TransDate']; if ($TransDate == ""){$TransDate = $_POST["TransDate"];}
$PLedger_number  = $_POST["PLedger_number"]; if ($PLedger_number == ""){$PLedger_number = $_GET["PLedger_number"];}
$PLedger_name  	 = $_POST["PLedger_name"]; if ($PLedger_name == ""){$PLedger_name = $_GET["PLedger_name"];}


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

$query1="select ledger.Ledger_number, ledger.Ledger_name, Supplier_PaymentTerms, Supplier_VAT, ifNull(Supplier_Import,0) As Supplier_Import, paymentcondition
 from suppliers
 inner join ledger on ledger.Ledger_number=suppliers.Ledger_number
 where ledger.ledger_number='".$_GET['lcode']."'";
$result1=mysql_query($query1,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($result1)){
	extract($rows);
}
//echo $query1."<br><br>";
$_GET['post']="1";
}

//User Access //Gilbert 19-11-2013
$querylog=" select ifNull(FI_Suppliers,0) As FI_Suppliers, ifNull(FI_SuppliersApp,0) As FI_SuppliersApp ".
          " 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=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>List of Suppliers</title>
<script type="text/javascript" language="javascript">

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

function opensupplierform(){

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


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

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

function SubmitForm() {
	vFrm       = document.form1.frm.value;
	vItem 	   = document.form1.item.value;
	vTransDate = document.form1.TransDate.value;
	vPerPage   = document.form1.perpage.value;
	
	document.form1.action='List_Supplier.php?PLedger_number=&lcode=&frm='+vFrm+'&item='+vItem+'&TransDate='+vTransDate+'&startrange=0&perpage='+vPerPage;
	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}
-->
</style></head>

<body>
<form id="form1" name="form1" method="post" action="" onsubmit="SubmitForm();">
  <table width="800" border="0" align="center">
    <tr>
      <th colspan="4" bgcolor="#DDDDDD"><span class="style1"> SUPPLIERS </span></th>
    </tr>
    <tr>
      <td bgcolor="#DDDDDD"> <span style="font-size: 11px; color: #999999; font-weight: bold;">Search for Supplier  <br> Number / Name</span></td>
      <td bgcolor="#DDDDDD" colspan="2">
		<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 name="paymentterms" type="hidden" id="paymentterms"  value="<?php echo $Supplier_PaymentTerms; ?>">
		<input name="supplierimport" type="hidden" id="supplierimport"  value="<?php echo $Supplier_Import; ?>">
		<input name="suppliervat" type="hidden" id="suppliervat"  value="<?php echo $Supplier_VAT; ?>">
		<input name="cur_code" type="hidden" id="cur_code" value="<?php echo $cur_code; ?>" />
		<input name="localexport" type="hidden" id="localexport" value="<?php echo $localexport; ?>" />
		<input name="paymentcondition" type="hidden" id="paymentcondition" value="<?php echo $paymentcondition; ?>" />
        <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 19-11-2013
	if($FI_SuppliersApp==1 && $FI_Suppliers==1){
		echo"<input type='button' name='addbtn' value='Add Supplier' onclick='opensupplierform();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>";
	}
	?>  
	  
		<input name="item" type="hidden" id="item" value="<?php echo $item; ?>" />
		<input name="frm" type="hidden" id="frm" value="<?php echo $frm; ?>" />
	    <input name="TransDate" type="hidden" id="TransDate" value="<?php echo $TransDate; ?>" /></td>
		
    </tr>
<?php if($_GET['post']=="1") { ?>
<script type="text/javascript" language="javascript">
var FormOption = document.form1.frm.value;
var ItemOption = document.form1.item.value;

if (FormOption == 2) {
	opener.document.purchasesjv.txtSupplierCode.value=document.form1.code1.value;
	opener.document.purchasesjv.txtSupplierName.value=document.form1.desc1.value;
	opener.document.purchasesjv.txtSupplierVat.value=document.form1.suppliervat.value;
	opener.document.purchasesjv.txtVatChk.value=document.form1.suppliervat.value;
	opener.document.purchasesjv.txtPaymentTerms.value=document.form1.paymentterms.value;
	opener.document.purchasesjv.txtSupplierCode.focus();
	opener.document.purchasesjv.txtSupplierCode.blur();
	opener.document.purchasesjv.submit(); self.close();
}
else if (FormOption == 3) {
	if(ItemOption != 55){
		opener.document.purchase.txtSupplier.value=document.form1.code1.value;
		opener.document.purchase.txtSupplierName.value=document.form1.desc1.value;
		opener.document.purchase.txtPaymentTerms.value=document.form1.paymentterms.value;
		opener.document.purchase.txtSupplierVat.value=document.form1.suppliervat.value;
		opener.document.purchase.txtPurVatChk.value=document.form1.suppliervat.value;

		if (opener.document.getElementById('txtPurCategory') && opener.document.getElementById('txtPurCategory').type == 'radio') {
			if (document.form1.supplierimport.value == 0)
				opener.document.purchase.txtPurCategory[0].checked = true;
			else	
				opener.document.purchase.txtPurCategory[1].checked = true;
		}	
		window.opener.SetMaturityDate();
		opener.document.purchase.txtSupplier.focus();
		opener.document.purchase.txtSupplier.blur();
		opener.document.purchase.submit();
	}else{ 
			opener.document.purchase.txtSupplier1.value		= document.form1.code1.value;
			opener.document.purchase.txtSupplierName1.value	= document.form1.desc1.value;
			opener.document.purchase.txtSupplierName1.focus();
	}
}
else if (FormOption == 4) {
	 
	if(ItemOption != 55){
		opener.document.purchase.txtSupplier.value		= document.form1.code1.value;
		opener.document.purchase.txtSupplierName.value	= document.form1.desc1.value;
		opener.document.purchase.txtPaymentTerms.value	= document.form1.paymentterms.value;
		opener.document.purchase.txtSupplierVat.value	= document.form1.suppliervat.value;
		opener.document.purchase.txtPorVatChk.value		= document.form1.suppliervat.value;
		opener.document.purchase.txtCurr.value			= document.form1.cur_code.value;
		opener.document.purchase.txtPaymentCondidions.value 		= document.form1.paymentcondition.value;
		
		if(opener.document.purchase.txtPorCategory0){
		if(document.form1.localexport.value == 0){
			opener.document.purchase.txtPorCategory0.checked = true;
			opener.document.purchase.txtPorCategory1.checked = false;
		} else {
			opener.document.purchase.txtPorCategory0.checked = false;
			opener.document.purchase.txtPorCategory1.checked = true;		
		}
		}
		if(document.form1.suppliervat.value==1)
		opener.document.purchase.ChkPorVat.checked = true;
		
		if (opener.document.getElementById('txtPorCategory') && opener.document.getElementById('txtPorCategory').type == 'radio') {
			if (document.form1.supplierimport.value == 0)
				opener.document.purchase.txtPorCategory[0].checked = true;
			else	
				opener.document.purchase.txtPorCategory[1].checked = true;
		}	

		opener.document.purchase.txtSupplier.focus();
		opener.document.purchase.txtSupplier.blur();
		//opener.document.purchase.submit();
	}else{ 
		opener.document.purchase.txtSupplier1.value		= document.form1.code1.value;
		opener.document.purchase.txtSupplierName1.value	= document.form1.desc1.value;
		opener.document.purchase.txtSupplierName1.focus();
	}

} else if (FormOption == 5) {
	opener.document.porderjv.txtSupplierCode.value=document.form1.code1.value;
	opener.document.porderjv.txtSupplierName.value=document.form1.desc1.value;
	opener.document.porderjv.txtSupplierVat.value=document.form1.suppliervat.value;
	opener.document.porderjv.txtVatChk.value=document.form1.suppliervat.value;
	opener.document.porderjv.txtSupplierCode.focus();
	opener.document.porderjv.txtSupplierCode.blur();
	opener.document.porderjv.submit();
} else {
	opener.document.purchase.txtSupplier.value=document.form1.code1.value;
	opener.document.purchase.txtSupplierName.value=document.form1.desc1.value;
	opener.document.purchase.txtPaymentTerms.value=document.form1.paymentterms.value;
	opener.document.purchase.txtSupplierVat.value=document.form1.suppliervat.value;

	if (opener.document.getElementById('txtPurCategory') && opener.document.getElementById('txtPurCategory').type == 'radio') {
		if (document.form1.supplierimport.value == 0)
			opener.document.purchase.txtPurCategory[0].checked = true;
		else	
			opener.document.purchase.txtPurCategory[1].checked = true;
	}	

	opener.document.purchase.txtSupplier.focus();
	opener.document.purchase.txtSupplier.blur();
	opener.document.purchase.txtSupplier.focus();
}
	
self.close();

</script>
<?php } ?>
    <tr bgcolor="#E5E5E5">
      <td width="136"><span class="style2">Supplier Number </span></td>
      <td width="411" colspan="2"><span class="style2">Supplier Name </span></td>
      <td width="57"><span class="style3"></span></td>
    </tr>
<?php
if (!empty($TransDate))
   $TransDate = explodeword($TransDate);

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

if (empty($TransDate))   
   $TransDate = 'null';
else
   $TransDate = "'".$TransDate."'"; 
   
$query1=" select ledger.Ledger_number, ledger.Ledger_name, suppliers.Supplier_PaymentTerms, suppliers.Supplier_VAT, suppliers.Currency_Code as Currency_Code, ifnull(supplier_export,0) as supplier_export ".
		" from suppliers inner join ledger on suppliers.ledger_number = ledger.ledger_number ".
        " where ledger.Ledger_number like '".$PLedger_number."%' ".
		" and ledger.Ledger_name like '%".$PLedger_name."%' ".
		" 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 ";
//echo $query1;		
$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;
//echo $query1;
$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="List_Supplier.php?lcode=<?php echo $Ledger_number; ?>&item=<?php echo $item; ?>&frm=<?php echo $frm; ?>&cur_code=<?php echo $Currency_Code; ?>&localexport=<?php echo $supplier_export; ?>"><?php echo $Ledger_number; ?></a></td>
		  <td colspan="2"><a href="List_Supplier.php?lcode=<?php echo $Ledger_number; ?>&item=<?php echo $item; ?>&frm=<?php echo $frm; ?>&cur_code=<?php echo $Currency_Code; ?>&localexport=<?php echo $supplier_export; ?>"><?php echo $Ledger_name; ?></a></td>
		  <td>&nbsp;</td>
		</tr>
	<?php
}

echo"<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>";


function explodeword($date)
{

$trim = explode('/',$date);
$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";
return $trim;
}
?>
  </table>
</form>
</body>
</html>
