<?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(discount) as discount,a.opening_balance,sum(return_amount) as return_amount,a.area_tamil
	FROM (
	
	SELECT sm.id,sm.ccode,sm.cname,sm.mobile,0 as pamount, 0 as payamount,0 as discount,sm.opening_balance,0 as return_amount,sm.area_tamil
	
		FROM customer_master as sm 
	
		WHERE sm.isdelete=1 
	
		GROUP BY sm.id,sm.ccode,sm.cname,sm.mobile,sm.opening_balance
		
		UNION ALL
	
		SELECT scm.id,scm.ccode,scm.cname, scm.mobile,sum(pdi.tamount) as pamount, 0 as payamount,0 as discount,0 as opening_balance,0 as return_amount,scm.area_tamil
	
		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,scm.opening_balance
		
		
		
		
		UNION ALL
		
		SELECT srm.id,srm.ccode,srm.cname,srm.mobile,  0 as pamount, 0 as payamount,0 as discount,0 as opening_balance,sum(rtn.tamount) as return_amount,srm.area_tamil
	
		FROM customer_master as srm 
	
		LEFT JOIN sale_return_header as rtn ON srm.id=rtn.customer_id 
	
		WHERE srm.isdelete=1  and rtn.status="" 
	
		GROUP BY srm.id,srm.ccode,srm.cname,srm.mobile,srm.opening_balance
		
		
		
		
		UNION ALL
		
		
		SELECT spm.id,spm.ccode,spm.cname,spm.mobile,0 as pamount,sum(sp.received_amount) as payamount,sum(discount) as discount,0 as opening_balance,0 as return_amount,spm.area_tamil
	
		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,spm.opening_balance
		
		
		
	)
	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"]."(".$row["area_tamil"].")";
	
	
		
	
	
	
	
	//$nestedData[] = $row["mobile"];
	$nestedData[] = $row["opening_balance"];
	$nestedData[] = $row["pamount"];
	$nestedData[] = $row["return_amount"];
	$nestedData[] = $row["payamount"];
	
	$nestedData[] = $row["discount"];
	
	$pamount1        = ($row['pamount']+$row['opening_balance'])-($row['payamount']+$row['discount']+$row['return_amount']);

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

?>
