db_onedev = $this->load->database("onedev", true); $this->db_oneklinik = $this->load->database("onedev", true); $this->load->library('ibl_encryptor'); } public function search() { $prm = $this->sys_input; $id = $this->db_onedev->escape_str($prm['id']); $sql = "SELECT orderID, orderDate, orderNumber, orderIsScreening, orderIsAnamnese, orderIsCheck, orderAge as patient_age, DATE_FORMAT(orderDate,'%d-%m-%Y') as order_date, orderM_ClinicUnitID, 'N' divider, M_PatientID, M_PatientNoReg, M_PatientPrefix, M_PatientSuffix, concat(M_TitleName,' ',IFNULL(M_PatientPrefix,''),' ',M_PatientName,' ',IFNULL(M_PatientSuffix,'')) M_PatientNameRaw, M_PatientNote, M_PatientNIK, M_PatientJabatan, M_PatientKedudukan, M_PatientPJ, M_PatientLocation, M_PatientJob, M_PatientM_SexID, M_SexName, M_TitleID, M_TitleName, M_PatientM_TitleID, M_PatientM_ReligionID, IFNULL(M_ReligionName,'-') M_ReligionName, M_PatientM_IdTypeID, IF(ISNULL(M_PatientSuspendID),'active','suspend') as status, M_PatientAddressRegionalCd, M_PatientName_enc, M_PatientHP_enc, M_PatientDOB_enc, M_PatientEmail_enc, M_PatientPhone_enc, M_PatientPOB_enc, M_PatientIDNumber_enc, M_PatientNIK_enc, M_PatientAddressDescription_enc FROM one_klinik.`order` JOIN m_patient ON M_PatientID = orderM_PatientID AND M_PatientIsActive = 'Y' JOIN m_title ON M_PatientM_TitleID = M_TitleID JOIN m_sex ON M_PatientM_SexID = M_SexID JOIN m_patientaddress ON M_PatientAddressM_PatientID = M_PatientID AND M_PatientAddressIsActive = 'Y' LEFT JOIN m_religion ON M_PatientM_ReligionID = M_ReligionID LEFT JOIN m_patientsuspend ON M_PatientSuspendM_PatientID = M_PatientID AND M_PatientSuspendIsActive = 'Y' WHERE orderNumber = '{$id}' AND M_PatientSuspendID IS NULL GROUP BY M_PatientID"; $query = $this->db_onedev->query($sql); if (!$query) { $this->sys_error_db("order rows", $this->db_onedev); 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_PatientNameRaw']; $rows[$k]['M_PatientHP'] = $enc->decrypt($v['M_PatientHP_enc']) ?? ''; $rows[$k]['M_PatientEmail'] = $enc->decrypt($v['M_PatientEmail_enc']) ?? ''; $rows[$k]['M_PatientPOB'] = $enc->decrypt($v['M_PatientPOB_enc']) ?? ''; $rows[$k]['M_PatientPhone'] = $enc->decrypt($v['M_PatientPhone_enc']) ?? ''; $rows[$k]['M_PatientIDNumber'] = $enc->decrypt($v['M_PatientIDNumber_enc']) ?? ''; $rows[$k]['M_PatientNIK'] = $enc->decrypt($v['M_PatientNIK_enc']) ?? ''; $rows[$k]['M_PatientDOB'] = $enc->decrypt($v['M_PatientDOB_enc']) ?? ''; $rows[$k]['dob_ina'] = $rows[$k]['M_PatientDOB']; $rows[$k]['M_PatientAddressDescription'] = $enc->decrypt($v['M_PatientAddressDescription_enc']) ?? ''; $rows[$k]['M_PatientAddress'] = $rows[$k]['M_PatientAddressDescription']; foreach (array_keys($rows[$k]) as $col) { if (substr($col, -4) === '_enc') unset($rows[$k][$col]); } unset($rows[$k]['M_PatientNameRaw']); $info = $this->db_onedev->query("SELECT fn_fo_patient_visit(?) info", [$v['M_PatientID']])->row(); $rows[$k]['info'] = json_decode($info->info); // Screening template berdasarkan poli order $cu_id = $v['orderM_ClinicUnitID'] ?? null; $rows[$k]['screening_template'] = null; $rows[$k]['screening_forms'] = null; $rows[$k]['order_screening'] = null; if ($cu_id) { $tpl = $this->db_oneklinik->query( "SELECT st.M_ScreeningTemplateID, st.M_ScreeningTemplateCode, st.M_ScreeningTemplateName FROM one_klinik.m_clinic_unit cu JOIN one_klinik.m_screening_template st ON st.M_ScreeningTemplateID = cu.M_ClinicUnitM_ScreeningTemplateID WHERE cu.M_ClinicUnitID = ?", [$cu_id] )->row_array(); $rows[$k]['screening_template'] = $tpl ?: null; if ($tpl && $tpl['M_ScreeningTemplateCode'] !== 'DEFAULT') { // Template dinamis (VAKSINASI / KHITAN): ambil form + jawaban yang sudah ada $forms = $this->db_oneklinik->query( "SELECT sf.M_ScreeningFormID, sf.M_ScreeningFormQuestion, sf.M_ScreeningFormAnswerType, sf.M_ScreeningFormOptions, sf.M_ScreeningFormSortOrder, sf.M_ScreeningFormIsRequired, sa.T_ScreeningAnswerValue AS answer FROM one_klinik.m_screening_form sf LEFT JOIN one_klinik.t_screening_answer sa ON sa.T_ScreeningAnswerM_ScreeningFormID = sf.M_ScreeningFormID AND sa.T_ScreeningAnswerOrderID = ? AND sa.T_ScreeningAnswerIsActive = 'Y' WHERE sf.M_ScreeningFormM_ScreeningTemplateID = ? AND sf.M_ScreeningFormIsActive = 'Y' ORDER BY sf.M_ScreeningFormSortOrder", [$v['orderID'], $tpl['M_ScreeningTemplateID']] )->result_array(); foreach ($forms as &$f) { $f['M_ScreeningFormOptions'] = $f['M_ScreeningFormOptions'] ? json_decode($f['M_ScreeningFormOptions'], true) : null; $f['answer'] = $f['answer'] !== null ? json_decode($f['answer'], true) : null; // Tandai option terpilih dengan value:true agar FE bisa render form pre-filled if ($f['answer'] && $f['M_ScreeningFormOptions'] && $f['M_ScreeningFormAnswerType'] !== 'text') { if ($f['M_ScreeningFormAnswerType'] === 'single') { $selected = [$f['answer']['id'] ?? '']; } else { $selected = array_column((array)$f['answer'], 'id'); } foreach ($f['M_ScreeningFormOptions'] as &$opt) { $opt['value'] = in_array($opt['id'], $selected); } unset($opt); } } unset($f); $rows[$k]['screening_forms'] = $forms; } else { // DEFAULT: pakai order_screening lama $rows[$k]['order_screening'] = $this->db_oneklinik->query( "SELECT * FROM one_klinik.order_screening WHERE orderScreeningOrderID = ? AND orderScreeningIsActive = 'Y'", [$v['orderID']] )->row_array() ?: null; } } } $this->sys_ok(["total" => count($rows), "records" => $rows]); } function get_data(){ if (! $this->isLogin) { $this->sys_error("Invalid Token"); exit; } $prm = $this->sys_input; $result = array('data' => ''); $sql = "SELECT orderScreeningKesanUmum as kesan_umum, orderScreeningValueKesadaran as kesadaran, orderScreeningValuePernafasan as pernafasan, orderScreeningValueResikoJatuh as resiko_jatuh, orderScreeningValueNyeriDada as nyeri_dada, orderScreeningValueSkalaNyeri as skala_nyeri, orderScreeningValueBatuk as batuk, orderScreeningValueKeputusan as keputusan FROM one_klinik.order_screening WHERE orderScreeningOrderID = ? AND orderScreeningIsActive = 'Y' LIMIT 1"; $query = $this->db_oneklinik->query($sql,array($prm['orderid'])); if(!$query){ $this->sys_error("Gagal get data"); echo $this->db_oneklinik->last_query(); } $result = $query->row_array(); $this->sys_ok($result); exit; } function getsexreg() { if (!$this->isLogin) { $this->sys_error("Invalid Token"); exit; } $rows = []; $rows['default_location'] = []; $rows['doctors'] = $this->db_onedev->query( "SELECT M_DoctorID as id, M_DoctorCode as code, M_DoctorName as name, M_DoctorMcuDefaultKlinik as is_default, M_DoctorMcuPriceKlinik as price FROM m_doctormcu JOIN m_doctor ON M_DoctorMcuM_DoctorID = M_DoctorID WHERE M_DoctorMcuIsActive = 'Y'" )->result_array(); $rows['default_doctor'] = []; foreach ($rows['doctors'] as $value) { if ($value['is_default'] == 'Y') { $rows['default_doctor'] = $value; break; } } $rows['titles'] = $this->db_onedev->query("SELECT * FROM m_title WHERE M_TitleIsActive = 'Y'")->result_array(); $rows['sexes'] = $this->db_onedev->query("SELECT * FROM m_sex WHERE M_SexIsActive = 'Y'")->result_array(); $rows['religions'] = $this->db_onedev->query("SELECT * FROM m_religion WHERE M_ReligionIsActive = 'Y'")->result_array(); $rows['kartuidentitass'] = $this->db_onedev->query("SELECT * FROM m_idtype WHERE M_IdTypeIsActive = 'Y'")->result_array(); $branch = $this->db_onedev->query("SELECT * FROM m_branch WHERE M_BranchIsDefault = 'Y' AND M_BranchIsActive = 'Y'")->row_array(); if ($branch) { $rows['default_location']['city_address'] = $this->db_onedev->query("SELECT * FROM m_city WHERE M_CityIsActive = 'Y' AND M_CityID = ?", [$branch['M_BranchM_CityID']])->row_array(); $rows['default_location']['cities'] = $this->db_onedev->query("SELECT * FROM m_city WHERE M_CityIsActive = 'Y' AND M_CityM_ProvinceID = ?", [$rows['default_location']['city_address']['M_CityM_ProvinceID']])->result_array(); $rows['default_location']['province_address'] = $this->db_onedev->query("SELECT * FROM m_province WHERE M_ProvinceIsActive = 'Y' AND M_ProvinceID = ?", [$rows['default_location']['city_address']['M_CityM_ProvinceID']])->row_array(); $rows['default_location']['provinces'] = $this->db_onedev->query("SELECT * FROM m_province WHERE M_ProvinceIsActive = 'Y'")->result_array(); $rows['default_location']['districts'] = $this->db_onedev->query("SELECT * FROM m_district WHERE M_DistrictIsActive = 'Y' AND M_DistrictM_CityID = ?", [$branch['M_BranchM_CityID']])->result_array(); $rows['default_location']['district_address'] = $this->db_onedev->query("SELECT * FROM m_district WHERE M_DistrictIsActive = 'Y' AND M_DistrictID = ?", [$branch['M_BranchM_DistrictID']])->row_array(); $rows['default_location']['kelurahans'] = $this->db_onedev->query("SELECT * FROM m_kelurahan WHERE M_KelurahanIsActive = 'Y' AND M_KelurahanM_DistrictID = ?", [$branch['M_BranchM_DistrictID']])->result_array(); $rows['default_location']['kelurahan_address'] = $this->db_onedev->query("SELECT * FROM m_kelurahan WHERE M_KelurahanIsActive = 'Y' AND M_KelurahanID = ?", [$branch['M_BranchM_KelurahanID']])->row_array(); } $this->sys_ok(["total" => count($rows), "records" => $rows]); exit; } protected function objToArray($obj) { // Not an object or array if (!is_object($obj) && !is_array($obj)) { return $obj; } // Parse array foreach ($obj as $key => $value) { $arr[$key] = $this->objToArray($value); } // Return parsed array return $arr; } public function list_patient() { $prm = $this->sys_input; $max_rst = 20; $tot_count = 0; $number_limit = 20; $number_offset = (!isset($prm['current_page'])?1:$prm['current_page'] - 1) * $number_limit ; $xdate = $prm['date']; $status = $prm['status']; $sql = "SELECT 'N' divider, M_PatientName, M_PatientName_enc, M_PatientHP, M_PatientHP_enc, M_PatientDOB, M_PatientDOB_enc, M_PatientEmail, M_PatientEmail_enc, M_PatientPhone, M_PatientPhone_enc, M_PatientPOB, M_PatientPOB_enc, M_PatientIDNumber, M_PatientIDNumber_enc, M_PatientNIK, M_PatientNIK_enc, M_PatientPhoto, M_PatientPhotoThumb, `order`.*,DATE_FORMAT(orderDate,'%d-%m-%Y') as date_order, '' as kode_status, '' as status, M_TitleName, M_PatientNoReg, M_PatientM_SexID FROM one_klinik.`order` JOIN m_patient ON orderM_PatientID = M_PatientID LEFT JOIN m_title ON M_PatientM_TitleID = M_TitleID WHERE orderIsActive = 'Y' AND DATE(orderDate) = ? AND orderIsScreening = ? LIMIT $number_limit offset $number_offset"; //echo $sql; $query = $this->db_oneklinik->query($sql,array($xdate,$status)); //echo $this->db_oneklinik->last_query(); if ($query) { $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']); } $result = array("total" => $tot_page, "records" => $rows, "sql"=> $this->db_oneklinik->last_query()); $this->sys_ok($result); } else { $this->sys_error_db("m_patient rows",$this->db_oneklinik); exit; } } function process_now(){ if (! $this->isLogin) { $this->sys_error("Invalid Token"); exit; } $prm = $this->sys_input; $userID = $this->sys_user['M_UserID']; //print_r($prm['subgroup']); $sql = "INSERT INTO one_klinik.order_status ( orderStatusOrderID, orderStatusCode, orderStatusValue, orderStatusUserID ) VALUES( ?,?,?,? )"; $query = $this->db_oneklinik->query($sql,array($prm['orderID'],'S','Y',$userID)); if(!$query){ $this->sys_error("Gagal Process"); } $sql = "UPDATE one_klinik.`order` SET orderIsScreening = 'Y', orderUserID = ? WHERE orderID = ?"; $query = $this->db_oneklinik->query($sql,array($userID,$prm['orderID'])); if(!$query){ $this->sys_error("Gagal Update"); } $result = array('process'=>'OK'); $this->sys_ok($result); exit; } function cancel_now(){ if (! $this->isLogin) { $this->sys_error("Invalid Token"); exit; } $prm = $this->sys_input; $userID = $this->sys_user['M_UserID']; //print_r($prm['subgroup']); $sql = "INSERT INTO one_klinik.order_status ( orderStatusOrderID, orderStatusCode, orderStatusValue, orderStatusUserID ) VALUES( ?,?,?,? )"; $query = $this->db_oneklinik->query($sql,array($prm['orderID'],'S','N',$userID)); if(!$query){ $this->sys_error("Gagal Process"); } $sql = "UPDATE one_klinik.`order` SET orderIsScreening = 'N', orderUserID = ? WHERE orderID = ?"; $query = $this->db_oneklinik->query($sql,array($userID,$prm['orderID'])); if(!$query){ $this->sys_error("Gagal Process"); } $result = array('process'=>'OK'); $this->sys_ok($result); exit; } function end_session(){ $this->endsession(); } 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 sebagai JSON object $answer_type = $item['answer_type'] ?? 'single'; if ($answer_type === 'text') { $stored_value = json_encode(['value' => $item['answer_label'] ?? '']); } else { $stored_value = json_encode(['id' => $item['answer_id'] ?? '', 'label' => $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']); } }