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 'N' AS divider, p.M_PatientName, p.M_PatientName_enc, p.M_PatientHP, p.M_PatientHP_enc, p.M_PatientDOB, p.M_PatientDOB_enc, p.M_PatientEmail, p.M_PatientEmail_enc, p.M_PatientPhone, p.M_PatientPhone_enc, p.M_PatientPOB, p.M_PatientPOB_enc, p.M_PatientIDNumber, p.M_PatientIDNumber_enc, p.M_PatientNIK, p.M_PatientNIK_enc, p.M_PatientPhoto, p.M_PatientPhotoThumb, p.M_PatientNoReg, p.M_PatientJob, p.M_PatientM_SexID, p.M_PatientM_TitleID, p.M_PatientM_IdTypeID, o.*, DATE_FORMAT(o.orderDate, '%d-%m-%Y') AS date_order, '' AS kode_status, 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_PatientHP'] = $enc->decrypt($v['M_PatientHP_enc'] ?? '') ?: $v['M_PatientHP']; $rows[$k]['M_PatientDOB'] = $enc->decrypt($v['M_PatientDOB_enc'] ?? '') ?: $v['M_PatientDOB']; $rows[$k]['M_PatientEmail'] = $enc->decrypt($v['M_PatientEmail_enc'] ?? '') ?: $v['M_PatientEmail']; $rows[$k]['M_PatientPhone'] = $enc->decrypt($v['M_PatientPhone_enc'] ?? '') ?: $v['M_PatientPhone']; $rows[$k]['M_PatientPOB'] = $enc->decrypt($v['M_PatientPOB_enc'] ?? '') ?: $v['M_PatientPOB']; $rows[$k]['M_PatientIDNumber'] = $enc->decrypt($v['M_PatientIDNumber_enc'] ?? '') ?: $v['M_PatientIDNumber']; $rows[$k]['M_PatientNIK'] = $enc->decrypt($v['M_PatientNIK_enc'] ?? '') ?: $v['M_PatientNIK']; $rows[$k]['patient_name'] = trim(($v['M_TitleName'] ?? '') . ' ' . $rows[$k]['M_PatientName']); 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 (?, ?, ?, 'FORM', '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); } }