219 lines
7.6 KiB
PHP
219 lines
7.6 KiB
PHP
<?php
|
|
require_once(__DIR__."/../config/db.php");
|
|
|
|
function _init_settings_db($conn) {
|
|
$create_sql = "CREATE TABLE IF NOT EXISTS sys_settings (
|
|
setting_key VARCHAR(100) PRIMARY KEY,
|
|
setting_value TEXT
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;";
|
|
try {
|
|
$conn->query($create_sql);
|
|
|
|
$create_changelog = "CREATE TABLE IF NOT EXISTS sys_changelog (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
release_date DATE NOT NULL,
|
|
version VARCHAR(50) NOT NULL,
|
|
description TEXT NOT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;";
|
|
$conn->query($create_changelog);
|
|
} catch (Exception $e) {
|
|
// Ignore table creation errors if it already exists
|
|
}
|
|
}
|
|
|
|
function get_changelogs() {
|
|
global $conn2;
|
|
if (!isset($conn2)) {
|
|
|
|
}
|
|
_init_settings_db($conn2);
|
|
|
|
$logs = [];
|
|
try {
|
|
$result = $conn2->query("SELECT * FROM sys_changelog ORDER BY release_date DESC, id DESC");
|
|
if ($result) {
|
|
while ($row = $result->fetch_assoc()) {
|
|
$logs[] = $row;
|
|
}
|
|
}
|
|
} catch (Exception $e) {}
|
|
return $logs;
|
|
}
|
|
|
|
function add_changelog($release_date, $version, $description) {
|
|
global $conn2;
|
|
if (!isset($conn2)) {
|
|
|
|
}
|
|
_init_settings_db($conn2);
|
|
|
|
try {
|
|
$stmt = $conn2->prepare("INSERT INTO sys_changelog (release_date, version, description) VALUES (?, ?, ?)");
|
|
if ($stmt) {
|
|
$stmt->bind_param("sss", $release_date, $version, $description);
|
|
$stmt->execute();
|
|
$stmt->close();
|
|
return true;
|
|
}
|
|
} catch (Exception $e) {}
|
|
return false;
|
|
}
|
|
|
|
function delete_changelog($id) {
|
|
global $conn2;
|
|
if (!isset($conn2)) {
|
|
|
|
}
|
|
try {
|
|
$stmt = $conn2->prepare("DELETE FROM sys_changelog WHERE id = ?");
|
|
if ($stmt) {
|
|
$stmt->bind_param("i", $id);
|
|
$stmt->execute();
|
|
$stmt->close();
|
|
return true;
|
|
}
|
|
} catch (Exception $e) {}
|
|
return false;
|
|
}
|
|
|
|
function get_app_settings() {
|
|
global $conn2;
|
|
if (!isset($conn2)) {
|
|
|
|
}
|
|
|
|
_init_settings_db($conn2);
|
|
|
|
$defaults = [
|
|
"login_header_main" => "ยินดีต้อนรับ",
|
|
"login_header_sub" => "HOSxP Web Service",
|
|
"sidebar_header_main" => "HOSxP Web",
|
|
"sidebar_header_sub" => "SMART MEDICAL SERVICE",
|
|
"footer_text" => "© {YEAR} โรงพยาบาลเกาะสมุย",
|
|
"line_notify_enable" => "0",
|
|
"line_notify_token" => "",
|
|
"telegram_notify_enable" => "0",
|
|
"telegram_bot_token" => "",
|
|
"telegram_chat_id" => "",
|
|
"strict_search_enable" => "1",
|
|
"auto_logout_enable" => "1",
|
|
"auto_logout_minutes" => "30",
|
|
"vaccine_sql" => "SELECT
|
|
vax.source_type,
|
|
vax.hn,
|
|
vax.vaccine_date,
|
|
vax.vaccine_time,
|
|
vax.vaccine_code,
|
|
vax.vaccine_name,
|
|
vax.vaccine_group,
|
|
vax.vaccine_lot_no,
|
|
vax.vaccine_place_name
|
|
FROM (
|
|
-- 1. ประวัติจากงานผู้ป่วยนอก (OPD / ovst_vaccine)
|
|
SELECT
|
|
'OPD Service' AS source_type,
|
|
o.hn,
|
|
o.vstdate AS vaccine_date,
|
|
o.vsttime AS vaccine_time,
|
|
pv.vaccine_code,
|
|
pv.vaccine_name,
|
|
pv.vaccine_group,
|
|
ov.vaccine_lot_no,
|
|
'โรงพยาบาล (OPD)' AS vaccine_place_name
|
|
FROM ovst_vaccine ov
|
|
INNER JOIN ovst o ON ov.vn = o.vn
|
|
INNER JOIN person_vaccine pv ON ov.person_vaccine_id = pv.person_vaccine_id
|
|
WHERE o.hn = 'ระบุ_HN'
|
|
|
|
UNION ALL
|
|
|
|
-- 2. ประวัติจากงานส่งเสริมสุขภาพนักเรียน (village_student_vaccine + list)
|
|
SELECT
|
|
'School Health (Student)' AS source_type,
|
|
p.patient_hn AS hn,
|
|
vsv.vaccine_date,
|
|
vsv.vaccine_time,
|
|
sv.vaccine_code,
|
|
sv.student_vaccine_name AS vaccine_name,
|
|
'School/Student' AS vaccine_group,
|
|
vsvl.vaccine_lotno AS vaccine_lot_no,
|
|
svp.student_vaccine_place_name AS vaccine_place_name
|
|
FROM village_student_vaccine vsv
|
|
INNER JOIN village_student vs ON vsv.village_student_id = vs.village_student_id
|
|
INNER JOIN person p ON vs.person_id = p.person_id
|
|
INNER JOIN village_student_vaccine_list vsvl ON vsv.village_student_vaccine_id = vsvl.village_student_vaccine_id
|
|
INNER JOIN student_vaccine sv ON vsvl.student_vaccine_id = sv.student_vaccine_id
|
|
LEFT JOIN student_vaccine_place svp ON vsv.student_vaccine_place_id = svp.student_vaccine_place_id
|
|
WHERE p.patient_hn = 'ระบุ_HN'
|
|
) vax
|
|
ORDER BY vax.vaccine_date DESC, vax.vaccine_time DESC;",
|
|
"ncd_sql" => "SELECT pp.person_id, v.hn ,p.cid, p.mobile_phone_number,
|
|
concat(p.pname,p.fname,' ',p.lname) as pt_name, pp.house_regist_type_id, p.birthday,
|
|
s.name as sex_name, p.moopart, t3.name as tmb_name, cl.regdate, cl.clinicmember_id,
|
|
TIMESTAMPDIFF(YEAR, birthday, CURDATE()) AS age_y , pe.name as pttype_name,
|
|
h.name as hosp_name, p.addrpart, ci.icd10
|
|
FROM opdscreen os
|
|
LEFT OUTER JOIN vn_stat v on v.vn=os.vn
|
|
LEFT OUTER JOIN ovst o on v.vn=o.vn
|
|
LEFT OUTER JOIN patient p on p.hn=os.hn
|
|
LEFT OUTER JOIN person pp on p.hn=pp.patient_hn
|
|
LEFT OUTER JOIN sex s on p.sex=s.code
|
|
LEFT OUTER JOIN thaiaddress t1 on t1.chwpart=p.chwpart and t1.amppart='00' and t1.tmbpart='00'
|
|
LEFT OUTER JOIN thaiaddress t2 on t2.chwpart=p.chwpart and t2.amppart=p.amppart and t2.tmbpart='00'
|
|
LEFT OUTER JOIN thaiaddress t3 on t3.chwpart=p.chwpart and t3.amppart=p.amppart and t3.tmbpart=p.tmbpart
|
|
LEFT OUTER JOIN clinicmember cl on p.hn=cl.hn
|
|
LEFT OUTER JOIN pttype pe on v.pttype=pe.pttype
|
|
LEFT OUTER JOIN hospcode h on o.hospmain=h.hospcode
|
|
LEFT OUTER JOIN clinic_persist_icd ci on cl.hn=ci.hn
|
|
WHERE cl.hn in(select hn from ovst where vstdate between ? and ? )
|
|
AND cl.clinic_member_status_id='1' {clinic_cond}
|
|
GROUP BY v.hn ORDER BY v.hn ASC",
|
|
"ncd_clinic_dm_code" => "001",
|
|
"ncd_clinic_ht_code" => "002",
|
|
"ncd_clinic_dlp_code" => "039",
|
|
"ncd_clinic_ckd_code" => "051"
|
|
];
|
|
|
|
$db_settings = [];
|
|
try {
|
|
$result = $conn2->query("SELECT setting_key, setting_value FROM sys_settings");
|
|
if ($result) {
|
|
while ($row = $result->fetch_assoc()) {
|
|
$db_settings[$row['setting_key']] = $row['setting_value'];
|
|
}
|
|
}
|
|
} catch (Exception $e) {
|
|
// Ignore fetch errors
|
|
}
|
|
|
|
return array_merge($defaults, $db_settings);
|
|
}
|
|
|
|
function save_app_settings($settings) {
|
|
global $conn2;
|
|
if (!isset($conn2)) {
|
|
|
|
}
|
|
|
|
_init_settings_db($conn2);
|
|
|
|
try {
|
|
$stmt = $conn2->prepare("INSERT INTO sys_settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)");
|
|
if ($stmt) {
|
|
foreach ($settings as $key => $value) {
|
|
// Ensure value is a string
|
|
$val_str = (string)$value;
|
|
$stmt->bind_param("ss", $key, $val_str);
|
|
$stmt->execute();
|
|
}
|
|
$stmt->close();
|
|
return true;
|
|
}
|
|
} catch (Exception $e) {
|
|
error_log("Failed to save app settings: " . $e->getMessage());
|
|
}
|
|
return false;
|
|
}
|
|
?>
|