<?php
include('includes/config/config.php');
include('includes/functions.php');
include('log_entry.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);
// $year1 = array(2020,2021);


error_log("values::" . $ohc_location . "   " . $patient_category . "  " . $dept . "   " . $emp_designation . "   " . $emp_cadre . "  " . $employer_contractor . "   " . $gender . "   " . $start_date . "   " . $end_date . "  " . $year . "  " . $month);



$qry_filter = "";

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


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

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

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

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($start_date) && $start_date != '') {
    $qry_date1 .= " and date(medical_entry_date) >=str_to_date('" . $start_date . "','%d-%m-%Y') ";
}
if (isset($end_date) && $end_date != '') {
    $qry_date1 .= " and date(medical_entry_date) <=str_to_date('" . $end_date . "','%d-%m-%Y') ";
}

if (isset($month) && $month != '') {
    $qry_date1 .= " and month(medical_entry_date) in (" . $month . ")";
}


$data = array();

if (isset($year) && $year != '' && count($year1) > 1) {

    for ($j = 0; $j < count($year1); $j++) {

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

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

        //for smoking
        $total_hbd_pat = 0;
        $count_smoking = "SELECT max(medical_exam_id),patient_id,smoking from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter $qry_doj) $qry_date1 $qry_date and smoking='1' GROUP by patient_id";

        error_log("count smoking:" . $count_smoking);
        $smoking_result = mysqli_query($conn, $count_smoking);

        if (mysqli_num_rows($smoking_result)) {
            $total_smoking_pat = mysqli_num_rows($smoking_result);
        } else {
            $total_smoking_pat = 0;
        }

        $data['smoking']['total'] = $total_smoking_pat;

        error_log("smoke total : " .  $data['smoking']['total']);

        $count_hbd = "SELECT max(medical_exam_id),patient_id,smoking from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter $qry_doj) $qry_date1 $qry_date and smoking='1' and bmi>=25 and bmi<30  GROUP by patient_id";

        error_log("count hbd:" . $count_hbd);
        $hbd_result = mysqli_query($conn, $count_hbd);

        if (mysqli_num_rows($hbd_result)) {
            $total_hbd_pat = mysqli_num_rows($hbd_result);
        } else {
            $total_hbd_pat = 0;
        }

        $data['smoking']['hbd_total'] = $total_hbd_pat;

        error_log("smoke hbd total : " . $data['smoking']['hbd_total']);
        if ($total_hbd_pat == 0 || $total_smoking_pat == 0) {
            $data['smoking'][$year1[$j]]['total'] = 0.00;
        } else {
            $data['smoking'][$year1[$j]]['total'] = round(($total_hbd_pat / $total_smoking_pat) * 100, 2);
        }

        error_log("smoke hbd : " . $data['smoking']['hbd']['per']);


        //for tobacco
        $total_hbd_pat = 0;
        $count_tobacco = "SELECT max(medical_exam_id),patient_id,tobacco from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter $qry_doj) $qry_date1 $qry_date and tobacco='1' GROUP by patient_id";

        error_log("count tobacco:" . $count_tobacco);
        $tobacco_result = mysqli_query($conn, $count_tobacco);

        if (mysqli_num_rows($tobacco_result)) {
            $total_tobacco_pat = mysqli_num_rows($tobacco_result);
        } else {
            $total_tobacco_pat = 0;
        }

        $data['tobacco']['total'] = $total_tobacco_pat;

        error_log("tobacco total : " .  $data['tobacco']['total']);

        $count_hbd = "SELECT max(medical_exam_id),patient_id,tobacco from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter $qry_doj) $qry_date1 $qry_date and tobacco='1' and bmi>=25 and bmi<30  GROUP by patient_id";

        error_log("count hbd:" . $count_hbd);
        $hbd_result = mysqli_query($conn, $count_hbd);

        if (mysqli_num_rows($hbd_result)) {
            $total_hbd_pat = mysqli_num_rows($hbd_result);
        } else {
            $total_hbd_pat = 0;
        }

        $data['tobacco']['hbd_total'] = $total_hbd_pat;

        error_log("tobacco hbd total : " . $data['tobacco']['hbd_total']);
        if ($total_hbd_pat == 0 || $total_tobacco_pat == 0) {
            $data['tobacco'][$year1[$j]]['total'] = 0.00;
        } else {
            $data['tobacco'][$year1[$j]]['total'] = round(($total_hbd_pat / $total_tobacco_pat) * 100, 2);
        }

        error_log("tobacco hbd : " . $data['tobacco']['hbd']['per']);


        //for alcohol
        $total_hbd_pat = 0;
        $count_alcohol = "SELECT max(medical_exam_id),patient_id,alcohol from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter $qry_doj) $qry_date1 $qry_date and alcohol='1' GROUP by patient_id";

        error_log("count alcohol:" . $count_alcohol);
        $alcohol_result = mysqli_query($conn, $count_alcohol);

        if (mysqli_num_rows($alcohol_result)) {
            $total_alcohol_pat = mysqli_num_rows($alcohol_result);
        } else {
            $total_alcohol_pat = 0;
        }

        $data['alcohol']['total'] = $total_alcohol_pat;

        error_log("alcohol total : " .  $data['alcohol']['total']);

        $count_hbd = "SELECT max(medical_exam_id),patient_id,alcohol from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter $qry_doj) $qry_date1 $qry_date and alcohol='1' and bmi>=25 and bmi<30  GROUP by patient_id";

        error_log("count hbd:" . $count_hbd);
        $hbd_result = mysqli_query($conn, $count_hbd);

        if (mysqli_num_rows($hbd_result)) {
            $total_hbd_pat = mysqli_num_rows($hbd_result);
        } else {
            $total_hbd_pat = 0;
        }

        $data['alcohol']['hbd_total'] = $total_hbd_pat;

        error_log("alcohol hbd total : " . $data['alcohol']['hbd_total']);
        if ($total_hbd_pat == 0 || $total_alcohol_pat == 0) {
            $data['alcohol'][$year1[$j]]['total'] = 0.00;
        } else {
            $data['alcohol'][$year1[$j]]['total'] = round(($total_hbd_pat / $total_alcohol_pat) * 100, 2);
        }

        error_log("alcohol hbd : " . $data['alcohol']['hbd']['per']);


        //for smoking+tobacco
        $count_smoking_tobacco = "SELECT max(medical_exam_id),patient_id,smoking from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter $qry_doj) $qry_date1 $qry_date and smoking='1' and tobacco='1' GROUP by patient_id";

        error_log("count smoking_tobacco:" . $count_smoking_tobacco);
        $smoking_tobacco_result = mysqli_query($conn, $count_smoking_tobacco);
        if (mysqli_num_rows($smoking_tobacco_result)) {
            $total_smoking_tobacco_pat = mysqli_num_rows($smoking_tobacco_result);
        } else {
            $total_smoking_tobacco_pat = 0;
        }

        $data['smoking_tobacco']['total'] = $total_smoking_tobacco_pat;

        $count_hbd = "SELECT max(medical_exam_id),patient_id,smoking from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter $qry_doj) $qry_date1 $qry_date and smoking='1' and tobacco='1' and bmi>=25 and bmi<30 GROUP by patient_id";

        error_log("count hbd:" . $count_hbd);
        $hbd_result = mysqli_query($conn, $count_hbd);
        if (mysqli_num_rows($hbd_result)) {
            $total_hbd_pat = mysqli_num_rows($hbd_result);
        } else {
            $total_hbd_pat = 0;
        }

        $data['smoking_tobacco']['hbd_total'] = $total_hbd_pat;


        if ($total_hbd_pat == 0 || $total_smoking_tobacco_pat == 0) {
            $data['smoking_tobacco'][$year1[$j]]['total'] = 0.00;
        } else {
            $data['smoking_tobacco'][$year1[$j]]['total'] = round(($total_hbd_pat / $total_smoking_tobacco_pat) * 100, 2);
        }

        error_log("smoking_tobacco hbd : " . $data['smoking_tobacco']['hbd']['per']);


        //for alcohol+smoking
        $total_hbd_pat = 0;
        $count_smoking_alcohol = "SELECT max(medical_exam_id),patient_id,smoking from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter $qry_doj) $qry_date1 $qry_date and smoking='1' and alcohol='1' GROUP by patient_id";

        error_log("count smoking_alcohol:" . $count_smoking_alcohol);
        $smoking_alcohol_result = mysqli_query($conn, $count_smoking_alcohol);
        if (mysqli_num_rows($smoking_alcohol_result)) {
            $total_smoking_alcohol_pat = mysqli_num_rows($smoking_alcohol_result);
        } else {
            $total_smoking_alcohol_pat = 0;
        }

        $data['smoking_alcohol']['total'] = $total_smoking_alcohol_pat;

        $count_hbd = "SELECT max(medical_exam_id),patient_id,smoking from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter $qry_doj) $qry_date1 $qry_date and smoking='1' and alcohol='1' and bmi>=25 and bmi<30 GROUP by patient_id";

        error_log("count hbd:" . $count_hbd);
        $hbd_result = mysqli_query($conn, $count_hbd);
        if (mysqli_num_rows($hbd_result)) {
            $total_hbd_pat = mysqli_num_rows($hbd_result);
        } else {
            $total_hbd_pat = 0;
        }

        $data['smoking_alcohol']['hbd_total'] = $total_hbd_pat;

        if ($total_hbd_pat == 0 || $total_smoking_alcohol_pat == 0) {
            $data['smoking_alcohol'][$year1[$j]]['total'] = 0.00;
        } else {
            $data['smoking_alcohol'][$year1[$j]]['total'] = round(($total_hbd_pat / $total_smoking_alcohol_pat) * 100, 2);
        }

        error_log("smoking_alcohol hbd : " . $data['smoking_alcohol']['hbd']['per']);



        //for alcohol+tobacco
        $total_hbd_pat = 0;
        $count_tobacco_alcohol = "SELECT max(medical_exam_id),patient_id,tobacco from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter $qry_doj) $qry_date1 $qry_date and tobacco='1' and alcohol='1' GROUP by patient_id";

        error_log("count tobacco_alcohol:" . $count_tobacco_alcohol);
        $tobacco_alcohol_result = mysqli_query($conn, $count_tobacco_alcohol);
        if (mysqli_num_rows($tobacco_alcohol_result)) {
            $total_tobacco_alcohol_pat = mysqli_num_rows($tobacco_alcohol_result);
        } else {
            $total_tobacco_alcohol_pat = 0;
        }

        $data['tobacco_alcohol']['total'] = $total_tobacco_alcohol_pat;

        $count_hbd = "SELECT max(medical_exam_id),patient_id,tobacco from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter $qry_doj) $qry_date1 $qry_date and tobacco='1' and alcohol='1' and bmi>=25 and bmi<30 GROUP by patient_id";

        error_log("count hbd:" . $count_hbd);
        $hbd_result = mysqli_query($conn, $count_hbd);
        if (mysqli_num_rows($hbd_result)) {
            $total_hbd_pat = mysqli_num_rows($hbd_result);
        } else {
            $total_hbd_pat = 0;
        }
        $data['tobacco_alcohol']['hbd_total'] = $total_hbd_pat;


        if ($total_hbd_pat == 0 || $total_tobacco_alcohol_pat == 0) {
            $data['tobacco_alcohol'][$year1[$j]]['total'] = 0.00;
        } else {
            $data['tobacco_alcohol'][$year1[$j]]['total'] = round(($total_hbd_pat / $total_tobacco_alcohol_pat) * 100, 2);
        }

        error_log("tobacco_alcohol hbd : " . $data['tobacco_alcohol']['hbd']['per']);



        //for smoking+alcohol+tobacco
        $total_hbd_pat = 0;
        $count_smoking_tobacco_alcohol = "SELECT max(medical_exam_id),patient_id,smoking from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter $qry_doj) $qry_date1 $qry_date and smoking='1' and tobacco='1' and alcohol='1' GROUP by patient_id";

        error_log("count smoking_tobacco_alcohol:" . $count_smoking_tobacco_alcohol);
        $smoking_tobacco_alcohol_result = mysqli_query($conn, $count_smoking_tobacco_alcohol);
        if (mysqli_num_rows($smoking_tobacco_alcohol_result)) {
            $total_smoking_tobacco_alcohol_pat = mysqli_num_rows($smoking_tobacco_alcohol_result);
        } else {
            $total_smoking_tobacco_alcohol_pat = 0;
        }

        $data['smoking_tobacco_alcohol']['total'] = $total_smoking_tobacco_alcohol_pat;

        $count_hbd = "SELECT max(medical_exam_id),patient_id,smoking from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter $qry_doj) $qry_date1 $qry_date and smoking='1' and tobacco='1' and alcohol='1' and bmi>=25 and bmi<30 GROUP by patient_id";

        error_log("count hbd:" . $count_hbd);
        $hbd_result = mysqli_query($conn, $count_hbd);
        if (mysqli_num_rows($hbd_result)) {
            $total_hbd_pat = mysqli_num_rows($hbd_result);
        } else {
            $total_hbd_pat = 0;
        }

        $data['smoking_tobacco_alcohol']['hbd_total'] = $total_hbd_pat;

        if ($total_hbd_pat == 0 || $total_smoking_tobacco_alcohol_pat == 0) {
            $data['smoking_tobacco_alcohol'][$year1[$j]]['total'] = 0.00;
        } else {
            $data['smoking_tobacco_alcohol'][$year1[$j]]['total'] = round(($total_hbd_pat / $total_smoking_tobacco_alcohol_pat) * 100, 2);
        }
    }
} else {
    if (isset($year2) && $year2 != '') {
        $qry_date1 .= " and year(medical_entry_date) ='" . $year2 . "'";
        $qry_filter .= " and year(doj) <= '" . $year2 . "'";
    }

    //for smoking
    $total_hbd_pat = 0;
    $count_smoking = "SELECT max(medical_exam_id),patient_id,smoking from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter) $qry_date1 and smoking='1' GROUP by patient_id";

    error_log("count smoking:" . $count_smoking);
    $smoking_result = mysqli_query($conn, $count_smoking);

    if (mysqli_num_rows($smoking_result)) {
        $total_smoking_pat = mysqli_num_rows($smoking_result);
    } else {
        $total_smoking_pat = 0;
    }

    $data['smoking']['total'] = $total_smoking_pat;

    error_log("smoke total : " .  $data['smoking']['total']);

    $count_hbd = "SELECT max(medical_exam_id),patient_id,smoking from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter) $qry_date1 and smoking='1' and bmi>=25 and bmi<30  GROUP by patient_id";

    error_log("count hbd:" . $count_hbd);
    $hbd_result = mysqli_query($conn, $count_hbd);

    if (mysqli_num_rows($hbd_result)) {
        $total_hbd_pat = mysqli_num_rows($hbd_result);
    } else {
        $total_hbd_pat = 0;
    }

    $data['smoking']['hbd_total'] = $total_hbd_pat;

    error_log("smoke hbd total : " . $data['smoking']['hbd_total']);
    if ($total_hbd_pat == 0 || $total_smoking_pat == 0) {
        $data['smoking']['hbd']['per'] = 0.00;
    } else {
        $data['smoking']['hbd']['per'] = round(($total_hbd_pat / $total_smoking_pat) * 100, 2);
    }

    error_log("smoke hbd : " . $data['smoking']['hbd']['per']);


    //for tobacco
    $total_hbd_pat = 0;
    $count_tobacco = "SELECT max(medical_exam_id),patient_id,tobacco from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter) $qry_date1 and tobacco='1' GROUP by patient_id";

    error_log("count tobacco:" . $count_tobacco);
    $tobacco_result = mysqli_query($conn, $count_tobacco);

    if (mysqli_num_rows($tobacco_result)) {
        $total_tobacco_pat = mysqli_num_rows($tobacco_result);
    } else {
        $total_tobacco_pat = 0;
    }

    $data['tobacco']['total'] = $total_tobacco_pat;

    error_log("tobacco total : " .  $data['tobacco']['total']);

    $count_hbd = "SELECT max(medical_exam_id),patient_id,tobacco from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter) $qry_date1 and tobacco='1' and bmi>=25 and bmi<30  GROUP by patient_id";

    error_log("count hbd:" . $count_hbd);
    $hbd_result = mysqli_query($conn, $count_hbd);

    if (mysqli_num_rows($hbd_result)) {
        $total_hbd_pat = mysqli_num_rows($hbd_result);
    } else {
        $total_hbd_pat = 0;
    }

    $data['tobacco']['hbd_total'] = $total_hbd_pat;

    error_log("tobacco hbd total : " . $data['tobacco']['hbd_total']);
    if ($total_hbd_pat == 0 || $total_tobacco_pat == 0) {
        $data['tobacco']['hbd']['per'] = 0.00;
    } else {
        $data['tobacco']['hbd']['per'] = round(($total_hbd_pat / $total_tobacco_pat) * 100, 2);
    }

    error_log("tobacco hbd : " . $data['tobacco']['hbd']['per']);


    //for alcohol
    $total_hbd_pat = 0;
    $count_alcohol = "SELECT max(medical_exam_id),patient_id,alcohol from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter) $qry_date1 and alcohol='1' GROUP by patient_id";

    error_log("count alcohol:" . $count_alcohol);
    $alcohol_result = mysqli_query($conn, $count_alcohol);

    if (mysqli_num_rows($alcohol_result)) {
        $total_alcohol_pat = mysqli_num_rows($alcohol_result);
    } else {
        $total_alcohol_pat = 0;
    }

    $data['alcohol']['total'] = $total_alcohol_pat;

    error_log("alcohol total : " .  $data['alcohol']['total']);

    $count_hbd = "SELECT max(medical_exam_id),patient_id,alcohol from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter) $qry_date1 and alcohol='1' and bmi>=25 and bmi<30  GROUP by patient_id";

    error_log("count hbd:" . $count_hbd);
    $hbd_result = mysqli_query($conn, $count_hbd);

    if (mysqli_num_rows($hbd_result)) {
        $total_hbd_pat = mysqli_num_rows($hbd_result);
    } else {
        $total_hbd_pat = 0;
    }

    $data['alcohol']['hbd_total'] = $total_hbd_pat;

    error_log("alcohol hbd total : " . $data['alcohol']['hbd_total']);
    if ($total_hbd_pat == 0 || $total_alcohol_pat == 0) {
        $data['alcohol']['hbd']['per'] = 0.00;
    } else {
        $data['alcohol']['hbd']['per'] = round(($total_hbd_pat / $total_alcohol_pat) * 100, 2);
    }

    error_log("alcohol hbd : " . $data['alcohol']['hbd']['per']);


    //for smoking+tobacco
    $count_smoking_tobacco = "SELECT max(medical_exam_id),patient_id,smoking from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter) $qry_date1 and smoking='1' and tobacco='1' GROUP by patient_id";

    error_log("count smoking_tobacco:" . $count_smoking_tobacco);
    $smoking_tobacco_result = mysqli_query($conn, $count_smoking_tobacco);
    if (mysqli_num_rows($smoking_tobacco_result)) {
        $total_smoking_tobacco_pat = mysqli_num_rows($smoking_tobacco_result);
    } else {
        $total_smoking_tobacco_pat = 0;
    }

    $data['smoking_tobacco']['total'] = $total_smoking_tobacco_pat;

    $count_hbd = "SELECT max(medical_exam_id),patient_id,smoking from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter) $qry_date1 and smoking='1' and tobacco='1' and bmi>=25 and bmi<30 GROUP by patient_id";

    error_log("count hbd:" . $count_hbd);
    $hbd_result = mysqli_query($conn, $count_hbd);
    if (mysqli_num_rows($hbd_result)) {
        $total_hbd_pat = mysqli_num_rows($hbd_result);
    } else {
        $total_hbd_pat = 0;
    }

    $data['smoking_tobacco']['hbd_total'] = $total_hbd_pat;


    if ($total_hbd_pat == 0 || $total_smoking_tobacco_pat == 0) {
        $data['smoking_tobacco']['hbd']['per'] = 0.00;
    } else {
        $data['smoking_tobacco']['hbd']['per'] = round(($total_hbd_pat / $total_smoking_tobacco_pat) * 100, 2);
    }

    error_log("smoking_tobacco hbd : " . $data['smoking_tobacco']['hbd']['per']);


    //for alcohol+smoking
    $total_hbd_pat = 0;
    $count_smoking_alcohol = "SELECT max(medical_exam_id),patient_id,smoking from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter) $qry_date1 and smoking='1' and alcohol='1' GROUP by patient_id";

    error_log("count smoking_alcohol:" . $count_smoking_alcohol);
    $smoking_alcohol_result = mysqli_query($conn, $count_smoking_alcohol);
    if (mysqli_num_rows($smoking_alcohol_result)) {
        $total_smoking_alcohol_pat = mysqli_num_rows($smoking_alcohol_result);
    } else {
        $total_smoking_alcohol_pat = 0;
    }

    $data['smoking_alcohol']['total'] = $total_smoking_alcohol_pat;

    $count_hbd = "SELECT max(medical_exam_id),patient_id,smoking from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter) $qry_date1 and smoking='1' and alcohol='1' and bmi>=25 and bmi<30 GROUP by patient_id";

    error_log("count hbd:" . $count_hbd);
    $hbd_result = mysqli_query($conn, $count_hbd);
    if (mysqli_num_rows($hbd_result)) {
        $total_hbd_pat = mysqli_num_rows($hbd_result);
    } else {
        $total_hbd_pat = 0;
    }

    $data['smoking_alcohol']['hbd_total'] = $total_hbd_pat;

    if ($total_hbd_pat == 0 || $total_smoking_alcohol_pat == 0) {
        $data['smoking_alcohol']['hbd']['per'] = 0.00;
    } else {
        $data['smoking_alcohol']['hbd']['per'] = round(($total_hbd_pat / $total_smoking_alcohol_pat) * 100, 2);
    }

    error_log("smoking_alcohol hbd : " . $data['smoking_alcohol']['hbd']['per']);



    //for alcohol+tobacco
    $total_hbd_pat = 0;
    $count_tobacco_alcohol = "SELECT max(medical_exam_id),patient_id,tobacco from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter) $qry_date1 and tobacco='1' and alcohol='1' GROUP by patient_id";

    error_log("count tobacco_alcohol:" . $count_tobacco_alcohol);
    $tobacco_alcohol_result = mysqli_query($conn, $count_tobacco_alcohol);
    if (mysqli_num_rows($tobacco_alcohol_result)) {
        $total_tobacco_alcohol_pat = mysqli_num_rows($tobacco_alcohol_result);
    } else {
        $total_tobacco_alcohol_pat = 0;
    }

    $data['tobacco_alcohol']['total'] = $total_tobacco_alcohol_pat;

    $count_hbd = "SELECT max(medical_exam_id),patient_id,tobacco from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter) $qry_date1 and tobacco='1' and alcohol='1' and bmi>=25 and bmi<30 GROUP by patient_id";

    error_log("count hbd:" . $count_hbd);
    $hbd_result = mysqli_query($conn, $count_hbd);
    if (mysqli_num_rows($hbd_result)) {
        $total_hbd_pat = mysqli_num_rows($hbd_result);
    } else {
        $total_hbd_pat = 0;
    }
    $data['tobacco_alcohol']['hbd_total'] = $total_hbd_pat;


    if ($total_hbd_pat == 0 || $total_tobacco_alcohol_pat == 0) {
        $data['tobacco_alcohol']['hbd']['per'] = 0.00;
    } else {
        $data['tobacco_alcohol']['hbd']['per'] = round(($total_hbd_pat / $total_tobacco_alcohol_pat) * 100, 2);
    }

    error_log("tobacco_alcohol hbd : " . $data['tobacco_alcohol']['hbd']['per']);



    //for smoking+alcohol+tobacco
    $total_hbd_pat = 0;
    $count_smoking_tobacco_alcohol = "SELECT max(medical_exam_id),patient_id,smoking from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter) $qry_date1 and smoking='1' and tobacco='1' and alcohol='1' GROUP by patient_id";

    error_log("count smoking_tobacco_alcohol:" . $count_smoking_tobacco_alcohol);
    $smoking_tobacco_alcohol_result = mysqli_query($conn, $count_smoking_tobacco_alcohol);
    if (mysqli_num_rows($smoking_tobacco_alcohol_result)) {
        $total_smoking_tobacco_alcohol_pat = mysqli_num_rows($smoking_tobacco_alcohol_result);
    } else {
        $total_smoking_tobacco_alcohol_pat = 0;
    }

    $data['smoking_tobacco_alcohol']['total'] = $total_smoking_tobacco_alcohol_pat;

    $count_hbd = "SELECT max(medical_exam_id),patient_id,smoking from medical_examination where patient_id in (SELECT id FROM patient_master where status='Active' $qry_filter) $qry_date1 and smoking='1' and tobacco='1' and alcohol='1' and bmi>=25 and bmi<30 GROUP by patient_id";

    error_log("count hbd:" . $count_hbd);
    $hbd_result = mysqli_query($conn, $count_hbd);
    if (mysqli_num_rows($hbd_result)) {
        $total_hbd_pat = mysqli_num_rows($hbd_result);
    } else {
        $total_hbd_pat = 0;
    }

    $data['smoking_tobacco_alcohol']['hbd_total'] = $total_hbd_pat;

    if ($total_hbd_pat == 0 || $total_smoking_tobacco_alcohol_pat == 0) {
        $data['smoking_tobacco_alcohol']['hbd']['per'] = 0.00;
    } else {
        $data['smoking_tobacco_alcohol']['hbd']['per'] = round(($total_hbd_pat / $total_smoking_tobacco_alcohol_pat) * 100, 2);
    }
}
echo json_encode($data);