Files
gravity/hosxp-webservice/api/get_health_checkup_history.php
2026-09-16 23:20:08 +07:00

111 lines
4.2 KiB
PHP

<?php
session_start();
require_once("../core/auth.php");
require_once("../config/db.php");
require_once("../core/utils.php");
header('Content-Type: application/json; charset=utf-8');
if (!isset($_POST['hn'])) {
echo json_encode(["status" => "error", "message" => "Missing parameters"]);
exit;
}
$hn_raw = decrypt_param($_POST['hn']);
$hn = mysqli_real_escape_string($conn1, $hn_raw);
$show_all = isset($_POST['show_all']) && $_POST['show_all'] === 'true';
$results = [];
try {
// 1. Find all visits for this patient that have Health Checkup Diagnosis
$sql_visits = "SELECT o.vstdate, o.vn
FROM ovstdiag od
JOIN ovst o ON o.vn = od.vn
LEFT JOIN icd101 i ON i.code = od.icd10
WHERE o.hn = '$hn'
AND (od.icd10 LIKE 'Z10%' OR od.icd10 LIKE 'Z00%' OR i.name LIKE '%Occupational%' OR i.name LIKE '%checkup%')
" . ($show_all ? "" : "AND o.vstdate >= DATE_SUB(CURDATE(), INTERVAL 2 YEAR)") . "
GROUP BY o.vstdate, o.vn
ORDER BY o.vstdate DESC
" . ($show_all ? "" : "LIMIT 20");
$res_visits = mysqli_query($conn1, $sql_visits);
if (!$res_visits || mysqli_num_rows($res_visits) == 0) {
echo json_encode(["status" => "success", "data" => []]);
exit;
}
$visits = [];
$prev_date = null;
while ($row = mysqli_fetch_assoc($res_visits)) {
$vn = $row['vn'];
$visit_data = [
'vstdate' => $row['vstdate'],
'vstdate_thai' => thai_date2($row['vstdate']),
'vn' => $vn,
'gap_days' => 0,
'labs' => [],
'xrays' => []
];
// 2. Fetch Labs for this visit
$sql_lab = "SELECT i.lab_items_name, l.lab_order_result, i.lab_items_normal_value
FROM lab_order l
JOIN lab_head h ON h.lab_order_number = l.lab_order_number
JOIN lab_items i ON i.lab_items_code = l.lab_items_code
WHERE h.vn = '$vn' AND (l.lab_order_result IS NOT NULL AND l.lab_order_result != '')
ORDER BY i.lab_items_name";
$res_lab = mysqli_query($conn1, $sql_lab);
if ($res_lab) {
while ($lrow = mysqli_fetch_assoc($res_lab)) {
$lrow['is_abnormal'] = isAbnormalLab($lrow['lab_order_result'], $lrow['lab_items_normal_value']);
$visit_data['labs'][] = $lrow;
}
}
// 3. Fetch X-Rays for this visit
$sql_xray = "SELECT xh.xray_list as item_name, xr.report_text as result
FROM xray_report xr
JOIN xray_head xh ON xh.vn = xr.vn
WHERE xh.vn = '$vn'";
$res_xray = mysqli_query($conn1, $sql_xray);
if ($res_xray) {
while ($xrow = mysqli_fetch_assoc($res_xray)) {
$visit_data['xrays'][] = $xrow;
}
}
// 4. Only include this visit if it has at least some results to compare
if (!empty($visit_data['labs']) || !empty($visit_data['xrays'])) {
$visits[] = $visit_data;
}
}
// 5. Calculate gaps (We fetched DESC, so index 0 is newest, index 1 is older)
for ($i = 0; $i < count($visits) - 1; $i++) {
$newer_date = new DateTime($visits[$i]['vstdate']);
$older_date = new DateTime($visits[$i + 1]['vstdate']);
$interval = $older_date->diff($newer_date);
$visits[$i]['gap_days'] = $interval->days;
if ($interval->y > 0) {
$text = "ห่างจากครั้งก่อน " . $interval->y . " ปี";
if ($interval->m > 0) $text .= " " . $interval->m . " เดือน";
if ($interval->d > 0) $text .= " " . $interval->d . " วัน";
$visits[$i]['gap_text'] = $text;
} else {
$visits[$i]['gap_text'] = "ห่างจากครั้งก่อน " . $interval->days . " วัน";
}
}
echo json_encode(["status" => "success", "data" => $visits]);
} catch (Exception $e) {
echo json_encode(["status" => "error", "message" => $e->getMessage()]);
}
?>