<?  
error_reporting(0);
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start();
session_start();  
include_once('../config/minho.php');
include_once('../config/autoloadClass.php');
$autoload		=	new autoloadClass(); 
$mecon			=	new condbClass();
$requestData	=	$_REQUEST; 
$columns = array(
	0	=>	'Fullname', 
	1	=>	'Block',
	2	=>	'Lot',
	3	=>	'ReservationDate',
	4	=>	'TotalReleases', 
	5	=>	'NetAmount',
	6	=>	'EWT',
	7	=>	'TotalCommission'  
);     

$rangeStart = isset($_REQUEST['range_start']) ?  $_REQUEST['range_start'] : date('Y-m-d', strtotime(date('01-m-Y')));
$rangeEnd 	= isset($_REQUEST['range_end'])   ? $_REQUEST['range_end']	  : date("Y-m-t", strtotime(date('Y-m-d')));
$tobedate 	= isset($_REQUEST['tobe_stat_date'])   ? $_REQUEST['tobe_stat_date']	  : "yes";
$resultQuery 		= "SELECT r.Id as Id, r.Block as Block, r.Lot as Lot, r.Model as Model, r.Project as Project, r.ReservationDate as ReservationDate, CONCAT(r.LastName,', ',r.FirstName,' ',r.MiddleName) as Fullname, da.TotalReleases as TotalReleases, da.NetAmount as NetAmount , da.EWT as EWT, da.TotalCommission as TotalCommission  FROM discrepancies_with_balance_alert da LEFT JOIN reservation r ON da.ReservationId=r.Id ";  
$sql			=	$resultQuery."  WHERE  r.Status='active'  "; 

if(!empty($requestData['search']['value']) ) { 
	$arr = explode(" ",$requestData['search']['value']);  
	foreach($arr as $ind){
		$ind = str_replace(',','',$ind);		
		$sql.=" AND ( r.Block LIKE '%".$ind."%' ";
		$sql.=" OR r.Lot LIKE '%".$ind."%' ";
		$sql.=" OR r.Project LIKE '%".$ind."%' ";
		$sql.=" OR r.Model LIKE '%".$ind."%' ";  
		$sql.=" OR da.TotalReleases LIKE '%".$ind."%' ";  
		$sql.=" OR da.NetAmount LIKE '%".$ind."%' ";  
		$sql.=" OR da.EWT LIKE '%".$ind."%' ";  
		$sql.=" OR da.TotalCommission LIKE '%".$ind."%' ";  		 
		$sql.=" OR CONCAT(r.LastName,', ',r.FirstName,' ',r.MiddleName) LIKE '%".htmlentities($ind)."%' "; 
		$sql.=" OR UPPER(DATE_FORMAT(r.ReservationDate,'%m/%d/%Y')) LIKE '%".$ind."%' ";
		$sql.=" OR UPPER(DATE_FORMAT(r.ReservationDate,'%d-%b-%Y')) LIKE '%".$ind."%' ) ";   
	}
}        

$mecon->query($sql);
$result			= $mecon->resultset(); 
$totalData		= count($result);
$totalFiltered	=	$totalData;

//$sql.=" ORDER BY bcm.ChequeDate DESC, ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']." ,cast(r.Block as unsigned) asc, cast(r.Lot as unsigned) asc LIMIT ".$requestData['start']." ,".$requestData['length']."   "; 


$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']." ,cast(Block as unsigned) asc, cast(Lot as unsigned) asc  LIMIT ".$requestData['start']." ,".$requestData['length']."   "; 


$mecon->query($sql);
$result			= $mecon->resultset();

$data = array();
 

$lack = "";
		for($a=0; $a<count($result); $a++){
				$nestedData	=	array();
				//$nestedData[] = $a+1;
				$nestedData[] =  strtoupper(html_entity_decode(stripslashes($result[$a]['Fullname'])));
				$nestedData[] = html_entity_decode(stripslashes($result[$a]['Block']));
				$nestedData[] = html_entity_decode(stripslashes($result[$a]['Lot'])); 
				$nestedData[] = (($result[$a]['ReservationDate']=='1970-01-01') || ($result[$a]['ReservationDate']=='')) ? "" : date('F d, Y', strtotime($result[$a]['ReservationDate']));
				$nestedData[] = number_format($result[$a]['TotalReleases'],2);
				$nestedData[] = number_format($result[$a]['NetAmount'],2);
				$nestedData[] = number_format($result[$a]['EWT'],2);
				$nestedData[] = number_format($result[$a]['TotalCommission'],2);
				
				 
				 
				
				 
			$data[] = $nestedData;
		} 

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

 

 
exit();
ob_end_flush();
