<?php
require "../commonincludefiles.php";
if($_SESSION["cCompany_id"]!="" && isset($_SESSION["cCompany_id"]))
{	
	$custid = Escape($_REQUEST['customer_id']);
	$qry= "SELECT 
				customer_email as email,
				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'];	
		$email =$result['email'];	
		
		$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 
						inv.customerinvoice_no,
						DATE_FORMAT(inv.customerinvoice_date,'%d-%m-%Y') as date,
						cpm.product_type,
						cpm.product_name,
						cpm.product_type,
						inv.status,
						invp.product_qty,
						invp.product_rate,
						floor(invp.product_qty*invp.product_rate) as due
					 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."' and
						(cpm.product_type = 'AMC' or (cpm.product_type != 'AMC' && inv.status != 'Paid'))";
	
		$out_product = dbQuery($qry_product);
		$data = array();
			//echo "<pre>";
		if (dbRows($out_product)){
			$i=0;
			while($result1 = dbArray($out_product))
			{
				if($result1['product_type'] != 'AMC'){
					$data[$i] = $result1;	
					$data[$i]['total']=  $result1['product_qty']*$result1['product_rate'];
					$data[$i]['calulation']=  $result1['product_qty']." * ".$result1['product_rate']." = ".$data[$i]['total'];
				$i++;	
				}
				else
				{
					if(!(isset($data['AMC']))){
						$data['AMC'] = $result1;
						$data['AMC']['total']= $result1['product_qty']*$result1['product_rate'];
						//$data['AMC']['calulation']=  $data['AMC']['product_qty']." * ".$data['AMC']['product_rate']." = ".$data['AMC']['total'];
					}else{
						$data['AMC']['date'] =  $result1['date'];
						$data['AMC']['product_name'] =  $result1['product_name'];
						$data['AMC']['total'] = $data['AMC']['product_rate'] + $result1['product_rate'];
						//$data['AMC']['calulation']=  $data['AMC']['product_rate']." + ".$result1['product_rate']." = ".$data['AMC']['total'];
					}
				}
			}
			foreach($data as $d){
				$product_name1 .= $d['date']." - ".$d['product_name']." ( ".$d['product_type']." ) ".$d['calulation'].",";
			}
		}else{
			$data=0;
			$product_name1 ="";
		}
	}
		else{
			//$product_name1 = implode(",", $product_name);
			$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."',cpmail1:'".$email."'}";	

}
?>