<?php  
session_start(); 
	include("connect.php");	
	include("connectp.php");
	header("Access-Control-Allow-Origin: *");

	
	$brmid = $_SESSION['linkarisebranchid'];
	$cmpmid = $_SESSION['linkarisecmpmid'];
	$umid = $_SESSION['linkariseumid'];	
	
		$q = $db7->prepare("select customermid, sum(invoicenetamount) as cc from res_invoice_master where cmpmid=$cmpmid group by customermid");
	//$q->bindValue(':p1', "$par1");
	//$q->bindValue(':p2',  "$par2");
	$q->execute();

	$response = array();
	$posts = array();
	
	$result="0";
	
	if ($q->rowCount() > 0)
	{
		$i=1;
		while($i<=$q->rowCount())
		{
			$rt = $q->fetch(PDO::FETCH_ASSOC);
			
			 	
				$partymid=$rt['customermid'];
				$total=$rt['cc'];
				$total=number_format($total,2,'.','');
				$_SESSION['partymid']=$partymid;
				

				$partyname='';
				$partymobile='';
				
				$eve1 = "select * from res_party_master where partymid ='$partymid'";

				$re1 = mysqli_query($conn, $eve1);
																			
				if(mysqli_num_rows($re1) > 0)

				{											
																				
				while($rt1 = mysqli_fetch_assoc($re1))
				{
					$partyname=$rt1['partyname'];
					$partymobile=$rt1['partymobile'];
						
				}
																				 
				}
				
				$paidamount=0;
				
				$eve3 = "select  sum(receiptamont) as paidamount from res_paymentin_master where partymid ='$partymid'";

				$re3 = mysqli_query($conn, $eve3);
																			
				if(mysqli_num_rows($re3) > 0)

				{											
																				
				while($rt3 = mysqli_fetch_assoc($re3))
				{
					$paidamount=$rt3['paidamount'];
						
				}
																				 
				}
				
				
				
			
				$pendingamount=number_format($total,2,'.','')-number_format($paidamount,2,'.','');
				$pendingamount=number_format($pendingamount,2,'.','');
					$pendingamount="<a href='pendingreceiptamount.php?partymid=$partymid'> $pendingamount</a>";
				if($pendingamount>0){
				$posts[] = array('srno'=> $i++, 'partyname'=> $partyname,'outamount'=>$pendingamount,'totalamount'=>$total,'paidamount'=>$paidamount,'invoicenetamount'=>$total,'mobile'=>$partymobile);
				}
		}
		 
		$response['posts'] = $posts;
		echo stripslashes(json_encode( array('data' => $posts)));
		//mysqli_close($conn);
		//return;
	}
	
   else
	{
		$result="0";
		$msg="Invalid Error";
		
		$posts[] = array('p1'=> $result,'p2'=> $msg);
	
		$response['posts'] = $posts;
		echo stripslashes(json_encode( array('item' => $posts)));
	}
   
	mysqli_close($conn);	
?>