<?php 
include_once "session.php";
$requestData= $_REQUEST;


if($_REQUEST['pageaction'] == "autocomplete_company"){
	
	$display_data = array();
	
	if(isset($_REQUEST['srch_keyword']) && $_REQUEST['srch_keyword'] !="" ){
		if(isset($_REQUEST['srch_type']) && $_REQUEST['srch_type'] !="" ){
			if($_REQUEST['srch_type'] == "country_search"){
				$str_query = " SELECT DISTINCT(`c_country`) as disp_rslt FROM `m_company` WHERE 1=1 and `c_country` like '%".$_REQUEST['srch_keyword']."%' LIMIT 10 ";
				$rslt_data = $db->get_rsltset($str_query);
			}
			else if($_REQUEST['srch_type'] == "state_search"){
				$str_query = " SELECT DISTINCT(`c_state`) as disp_rslt FROM `m_company` WHERE 1=1 and `c_state` like '%".$_REQUEST['srch_keyword']."%' LIMIT 10 ";
				$rslt_data = $db->get_rsltset($str_query);
			}
			else if($_REQUEST['srch_type'] == "city_search"){
				$str_query = " SELECT DISTINCT(`c_city`) as disp_rslt FROM `m_company` WHERE 1=1 and `c_city` like '%".$_REQUEST['srch_keyword']."%' LIMIT 10 ";
				$rslt_data = $db->get_rsltset($str_query);
			}
		}
	}	
		
	if(isset($rslt_data)){		
		foreach($rslt_data as $rslt_data_S)	{
			$display_data[] = array("value"=> $rslt_data_S['disp_rslt'],"data"=> $rslt_data_S['disp_rslt']);	
		}	
	}  
		
	
	$suggestions = array("suggestions"=>$display_data);
	echo json_encode($suggestions);
}
else if($_REQUEST['pageaction'] == "autocomplete_supplier"){
	
	$display_data = array();
	
	if(isset($_REQUEST['srch_keyword']) && $_REQUEST['srch_keyword'] !="" ){
		if(isset($_REQUEST['srch_type']) && $_REQUEST['srch_type'] !="" ){
			if($_REQUEST['srch_type'] == "country_search"){
				$str_query = " SELECT DISTINCT(`sup_country`) as disp_rslt FROM `m_supplier` WHERE 1=1 and `sup_country` like '%".$_REQUEST['srch_keyword']."%' LIMIT 10 ";
				$rslt_data = $db->get_rsltset($str_query);
			}
			else if($_REQUEST['srch_type'] == "state_search"){
				$str_query = " SELECT DISTINCT(`sup_state`) as disp_rslt FROM `m_supplier` WHERE 1=1 and `sup_state` like '%".$_REQUEST['srch_keyword']."%' LIMIT 10 ";
				$rslt_data = $db->get_rsltset($str_query);
			}
			else if($_REQUEST['srch_type'] == "city_search"){
				$str_query = " SELECT DISTINCT(`sup_city`) as disp_rslt FROM `m_supplier` WHERE 1=1 and `sup_city` like '%".$_REQUEST['srch_keyword']."%' LIMIT 10 ";
				$rslt_data = $db->get_rsltset($str_query);
			}
		}
	}	
		
	if(isset($rslt_data)){		
		foreach($rslt_data as $rslt_data_S)	{
			$display_data[] = array("value"=> $rslt_data_S['disp_rslt'],"data"=> $rslt_data_S['disp_rslt']);	
		}	
	}  
		
	
	$suggestions = array("suggestions"=>$display_data);
	echo json_encode($suggestions);
}
else if($_REQUEST['pageaction'] == "autocomplete_customer"){
	
	$display_data = array();
	
	if(isset($_REQUEST['srch_keyword']) && $_REQUEST['srch_keyword'] !="" ){
		if(isset($_REQUEST['srch_type']) && $_REQUEST['srch_type'] !="" ){
			if($_REQUEST['srch_type'] == "country_search"){
				$str_query = " SELECT DISTINCT(`cus_country`) as disp_rslt FROM `m_customer` WHERE 1=1 and `cus_country` like '%".$_REQUEST['srch_keyword']."%' LIMIT 10 ";
				$rslt_data = $db->get_rsltset($str_query);
			}
			else if($_REQUEST['srch_type'] == "state_search"){
				$str_query = " SELECT DISTINCT(`cus_state`) as disp_rslt FROM `m_customer` WHERE 1=1 and `cus_state` like '%".$_REQUEST['srch_keyword']."%' LIMIT 10 ";
				$rslt_data = $db->get_rsltset($str_query);
			}
			else if($_REQUEST['srch_type'] == "city_search"){
				$str_query = " SELECT DISTINCT(`cus_city`) as disp_rslt FROM `m_customer` WHERE 1=1 and `cus_city` like '%".$_REQUEST['srch_keyword']."%' LIMIT 10 ";
				$rslt_data = $db->get_rsltset($str_query);
			}
		}
	}	
		
	if(isset($rslt_data)){		
		foreach($rslt_data as $rslt_data_S)	{
			$display_data[] = array("value"=> $rslt_data_S['disp_rslt'],"data"=> $rslt_data_S['disp_rslt']);	
		}	
	}  
		
	
	$suggestions = array("suggestions"=>$display_data);
	echo json_encode($suggestions);
}
else if($_REQUEST['pageaction'] == "autocomplete_salesentry"){
	$display_data = array();
	
	if(isset($_REQUEST['srch_keyword']) && $_REQUEST['srch_keyword'] !="" ){
		if(isset($_REQUEST['srch_type']) && $_REQUEST['srch_type'] !="" ){
			if($_REQUEST['srch_type'] == "pdt_sku"){
				//$str_query = " SELECT DISTINCT(`pdt_sku`) as disp_rslt FROM `t_purchase_entry_details` WHERE 1=1 and IsActive=1 and `pdt_sku` like '%".$_REQUEST['srch_keyword']."%' LIMIT 10 "; and t2.company_id='".$company_name."'
				
				$str_query = " select t1.`pdt_sku` as disp_rslt, t1.p_invoice_det_id from t_purchase_entry_details t1
				inner join t_purchase_entry_main t2 on t1.p_invoice_no = t2.p_invoice_no and t2.IsActive=1
				where 1=1 and t1.IsActive = 1 and t1.pdt_sku like '%".$_REQUEST['srch_keyword']."%' order by (t1.pdt_sku = '".$_REQUEST['srch_keyword']."') desc, length(t1.pdt_sku) LIMIT 10  ";
				
				$rslt_data = $db->get_rsltset($str_query);
			}			
		}
	}	
		
	if(isset($rslt_data)){		
		foreach($rslt_data as $rslt_data_S)	{
			$display_data[] = array("value"=> $rslt_data_S['disp_rslt'],"data"=> $rslt_data_S['p_invoice_det_id']);	
		}	
	}  
		
	
	$suggestions = array("suggestions"=>$display_data);
	echo json_encode($suggestions);
}else if($_REQUEST['pageaction'] == "autocomplete_purchase_entry"){
	$display_data = array();
	
	if(isset($_REQUEST['srch_keyword']) && $_REQUEST['srch_keyword'] !="" ){
		if(isset($_REQUEST['srch_type']) && $_REQUEST['srch_type'] !="" ){
			if($_REQUEST['srch_type'] == "saree_desc"){
				//$str_query = " SELECT DISTINCT(`pdt_sku`) as disp_rslt FROM `t_purchase_entry_details` WHERE 1=1 and IsActive=1 and `pdt_sku` like '%".$_REQUEST['srch_keyword']."%' LIMIT 10 "; and t2.company_id='".$company_name."'
				
				$str_query = " select DISTINCT t1.`saree_desc` as disp_rslt, t1.p_invoice_det_id from t_purchase_entry_details t1
				inner join t_purchase_entry_main t2 on t1.p_invoice_no = t2.p_invoice_no and t2.IsActive=1
				where 1=1 and t1.IsActive = 1  and t2.sup_id = ".$_REQUEST['supplier']." and t1.saree_desc like '%".$_REQUEST['srch_keyword']."%' order by (t1.saree_desc = '".$_REQUEST['srch_keyword']."') desc, length(t1.saree_desc) LIMIT 10  ";							
				$rslt_data = $db->get_rsltset($str_query);
			}			
		}
	}
		
	if(isset($rslt_data)){		
		foreach($rslt_data as $rslt_data_S)	{
			$id = $rslt_data_S['p_invoice_det_id'];
			$res[$id] = $rslt_data_S['disp_rslt'];
			//$display_data[] = array("value"=> $rslt_data_S['disp_rslt'],"data"=> $rslt_data_S['p_invoice_det_id']);	
		}	
		$res = array_unique($res,SORT_REGULAR);	
		print_r($res); exit;
		foreach($res as $key => $r)	{	
			$display_data[] = array("value"=> $r,"data"=> $key);	
		}
	}  		
	$suggestions = array("suggestions"=>$display_data);
	echo json_encode($suggestions);
}
else{
	echo "No-Data";
}
?>