From 1e87def6a754857090be47dc4386c8eb9d6bf703 Mon Sep 17 00:00:00 2001 From: "sas.fajri" Date: Thu, 11 Jun 2026 10:24:06 +0700 Subject: [PATCH] FHM09062601IBL - buat controller Ttv (search, getttv, savettv, getsexreg) + kolom orderIsTTV Co-Authored-By: Claude Sonnet 4.6 --- application/controllers/klinik/Ttv.php | 315 ++++++++++++++++++ .../2026-06-11-alter-order-add-orderIsTTV.sql | 5 + 2 files changed, 320 insertions(+) create mode 100644 application/controllers/klinik/Ttv.php create mode 100644 sql/manual_changes/2026-06-11-alter-order-add-orderIsTTV.sql diff --git a/application/controllers/klinik/Ttv.php b/application/controllers/klinik/Ttv.php new file mode 100644 index 00000000..c0430979 --- /dev/null +++ b/application/controllers/klinik/Ttv.php @@ -0,0 +1,315 @@ +db_onedev = $this->load->database("onedev", true); + $this->db_oneklinik = $this->load->database("onedev", true); + $this->load->library('ibl_encryptor'); + } + + // ----------------------------------------------------------------------- + // POST /klinik/ttv/search + // Listing order yang sudah selesai screening (orderIsScreening='D') + // ----------------------------------------------------------------------- + public function search() + { + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + return; + } + + $prm = $this->sys_input; + $limit = 20; + $offset = (max(1, intval($prm['current_page'] ?? 1)) - 1) * $limit; + + $where = ["o.orderIsScreening = 'D'", "o.orderIsActive = 'Y'"]; + $binds = []; + + // Filter tanggal + $start_date = $prm['start_date'] ?? date('Y-m-d'); + $where[] = "DATE(o.orderDate) = ?"; + $binds[] = $start_date; + + // Filter status TTV + $status = $prm['status'] ?? ''; + if ($status !== '') { + $where[] = "o.orderIsTTV = ?"; + $binds[] = $status; + } + + // Filter noreg + $noreg = trim($prm['noreg'] ?? ''); + if ($noreg !== '') { + $where[] = "p.M_PatientNoReg LIKE ?"; + $binds[] = '%' . $noreg . '%'; + } + + // Filter nama / HP via trigram index (PDP-safe) + $search = trim($prm['search'] ?? ''); + if ($search !== '') { + $where[] = "(p.M_PatientName_bidx LIKE ? OR p.M_PatientHP_bidx LIKE ?)"; + $binds[] = '%' . $search . '%'; + $binds[] = '%' . $search . '%'; + } + + $where_sql = implode(' AND ', $where); + + $sql = "SELECT + o.orderID, + o.orderIsTTV, + o.orderDate, + DATE_FORMAT(o.orderDate, '%d-%m-%Y') AS order_date, + o.orderAge AS patient_age, + o.orderM_ClinicUnitID, + p.M_PatientID, + p.M_PatientNoReg, + p.M_PatientPhoto, + p.M_PatientPhotoThumb, + p.M_PatientJob, + p.M_PatientM_SexID, + p.M_PatientM_TitleID, + p.M_PatientM_IdTypeID, + p.M_PatientName, p.M_PatientName_enc, + p.M_PatientDOB, p.M_PatientDOB_enc, + p.M_PatientHP, p.M_PatientHP_enc, + p.M_PatientIDNumber, p.M_PatientIDNumber_enc, + s.M_SexName, + t.M_TitleName + FROM one_klinik.`order` o + JOIN m_patient p ON p.M_PatientID = o.orderM_PatientID AND p.M_PatientIsActive = 'Y' + JOIN m_sex s ON s.M_SexID = p.M_PatientM_SexID + JOIN m_title t ON t.M_TitleID = p.M_PatientM_TitleID + WHERE $where_sql + ORDER BY o.orderDate ASC + LIMIT $limit OFFSET $offset"; + + $query = $this->db_oneklinik->query($sql, $binds); + if (!$query) { + $this->sys_error_db("ttv search", $this->db_oneklinik); + return; + } + + $rows = $query->result_array(); + $enc = $this->ibl_encryptor; + + foreach ($rows as $k => $v) { + $rows[$k]['M_PatientName'] = $enc->decrypt($v['M_PatientName_enc'] ?? '') ?: $v['M_PatientName']; + $rows[$k]['M_PatientDOB'] = $enc->decrypt($v['M_PatientDOB_enc'] ?? '') ?: $v['M_PatientDOB']; + $rows[$k]['M_PatientHP'] = $enc->decrypt($v['M_PatientHP_enc'] ?? '') ?: $v['M_PatientHP']; + $rows[$k]['M_PatientIDNumber'] = $enc->decrypt($v['M_PatientIDNumber_enc'] ?? '') ?: $v['M_PatientIDNumber']; + + foreach (array_keys($rows[$k]) as $col) { + if (substr($col, -4) === '_enc') unset($rows[$k][$col]); + } + } + + $this->sys_ok(['total' => count($rows), 'records' => $rows]); + } + + // ----------------------------------------------------------------------- + // POST /klinik/ttv/getttv + // Load data TTV yang sudah pernah disimpan untuk satu order + // ----------------------------------------------------------------------- + public function getttv() + { + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + return; + } + + $prm = $this->sys_input; + $orderid = intval($prm['orderid'] ?? 0); + + if (!$orderid) { + $this->sys_error("orderid required"); + return; + } + + $row = $this->db_oneklinik->query( + "SELECT orderDoctorVitalSign, orderDoctorSaran AS xnote + FROM one_klinik.order_doctor + WHERE orderDoctorOrderID = ? + ORDER BY orderDoctorID DESC LIMIT 1", + [$orderid] + )->row_array(); + + $fisiks = null; + $xnote = ''; + + if ($row) { + $fisiks = $row['orderDoctorVitalSign'] + ? json_decode($row['orderDoctorVitalSign'], true) + : null; + $xnote = $row['xnote'] ?? ''; + } + + $this->sys_ok(['fisiks' => $fisiks, 'xnote' => $xnote]); + } + + // ----------------------------------------------------------------------- + // POST /klinik/ttv/savettv + // Simpan TTV ke order_doctor + order_tanda_vital, set orderIsTTV='D' + // ----------------------------------------------------------------------- + public function savettv() + { + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + return; + } + + $prm = $this->sys_input; + $userID = $this->sys_user['M_UserID']; + $orderid = intval($prm['orderid'] ?? 0); + $fisiks = $prm['fisiks'] ?? []; + $xnote = $prm['xnote'] ?? ''; + + if (!$orderid) { + $this->sys_error("orderid required"); + return; + } + + $fisiks_json = json_encode($fisiks); + + // 1. Upsert order_doctor + $exists = $this->db_oneklinik->query( + "SELECT orderDoctorID FROM one_klinik.order_doctor WHERE orderDoctorOrderID = ? LIMIT 1", + [$orderid] + )->row_array(); + + if ($exists) { + $ok = $this->db_oneklinik->query( + "UPDATE one_klinik.order_doctor + SET orderDoctorVitalSign = ?, + orderDoctorSaran = ?, + orderDoctorLastUpdated = NOW() + WHERE orderDoctorOrderID = ?", + [$fisiks_json, $xnote, $orderid] + ); + } else { + $ok = $this->db_oneklinik->query( + "INSERT INTO one_klinik.order_doctor + (orderDoctorOrderID, orderDoctorVitalSign, orderDoctorSaran, + orderDoctorType, orderDoctorIsActive, orderDoctorUserID, orderDoctorCreated) + VALUES (?, ?, ?, 'TEXT', 'Y', ?, NOW())", + [$orderid, $fisiks_json, $xnote, $userID] + ); + } + + if (!$ok) { + $this->sys_error_db("upsert order_doctor", $this->db_oneklinik); + return; + } + + // 2. Parse fisiks → nilai terstruktur untuk order_tanda_vital + $ttv = [ + 'pulse' => 0, + 'sistole' => 0, + 'diastole' => 0, + 'temperature' => 0, + 'weight' => 0, + 'height' => 0, + 'saturation' => 0, + ]; + + foreach ((array)$fisiks as $item) { + $code = $item['id_code'] ?? ''; + $value = trim($item['value'] ?? ''); + switch ($code) { + case 'tanda_vital_1': $ttv['pulse'] = intval($value); break; + case 'tanda_vital_5': + $parts = explode('/', $value); + $ttv['sistole'] = intval($parts[0] ?? 0); + $ttv['diastole'] = intval($parts[1] ?? 0); + break; + case 'tanda_vital_6': $ttv['temperature'] = intval($value); break; + case 'tanda_vital_7': $ttv['saturation'] = intval($value); break; + case 'status_gizi_1': $ttv['weight'] = intval($value); break; + case 'status_gizi_2': $ttv['height'] = intval($value); break; + } + } + + // 3. Upsert order_tanda_vital + $tv_exists = $this->db_oneklinik->query( + "SELECT orderTandaVitalID FROM one_klinik.order_tanda_vital WHERE orderTandaVitalOrderID = ? LIMIT 1", + [$orderid] + )->row_array(); + + if ($tv_exists) { + $this->db_oneklinik->query( + "UPDATE one_klinik.order_tanda_vital SET + orderTandaVitalPulse = ?, + orderTandaVitalSistole = ?, + orderTandaVitalDiastole = ?, + orderTandaVitalTemperature = ?, + orderTandaVitalWeight = ?, + orderTandaVitalHeight = ?, + orderTandaVitalSaturation = ?, + orderTandaVitalUserID = ?, + orderTandaVitalLastUpdated = NOW() + WHERE orderTandaVitalOrderID = ?", + [$ttv['pulse'], $ttv['sistole'], $ttv['diastole'], + $ttv['temperature'], $ttv['weight'], $ttv['height'], + $ttv['saturation'], $userID, $orderid] + ); + } else { + $this->db_oneklinik->query( + "INSERT INTO one_klinik.order_tanda_vital + (orderTandaVitalOrderID, orderTandaVitalPulse, orderTandaVitalSistole, + orderTandaVitalDiastole, orderTandaVitalTemperature, orderTandaVitalWeight, + orderTandaVitalHeight, orderTandaVitalSaturation, + orderTandaVitalIsActive, orderTandaVitalUserID, orderTandaVitalCreated) + VALUES (?,?,?,?,?,?,?,?,'Y',?,NOW())", + [$orderid, $ttv['pulse'], $ttv['sistole'], $ttv['diastole'], + $ttv['temperature'], $ttv['weight'], $ttv['height'], + $ttv['saturation'], $userID] + ); + } + + // 4. Update status order + $this->db_oneklinik->query( + "UPDATE one_klinik.`order` SET orderIsTTV = 'D', orderUserID = ? WHERE orderID = ?", + [$userID, $orderid] + ); + + $this->sys_ok(['process' => 'OK']); + } + + // ----------------------------------------------------------------------- + // POST /klinik/ttv/getsexreg + // Return kartuidentitass, sexes, titles, religions + // ----------------------------------------------------------------------- + public function getsexreg() + { + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + return; + } + + $rows = []; + + $rows['kartuidentitass'] = $this->db_onedev->query( + "SELECT * FROM m_idtype WHERE M_IdTypeIsActive = 'Y'" + )->result_array(); + + $rows['sexes'] = $this->db_onedev->query( + "SELECT * FROM m_sex WHERE M_SexIsActive = 'Y'" + )->result_array(); + + $rows['titles'] = $this->db_onedev->query( + "SELECT * FROM m_title WHERE M_TitleIsActive = 'Y'" + )->result_array(); + + $rows['religions'] = $this->db_onedev->query( + "SELECT * FROM m_religion WHERE M_ReligionIsActive = 'Y'" + )->result_array(); + + $this->sys_ok($rows); + } +} diff --git a/sql/manual_changes/2026-06-11-alter-order-add-orderIsTTV.sql b/sql/manual_changes/2026-06-11-alter-order-add-orderIsTTV.sql new file mode 100644 index 00000000..4efc8b07 --- /dev/null +++ b/sql/manual_changes/2026-06-11-alter-order-add-orderIsTTV.sql @@ -0,0 +1,5 @@ +-- Tambah kolom status TTV ke tabel order +-- N = belum diisi, D = sudah selesai diisi +ALTER TABLE one_klinik.`order` + ADD COLUMN `orderIsTTV` CHAR(1) NOT NULL DEFAULT 'N' + AFTER `orderIsScreening`;