<?php 

require "../commonincludefiles.php";

ini_set('max_execution_time', 0);

if(isset($_POST['customer_id']))
{
	$Cust_id1 = 	$_POST['customer_id'];
}
else
{
	$Cust_id1 = "";
}

if($_SESSION["cCompany_id"]!="" && isset($_SESSION["cCompany_id"]))
{	
 	$qry = "select distinct customer_id, customer_orgname from lms_customer";//echo $name;	
	$phone = $_POST["phone"];
    
	/*$qry = "SELECT 
				lc.customer_id,
				lc.customer_address,
				lc.`customer_orgname`,
				lci.dueamount,
				lci.customerinvoice_id,
				lsip.product_id,
				lsip.product_qty,
				lsip.product_rate,
				GROUP_CONCAT(lp.product_name ORDER BY lp.product_id ASC SEPARATOR ',') as product_name
			   
			FROM 
				lms_customer as lc 
				INNER JOIN lms_customerinvoice as lci ON lc.customer_id= lci.customer_id 
				INNER JOIN lms_customerinvoiceproduct as lsip on lsip.customerinvoice_id = lci.customerinvoice_id  
				left join lms_productmaster as lp  on lp.product_id = lsip.product_id ";
*/
	 
	//$qry = "select customer_id, customer_orgname,customer_address, customer_phone1, customer_cpmobile1 , customer_cpname1 from lms_customer";
	
	if($Cust_id1!="")
	{
		$qry = $qry." WHERE  customer_id = '".$Cust_id1."' ";
	}
	
	if ($phone != "")
	{
$qry = $qry." WHERE `customer_phone1` LIKE '%".$phone."%' OR `customer_phone2` LIKE '%".$phone."%' OR customer_cpmobile1 LIKE '%".$phone."%' OR `customer_orgname` LIKE '%".$phone."%' OR `customer_id` LIKE '%".$phone."%'";
	}
	$qry .= " Order By customer_orgname";
 	$result=dbQuery($qry);
	
	$total = dbRows($result);
	$i=0;
	$distictData = array("count" => $total, "rows" => array());
	if	(dbRows($result) > 0) 
		   {
				$temparray = array();
				while($row = dbArray($result)){
					$temparray["cust_id"]  = $row["cust_id"];
					$temparray["customer_orgname"]  = $row["customer_orgname"];
					$temparray["customer_address"]  = $row["customer_address"];
				
					
					
					$temparray["customer_id"]  = $row["customer_id"];
					/*$temparray["amount_desc"]  = $row["product_name"];
					$temparray["dueamount"]  = $row["dueamount"];*/
					
					$distictData["rows"][$i] = $temparray;   
				$i++;
			}										
	}				   		 
 	echo json_encode($distictData);	
	
}
?>