<?  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	=>	'Id',
	1	=>	'FullName',
	2	=>	'Block',
	3	=>	'Lot',
	4	=>	'Broker',
	5	=>	'Tin',
	6	=>	'FullCheck',
	7	=>	'TotalAmount',
	8	=>	'TotalWHT',
	9	=>	'FullDate' 
);   

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



$whereCond = "";
if(!empty($requestData['search']['value']) ) { 
	$arr = explode(" ",$requestData['search']['value']);  
	foreach($arr as $ind){
		$ind = str_replace(',','',$ind);		
		$whereCond.=" AND ( r.Project LIKE '%".$ind."%' ";
		$whereCond.=" OR r.Model LIKE '%".$ind."%' ";
		$whereCond.=" OR r.Block LIKE '%".$ind."%' ";
		$whereCond.=" OR r.Lot LIKE '%".$ind."%' ";
		$whereCond.=" OR r.Broker LIKE '%".$ind."%' ";
		$whereCond.=" OR CONCAT(r.LastName,', ',r.FirstName,' ',r.MiddleName) LIKE '%".htmlentities($ind)."%' ";
		$whereCond.=" OR UPPER(DATE_FORMAT(rb.FullDate,'%m/%d/%Y')) LIKE '%".$ind."%' ";
		$whereCond.=" OR UPPER(DATE_FORMAT(r.ReservationDate,'%m/%d/%Y')) LIKE '%".$ind."%' ) ";   
	}
}


if($tobedate=="yes"){
	if( !empty($rangeStart) && !empty($rangeEnd) ){
		$whereCond .= " AND rb.FullDate BETWEEN ('".date('Y-m-d', strtotime($rangeStart))."') AND ('".date('Y-m-d', strtotime($rangeEnd))."')";
	}else if( !empty($rangeStart)){
		$whereCond .= " AND rb.FullDate >= ('".date('Y-m-d', strtotime($rangeStart))."') ";
	}else if( !empty($rangeEnd)){
		$whereCond .= " AND rb.FullDate <= ('".date('Y-m-d', strtotime($rangeEnd))."') ";
	}
}

$resultQuery 		= "SELECT r.Id as Id, r.Block as Block, r.Lot as Lot, r.Broker as Broker, CONCAT(r.LastName,', ',r.FirstName,' ', r.MiddleName) as Fullname, r.ReservationDate, r.ReservationStatus, r.Project, r.Model, DATE_FORMAT(rb.FullDate, '%m/%d/%Y') as FullDate, rb.FullCheck as FullCheck, rb.TotalAmount as TotalAmount, rb.TotalWHT as TotalWHT, (SELECT TIN FROM tin WHERE type='broker' AND UPPER(CompanyName)=UPPER(r.Broker)) as Tin FROM releases_to_broker_for_wht rb LEFT JOIN reservation r ON rb.ReservationId=r.Id WHERE r.Status='active' $whereCond "; 
 

  $sql			=	$resultQuery ;

$mecon->query($sql);
$result			= $mecon->resultset(); 
$totalData		= count($result);
$totalFiltered	=	$totalData;
  
 $sql.=" ORDER BY ". $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 Broker, Id, ReleaseType LIMIT ".$requestData['start']." ,".$requestData['length'] ;

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

		for($a=0; $a<count($result); $a++){
			$nestedData=array(); 
			$nestedData[] = $result[$a]['Id'];
			$nestedData[] = $result[$a]['Fullname'];
			$nestedData[] = $result[$a]['Block'];
			$nestedData[] = $result[$a]['Lot'];
			$nestedData[] = $result[$a]['Broker']; //Realty
			$nestedData[] = $result[$a]['Tin'];
			$nestedData[] = $result[$a]['FullCheck']; //Account No. of Broker	
			$nestedData[] = ($result[$a]['TotalAmount']!=0.00) ? number_format($result[$a]['TotalAmount'],2) : ''; //Full Commission
			$nestedData[] = ($result[$a]['TotalWHT']!=0.00) ? number_format($result[$a]['TotalWHT'],2) : ''; 	//EWT
			$nestedData[] = $result[$a]['FullDate'];	//Date Processed 
			$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();