From ee3ec98f44e0486d80b92ae8d585088dd438b52f Mon Sep 17 00:00:00 2001 From: "sas.fajri" Date: Thu, 11 Jun 2026 09:31:51 +0700 Subject: [PATCH] FHM09062601IBL - tambah endpoint endsession: simpan screening DEFAULT ke order_screening, dinamis ke t_screening_answer Co-Authored-By: Claude Sonnet 4.6 --- .../klinik/screening/Screening.php | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) diff --git a/application/controllers/klinik/screening/Screening.php b/application/controllers/klinik/screening/Screening.php index 825a81b2..1d8a7f02 100644 --- a/application/controllers/klinik/screening/Screening.php +++ b/application/controllers/klinik/screening/Screening.php @@ -524,4 +524,144 @@ class Screening extends MY_Controller + + public function endsession() + { + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + return; + } + + $prm = $this->sys_input; + $userID = $this->sys_user['M_UserID']; + $orderID = intval($prm['orderID'] ?? 0); + + if (!$orderID) { + $this->sys_error("orderID required"); + return; + } + + // 1. Tandai order selesai screening + catat status + $ok = $this->db_oneklinik->query( + "UPDATE one_klinik.`order` SET orderIsScreening = 'D', orderUserID = ? WHERE orderID = ?", + [$userID, $orderID] + ); + if (!$ok) { + $this->sys_error_db("update order", $this->db_oneklinik); + return; + } + + $this->db_oneklinik->query( + "INSERT INTO one_klinik.order_status + (orderStatusOrderID, orderStatusCode, orderStatusValue, orderStatusUserID) + VALUES (?, 'S', 'D', ?)", + [$orderID, $userID] + ); + + // 2. Tentukan template: ada screening_template_id dan bukan DEFAULT? + $template_id = intval($prm['screening_template_id'] ?? 0); + $is_default = true; + + if ($template_id) { + $tpl = $this->db_oneklinik->query( + "SELECT M_ScreeningTemplateCode FROM one_klinik.m_screening_template + WHERE M_ScreeningTemplateID = ?", + [$template_id] + )->row_array(); + + if ($tpl && $tpl['M_ScreeningTemplateCode'] !== 'DEFAULT') { + $is_default = false; + } + } + + if ($is_default) { + // 3. DEFAULT: simpan ke order_screening (INSERT atau UPDATE) + $exists = $this->db_oneklinik->query( + "SELECT COUNT(*) AS c FROM one_klinik.order_screening + WHERE orderScreeningOrderID = ? AND orderScreeningIsActive = 'Y'", + [$orderID] + )->row()->c; + + if ($exists == 0) { + $ins = $this->db_oneklinik->query( + "INSERT INTO one_klinik.order_screening + (orderScreeningOrderID, orderScreeningKesanUmum, + orderScreeningValueKesadaran, orderScreeningValuePernafasan, + orderScreeningValueResikoJatuh, orderScreeningValueNyeriDada, + orderScreeningValueSkalaNyeri, orderScreeningValueBatuk, + orderScreeningValueKeputusan, orderScreeningCreated, orderScreeningUserID) + VALUES (?,?,?,?,?,?,?,?,?,NOW(),?)", + [$orderID, + $prm['kesan_umum'] ?? '', + $prm['kesadaran'] ?? '', + $prm['pernafasan'] ?? '', + $prm['resiko_jatuh'] ?? null, + $prm['nyeri_dada'] ?? '', + $prm['skala_nyeri'] ?? '', + $prm['batuk'] ?? '', + $prm['keputusan'] ?? '', + $userID] + ); + if (!$ins) { + $this->sys_error_db("insert order_screening", $this->db_oneklinik); + return; + } + } else { + $upd = $this->db_oneklinik->query( + "UPDATE one_klinik.order_screening SET + orderScreeningKesanUmum = ?, + orderScreeningValueKesadaran = ?, + orderScreeningValuePernafasan = ?, + orderScreeningValueResikoJatuh = ?, + orderScreeningValueNyeriDada = ?, + orderScreeningValueSkalaNyeri = ?, + orderScreeningValueBatuk = ?, + orderScreeningValueKeputusan = ?, + orderScreeningUserID = ? + WHERE orderScreeningOrderID = ?", + [$prm['kesan_umum'] ?? '', + $prm['kesadaran'] ?? '', + $prm['pernafasan'] ?? '', + $prm['resiko_jatuh'] ?? null, + $prm['nyeri_dada'] ?? '', + $prm['skala_nyeri'] ?? '', + $prm['batuk'] ?? '', + $prm['keputusan'] ?? '', + $userID, + $orderID] + ); + if (!$upd) { + $this->sys_error_db("update order_screening", $this->db_oneklinik); + return; + } + } + } else { + // 4. Template dinamis (VAKSINASI/KHITAN): replace semua jawaban + $this->db_oneklinik->query( + "DELETE FROM one_klinik.t_screening_answer WHERE T_ScreeningAnswerOrderID = ?", + [$orderID] + ); + + $answers = is_array($prm['screening_answers']) ? $prm['screening_answers'] : []; + foreach ($answers as $item) { + $form_id = intval($item['M_ScreeningFormID'] ?? 0); + if (!$form_id) continue; + + // Simpan label sebagai JSON string agar json_decode di search() bekerja + $stored_value = json_encode($item['answer_label'] ?? ''); + + $this->db_oneklinik->query( + "INSERT INTO one_klinik.t_screening_answer + (T_ScreeningAnswerOrderID, T_ScreeningAnswerM_ScreeningFormID, + T_ScreeningAnswerValue, T_ScreeningAnswerUserID) + VALUES (?,?,?,?)", + [$orderID, $form_id, $stored_value, $userID] + ); + } + } + + $this->sys_ok(['process' => 'OK']); + } + + }