<?php
session_start();
	include("connect.php");	
	header("Access-Control-Allow-Origin: *");
$cmpmid=$_SESSION['linkarisecmpmid'];

$tt1="";
	if(isset($_SESSION['partymid']))
	{
		
			$partymid=$_SESSION['partymid'];
			$tt1=" and customermid='$partymid'";
		
	}
		/* if($status=='All'){
		$eve = "select * from res_invoice_master where date(res_invoice_master.invoicedate) between '$lower' and '$upper' and invtype='i' ORDER BY invoicemid desc";
		}
		else{
		$eve = "select * from res_invoice_master where date(res_invoice_master.invoicedate) between '$lower' and '$upper' and invoicestatus='$status' and invtype='i' ORDER BY invoicemid desc";
		}
	
	$re = mysqli_query($conn, $eve); */

	
	
		
	 $eve = "select * from res_invoice_master where cmpmid=$cmpmid $tt1 ORDER BY invoicemid desc";
	$re = mysqli_query($conn, $eve) or die("Query Failed"); 

	$response = array();
	$posts = array();

	$result="0";

	if(mysqli_num_rows($re) > 0)
	{
		$sno=0;
		 while($rtgby2 = mysqli_fetch_assoc($re))
		 {

				$sno++;
				$invoicemid=$rtgby2['invoicemid'];	

					$invoicefordisplay=$rtgby2['invoicefordisplay'];
					$invoicestatus=$rtgby2['invoicestatus'];
					$invoicenetamount=$rtgby2['invoicenetamount'];

					$invoicedate=date("d-m-Y", strtotime($rtgby2['invoicedate']));
					$invoicestatus=$rtgby2['invoicestatus'];
					$customermid=$rtgby2['customermid'];
					
				 $eve71="select sum(receiptamont) as cc1 from res_paymentin_master where receiptrefrenceid=".$invoicemid;
					$re71 = mysqli_query($conn, $eve71);
					$receiptamont="0";
					while($rt71 = mysqli_fetch_assoc($re71))
					{
							$receiptamont=$rt71['cc1'];
						
					}
					$pendingamount=$invoicenetamount-$receiptamont;
					
					$cname="";
					$cmobile="";
					$eve7="select * from res_party_master where partymid=$customermid";
					$re7 = mysqli_query($conn, $eve7);
					while($rt7 = mysqli_fetch_assoc($re7))
					{
							$cname=$rt7['partyname'];
							$cmobile=$rt7['partymobile'];
					}
				if($pendingamount>0){
				$posts[] = array('sno'=> $sno, 'invoicefordisplay'=> $invoicefordisplay, 'invoicedate'=> $invoicedate, 'cname'=> $cname,'invoicenetamount'=> $invoicenetamount,'pendingamount'=> $pendingamount,'invoicemid'=>$invoicemid);
				}
		 }

		$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);


?>
