<?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 => 'invoice',
			1 => 'invoicedate',
			2 => 'sname',
			3 => 'amount',
			4 => 'pay_type',
			5 => 'id'
			
		);

		$sql = "SELECT sup_p.invoice, sup_p.invoicedate,sup_p.supplierid, sup_p.amount, sup_p.pay_type,sup_p.paymentreference,sup_p.id,sm.sname ";
		$sql.=" FROM supplier_payment as sup_p  
		LEFT JOIN  supplier_master as sm ON sm.id=sup_p.supplierid  WHERE sup_p.isdelete=1 and sup_p.status=''  and sup_p.purchase_type='Accounts'";

	$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 (  invoice LIKE '".$requestData['search']['value']."%' ";
		$sql.=" OR invoicedate LIKE '".$requestData['search']['value']."%' ";
		$sql.=" OR sname LIKE '".$requestData['search']['value']."%' ";
		$sql.=" OR amount LIKE '".$requestData['search']['value']."%' ";
		$sql.=" OR pay_type LIKE '".$requestData['search']['value']."%' ";
		$sql.=" OR paymentreference LIKE '".$requestData['search']['value']."%' ";

		$sql.=" OR sup_p.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["invoice"];
	$nestedData[] = $row["invoicedate"];	
	$nestedData[] = $row["sname"];
	$nestedData[] = $row["amount"];
	$nestedData[] = $row["pay_type"];
	$nestedData[] = $row["paymentreference"];


	if($userpermissionEdit==1) {
		$edit .="<a href='accOverPayementEdit.php?spid=".$row['id']."'  id='accOver_".$row['id']."' class='btn btn-primary'>Edit</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);  

?>
