prepare($sql_pt); if($stmt) { $stmt->bind_param("s", $hn); $stmt->execute(); $res = $stmt->get_result(); if($row = $res->fetch_assoc()) { $patient_name = $row['pname'] . $row['fname'] . " " . $row['lname']; } $stmt->close(); } // Auto-select latest visit if $vdate is empty so user doesn't see a blank page if (empty($vdate)) { $sql_latest = "SELECT vstdate, vn, an FROM ovst WHERE hn=? ORDER BY vstdate DESC LIMIT 1"; $stmt_latest = $conn1->prepare($sql_latest); if($stmt_latest) { $stmt_latest->bind_param("s", $hn); $stmt_latest->execute(); $res_latest = $stmt_latest->get_result(); if($row_latest = $res_latest->fetch_assoc()) { $vdate = $row_latest['vstdate']; $nn = ($row_latest['an'] != NULL) ? $row_latest['an'] : $row_latest['vn']; } $stmt_latest->close(); } } // Log that the patient was viewed if (!empty($_SESSION['account'])) { system_log($conn2, $_SESSION['account'], 'VIEW_PATIENT', ['hn' => $hn, 'vdate' => $vdate, 'nn' => $nn]); } // Check if current visit is IPD if (!empty($vdate) && !empty($nn)) { $sql_check_ipd = "SELECT an FROM ovst WHERE hn=? AND vstdate=? AND (vn=? OR an=?)"; $stmt_check = $conn1->prepare($sql_check_ipd); if($stmt_check) { $stmt_check->bind_param("ssss", $hn, $vdate, $nn, $nn); $stmt_check->execute(); $res_check = $stmt_check->get_result(); if($row_check = $res_check->fetch_assoc()) { if (!empty($row_check['an'])) { $is_ipd_visit = true; } } $stmt_check->close(); } } // Fetch Upcoming Appointments $upcoming_appointments = []; try { $sql_app = "SELECT a.nextdate, a.nexttime, c.name as clinic_name FROM oapp a LEFT OUTER JOIN clinic c ON a.clinic = c.clinic LEFT OUTER JOIN patient p ON a.hn = p.hn WHERE DATEDIFF(a.nextdate, CURDATE()) > 1 AND a.hn = ? ORDER BY a.nextdate ASC, a.nexttime ASC"; $stmt_app = $conn1->prepare($sql_app); if($stmt_app) { $stmt_app->bind_param("s", $hn); $stmt_app->execute(); $res_app = $stmt_app->get_result(); while($row_app = $res_app->fetch_assoc()) { $upcoming_appointments[] = $row_app; } $stmt_app->close(); } } catch (Exception $e) { // Log the error but don't break the page error_log("Failed to fetch appointments: " . $e->getMessage()); } // Fetch Vaccines $vaccines_by_group = []; try { $app_settings = get_app_settings(); $vaccine_sql_template = $app_settings['vaccine_sql'] ?? ''; if (!empty($vaccine_sql_template)) { // Protect against basic SQL injection while supporting the arbitrary query design $safe_hn = $conn1->real_escape_string($hn); $vaccine_sql = str_replace("'ระบุ_HN'", "'" . $safe_hn . "'", $vaccine_sql_template); $res_vax = $conn1->query($vaccine_sql); if ($res_vax) { while($row_vax = $res_vax->fetch_assoc()) { $group = $row_vax['vaccine_group'] ?? 'อื่นๆ (Others)'; if (!isset($vaccines_by_group[$group])) { $vaccines_by_group[$group] = []; } $vaccines_by_group[$group][] = $row_vax; } } } } catch (Exception $e) { error_log("Failed to fetch vaccines: " . $e->getMessage()); } } $bg_class = $is_ipd_visit ? "bg-gradient-to-br from-yellow-50 to-amber-100" : "bg-gradient-to-br from-emerald-50 to-emerald-100"; ob_start(); ?>

ประวัติรับบริการ

HN:

    = DATE_SUB(CURDATE(), INTERVAL 1 YEAR)"; $limit_clause = ""; } else if ($filter_type === '3_years') { $where_clause .= " AND vstdate >= DATE_SUB(CURDATE(), INTERVAL 3 YEAR)"; $limit_clause = ""; } else if ($filter_type === '5_years') { $where_clause .= " AND vstdate >= DATE_SUB(CURDATE(), INTERVAL 5 YEAR)"; $limit_clause = ""; } else if ($filter_type === 'all_time') { $limit_clause = ""; } else if ($filter_type === 'custom' && !empty($start_date) && !empty($end_date)) { $where_clause .= " AND vstdate BETWEEN ? AND ?"; $params[] = $start_date; $params[] = $end_date; $types .= "ss"; $limit_clause = ""; } $sql3 = "SELECT * FROM ovst WHERE $where_clause ORDER BY vstdate DESC $limit_clause"; $stmt3 = $conn1->prepare($sql3); if($stmt3) { $stmt3->bind_param($types, ...$params); $stmt3->execute(); $res3 = $stmt3->get_result(); if ($res3->num_rows === 0) { echo "
    ไม่พบประวัติในช่วงเวลาที่เลือก
    "; } $all_visits = []; $selected_visit = null; while ($row3 = $res3->fetch_assoc()) { $current_nn_val = ($row3['an'] != NULL) ? $row3['an'] : $row3['vn']; if ($vdate == $row3['vstdate'] && $nn == $current_nn_val) { $selected_visit = $row3; } else { $all_visits[] = $row3; } } // Check if a visit was selected but not found in the list (e.g. it's older than the limit/filter) if ($vdate && $nn && !$selected_visit) { $sql_missing = "SELECT * FROM ovst WHERE hn=? AND vstdate=? AND (vn=? OR an=?)"; $stmt_m = $conn1->prepare($sql_missing); if($stmt_m) { $stmt_m->bind_param("ssss", $hn, $vdate, $nn, $nn); $stmt_m->execute(); $res_m = $stmt_m->get_result(); if ($row_m = $res_m->fetch_assoc()) { $selected_visit = $row_m; } $stmt_m->close(); } } // Render Selected Visit First (Pinned at Top) if ($selected_visit) { $nn_val = ($selected_visit['an'] != NULL) ? $selected_visit['an'] : $selected_visit['vn']; $enc_hn = htmlspecialchars(encrypt_param($hn)); $enc_vdate = htmlspecialchars(encrypt_param($selected_visit['vstdate'])); $enc_nn = htmlspecialchars(encrypt_param($nn_val)); echo "
    "; echo "
    "; echo ""; echo ""; echo ""; echo "
    "; } // Render the rest of the visits foreach ($all_visits as $row3) { $nn_val = ($row3['an'] != NULL) ? $row3['an'] : $row3['vn']; $enc_hn = htmlspecialchars(encrypt_param($hn)); $enc_vdate = htmlspecialchars(encrypt_param($row3['vstdate'])); $enc_nn = htmlspecialchars(encrypt_param($nn_val)); $filter_inputs = "" . "" . "" . ""; echo "
    "; echo ""; echo ""; echo ""; echo $filter_inputs; echo "
    "; } $stmt3->close(); } } ?>

ค้นหารายการ

พิมพ์คำค้นหาเพื่อเริ่มค้นหา

วันที่รับบริการ: VN/AN:
ค้นหาคนอื่น

ยังไม่ได้เลือกรายการรับบริการ

กรุณาคลิกเลือกวันที่มารับบริการจากเมนูด้านซ้ายเพื่อดูรายละเอียดการตรวจรักษาและผลการวินิจฉัยโรค

ค้นหาผู้รับบริการคนอื่น