<?php
require_once("config/configDB.php"); 
// storing  request (ie, get/post) global array to a variable  
$requestData= $_REQUEST;
$userpermissionEdit 		= $_POST['userpermissionEdit'];
$userpermissionDelete 		= $_POST['userpermissionDelete'];



//a.id,a.scode,a.sname,a.mobile,a.invoicedate,a.pamount,a.category

	$columns = array( 
		// datatable column index  => database column name
			0 => 'ccode',
			1 => 'cname',
			2 => 'mobile',
			3 => 'pamount',
			4 => 'payamount',
			5 => 'id'
			
		);

	$sql = 'SELECT a.id,a.ccode,a.cname,a.mobile,  sum(a.pamount) as pamount , sum(payamount) as payamount,sum(extra_amount) as extra_amount,a.opening_balance,sum(discount) as discount
	FROM (
	
	SELECT sm.id,sm.ccode,sm.cname,sm.mobile,0 as pamount, 0 as payamount,0 as extra_amount,sm.opening_balance,0 as discount
	
		FROM customer_master as sm 
	
		WHERE sm.isdelete=1 
	
		GROUP BY sm.id,sm.ccode,sm.cname,sm.mobile
		
		UNION ALL
	
		SELECT scm.id,scm.ccode,scm.cname, scm.mobile,sum(pdi.tamount) as pamount, 0 as payamount,sum(pdi.extra_amount) as extra_amount,scm.opening_balance,0 as discount
	
		FROM customer_master as scm 
	
		LEFT JOIN saleheader as pdi ON scm.id=pdi.customer_id 
	
		WHERE scm.isdelete=1  and pdi.status="" 
	
		  GROUP BY scm.id,scm.ccode,scm.cname,scm.mobile
		
		
		UNION ALL
		
		
		SELECT spm.id,spm.ccode,spm.cname,spm.mobile,0 as pamount,sum(sp.received_amount) as payamount,0 as extra_amount,spm.opening_balance,sum(sp.discount) as discount
	
		FROM customer_master as spm 
	
		LEFT JOIN customer_payment_header as sp ON spm.id=sp.customerID
	
		WHERE spm.isdelete=1 and sp.flag=1 and sp.status=""
	
		GROUP BY spm.id,spm.ccode,spm.cname,  spm.mobile
		
		
		
	)
	a  
	';
	//echo $sql;

	$query=mysqli_query($conn, $sql) or die("brandgriddata.php: get brand Data step 1");
	$totalData = mysqli_num_rows($query);
	$totalFiltered = $totalData;
	
	
	
	if( !empty($requestData['search']['value']) ) {   
		$sql.=" where (  a.ccode LIKE '".$requestData['search']['value']."%' ";
		$sql.=" OR a.cname LIKE '".$requestData['search']['value']."%' ";
		$sql.=" OR a.mobile LIKE '".$requestData['search']['value']."%' ";
		$sql.=" OR pamount LIKE '".$requestData['search']['value']."%' ";
		$sql.=" OR payamount LIKE '".$requestData['search']['value']."%' ";

		$sql.=" OR a.id LIKE '".$requestData['search']['value']."%' )";
	}

	//echo $sql;

$query=mysqli_query($conn, $sql) or die("brandgriddata.php: get brand Data step 2".$sql);
$totalFiltered = mysqli_num_rows($query); 

$sql.=" GROUP BY a.id ORDER BY  a.cname  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";

//echo $sql;
$query=mysqli_query($conn, $sql) or die("brandgriddata.php: get brand Data step 3".$sql);
	

$data = array();
while( $row=mysqli_fetch_array($query) ) { 
	$nestedData=array(); 

	$edit  = "";

	$nestedData[] = $row["ccode"];
	$nestedData[] = $row["cname"];
	
	
		
	
	
	
	
	//$nestedData[] = $row["mobile"];
	$nestedData[] = $row["opening_balance"];
	$nestedData[] = $row["pamount"];
	//$nestedData[] = $row["return_amount"];
	$nestedData[] = $row["payamount"];
	
	$nestedData[] = $row["discount"];
	
	$pamount1        = ($row['pamount']+$row['extra_amount']+$row['opening_balance'])-($row['payamount']+$row["discount"]);

$pamount=number_format($pamount1,2,'.','');
	
	$nestedData[] = $pamount;


	if($userpermissionEdit==1) {
		$edit .="<a href='paysalesAdd.php?id=".$row['id']."'  id='accOver_".$row['id']."' class='btn btn-primary'>Pay Now</a>&nbsp";
	}	
	

	$Delete  = "";
	if($userpermissionDelete==1) {
		//$Delete .="<a id='accOverDelete_".$row['id']."' class='btn btn-primary' onclick='ConfirmaccOverDelete(".$row['id'].")' >Delete</a>";
	}
	$nestedData[] = $edit."&nbsp".$Delete;
	
	$data[] = $nestedData;
}

$json_data = array(
			"draw"            => intval( $requestData['draw'] ),   
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ), 
			"data"            => $data   
			);

echo json_encode($json_data);  

?>
