<?php
require_once("config/configDB.php"); 
// storing  request (ie, get/post) global array to a variable  
$requestData= $_REQUEST;

$tripfromDate 	= '';
if($_POST['tripfromDate']){
	$invoicedate_temp   = new DateTime(stripslashes($_POST['tripfromDate']));
	$tripfromDate 		= $invoicedate_temp->format('Y-m-d');
}
$triptoDate 	= '';
if($_POST['triptoDate']) {
	$invoicedate_temp1   = new DateTime(stripslashes($_POST['triptoDate']));
	$triptoDate 		= $invoicedate_temp1->format('Y-m-d');
}
$tripvehicle     = $_POST['tripvehicle'];
//$supplierid		 = $_POST['supplierid'];

 	$columns = array( 
		0 => 'id', 
		1 => 'ish.invoicedate',
		2 => 'ish.vehicleno',
		3 => 'ish.givenby',
		4 => 'ish.receivedby',
		5 => 'ish.remarks'
	);
		

		$sql = "SELECT ish.invoicedate, ish.vehicleno, ish.givenby, ish.receivedby,ish.remarks,ish.id,ish.vehicleno 
		FROM internalpro_explosive_hdr as ish  WHERE ish.isdelete=1";

	 if($tripfromDate !=''  && $triptoDate !=''   ) {
	 	$sql.=" and ish.invoicedate BETWEEN '".$tripfromDate."' AND '".$triptoDate."'";

	 }  else if($tripfromDate !=''  && $triptoDate !=''   ) {
		$sql.=" and ish.vehicleno='".$tripvehicle."'  and   ish.invoicedate BETWEEN '".$tripfromDate."' AND '".$triptoDate."'  ";

   	 }  


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

	if( !empty($requestData['search']['value']) ) {   
		$sql.=" AND ( ish.givenby LIKE '".$requestData['search']['value']."%' ";
		//$sql.=" OR ish.givenby LIKE '".$requestData['search']['value']."%' ";
		$sql.=" OR ish.receivedby LIKE '".$requestData['search']['value']."%' ";
		$sql.=" OR ish.remarks LIKE '".$requestData['search']['value']."%' ";
		$sql.="  )";
	}

	


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

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

	$edit  = "";
	  
	
	//$nestedData[] = $row["id"];
	$invoicedate = new DateTime($row["invoicedate"]); 
	$nestedData[] = $invoicedate->format('d-m-Y'); //only fetching date
	//$nestedData[] = $row["vehicleno"];
	
	
	
	
	 $edit_id  = $row["id"];
	$Details_Result  = mysqli_query($conn,"SELECT  ppsd.explosiveID, spm.explosivename ,ppsd.explosiveqty 
FROM internalpro_explosive_dtl as ppsd 
LEFT JOIN explosive_master as spm ON spm.id=ppsd.explosiveID

WHERE  ppsd.hdr_id='$edit_id' ");
	 $DetailsTable ='';
	 while($drow = mysqli_fetch_array($Details_Result))
	 {
	 	$DetailsTable .= $drow['explosivename'].'-&nbsp&nbsp Qty&nbsp:&nbsp'.$drow['explosiveqty'].',&nbsp&nbsp&nbsp';	
	 }

	
	// $nestedData[] 		= 
	// $nestedData[] 		= $row["amount"];
	// $nextservicedate 	= new DateTime($row["nextservicedate"]); 
	// $nestedData[] 		= $nextservicedate->format('d-m-Y');
	// $nestedData[] 		= $row["nextservicekm"];
		
	
	$nestedData[] = $DetailsTable;
	$nestedData[] = $row["givenby"];
	$nestedData[] = $row["receivedby"];
	
	$data[] = $nestedData;
}

$json_data = array(
			"draw"            => intval( $requestData['draw'] ),   
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ), 
			"data"            => $data   
			);

echo json_encode($json_data);  

?>
