<?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();
$requestData	=	$_REQUEST; 
$columns = array(
	0	=>	'Id', 
	1	=>	'DateIssued',
	2	=>	'DatePOIssued',
	3	=>	'Contractor',
	4	=>	'Block',
	5	=>	'Lot',
	6	=>	'Units',
	7	=>	'ContractPrice',
	8	=>	'ReleasesRemarks'
);   
//Nov1,2020 changes -> rename DateIssued db column to "Date PO issued" in label UI and DateISSued db column rename label to Completion Date

$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 Id, DateIssued, DatePOIssued, Contractor, Block, Lot, Units, ContractPrice, ReleasesRemarks from releases_to_contractor"; 
   
$sql			=	$resultQuery."  WHERE Status = 'active' ";
if(!empty($requestData['search']['value']) ) { 
	$arr = explode(" ",$requestData['search']['value']);  
	foreach($arr as $ind){
		$ind = str_replace(',','',$ind);
		$sql.=" AND (Block LIKE '%".$ind."%' "; 
		$sql.=" OR Lot LIKE '%".$ind."%' "; 
		$sql.=" OR Units LIKE '%".$ind."%' "; 
		$sql.=" OR UPPER(DATE_FORMAT(DateIssued,'%m/%d/%Y')) LIKE '%".$ind."%' ";  
		$sql.=" OR Contractor LIKE '%".htmlentities($ind)."%' ";
		$sql.=" OR ContractPrice LIKE '%".htmlentities($ind)."%' ";
		$sql.=" OR ReleasesRemarks LIKE '%".$ind."%'  ) ";		 
	}
}
if($tobedate=="yes"){
	if( !empty($rangeStart) && !empty($rangeEnd) ){
		$sql .= " AND DateIssued BETWEEN ('".date('Y-m-d', strtotime($rangeStart))."') AND ('".date('Y-m-d', strtotime($rangeEnd))."')";
	}else if( !empty($rangeStart)){
		$sql .= " AND DateIssued >= ('".date('Y-m-d', strtotime($rangeStart))."') ";
	}else if( !empty($rangeEnd)){
		$sql .= " AND DateIssued <= ('".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']."  ,cast(Block as unsigned) asc, cast(Lot as unsigned) asc LIMIT ".$requestData['start']." ,".$requestData['length']."   "; 
 
$data = array(); 
$mecon->query($sql);
$result			= $mecon->resultset();  

		for($a=0; $a<count($result); $a++){
			$nestedData	=	array(); 
	$nestedData[] = $a+1;
			$nestedData[] = (($result[$a]['DateIssued']=='1970-01-01') || ($result[$a]['DateIssued']=='')) ? "" : date('m/d/Y', strtotime($result[$a]['DateIssued']));

			$nestedData[] = (($result[$a]['DatePOIssued']=='1970-01-01') || ($result[$a]['DatePOIssued']=='')) ? "" : date('m/d/Y', strtotime($result[$a]['DatePOIssued']));

			$nestedData[] = $result[$a]['Contractor'];
			$nestedData[] = $result[$a]['Block'];
			$nestedData[] = $result[$a]['Lot'];
			
			$nestedData[] = $result[$a]['Units'];  
			$nestedData[] = !empty($result[$a]['ContractPrice']) ? number_format($result[$a]['ContractPrice'],2) : "";

			$nestedData[] = $result[$a]['ReleasesRemarks'];
			
			$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();