<?php

$q = strtolower($_GET["q"]);
if (!$q) return;
include '../db_connect.php';

  if(isset($_GET['mode'])&&$_GET['mode']=='vendor')
  {
  $items = mysqli_query($con,"SELECT * FROM commst where type like 'VENDOR' order by compid ");

  	echo "SELF| \n";
	while($row = mysqli_fetch_array($items)) 
	{
	  if (strpos(strtolower($row['compid']), $q) !== false) 
	  {
	  echo "$row[compid]|$row[telephone]|$row[cmpcity]|$row[conper]|$row[telephone]|$row[email]|$row[ccid]|$row[name]|$row[tds]\n";
	  }
	}
  
  }
	
	
	
  else if(isset($_GET['mode'])&&$_GET['mode']=='all')
  {
  $items = mysqli_query($con,"SELECT compid,telephone FROM commst  order by compid ");
  
  
	while($row = mysqli_fetch_array($items)) 
	{
	  if (strpos(strtolower($row['compid']), $q) !== false) 
	  {
	  echo "$row[compid]\n";
	  }
	}
  
  }
	
	
	
  else if(isset($_GET['mode'])&&$_GET['mode']=='jv')
  {
  $items = mysqli_query($con,"SELECT ledger, alias  FROM ledgerreg  order by ledger ");
  
	while($row = mysqli_fetch_array($items)) 
	{
	  if (strpos(strtolower($row['alias']), $q) !== false) 
	  {
	  echo "$row[alias]|$row[ledger]\n";
	  }
	}
  
  }
	
	
	
	
  else if(isset($_GET['mode'])&&$_GET['mode']=='vald')
  {
  $items = mysqli_query($con,"SELECT ledger, alias  FROM ledgerreg  order by ledger ");
  
  
	while($row = mysqli_fetch_array($items)) 
	{
	  if (strpos(strtolower($row['alias']), $q) !== false) 
	  {
	  echo "$row[alias]|$row[ledger]\n";
	  }
	}
  
  }
  
	
  else if(isset($_GET['mode'])&&$_GET['mode']=='PA')
  {
  $items = mysqli_query($con,"SELECT ledger, alias  FROM ledgerreg ");
  //where ledtype like 'PURCHASE ACCOUNT' or ledtype like 'SUNDRY CREDITORS' or ledtype like 'DIRECT EXPENSE' or ledtype like 'LOAN ACCOUNT' order by ledger ");
  
	while($row = mysqli_fetch_array($items)) 
	{
	  if (strpos(strtolower($row['alias']), $q) !== false) 
	  {
	  echo "$row[alias]|$row[ledger]\n";
	  }
	}
  
  }
	
	
	
  else if(isset($_GET['mode'])&&$_GET['mode']=='RE')
  {
  $items = mysqli_query($con,"SELECT ledger, alias  FROM ledgerreg where ledtype like 'SUNDRY DEBTORS' or ledtype like 'DIRECT EXPENCES' or ledtype like 'CASH BANK' order by ledger ");
  
	while($row = mysqli_fetch_array($items)) 
	{
	  if (strpos(strtolower($row['alias']), $q) !== false) 
	  {
	  echo "$row[alias]|$row[ledger]\n";
	  }
	}
  
  }
	
	
  else if(isset($_GET['mode'])&&$_GET['mode']=='naperiod')
  {
  	$output = array();
  	$sql = "SELECT naopen, naclose  FROM commst where compid like '$_POST[billedto]' ";
  
	if($result=mysqli_query($con,$sql))
	{
	
	$row = mysqli_fetch_assoc($result);
	
	$output = $row;
	
	echo json_encode($output);;
	
	
	}
  
  }


  else
  {
  
  $items = mysqli_query($con,"SELECT compid,name,cmpcity,conper,telephone,email,ccid,`limit`,usedlimit FROM commst where type like 'CUSTOMER' order by compid ");
  
	while($row = mysqli_fetch_array($items)) 
	{
	  if (strpos(strtolower($row['compid']), $q) !== false) 
	  {
	  $creditbolck = 0;
	  if($row[usedlimit]>=$row[limit]&&!empty($row[limit]))
	  $creditbolck = 1;
	  echo "$row[compid]|$row[cmpcity]|$row[conper]|$row[telephone]|$row[email]|$row[ccid]|$row[name]|$creditbolck\n";
	  }
	}
  }
mysqli_close($con);


?>