<?php
include('includes/config/config.php');
error_reporting(E_ERROR | E_PARSE);
$ohc_type = $_REQUEST['ohc_location'];
$patient_category = $_REQUEST['patient_category'];
$dept = $_REQUEST['dept'];
$emp_designation = $_REQUEST['emp_designation'];
$emp_cadre = $_REQUEST['emp_cadre'];
$employer_contractor = $_REQUEST['employer_contractor'];
$gender = $_REQUEST['gender'];
$start_date = $_REQUEST['startDate'];
$end_date = $_REQUEST['endDate'];
$year = implode(',', $_REQUEST['year']);
$month = implode(',', $_REQUEST['month']);

$year2 = implode(',', $_REQUEST['year']);

$year1 = explode(',', $year);


$qry_filter = "";
$qry_date = "";
$qry_date1 = "";

if (isset($ohc_type) && $ohc_type != '') {
	$qry_filter .= " and ohc_location_id ='" . $ohc_type . "' ";
}


if (isset($patient_category) && $patient_category != '') {
	$qry_filter .= " and emp_cat_id ='" . $patient_category . "' ";
}

if (isset($dept) && $dept != '') {
	$qry_filter .= " and dept_id ='" . $dept . "' ";
}

if (isset($emp_designation) && $emp_designation != '') {
	$qry_filter .= " and designation_id ='" . $emp_designation . "' ";
}

if (isset($emp_cadre) && $emp_cadre != '') {
	$qry_filter .= " and emp_cadre ='" . $emp_cadre . "' ";
}

if (isset($employer_contractor) && $employer_contractor != '') {
	$qry_filter .= " and employer_contractor_id ='" . $employer_contractor . "' ";
}

if (isset($gender) && $gender != '') {
	$qry_filter .= " and gender ='" . $gender . "' ";
}

if (isset($start_date) && $start_date != '') {
	$qry_date .= " and medical_entry_date >=str_to_date('" . $start_date . "','%d-%m-%Y') ";
	$qry_date1 .= " and date(doj) >=str_to_date('" . $start_date . "','%d-%m-%Y')";
}
if (isset($end_date) && $end_date != '') {
	$qry_date .= " and medical_entry_date <=str_to_date('" . $end_date . "','%d-%m-%Y') ";
	$qry_date1 .= " and date(doj) <=str_to_date('" . $start_date . "','%d-%m-%Y')";
}
error_log("values::" . $ohc_location . "   " . $patient_category . "  " . $dept . "   " . $emp_designation . "   " . $emp_cadre . "  " . $employer_contractor . "   " . $gender . "   " . $start_date . "   " . $end_date . "  " . $year . "  " . $month . "   " . $ohc_type);

$i = 0;
$record = array();
if (isset($year) && $year != '' && sizeof($year1) > 1) {
	for ($j = 0; $j < sizeof($year1); $j++) {

		error_log("year:" . $year1[$j] . " size:" . sizeof($year1));
		$qry_date = "";
		$qry_date1 = "";

		$qry_date .= "and year(medical_entry_date) ='" . $year1[$j] . "'";
		$qry_date1 .= " and year(doj)<= '" . $year1[$j] . "'";
		$total = 0;

		$pat_query = "SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=0 and YEAR(CURDATE())-year(doj)<2 $qry_date1 $qry_filter ORDER by id asc";

		$pat_result = mysqli_query($conn, $pat_query);

		$total = mysqli_num_rows($pat_result);

		$percentage_for_0_2 = 0;
		$query = "select max(medical_exam_id) from medical_examination where patient_id in (SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=0 and YEAR(CURDATE())-year(doj)<2 $qry_date1 $qry_filter ORDER by id asc) $qry_date and diabetes='1' GROUP BY patient_id ORDER BY patient_id asc";

		$result = mysqli_query($conn, $query);

		error_log("exposure and hypertension:" . $query);

		$result_count = mysqli_num_rows($result);
		if ($total == 0 || $result_count == 0) {
			$record['0-2'][$year1[$j]]['total'] = 0;
		} else {
			$percentage_for_0_2 = ($result_count / $total) * 100;
			$record['0-2'][$year1[$j]]['total'] = round($percentage_for_0_2, 2);
		}

		$pat_query = "SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=2 and YEAR(CURDATE())-year(doj)<5 $qry_date1 $qry_filter ORDER by id asc";

		$pat_result = mysqli_query($conn, $pat_query);

		$total = mysqli_num_rows($pat_result);

		$percentage_for_2_5 = 0;
		$query = "select max(medical_exam_id) from medical_examination where patient_id in (SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=2 and YEAR(CURDATE())-year(doj)<5 $qry_date1 $qry_filter ORDER by id asc) $qry_date and diabetes='1' GROUP BY patient_id ORDER BY patient_id asc";

		$result = mysqli_query($conn, $query);
		error_log("exposure and hypertension:" . $query);

		$result_count = mysqli_num_rows($result);
		if ($total == 0 || $result_count == 0) {
			$record['2-5'][$year1[$j]]['total'] = 0;
		} else {
			$percentage_for_2_5 = ($result_count / $total) * 100;
			$record['2-5'][$year1[$j]]['total'] = round($percentage_for_2_5, 2);
		}

		$pat_query = "SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=5 and YEAR(CURDATE())-year(doj)<10 $qry_date1 $qry_filter ORDER by id asc";

		$pat_result = mysqli_query($conn, $pat_query);

		$total = mysqli_num_rows($pat_result);

		$percentage_for_5_10 = 0;
		$query = "select max(medical_exam_id) from medical_examination where patient_id in (SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=5 and YEAR(CURDATE())-year(doj)<10 $qry_date1 $qry_filter ORDER by id asc) $qry_date and diabetes='1' GROUP BY patient_id ORDER BY patient_id asc";

		$result = mysqli_query($conn, $query);

		error_log("exposure and hypertension:" . $query);

		$result_count = mysqli_num_rows($result);
		if ($total == 0 || $result_count == 0) {
			$record['5-10'][$year1[$j]]['total'] = 0;
		} else {
			$percentage_for_5_10 = ($result_count / $total) * 100;
			$record['5-10'][$year1[$j]]['total'] = round($percentage_for_5_10, 2);
		}

		$pat_query = "SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=10 and YEAR(CURDATE())-year(doj)<15 $qry_date1 $qry_filter ORDER by id asc";

		$pat_result = mysqli_query($conn, $pat_query);

		$total = mysqli_num_rows($pat_result);

		$percentage_for_10_15 = 0;
		$query = "select max(medical_exam_id) from medical_examination where patient_id in (SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=10 and YEAR(CURDATE())-year(doj)<15 $qry_date1 $qry_filter ORDER by id asc) $qry_date and diabetes='1' GROUP BY patient_id ORDER BY patient_id asc";

		$result = mysqli_query($conn, $query);
		error_log("exposure and hypertension:" . $query);

		$result_count = mysqli_num_rows($result);
		if ($total == 0 || $result_count == 0) {
			$record['10-15'][$year1[$j]]['total'] = 0;
		} else {
			$percentage_for_10_15 = ($result_count / $total) * 100;
			$record['10-15'][$year1[$j]]['total'] =  round($percentage_for_10_15, 2);
		}

		$pat_query = "SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=15 and YEAR(CURDATE())-year(doj)<20 $qry_date1 $qry_filter ORDER by id asc";

		$pat_result = mysqli_query($conn, $pat_query);

		$total = mysqli_num_rows($pat_result);

		$percentage_for_15_20 = 0;
		$query = "select max(medical_exam_id) from medical_examination where patient_id in (SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=15 and YEAR(CURDATE())-year(doj)<20 $qry_date1 $qry_filter ORDER by id asc) $qry_date and diabetes='1' GROUP BY patient_id ORDER BY patient_id asc";

		$result = mysqli_query($conn, $query);
		error_log("exposure and hypertension:" . $query);

		$result_count = mysqli_num_rows($result);
		if ($total == 0 || $result_count == 0) {
			$record['15-20'][$year1[$j]]['total'] = 0;
		} else {
			$percentage_for_15_20 = ($result_count / $total) * 100;
			$record['15-20'][$year1[$j]]['total'] = round($percentage_for_15_20, 2);
		}

		$pat_query = "SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=20 and YEAR(CURDATE())-year(doj)<25 $qry_date1 $qry_filter ORDER by id asc";

		$pat_result = mysqli_query($conn, $pat_query);

		$total = mysqli_num_rows($pat_result);

		$percentage_for_20_25 = 0;
		$query = "select max(medical_exam_id) from medical_examination where patient_id in (SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=20 and YEAR(CURDATE())-year(doj)<25 $qry_date1 $qry_filter ORDER by id asc) $qry_date and diabetes='1' GROUP BY patient_id ORDER BY patient_id asc";

		$result = mysqli_query($conn, $query);
		error_log("exposure and hypertension:" . $query);

		$result_count = mysqli_num_rows($result);
		if ($total == 0 || $result_count == 0) {
			$record['20-25'][$year1[$j]]['total'] = 0;
		} else {
			$percentage_for_20_25 = ($result_count / $total) * 100;
			$record['20-25'][$year1[$j]]['total'] = round($percentage_for_20_25, 2);
		}

		$pat_query = "SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=25 $qry_date1 $qry_filter ORDER by id asc";

		$pat_result = mysqli_query($conn, $pat_query);

		$total = mysqli_num_rows($pat_result);

		$percentage_for_25 = 0;
		$query = "select max(medical_exam_id) from medical_examination where patient_id in (SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=25 $qry_date1 $qry_filter ORDER by id asc) $qry_date and diabetes='1' GROUP BY patient_id ORDER BY patient_id asc";

		$result = mysqli_query($conn, $query);
		error_log("exposure and hypertension:" . $query);

		$result_count = mysqli_num_rows($result);

		if ($total == 0 || $result_count == 0) {
			$record['>=25'][$year1[$j]]['total'] = 0;
		} else {
			$percentage_for_25 = ($result_count / $total) * 100;
			$record['>=25'][$year1[$j]]['total'] = round($percentage_for_25, 2);
		}
	}
} else {
	if (isset($year2) && $year2 != '') {
		$qry_date .= " and year(medical_entry_date) ='" . $year2 . "'";
		$qry_date1 .= " and year(doj) <='" . $year2 . "'";
	}

	$total = 0;

	$pat_query = "SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=0 and YEAR(CURDATE())-year(doj)<2 $qry_date1 $qry_filter ORDER by id asc";

	$pat_result = mysqli_query($conn, $pat_query);

	$total = mysqli_num_rows($pat_result);

	$record['0-2']['total'] = $total;

	$percentage_for_0_2 = 0;
	$query = "select max(medical_exam_id) from medical_examination where patient_id in (SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=0 and YEAR(CURDATE())-year(doj)<2 $qry_date1 $qry_filter ORDER by id asc) $qry_date and diabetes='1' GROUP BY patient_id ORDER BY patient_id asc";

	$result = mysqli_query($conn, $query);
	error_log("exposure and hypertension:" . $query);

	$result_count = mysqli_num_rows($result);

	$record['0-2']['hbd'] = $result_count;
	if ($total == 0 || $result_count == 0) {
		$record['0-2']['per'] = 0;
	} else {
		$percentage_for_0_2 = ($result_count / $total) * 100;
		$record['0-2']['per'] = round($percentage_for_0_2, 2);
	}

	$pat_query = "SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=2 and YEAR(CURDATE())-year(doj)<5 $qry_date1 $qry_filter ORDER by id asc";

	$pat_result = mysqli_query($conn, $pat_query);

	$total = mysqli_num_rows($pat_result);

	$record['2-5']['total'] = $total;

	$percentage_for_2_5 = 0;
	$query = "select max(medical_exam_id) from medical_examination where patient_id in (SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=2 and YEAR(CURDATE())-year(doj)<5 $qry_date1 $qry_filter ORDER by id asc) $qry_date and diabetes='1' GROUP BY patient_id ORDER BY patient_id asc";

	$result = mysqli_query($conn, $query);
	error_log("exposure and hypertension:" . $query);

	$result_count = mysqli_num_rows($result);

	$record['2-5']['hbd'] = $result_count;
	if ($total == 0 || $result_count == 0) {
		$record['2-5']['per']  = 0;
	} else {
		$percentage_for_2_5 = ($result_count / $total) * 100;
		$record['2-5']['per']  = round($percentage_for_2_5, 2);
	}

	$pat_query = "SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=5 and YEAR(CURDATE())-year(doj)<10 $qry_date1 $qry_filter ORDER by id asc";

	$pat_result = mysqli_query($conn, $pat_query);

	$total = mysqli_num_rows($pat_result);

	$record['5-10']['total'] = $total;

	$percentage_for_5_10 = 0;
	$query = "select max(medical_exam_id) from medical_examination where patient_id in (SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=5 and YEAR(CURDATE())-year(doj)<10 $qry_date1 $qry_filter ORDER by id asc) $qry_date and diabetes='1' GROUP BY patient_id ORDER BY patient_id asc";

	$result = mysqli_query($conn, $query);
	error_log("exposure and hypertension:" . $query);

	$result_count = mysqli_num_rows($result);

	$record['5-10']['hbd'] = $result_count;
	if ($total == 0 || $result_count == 0) {
		$record['5-10']['per']  = 0;
	} else {
		$percentage_for_5_10 = ($result_count / $total) * 100;
		$record['5-10']['per']  = round($percentage_for_5_10, 2);
	}


	$pat_query = "SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=10 and YEAR(CURDATE())-year(doj)<15 $qry_date1 $qry_filter ORDER by id asc";

	$pat_result = mysqli_query($conn, $pat_query);

	$total = mysqli_num_rows($pat_result);

	$record['10-15']['total'] = $total;

	$percentage_for_10_15 = 0;
	$query = "select max(medical_exam_id) from medical_examination where patient_id in (SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=10 and YEAR(CURDATE())-year(doj)<15 $qry_date1 $qry_filter ORDER by id asc) $qry_date and diabetes='1' GROUP BY patient_id ORDER BY patient_id asc";

	$result = mysqli_query($conn, $query);
	error_log("exposure and hypertension:" . $query);

	$result_count = mysqli_num_rows($result);

	$record['10-15']['hbd'] = $result_count;
	if ($total == 0 || $result_count == 0) {
		$record['10-15']['per']  = 0;
	} else {
		$percentage_for_10_15 = ($result_count / $total) * 100;
		$record['10-15']['per']  = round($percentage_for_10_15, 2);
	}

	$pat_query = "SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=15 and YEAR(CURDATE())-year(doj)<20 $qry_date1 $qry_filter ORDER by id asc";

	$pat_result = mysqli_query($conn, $pat_query);

	$total = mysqli_num_rows($pat_result);

	$record['15-20']['total'] = $total;

	$percentage_for_15_20 = 0;
	$query = "select max(medical_exam_id) from medical_examination where patient_id in (SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=15 and YEAR(CURDATE())-year(doj)<20 $qry_date1 $qry_filter ORDER by id asc) $qry_date and diabetes='1' GROUP BY patient_id ORDER BY patient_id asc";

	$result = mysqli_query($conn, $query);
	error_log("exposure and hypertension:" . $query);

	$result_count = mysqli_num_rows($result);

	$record['15-20']['hbd'] = $result_count;
	if ($total == 0 || $result_count == 0) {
		$record['15-20']['per']  = 0;
	} else {
		$percentage_for_15_20  = ($result_count / $total) * 100;
		$record['15-20']['per']  = round($percentage_for_15_20, 2);
	}

	$pat_query = "SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=20 and YEAR(CURDATE())-year(doj)<25 $qry_date1 $qry_filter ORDER by id asc";

	$pat_result = mysqli_query($conn, $pat_query);

	$total = mysqli_num_rows($pat_result);

	$record['20-25']['total'] = $total;

	$percentage_for_20_25 = 0;
	$query = "select max(medical_exam_id) from medical_examination where patient_id in (SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=20 and YEAR(CURDATE())-year(doj)<25 $qry_date1 $qry_filter ORDER by id asc) $qry_date and diabetes='1' GROUP BY patient_id ORDER BY patient_id asc";

	$result = mysqli_query($conn, $query);
	error_log("exposure and hypertension:" . $query);

	$result_count = mysqli_num_rows($result);

	$record['20-25']['hbd'] = $result_count;
	if ($total == 0 || $result_count == 0) {
		$record['20-25']['per']   = 0;
	} else {
		$percentage_for_20_25  = ($result_count / $total) * 100;
		$record['20-25']['per']   = round($percentage_for_20_25, 2);
	}


	$pat_query = "SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=25 $qry_date1 $qry_filter ORDER by id asc";

	$pat_result = mysqli_query($conn, $pat_query);

	$total = mysqli_num_rows($pat_result);

	$record['>=25']['total'] = $total;

	$percentage_for_25 = 0;
	$query = "select max(medical_exam_id) from medical_examination where patient_id in (SELECT id FROM `patient_master` where status='Active' and YEAR(CURDATE())-year(doj)>=25 $qry_date1 $qry_filter ORDER by id asc) $qry_date and diabetes='1' GROUP BY patient_id ORDER BY patient_id asc";

	$result = mysqli_query($conn, $query);
	error_log("exposure and hypertension:" . $query);

	$result_count = mysqli_num_rows($result);

	$record['>=25']['hbd'] = $result_count;
	if ($total == 0 || $result_count == 0) {
		$record['>=25']['per']   = 0;
	} else {
		$percentage_for_25  = ($result_count / $total) * 100;
		$record['>=25']['per']   = round($percentage_for_25, 2);
	}
}
echo json_encode($record);