<?php
require "../commonincludefiles.php";
if($_SESSION["cCompany_id"]!="" && isset($_SESSION["cCompany_id"]))
{	
	$custid = $_REQUEST['customer_id'];
	$qry= "SELECT 
				customer_address as add1 , 
	 			customer_phone1 as mob ,	
	 			customer_phone2 as phone ,
	 			customer_cpname1 as per,
				customer_cpmobile1 as per_no
			FROM `lms_customer`
			WHERE `customer_id` ='".$custid."'";
						
	
		$out = dbQuery($qry);
		$result = dbArray($out);
		
		$person =$result['per'];
		$person_no =$result['per_no'];
		$add=$result['add1'];	
		$mob =$result['mob'];		
		$phone =$result['phone'];	
		
		$due_qry= "SELECT 
					SUM(inv.dueamount) AS dueamount
				FROM 
					lms_customerinvoice inv,
					lms_customer cust
				where 
					inv.customer_id=cust.customer_id
					AND inv.status != 'Paid'
					AND inv.customer_id='".$custid."'
					group by inv.customer_id ";	
		$out_due = dbQuery($due_qry);
		$result_due = dbArray($out_due);		
		$dueamount = $result_due['dueamount'];
		
	$qry_billchk  = " SELECT * from lms_customerinvoice WHERE customer_id='".$custid."'";	
	$out_billchk = dbQuery($qry_billchk);
	if(dbRows($out_billchk) > 0){
	$qry_product =  
		 "SELECT 
				group_concat(concat('Inv : ',inv.customerinvoice_no,'  ',DATE_FORMAT(inv.customerinvoice_date,'%d-%m-%Y'),'  ',cpm.product_type,' ',cpm.product_name,' ( ',cpm.product_type,' )',' : ',floor(invp.product_qty),' * ',invp.product_rate,' = ',floor(invp.product_qty*invp.product_rate)) ORDER BY cpm.product_id ASC SEPARATOR ',') as product_name,inv.status,cpm.product_type
		FROM 
			lms_customerinvoice inv,
			lms_customerinvoiceproduct invp,
			lms_productmaster cpm
		where 
			
			invp.customerinvoice_id = inv.customerinvoice_id AND
			invp.product_id = cpm.product_id AND 
			inv.customer_id='".$custid."'
		group by inv.customerinvoice_id";
		/*echo $qry_product;
		exit;*/
		
 		$out_product = dbQuery($qry_product);
		if(dbRows($out_product) > 0){
		$i=0;
		while($result1 = dbArray($out_product)){
			if($result1['status'] != 'paid' ||  $result1['product_type'] == 'AMC' ){
				$product_name[] = $result1['product_name'];
				$i++;
			}
			else{
				continue;
			}
		}
		$product_name1 = implode(",", $product_name);
		}
	}
	else{
			$product_name1 = "Bill is not Avaible";
		}
		echo "{success:true,add:'".$add."' ,per:'".$person."'  ,per_no:'".$person_no."',mob1:'".$mob."'  ,ph:'".$phone."',dueamount:'".$dueamount."',product_name:'".$product_name1."'}";	

}
?>