<?php
include("../db.php");
$requestData= $_REQUEST;
$s = $_POST['s'];
$e = $_POST['e'];
$columns = array( 
	0 =>  'name_cus',
	1 =>  'x',
	2 =>  'y',
	3 =>  'date',
    4 =>  'time',
    5 => 'name_emp',
	6 => 'id_scanner'
);

$sql = "
SELECT * FROM `mony` left join `cus` on `cus`.`id_cus` = `mony`.`idCus` left join `member` on `member`.`id_member` = `cus`.`id_member`  left join `emp` on `emp`.`id_emp` = `mony`.`id_user` where `mony`.`date` between '$s' and '$e'
";
$query=mysqli_query($db, $sql);
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;
$sql = "
SELECT * FROM `mony` left join `cus` on `cus`.`id_cus` = `mony`.`idCus` left join `member` on `member`.`id_member` = `cus`.`id_member`  left join `emp` on `emp`.`id_emp` = `mony`.`id_user` where `mony`.`date` between '$s' and '$e'
";
if( !empty($requestData['search']['value']) ) {  
	$sql.=" AND ( name_cus LIKE '".$requestData['search']['value']."%'  ";
	$sql.=" OR `x` LIKE '".$requestData['search']['value']."%' ";       
	$sql.=" OR `y` LIKE '".$requestData['search']['value']."%' ";       
	$sql.=" OR date LIKE '".$requestData['search']['value']."%' ";       
	$sql.=" OR name_member LIKE '".$requestData['search']['value']."%' ";       
	$sql.=" OR id_scanner LIKE '".$requestData['search']['value']."%' ";       
	$sql.=" OR name_emp LIKE '".$requestData['search']['value']."%' )";    
}
$query=mysqli_query($db, $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($db, $sql);
$data = array();
while( $row=mysqli_fetch_array($query)) {
	$nestedData=array(); 
	$nestedData[] = $row["name_cus"];
	$nestedData[] = $row["name_member"];
	$nestedData[] = $row['id_scanner'];
	$nestedData[] = $row["x"];
	$nestedData[] = $row["name_emp"];
	$nestedData[] = $row["y"];
	$nestedData[] = $row["date"];
	$nestedData[] = $row["time"];
	$data[] = $nestedData;
}
$json_data = array(
			"draw"            => intval( $requestData['draw']),   
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ),
			"data"            => $data
			);

echo json_encode($json_data);

?>



