<?php
require_once("config/configDB.php"); 
// storing  request (ie, get/post) global array to a variable  
$requestData= $_REQUEST;
$userpermissionEdit 	= $_POST['userpermissionEdit'];
$userpermissionDelete 	= $_POST['userpermissionDelete'];
$columns = array( 
// datatable column index  => database column name
	0 => 'id', 
	1 => 'dieseldate',
	2 => 'suppliername',
	3 => 'amount',
	4 => 'vehicleno',
	5 => 'litres'
);

$sql = "SELECT id FROM purchase_dieselinvoice where isdelete=1";
$query=mysqli_query($conn, $sql) or die("brandgriddata.php: get brand Data step 1");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  
// a dies(dieselinvoice, dieseldate, supplierid, supplierinvoice, amount, vehicleno, litres,accountyear,isdelete,createdby,userID,createdate)
$sql = "SELECT dies.dieseldate,dies.supplierid,dies.supplierinvoice,dies.amount,dies.litres,dies.id,dies.vehicleno,sm.sname as suppliername";
$sql.=" FROM purchase_dieselinvoice as dies  
		LEFT JOIN  supplier_master as sm ON dies.supplierid=sm.id WHERE 1=1 and dies.isdelete=1 and sm.isdelete=1 and dies.status=''   ";
if( !empty($requestData['search']['value']) ) {   

	$sql.=" AND ( dies.dieseldate LIKE '".$requestData['search']['value']."%' ";	
	$sql.=" OR suppliername LIKE '".$requestData['search']['value']."%' ";
	//$sql.=" OR dies.supplierinvoice LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR dies.amount LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR dies.vehicleno LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR dies.litres LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR dies.id LIKE '".$requestData['search']['value']."%' )";
}
$query=mysqli_query($conn, $sql) or die("brandgriddata.php: get brand Data step 2");
$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");
	

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

	$nestedData[] = $row["id"];
	$tripedate = new DateTime($row["dieseldate"]); 
	$nestedData[] = $tripedate->format('d-m-Y'); //only fetching date
	$nestedData[] = $row["suppliername"];
	//$nestedData[] = $row["supplierinvoice"];
	$nestedData[] = $row["amount"];
	$nestedData[] = $row["vehicleno"];
	$nestedData[] = $row["litres"];
	
	$edit  = "";
	if($userpermissionEdit==1) {
		$edit .="<a href='piDieselInvoiceEdit.php?id=".$row['id']."' id='piDieselInvoice_".$row['id']."' class='btn btn-primary'>Edit</a>&nbsp";
	}

	$Delete  = "";
	if($userpermissionDelete==1) {
		$Delete .="<a id='piDieselInvoiceDelete_".$row['id']."' class='btn btn-primary' onclick='ConfirmpiDieselInvoiceDelete(".$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);  

?>
