<?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.cashno as invoice, sup_p.collectiondate as invoicedate,sup_p.supplierID as supplierid, sup_p.received_amount as amount, sup_p.paymenttype as pay_type,sup_p.paymentreference,sup_p.id,sm.sname,sup_p.payment_through FROM supplier_payment_header  as sup_p  
		LEFT JOIN  supplier_master as sm ON sm.id=sup_p.supplierid  WHERE  sup_p.status=''  and sup_p.payment_through='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 (  cashno LIKE '".$requestData['search']['value']."%' ";
		$sql.=" OR collectiondate LIKE '".$requestData['search']['value']."%' ";
		$sql.=" OR sname LIKE '".$requestData['search']['value']."%' ";
		$sql.=" OR received_amount LIKE '".$requestData['search']['value']."%' ";
		$sql.=" OR paymenttype 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["payment_through"];


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

?>
