<?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 => 'scode',
			1 => 'sname',
			2 => 'mobile',
			3 => 'pamount',
			4 => 'payamount',
			5 => 'id'
			
		);

	$sql = 'SELECT a.id,a.scode,a.sname,a.mobile,sum(a.pamount) as pamount , sum(payamount) as payamount
	FROM (
		SELECT sm.id,sm.scode,sm.sname,sm.mobile,sum(pdi.tamount) as pamount, 0 as payamount 
	
		FROM supplier_master as sm 
	
		LEFT JOIN purchaseheader as pdi ON sm.id=pdi.supplier_id 
	
		WHERE sm.isdelete=1 and pdi.flag=1 and pdi.status="" 
	
		GROUP BY sm.id
		
		
		
		
		UNION ALL
		
		
		SELECT sm.id,sm.scode,sm.sname,sm.mobile,0 as pamount,sum(sp.received_amount) as payamount 
	
		FROM supplier_master as sm 
	
		LEFT JOIN supplier_payment_header as sp ON sm.id=sp.supplierID
	
		WHERE sm.isdelete=1 and sp.flag=1 and sp.status=""
	
		GROUP BY sm.id
		
	)
	a GROUP BY a.id';

	$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.=" AND (  a.scode LIKE '".$requestData['search']['value']."%' ";
		$sql.=" OR a.sname 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']."%' )";
	}

	

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

$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";


$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["scode"];
	$nestedData[] = $row["sname"];
	
	
		
	
	
	
	
	$nestedData[] = $row["mobile"];
	$nestedData[] = $row["pamount"];
	$nestedData[] = $row["payamount"];
	$nestedData[] = $row["pamount"]-$row["payamount"];


	if($userpermissionEdit==1) {
		$edit .="<a href='paypurchaseAdd.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);  

?>
