<?php
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();
$formClass		=	new formClass();
$requestData	=	$_REQUEST; 
$columns = array(
	0	=>	'Id', 
	1	=>	'ReservationDate',
	2	=>	'Phase',
	3	=>	'Block',
	4	=>	'Lot',
	5	=>	'FullName', 
	6	=>	'Broker', 
	7	=>	'Id', 
	8	=>	'ReservationStatus' 
);   


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

$resultQuery 		= "SELECT r.ReservationStatus as ReservationStatus, r.Id as Id, r.Phase as Phase, r.PricelistDate as PricelistDate, 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, r.Broker as Broker, r.Phase as Phase, m.HouseConstruction as HouseConstruction FROM reservation r LEFT JOIN monitoring_folders m ON m.ReservationId=r.Id ";   
$sql			=	$resultQuery.'  WHERE  r.Status=\'active\' AND r.ReservationStatus=\'cancelled\' '; 

if(!empty($requestData['search']['value']) ) { 
	$arr = explode(" ",$requestData['search']['value']);  
	foreach($arr as $ind){
		$ind = str_replace(',','',$ind);
		$sql.=" AND (r.Phase LIKE '%".$ind."%' "; 
		$sql.=" OR m.HouseConstruction LIKE '%".$ind."%' ";
		$sql.=" OR r.Block LIKE '%".$ind."%' ";
		$sql.=" OR r.Lot LIKE '%".$ind."%' ";
		$sql.=" OR r.Project LIKE '%".$ind."%' ";
		$sql.=" OR r.Model LIKE '%".$ind."%' "; 
		$sql.=" OR r.Broker LIKE '%".htmlentities($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."%' ) ";   
	}
}       

 
if( !empty($requestData['columns'][0]['search']['value']) && !empty($requestData['columns'][1]['search']['value']) ){
    $sql .= " AND r.ReservationDate BETWEEN ('".date('Y-m-d', strtotime($requestData['columns'][0]['search']['value']))."') AND ('".date('Y-m-d', strtotime($requestData['columns'][1]['search']['value']))."')";
}else if( !empty($requestData['columns'][0]['search']['value'])){
    $sql .= " AND r.ReservationDate >= ('".date('Y-m-d', strtotime($requestData['columns'][0]['search']['value']))."') ";
}else if( !empty($requestData['columns'][1]['search']['value'])){
    $sql .= " AND r.ReservationDate <= ('".date('Y-m-d', strtotime($requestData['columns'][1]['search']['value']))."') ";
}


if( !empty($rangeStart) && !empty($rangeEnd) ){
    $sql .= " AND r.ReservationDate BETWEEN ('".date('Y-m-d', strtotime($rangeStart))."') AND ('".date('Y-m-d', strtotime($rangeEnd))."')";
}else if( !empty($rangeStart)){
    $sql .= " AND r.ReservationDate >= ('".date('Y-m-d', strtotime($rangeStart))."') ";
}else if( !empty($rangeEnd)){
    $sql .= " AND r.ReservationDate <= ('".date('Y-m-d', strtotime($rangeEnd))."') ";
}

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

$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']." 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[] = (($result[$a]['ReservationDate']=='1970-01-01') || ($result[$a]['ReservationDate']=='')) ? "" : date('m/d/Y', strtotime($result[$a]['ReservationDate']));
				$nestedData[] = html_entity_decode(stripslashes($result[$a]['Phase']));
				$nestedData[] = html_entity_decode(stripslashes($result[$a]['Block']));
				$nestedData[] = html_entity_decode(stripslashes($result[$a]['Lot']));
				$nestedData[] = strtoupper(html_entity_decode(stripslashes($result[$a]['Fullname'])));
				$nestedData[] = html_entity_decode(stripslashes($result[$a]['Broker']));
			//	$nestedData[] = html_entity_decode(stripslashes($result[$a]['PricelistStatus']));  
				$pricedate	= html_entity_decode(stripslashes($result[$a]['PricelistDate'])); 
				$phase		= html_entity_decode(stripslashes($result[$a]['Phase'])); 
				$block		= html_entity_decode(stripslashes($result[$a]['Block'])); 
				$lot		= html_entity_decode(stripslashes($result[$a]['Lot'])); 
				$project	= html_entity_decode(stripslashes($result[$a]['Project'])); 
				$models		= html_entity_decode(stripslashes($result[$a]['Model'])); 
				
				$other = $formClass->getOtherDetails($pricedate,$models,$project,$phase,$block,$lot);		
				//$nestedData[] = $other['pricelist_status'];
				
				$nestedData[] = (!empty($result[$a]['HouseConstruction']) || $result[$a]['HouseConstruction']=NULL) ? $result[$a]['HouseConstruction'] : "";
				//$nestedData[] = (!empty($result[$a]['HouseConstruction']) || $result[$a]['HouseConstruction']=NULL) ? $result[$a]['HouseConstruction'] : $other['pricelist_status'];
			
			
				$nestedData[] = strtoupper(html_entity_decode(stripslashes($result[$a]['ReservationStatus'])));
			$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();
