<?php
error_reporting(0);
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start();
session_start();
session_cache_limiter('private');
$cache_limiter 	= session_cache_limiter();
session_cache_expire(60);
$cache_expire 	= session_cache_expire(); 
date_default_timezone_set('Asia/Manila'); 
$CURRENT_DATE 	= date('Y-m-d H:i:s');
include_once('../config/minho.php');
include_once('../config/autoloadClass.php');
$autoload	=	new autoloadClass();
$mecon		=	new condbClass();
$mid = isset($_POST['id']) ? trim(strip_tags($_POST['id'])) : "";
$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['check_tobedelivered_date'])   ? $_REQUEST['check_tobedelivered_date']	  : "yes"; 
 
$cond3 = "";
$cond2 = "";
if($tobedate=="yes"){
	if( !empty($rangeStart) && !empty($rangeEnd) ){ 
		$cond2 .= " AND r.ReservationDate BETWEEN ('".date('Y-m-d', strtotime($rangeStart))."') AND ('".date('Y-m-d', strtotime($rangeEnd))."')";
		$cond3 .= " AND rr.ReservationDate BETWEEN ('".date('Y-m-d', strtotime($rangeStart))."') AND ('".date('Y-m-d', strtotime($rangeEnd))."')";
		 
	}else if( !empty($rangeStart)){ 
		$cond2 .= " AND r.ReservationDate >= ('".date('Y-m-d', strtotime($rangeStart))."') ";
		$cond3 .= " AND rr.ReservationDate >= ('".date('Y-m-d', strtotime($rangeStart))."') ";
		 
	}else if( !empty($rangeEnd)){ 
		$cond2 .= " AND r.ReservationDate <= ('".date('Y-m-d', strtotime($rangeEnd))."') ";
		$cond3 .= " AND rr.ReservationDate <= ('".date('Y-m-d', strtotime($rangeEnd))."') ";
		 
	}
}	 

$resultQuery = "SELECT r.ReservationDate as ReservationDate, r.EmployeeType as EmployeeType, COUNT(bpm.Financing) as EmployeeTypeCount, 
ROUND(COUNT(bpm.Financing) * 100 / t.s) AS total  
FROM reservation r LEFT JOIN buyer_payment_monitoring bpm ON bpm.ReservationId=r.Id 
CROSS JOIN (SELECT COUNT(rr.Id) AS s FROM reservation rr WHERE rr.EmployeeType!='-' AND rr.Status='active' $cond3 ) t  
WHERE r.EmployeeType!='-' AND r.Status='active' AND bpm.Financing='pagibig' ";

$sql = $resultQuery;
$sql .= $cond2;   

$mecon->query($sql); 
$main			= $mecon->resultset();  
$arr1['pagibig_employeetype']	= $main[0]['EmployeeType'];
$arr1['pagibig_employeecount']	= $main[0]['EmployeeTypeCount'];
$arr1['pagibig_employeetotal']	= $main[0]['total']; 

$arr3['data'] = $arr1;
echo json_encode($arr3); 
exit();
ob_end_flush();
