From 2d9ff5b1b77fc49d881c8d9e955593babfb79ffa Mon Sep 17 00:00:00 2001 From: "sas.fajri" Date: Mon, 11 May 2026 14:39:34 +0700 Subject: [PATCH] Add SP and sync endpoint for MCU participant daily by preregister Co-Authored-By: Claude Sonnet 4.6 --- .../mcuoffline/Preregisterappcponev8.php | 2082 +++++++++++++++++ ...h_mcu_participant_daily_by_preregister.sql | 92 + 2 files changed, 2174 insertions(+) create mode 100644 application/controllers/mockup/mcuoffline/Preregisterappcponev8.php create mode 100644 scripts/sql/2026-05-11_create_sp_refresh_mcu_participant_daily_by_preregister.sql diff --git a/application/controllers/mockup/mcuoffline/Preregisterappcponev8.php b/application/controllers/mockup/mcuoffline/Preregisterappcponev8.php new file mode 100644 index 0000000..383fb02 --- /dev/null +++ b/application/controllers/mockup/mcuoffline/Preregisterappcponev8.php @@ -0,0 +1,2082 @@ +db_onedev = $this->load->database("onedev", true); + $this->db_dashboard = $this->load->database("cpone_dashboard", true); + } + + private function sync_schedule_and_daily($preregisterID) + { + $preregisterID = intval($preregisterID); + if ($preregisterID <= 0) { + return; + } + $this->db_onedev->query("CALL cpone.sp_refresh_mcu_participant_daily_by_preregister(?)", array($preregisterID)); + } + public function getsetup() + { + try { + // if (!$this->isLogin) { + // $this->sys_error("Invalid Token"); + // exit; + // } + $sql = "SELECT * + FROM mgm_mcu + WHERE Mgm_McuIsActive = 'Y' AND ( CURDATE() BETWEEN Mgm_McuStartDate AND Mgm_McuEndDate ) + ORDER BY Mgm_McuStartDate ASC, Mgm_McuEndDate ASC"; + $qry = $this->db_onedev->query($sql, []); + $last_qry = $this->db_onedev->last_query(); + if (!$qry) { + $message = $this->db_onedev->error(); + $message['last_qry'] = $last_qry; + $this->sys_error($message); + exit; + } + $data = $qry->result_array(); + $result = [ + "records" => $data, + ]; + $this->sys_ok($result); + } catch (Exception $exc) { + $message = $exc->getMessage(); + $this->sys_error($message); + } + } + + function generate_code_string() + { + $length = 5; + $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $code = ''; + for ($i = 0; $i < $length; $i++) { + $code .= $characters[rand(0, strlen($characters) - 1)]; + } + return $code; + } + + function generate_uuid() + { + // Generate 16 bytes (128 bits) of random data + $data = random_bytes(16); + + // Set version to 0100 + $data[6] = chr(ord($data[6]) & 0x0f | 0x40); + // Set bits 6-7 to 10 + $data[8] = chr(ord($data[8]) & 0x3f | 0x80); + + // Output the 36 character UUID + return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); + } + + function generate_code_form() + { + try { + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + $prm = $this->sys_input; + $mgm_mcu_id = $prm['id']; + $sql = "SELECT Mcu_PreregisterPatientsID + FROM mcu_preregister_patients + WHERE + Mcu_PreregisterPatientsMgm_McuID = ? AND + ( + Mcu_PreregisterPatientsT_OrderHeaderID = 0 OR + Mcu_PreregisterPatientsIsRegistered = 'N' + ) AND + Mcu_PreregisterPatientsIsActive = 'Y' + "; + $qry = $this->db_onedev->query($sql, [$mgm_mcu_id]); + $last_qry = $this->db_onedev->last_query(); + //echo $last_qry; + //exit; + if (!$qry) { + $message = $this->db_onedev->error(); + $message['last_qry'] = $last_qry; + $this->sys_error($message); + exit; + } + $data = $qry->result_array(); + if (count($data) > 0) { + foreach ($data as $k => $v) { + + $sql = "SELECT COUNT(*) as total + FROM form_riwayat_pasien + WHERE FormRiwayatPasienPreregisterID = ? AND + FormRiwayatPasienIsActive = 'Y' + "; + $qry = $this->db_onedev->query($sql, [$v['Mcu_PreregisterPatientsID']]); + //echo $this->db_onedev->last_query(); + //exit; + if ($qry) { + $total = $qry->result_array()[0]['total']; + if ($total == 0) { + $code = $this->generate_code_string(); + $uuid = $this->generate_uuid(); + + $sql = "INSERT INTO form_riwayat_pasien ( + FormRiwayatPasienPreregisterID, + FormRiwayatPasienCode, + FormRiwayatPasienUUID, + FormRiwayatPasienCreated) + VALUES (?, ?, ?, NOW())"; + $qry = $this->db_onedev->query($sql, [$v['Mcu_PreregisterPatientsID'], $code, $uuid]); + } + } + } + } + $result = [ + "records" => $data, + ]; + $this->sys_ok($result); + } catch (Exception $exc) { + $message = $exc->getMessage(); + $this->sys_error($message); + } + } + + function search_patient_listing() + { + $prm = $this->sys_input; + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + + $search = $prm["search"]; + $setup = $prm["setup"]; + $status = $prm["status"]; + $sqlStatus = ""; + if ($status == 'Y') { + $sqlStatus = "AND Mcu_PreregisterPatientsIsRegistered = 'Y'"; + } else if ($status == 'N') { + $sqlStatus = "AND Mcu_PreregisterPatientsIsRegistered = 'N'"; + } + $q = [ + 'name' => '', + 'hp' => '', + 'dob' => '', + 'address' => '' + ]; + + + + $sql_where = ""; + if ($prm['search'] != '') { + $e = explode('+', $prm['search']); + if (count($e) == 1) { + $sql_where .= "AND Mcu_PreregisterPatientsPatientName like CONCAT('%','{$e[0]}','%')"; + } + if (count($e) == 2) { + $sql_where .= "AND ( Mcu_PreregisterPatientsPatientName like CONCAT('%','{$e[0]}','%') AND Mcu_PreregisterPatientsNIP like CONCAT('%','{$e[1]}','%')) "; + } + } + + if (!empty($prm['mcudate'])) { + $mcudate = $prm['mcudate']; + $sql_where .= " AND Mcu_PreregisterDateCheckinSchedule = '{$mcudate}'"; + } + + $number_limit = 10; + $number_offset = ($prm['current_page'] - 1) * $number_limit; + + // if($setup['Mgm_McuM_BranchID']== '100'){ + + // } + + $sql = " SELECT count(*) as total + FROM mcu_preregister_patients + JOIN m_patient ON Mcu_PreregisterPatientsM_PatientID = M_PatientID + LEFT join terminology on attribute_path = 'Address.country' AND code = M_PatientAddressCountry + LEFT JOIN m_title ON Mcu_PreregisterPatientsM_TitleID = M_TitleID + LEFT JOIN t_orderheader ON Mcu_PreregisterPatientsT_OrderHeaderID = T_OrderHeaderID + LEFT JOIN mcu_preregister_date ON Mcu_PreregisterDateMcu_PreregisterPatientsID = Mcu_PreregisterPatientsID + WHERE + Mcu_PreregisterPatientsIsActive = 'Y' AND Mcu_PreregisterPatientsMgm_McuID = {$setup['Mgm_McuID']} + $sql_where + $sqlStatus + "; + //echo $sql; + $query = $this->db_onedev->query($sql); + $tot_count = 0; + $tot_page = 0; + if ($query) { + $tot_count = $query->result_array()[0]["total"]; + $tot_page = ceil($tot_count / $number_limit); + } else { + $this->sys_error_db("m_doctor count", $this->db_onedev); + exit; + } + + + $sql = " SELECT * + , '' as agreement, + '' as packets, + '' as tests, + CONCAT(IF(ISNULL(M_TitleName),'',CONCAT(M_TitleName,'.')), + ' ', + IFNULL(Mcu_PreregisterPatientsPatientPrefix,''), + ' ', + Mcu_PreregisterPatientsPatientName, + ' ', + IFNULL(Mcu_PreregisterPatientsPatientSuffix,'')) as patient_fullname, + DATE_FORMAT(Mcu_PreregisterPatientsDOB,'%d-%m-%Y') as dob, + DATE_FORMAT(Mcu_PreregisterPatientsDOB,'%d-%m-%Y') as Mcu_PreregisterPatientsDOB, + M_PatientAddress, + M_PatientAddressCity, + M_PatientAddressCountry, + IFNULL(display, 'Indonesia') as countryName, + M_PatientAddressRT, + M_PatientAddressRW, + M_PatientReligionCode, + CorporateEmail, + IFNULL(FormRiwayatPasienCode, 'Belum digenerate') as code_form, + IFNULL(FormRiwayatPasienUUID, '') as uuid_form, + Mcu_PreregisterDateCheckinSchedule, + pcc.PreregisterCheckInCheckOutID, + pcc.PreregisterCheckInCheckInDate, + pcc.PreregisterCheckInCheckOutInTime, + pcc.PreregisterCheckInCheckOutDate, + pcc.PreregisterCheckInCheckOutOutTime + FROM mcu_preregister_patients + JOIN m_patient ON Mcu_PreregisterPatientsM_PatientID = M_PatientID + JOIN mgm_mcu ON Mcu_PreregisterPatientsMgm_McuID = Mgm_McuID + JOIN corporate ON Mgm_McuCorporateID = CorporateID + LEFT join terminology on attribute_path = 'Address.country' AND code = M_PatientAddressCountry + LEFT JOIN m_title ON Mcu_PreregisterPatientsM_TitleID = M_TitleID + LEFT JOIN t_orderheader ON Mcu_PreregisterPatientsT_OrderHeaderID = T_OrderHeaderID + LEFT JOIN form_riwayat_pasien ON Mcu_PreregisterPatientsID = FormRiwayatPasienPreregisterID AND FormRiwayatPasienIsActive = 'Y' + LEFT JOIN mcu_preregister_date ON Mcu_PreregisterDateMcu_PreregisterPatientsID = Mcu_PreregisterPatientsID + LEFT JOIN ( + SELECT c.* + FROM preregister_checkin_checkout c + JOIN ( + SELECT + PreregisterCheckInCheckOutPreregisterID, + PreregisterCheckInCheckOutT_OrderHeaderID, + MAX(PreregisterCheckInCheckOutID) AS last_id + FROM preregister_checkin_checkout + GROUP BY + PreregisterCheckInCheckOutPreregisterID, + PreregisterCheckInCheckOutT_OrderHeaderID + ) latest + ON latest.last_id = c.PreregisterCheckInCheckOutID + ) pcc + ON pcc.PreregisterCheckInCheckOutPreregisterID = Mcu_PreregisterPatientsID + AND pcc.PreregisterCheckInCheckOutT_OrderHeaderID = T_OrderHeaderID + WHERE + Mcu_PreregisterPatientsIsActive = 'Y' AND Mcu_PreregisterPatientsMgm_McuID = {$setup['Mgm_McuID']} + $sql_where + $sqlStatus + ORDER BY T_OrderHeaderDate DESC, Mcu_PreregisterPatientsIsRegistered ASC, Mcu_PreregisterPatientsPatientName ASC + limit $number_limit offset $number_offset"; + + $query = $this->db_onedev->query($sql); + //echo $sql; + if (!$query) { + $message = $this->db_onedev->error(); + $message['last_qry'] = + $this->db_onedev->last_query(); + $this->sys_error($message); + exit; + }; + $lastQuerySearch = + $this->db_onedev->last_query(); + $rows = $query->result_array(); + if ($rows) { + foreach ($rows as $k => $v) { + $rows[$k]['packets'] = array(); + $rows[$k]['tests'] = array(); + $join_test = ''; + $filter_paket = "AND T_PacketSasCode IN ('')"; + $filter_test = "AND T_TestSasCode IN ('')"; + + $testFromOptional = array(); + if ($v['Mcu_PreregisterPatientsOrders'] != '') { + $xjoin_tests = explode(',', $v['Mcu_PreregisterPatientsOrders']); + $unique_tests = array(); // array untuk menyimpan nilai unik + + if ($xjoin_tests) { + foreach ($xjoin_tests as $kjt => $vjt) { + $vjt = trim($vjt); + + $opt = ""; + if (substr($vjt, -4) === '-opt') { + $opt = trim(explode("_", $vjt)[1]); + $vjt = trim(explode("_", $vjt)[0]); + // "10610200_163-opt" + } + + if (!in_array($vjt, $unique_tests)) { + $packet = trim(explode("-", $opt)[0]); + $testFromOptional[$vjt] = $packet; + + $unique_tests[] = $vjt; + if ($join_test != '') + $join_test .= ","; + $join_test .= "'{$vjt}'"; + } + } + } + //print_r($unique_tests)."
"; + //echo $join_test; + $filter_paket = "AND T_PacketSasCode IN ({$join_test})"; + $filter_test = "AND T_TestSasCode IN ({$join_test}) "; + } + $sql = "SELECT DISTINCT T_PacketID as id, T_PacketName as name, T_PacketSasCode as code, nat_test + FROM ss_price_mou + JOIN t_packet ON T_TestID = T_PacketID AND T_PacketIsActive = 'Y' + JOIN mgm_mcupacket ON T_PacketID = Mgm_McuPacketT_PacketID + AND Mgm_McuPacketIsActive = 'Y' AND Mgm_McuPacketMgm_McuID = {$setup['Mgm_McuID']} + WHERE + T_PriceT_PriceHeaderID = {$setup['Mgm_McuT_PriceHeaderID']} AND is_packet = 'Y' $filter_paket"; + //echo $sql; + $rows[$k]['packets'] = $this->db_onedev->query($sql)->result_array(); + if ($rows[$k]['packets']) { + foreach ($rows[$k]['packets'] as $kp => $vp) { + $rows[$k]['packets'][$kp]['nat_test'] = json_decode($vp['nat_test']); + } + } + + $sql = "SELECT test.T_TestID as id, test.T_TestName as name, test.T_TestSasCode as code, nat_test + FROM ss_price_mou + JOIN t_test test ON test.T_TestID = ss_price_mou.T_TestID AND T_TestIsActive = 'Y' + WHERE + T_PriceT_PriceHeaderID = {$setup['Mgm_McuT_PriceHeaderID']} AND is_packet = 'N' AND + T_PriceIsCito = 'N' $filter_test"; + //echo $sql; + $rows[$k]['tests'] = $this->db_onedev->query($sql)->result_array(); + if ($rows[$k]['tests']) { + foreach ($rows[$k]['tests'] as $kp => $vp) { + $rows[$k]['tests'][$kp]['nat_test'] = json_decode($vp['nat_test']); + // if test from optional packet + if (isset($testFromOptional[$vp['code']])) { + $rows[$k]['tests'][$kp]['T_PacketDetailT_PacketID'] = $testFromOptional[$vp['code']]; + } + } + } + } + $pids = array(); + $sql = "SELECT Mcu_PreregisterPatientsM_PatientID as id + FROM mcu_preregister_patients + WHERE + Mcu_PreregisterPatientsIsActive = 'Y' AND Mcu_PreregisterPatientsMgm_McuID = '{$setup['Mgm_McuID']}'"; + + $rows_all = $this->db_onedev->query($sql)->result_array(); + //echo $sql; + if ($rows_all) { + foreach ($rows_all as $ka => $va) { + array_push($pids, $va['id']); + } + } + } + //$this->_add_address($rows); + $result = array( + "total" => $tot_page, + "total_data" => $tot_count, + "records" => $rows, + "pids" => $pids, + "sql" => $this->db_onedev->last_query(), + "sqlsrc" => $lastQuerySearch + ); + $this->sys_ok($result); + exit; + } + + function search_patient_table() + { + $prm = $this->sys_input; + $setup = $prm['setup']; + + $max_rst = 100; + $tot_count = 0; + $number_limit = 10; + $number_offset = (!isset($prm['current_page']) ? 1 : $prm['current_page'] - 1) * $number_limit; + if (isset($prm['reload'])) { + $number_limit = intval($prm['current_page']) * 10; + $number_offset = 0; + } + + $q = [ + 'name' => '', + 'hp' => '', + 'dob' => '', + 'address' => '' + ]; + + + + if ($prm['search'] != '') { + $e = explode('+', $prm['search']); + if (isset($e[0])) + $q['name'] = "AND M_PatientName LIKE '%{$e[0]}%'"; + if (isset($e[1])) + $q['dob'] = "AND ((DATE_FORMAT(M_PatientDOB, '%d-%m-%Y') LIKE '%{$e[1]}%' and M_PatientDOB IS NOT NULL) OR (M_PatientDOB IS NULL AND '{$e[1]}' = ''))"; + if (isset($e[2])) + $q['nik'] = "AND M_PatientNIP LIKE '%{$e[2]}%'"; + } + + + $sql = "SELECT m_patient.*, + 'N' divider, + concat(IFNULL(M_TitleName,''),' ',IFNULL(M_PatientPrefix,''),' ',M_PatientName,' ',IFNULL(M_PatientSuffix,'')) M_PatientName, + M_PatientName M_PatientRealName, M_TitleID, M_TitleName, M_PatientGender, + DATE_FORMAT(M_PatientDOB,'%d-%m-%Y') as dob_ina, + IFNULL(M_PatientReligionCode, '-') M_PatientReligionCode, + M_PatientNoReg as Mcu_PreregisterPatientsPID, + M_PatientIdentifierValue as Mcu_PreregisterPatientsKTP, + M_PatientID as Mcu_PreregisterPatientsM_PatientID, + M_TitleID as Mcu_PreregisterPatientsM_TitleID, + M_PatientName as Mcu_PreregisterDetailsPatientName, + M_PatientGender as Mcu_PreregisterPatientsPatientName, + M_PatientDOB as Mcu_PreregisterPatientsDOB, + IFNULL(M_PatientReligionCode,0) as Mcu_PreregisterPatientsReligion, + M_PatientEmail as Mcu_PreregisterPatientsEmail, + M_PatientHP as Mcu_PreregisterPatientsHp, + M_PatientPosisi as Mcu_PreregisterPatientsPosisi, + M_PatientDivisi as Mcu_PreregisterPatientsDivisi, + M_PatientJob as Mcu_PreregisterPatientsJob, + M_PatientDepartement as Mcu_PreregisterPatientsDepartment + from + m_patient + LEFT join m_title on M_PatientM_TitleID = M_TitleID + where M_PatientIsActive = 'Y' + {$q['name']} + {$q['dob']} + {$q['nik']} + group by M_PatientID + limit $number_limit offset $number_offset"; + //echo $sql; + $query = $this->db_onedev->query($sql); + $qryListing = $this->db_onedev->last_query(); + + if ($query) { + $rows = $query->result_array(); + if ($rows) { + $per_divider = 1; + foreach ($rows as $k => $v) { + if ($per_divider == 10) { + $rows[$k]['divider'] = 'Y'; + } + $per_divider = $per_divider + 1; + if ($per_divider > 10) + $per_divider = 1; + } + } + $pids = array(); + $sql = "SELECT Mcu_PreregisterPatientsM_PatientID as id + FROM mcu_preregister_patients + WHERE + Mcu_PreregisterPatientsIsActive = 'Y' AND Mcu_PreregisterPatientsMgm_McuID = '{$setup['Mgm_McuID']}'"; + + $rows_all = $this->db_onedev->query($sql)->result_array(); + //echo $sql; + if ($rows_all) { + foreach ($rows_all as $ka => $va) { + array_push($pids, $va['id']); + } + } + + $result = array("total" => "", "records" => $rows, "pids" => $pids, "sql" => $this->db_onedev->last_query(), "qrylisting" => $qryListing); + $this->sys_ok($result); + } else { + $this->sys_error_db("m_patient rows", $this->db_onedev); + exit; + } + } + + function doReCheckin() + { + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + + $prm = $this->sys_input; + $userid = $this->sys_user["M_UserID"]; + $preregister_id = isset($prm['preregister_id']) ? $prm['preregister_id'] : 0; + $orderheader_id = isset($prm['orderheader_id']) ? $prm['orderheader_id'] : 0; + $mcu_id = isset($prm['mcu_id']) ? $prm['mcu_id'] : 0; + + if (intval($preregister_id) <= 0 || intval($orderheader_id) <= 0 || intval($mcu_id) <= 0) { + $this->sys_error("Parameter preregister_id, orderheader_id, dan mcu_id wajib diisi."); + exit; + } + + $this->db_onedev->trans_begin(); + + $sql_checkin_checkout = "INSERT INTO preregister_checkin_checkout ( + PreregisterCheckInCheckOutPreregisterID, + PreregisterCheckInCheckOutT_OrderHeaderID, + PreregisterCheckInCheckInDate, + PreregisterCheckInCheckOutInTime, + PreregisterCheckInCheckOutInUserID, + PreregisterCheckInCheckOutIsActive, + PreregisterCheckInCheckOutLastUpdated, + PreregisterCheckInCheckOutCreated + ) VALUES (?, ?, CURDATE(), CURTIME(), ?, 'Y', NOW(), NOW())"; + $query_checkin_checkout = $this->db_onedev->query($sql_checkin_checkout, array($preregister_id, $orderheader_id, $userid)); + if (!$query_checkin_checkout) { + $this->db_onedev->trans_rollback(); + $this->sys_error_db("insert preregister_checkin_checkout", $this->db_onedev); + exit; + } + + $sql_mcu_checkinout = "INSERT INTO cpone_dashboard.mcu_checkinout ( + Mcu_CheckinoutMcuID, + Mcu_CheckinoutPreregisterID, + Mcu_CheckinoutOrderID, + Mcu_CheckinoutDate, + Mcu_CheckinoutInTime, + Mcu_CheckinoutIsActive, + Mcu_CheckinoutSyncedAt + ) VALUES (?, ?, ?, CURDATE(), CURTIME(), 'Y', NOW())"; + $query_mcu_checkinout = $this->db_onedev->query($sql_mcu_checkinout, array($mcu_id, $preregister_id, $orderheader_id)); + if (!$query_mcu_checkinout) { + $this->db_onedev->trans_rollback(); + $this->sys_error_db("insert cpone_dashboard.mcu_checkinout", $this->db_onedev); + exit; + } + + /* + + $sql_mcu_preregister_date = "INSERT INTO mcu_preregister_date ( + Mcu_PreregisterDateMcu_PreregisterPatientsID, + Mcu_PreregisterDateCheckinSchedule, + Mcu_PreregisterDateIsActive, + Mcu_PreregisterDateCreated, + Mcu_PreregisterDateCreatedUserID, + Mcu_PreregisterDateLastUpdated, + Mcu_PreregisterDateLastUpdatedID + ) VALUES (?, CURDATE(), 'Y', NOW(), ?, NOW(), ?) + ON DUPLICATE KEY UPDATE + Mcu_PreregisterDateCheckinSchedule = CURDATE(), + Mcu_PreregisterDateIsActive = 'Y', + Mcu_PreregisterDateLastUpdated = NOW(), + Mcu_PreregisterDateLastUpdatedID = VALUES(Mcu_PreregisterDateLastUpdatedID)"; + $query_mcu_preregister_date = $this->db_smartone->query($sql_mcu_preregister_date, array($preregister_id, $userid, $userid)); + if (!$query_mcu_preregister_date) { + $this->db_smartone->trans_rollback(); + $this->sys_error_db(["status" => "ERR", "message" => "insert/update mcu_preregister_date | " . + $this->db_smartone->error()["message"], "debug" => $this->db_smartone->last_query()]); + exit; + } + + $sql_mcu_participant_daily = "INSERT INTO cpone_dashboard.mcu_participant_daily ( + Mcu_ParticipantDailyMcuID, + Mcu_ParticipantDailyDate, + Mcu_ParticipantDailyTotal, + Mcu_ParticipantDailyIsActive + ) + SELECT + p.Mcu_PreregisterPatientsMgm_McuID, + d.Mcu_PreregisterDateCheckinSchedule, + COUNT(*) AS total_participant, + 'Y' + FROM mcu_preregister_date d + JOIN mcu_preregister_patients p + ON p.Mcu_PreregisterPatientsID = d.Mcu_PreregisterDateMcu_PreregisterPatientsID + WHERE d.Mcu_PreregisterDateCheckinSchedule = CURDATE() + AND p.Mcu_PreregisterPatientsMgm_McuID = ? + AND d.Mcu_PreregisterDateIsActive = 'Y' + AND p.Mcu_PreregisterPatientsIsActive = 'Y' + GROUP BY d.Mcu_PreregisterDateCheckinSchedule, p.Mcu_PreregisterPatientsMgm_McuID + ON DUPLICATE KEY UPDATE + Mcu_ParticipantDailyTotal = VALUES(Mcu_ParticipantDailyTotal), + Mcu_ParticipantDailyIsActive = 'Y', + Mcu_ParticipantDailyLastUpdated = CURRENT_TIMESTAMP"; + $query_mcu_participant_daily = $this->db_smartone->query($sql_mcu_participant_daily, array($mcu_id)); + if (!$query_mcu_participant_daily) { + $this->db_smartone->trans_rollback(); + $this->sys_error_db(["status" => "ERR", "message" => "insert/update mcu_participant_daily | " . + $this->db_smartone->error()["message"], "debug" => $this->db_smartone->last_query()]); + exit; + } + + */ + + $this->db_onedev->trans_commit(); + + $this->sys_ok(array( + "records" => array( + "preregister_id" => $preregister_id, + "orderheader_id" => $orderheader_id, + "mcu_id" => $mcu_id + ) + )); + exit; + } + + function saveCheckinScheduleDate() + { + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + + $prm = $this->sys_input; + $userid = $this->sys_user["M_UserID"]; + $id = isset($prm['id']) ? intval($prm['id']) : 0; + $next_date = isset($prm['next_date']) ? $prm['next_date'] : ''; + + if ($id <= 0 || $next_date == '') { + $this->sys_error("Parameter id dan next_date wajib diisi."); + exit; + } + + $sql = "UPDATE mcu_preregister_date SET + Mcu_PreregisterDateCheckinSchedule = ?, + Mcu_PreregisterDateLastUpdated = NOW(), + Mcu_PreregisterDateLastUpdatedID = ? + WHERE Mcu_PreregisterDateMcu_PreregisterPatientsID = ?"; + $qry = $this->db_onedev->query($sql, array($next_date, $userid, $id)); + if (!$qry) { + $message = $this->db_onedev->error(); + $message['qry'] = $this->db_onedev->last_query(); + $this->sys_error($message); + exit; + } + + $this->sys_ok(array( + "records" => array( + "id" => $id, + "next_date" => $next_date + ) + )); + exit; + } + + function syncSchedule() + { + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + + $prm = $this->sys_input; + $preregister_id = isset($prm['preregister_id']) ? intval($prm['preregister_id']) : 0; + + if ($preregister_id <= 0) { + $this->sys_error("Parameter preregister_id wajib diisi."); + exit; + } + + $this->sync_schedule_and_daily($preregister_id); + + $this->sys_ok(array( + "records" => array( + "preregister_id" => $preregister_id + ) + )); + exit; + } + + function savenewform() + { + // $this->db_onedev->trans_begin(); + $this->db_onedev->trans_begin(); + // $this->db_onedev->trans_rollback(); + // $this->db_onedev->trans_commit(); + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + $userid = $this->sys_user["M_UserID"]; + $prm = $this->sys_input; + //print_r($prm); + $setup = $prm['setup']; + $sql = "SELECT CorporateCode FROM corporate WHERE CorporateID = '{$setup['Mgm_McuCorporateID']}'"; + $qry = $this->db_onedev->query($sql); + if (!$qry) { + $message = $this->db_onedev->error(); + $this->db_onedev->trans_rollback(); + $this->sys_error($message); + exit; + } + $corporateCode = $qry->row_array()['CorporateCode']; + $v = $prm; + + $name = str_replace("'", "\\'", $v['M_PatientRealName']); + $nameEscape = $this->db_onedev->escape($v['M_PatientRealName']); + // print_r($name); + $pdob = date('Y-m-d', strtotime($v['M_PatientDOB'])); + + + $query = "INSERT INTO mcu_preregister_patients ( + Mcu_PreregisterPatientsMgm_McuID, + Mcu_PreregisterPatientsPID, + Mcu_PreregisterPatientsM_PatientID, + Mcu_PreregisterPatientsKTP, + Mcu_PreregisterPatientsPatientPrefix, + Mcu_PreregisterPatientsPatientName, + Mcu_PreregisterPatientsPatientSuffix, + Mcu_PreregisterPatientsGender, + Mcu_PreregisterPatientsDOB, + Mcu_PreregisterPatientsReligion, + Mcu_PreregisterPatientsJob, + Mcu_PreregisterPatientsEmail, + Mcu_PreregisterPatientsHp, + Mcu_PreregisterPatientsPosisi, + Mcu_PreregisterPatientsDivisi, + Mcu_PreregisterPatientsLocation, + Mcu_PreregisterPatientsCreated, + Mcu_PreregisterPatientsUserID, + Mcu_PreregisterPatientsM_TitleID, + Mcu_PreregisterPatientsCorporateCode, + Mcu_PreregisterPatientsDepartment, + Mcu_PreregisterPatientsNIP + ) + VALUES( + ?,?,?,?,?,?,?, + ?, + ?, + ?, + ?, + ?, + ?, + ?, + ?, + ?, + NOW(), + ?, + ?, + ?, + ?, + ? + )"; + //echo $query; + $rows = $this->db_onedev->query($query, [ + $setup['Mgm_McuID'], + $v['Mcu_PreregisterPatientsPID'], + $v['M_PatientID'], + $v['Mcu_PreregisterPatientsKTP'], + $v['M_PatientPrefix'], + $v['M_PatientRealName'], + $v['M_PatientSuffix'], + $v['M_PatientGender'], + $pdob, + $v['M_PatientReligionCode'], + $v['M_PatientJob'], + $v['M_PatientEmail'], + $v['M_PatientHp'], + $v['M_PatientPosisi'], + $v['M_PatientDivisi'], + $v['M_PatientLocation'], + $userid, + $v['Mcu_PreregisterPatientsM_TitleID'], + $corporateCode, + $v['M_PatientDepartement'], + $v['M_PatientNIP'] + ]); + if (!$rows) { + $message = $this->db_onedev->error(); + $this->db_onedev->trans_rollback(); + $this->sys_error($message); + exit; + } + $last_id_x = $this->db_onedev->insert_id(); + if (intval($v['Mcu_PreregisterPatientsM_PatientID']) == 0) { + + $sql = "SELECT fn_numbering_cpone('P') as number"; + $rows = $this->db_onedev->query($sql); + if (!$rows) { + $message = $this->db_onedev->error(); + $this->db_onedev->trans_rollback(); + $this->sys_error($message); + exit; + } + $number = $rows->result_array()[0]['number']; + + $title_id = $v['Mcu_PreregisterPatientsM_TitleID']; + // $religion_id = $v['Mcu_PreregisterDetailsM_ReligionID']; + $religion_id = ''; + $nameNewP = $this->db_onedev->escape($v["Mcu_PreregisterPatientsPatientName"]); + $nameNewPn = str_replace("'", "\\'", $v['Mcu_PreregisterPatientsPatientName']); + + + + $sql = "INSERT INTO m_patient ( + M_PatientPrefix, + M_PatientName, + M_PatientSuffix, + M_PatientM_TitleID, + M_PatientGender, + M_PatientDOB, + M_PatientIdentifierCode, + M_PatientIdentifierSystem, + M_PatientIdentifierValue, + M_PatientPosisi, + M_PatientDivisi, + M_PatientLocation, + M_PatientJob, + M_PatientEmail, + M_PatientHP, + M_PatientCreatedUserID, + M_PatientNIP, + M_PatientDepartement, + M_PatientNoReg, + M_PatientCreated, + M_PatientRegisteredByCorporateID, + M_PatientAddressRegionalCd, + M_PatientAddressCity, + M_PatientAddressRT, + M_PatientAddressRW, + M_PatientAddressVillage, + M_PatientAddressDistrict, + M_PatientAddressState, + M_PatientAddressCountry + ) + VALUES( + '{$v["Mcu_PreregisterPatientsPatientPrefix"]}', + '{$nameNewPn}', + '{$v["Mcu_PreregisterPatientsPatientSuffix"]}', + '{$title_id}', + '{$v["Mcu_PreregisterPatientsM_SexCode"]}', + '{$pdob}', + 'NNIDN', + 'http://terminology.hl7.org/CodeSystem/v2-0203', + '{$v["Mcu_PreregisterPatientsKTP"]}', + '{$v['Mcu_PreregisterPatientsPosisi']}', + '{$v['Mcu_PreregisterPatientsDivisi']}', + '{$v['Mcu_PreregisterPatientsLocation']}', + '{$v['Mcu_PreregisterPatientsJob']}', + '{$v['Mcu_PreregisterPatientsEmail']}', + '{$v['Mcu_PreregisterPatientsHp']}', + '{$userid}', + '{$v['Mcu_PreregisterPatientsNIK']}', + '{$v['Mcu_PreregisterPatientsDepartment']}', + '{$number}', + NOW(), + '{$setup['Mgm_McuCorporateID']}', + '{$v['inp_RegionalCode']}', + '{$v['inp_CorporateAddressCity']}', + '{$v['inp_CorporateAddressRT']}', + '{$v['inp_CorporateAddressRW']}', + '{$v['inp_CorporateAddressVillage']}', + '{$v['inp_CorporateAddressDistrict']}', + '{$v['inp_CorporateAddressState']}', + '{$v['country']}' + )"; + //echo $sql; + $rows = $this->db_onedev->query($sql); + if (!$rows) { + $message = $this->db_onedev->error(); + $message['qry'] = $this->db_onedev->last_query(); + $this->db_onedev->trans_rollback(); + $this->sys_error($message); + exit; + } + $patient_id = $this->db_onedev->insert_id(); + $sql = "UPDATE mcu_preregister_patients SET Mcu_PreregisterPatientsM_PatientID = {$patient_id}, Mcu_PreregisterPatientsPID ='{$number}' WHERE Mcu_PreregisterPatientsID = {$last_id_x}"; + $rows = $this->db_onedev->query($sql); + if (!$rows) { + $message = $this->db_onedev->error(); + $this->db_onedev->trans_rollback(); + $this->sys_error($message); + exit; + } + } + + + $result = array( + "total" => 1, + "records" => array('status' => 'OK') + ); + $this->db_onedev->trans_commit(); + $this->sys_ok($result); + exit; + } + function getsexreg() + { + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + $rows = []; + $query = " SELECT * + FROM m_title + WHERE + M_TitleIsActive = 'Y' + "; + $gender = [ + [ + "M_SexID" => "MALE", + "M_SexCode" => "MALE", + "m_sexname" => "Laki Laki" + ], + [ + "M_SexID" => "FEMALE", + "M_SexCode" => "FEMALE", + "m_sexname" => "Perempuan" + ], + [ + "M_SexID" => "OTHER", + "M_SexCode" => "OTHER", + "m_sexname" => "Lainnya" + ] + ]; + //echo $query; + $rows['titles'] = $this->db_onedev->query($query)->result_array(); + + + $rows['sexes'] = $gender; + $query = " SELECT * FROM terminology + WHERE attribute_path = 'Person.religion.code' + AND code_system = 'xcpone.code.religion' + AND status_cd = 'normal' + ORDER BY order_no + "; + //echo $query; + $rows['religions'] = $this->db_onedev->query($query)->result_array(); + + $query = " SELECT * FROM terminology + WHERE attribute_path = 'Person.identifier.type' + AND code_system = 'http://terminology.hl7.org/CodeSystem/v2-0203' + AND status_cd = 'normal' + ORDER BY order_no + "; + //echo $query; + $rows['kartuidentitass'] = $this->db_onedev->query($query)->result_array(); + + + $result = array( + "total" => count($rows), + "records" => $rows, + ); + $this->sys_ok($result); + exit; + } + function removepatient() + { + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + $userid = $this->sys_user["M_UserID"]; + $prm = $this->sys_input; + + $query = " UPDATE mcu_preregister_patients SET + Mcu_PreregisterPatientsIsActive = 'N', + Mcu_PreregisterPatientsUserID = {$userid} + WHERE + Mcu_PreregisterPatientsID = {$prm['Mcu_PreregisterPatientsID']}"; + $this->db_onedev->query($query); + $result = array( + "total" => 1, + "records" => array('status' => 'OK') + ); + + $this->sys_ok($result); + exit; + } + function getdatapackets() + { + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + $prm = $this->sys_input; + $search = $prm["search"]; + $setup = $prm["setup"]; + + $number_limit = 10; + $number_offset = ($prm['current_page'] - 1) * $number_limit; + + $sql = "SELECT DISTINCT + T_PacketID as id, + T_PacketName as name, + T_PacketSasCode as code, + T_PacketIsOptional, + nat_test, + ss_price_mou.T_PriceTotal as price + FROM ss_price_mou + JOIN t_packet ON T_TestID = T_PacketID AND T_PacketIsActive = 'Y' + JOIN mgm_mcupacket ON T_PacketID = Mgm_McuPacketT_PacketID + AND Mgm_McuPacketIsActive = 'Y' AND Mgm_McuPacketMgm_McuID = {$setup['Mgm_McuID']} + WHERE + Ss_PriceMouMgm_McuID = {$setup['Mgm_McuID']} AND is_packet = 'Y' AND + ( T_TestName LIKE CONCAT('%','{$search}','%') )"; + + $sql = "SELECT count(*) as total + FROM ( + SELECT distinct T_PacketID as id, T_PacketName as name, T_PacketSasCode as code, nat_test + FROM ss_price_mou + JOIN t_packet ON T_TestID = T_PacketID AND T_PacketIsActive = 'Y' + JOIN mgm_mcupacket ON T_PacketID = Mgm_McuPacketT_PacketID + AND Mgm_McuPacketIsActive = 'Y' AND Mgm_McuPacketMgm_McuID = {$setup['Mgm_McuID']} + WHERE + T_PriceT_PriceHeaderID = {$setup['Mgm_McuT_PriceHeaderID']} AND is_packet = 'Y' AND + ( T_TestName LIKE CONCAT('%','{$search}','%') ) + ) x + "; + //echo $sql; + $query = $this->db_onedev->query($sql, []); + $tot_count = 0; + $tot_page = 0; + if ($query) { + $tot_count = $query->result_array()[0]["total"]; + $tot_page = ceil($tot_count / $number_limit); + } else { + $this->sys_error_db("m_doctor count", $this->db_onedev); + exit; + } + + $query = "SELECT DISTINCT + T_PacketID as id, + T_PacketName as name, + T_PacketSasCode as code, + nat_test, + T_PacketIsOptional, + ss_price_mou.T_PriceTotal as price + FROM ss_price_mou + JOIN t_packet ON T_TestID = T_PacketID AND T_PacketIsActive = 'Y' AND T_PacketIsGenerated = 'Y' + JOIN mgm_mcupacket ON T_PacketID = Mgm_McuPacketT_PacketID + AND Mgm_McuPacketIsActive = 'Y' AND Mgm_McuPacketMgm_McuID = {$setup['Mgm_McuID']} + WHERE + T_PriceT_PriceHeaderID = {$setup['Mgm_McuT_PriceHeaderID']} AND is_packet = 'Y' AND + ( T_TestName LIKE CONCAT('%','{$search}','%') ) + ORDER BY T_PacketSasCode ASC + limit $number_limit offset $number_offset"; + //echo $query; + $rows = $this->db_onedev->query($query)->result_array(); + if ($rows) { + foreach ($rows as $kp => $vp) { + $rows[$kp]['nat_test'] = json_decode($vp['nat_test']); + } + } + + $result = array( + "total" => $tot_page, + "records" => $rows, + 'qry' => $this->db_onedev->last_query(), + ); + $this->sys_ok($result); + exit; + } + + function getdatatests() + { + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + $prm = $this->sys_input; + $search = $prm["search"]; + $setup = $prm["setup"]; + $number_limit = 10; + $number_offset = ($prm['current_page'] - 1) * $number_limit; + + $sql = "SELECT test.T_TestID as id, test.T_TestName as name, test.T_TestSasCode as code, nat_test + FROM ss_price_mou + JOIN t_test test ON test.T_TestID = ss_price_mou.T_TestID AND T_TestIsActive = 'Y' + JOIN t_price price ON test.T_TestID = price.T_PriceT_TestID AND T_PriceIsActive = 'Y' + AND T_PriceT_PriceHeaderID = {$setup['Mgm_McuT_PriceHeaderID']} + AND (test.T_TestName LIKE CONCAT('%','{$search}','%') OR test.T_TestSasCode LIKE CONCAT('%','{$search}','%')) + WHERE + Ss_PriceMouMgm_McuID = {$setup['Mgm_McuID']} AND is_packet = 'N' AND + price.T_PriceIsCito = 'N'"; + + $sql = "SELECT count(*) as total + FROM ( + SELECT test.T_TestID as id, test.T_TestName as name, test.T_TestSasCode as code, nat_test + FROM ss_price_mou + JOIN t_test test ON test.T_TestID = ss_price_mou.T_TestID AND T_TestIsActive = 'Y' + JOIN t_price price ON test.T_TestID = price.T_PriceT_TestID AND T_PriceIsActive = 'Y' + AND price.T_PriceT_PriceHeaderID = {$setup['Mgm_McuT_PriceHeaderID']} + AND (test.T_TestName LIKE CONCAT('%','{$search}','%') OR test.T_TestSasCode LIKE CONCAT('%','{$search}','%')) + WHERE + ss_price_mou.T_PriceT_PriceHeaderID = {$setup['Mgm_McuT_PriceHeaderID']} + AND is_packet = 'N' + AND price.T_PriceIsCito = 'N' + ) x + "; + //echo $sql; + $query = $this->db_onedev->query($sql, []); + $tot_count = 0; + $tot_page = 0; + if ($query) { + $tot_count = $query->result_array()[0]["total"]; + $tot_page = ceil($tot_count / $number_limit); + } else { + $this->sys_error_db("m_doctor count", $this->db_onedev); + exit; + } + $query = "SELECT test.T_TestID as id, test.T_TestName as name, test.T_TestSasCode as code, nat_test,ss_price_mou.T_PriceTotal as price + FROM ss_price_mou + JOIN t_test test ON test.T_TestID = ss_price_mou.T_TestID AND T_TestIsActive = 'Y' + JOIN t_price price ON test.T_TestID = price.T_PriceT_TestID AND T_PriceIsActive = 'Y' + AND price.T_PriceT_PriceHeaderID = {$setup['Mgm_McuT_PriceHeaderID']} + AND (test.T_TestName LIKE CONCAT('%','{$search}','%') OR test.T_TestSasCode LIKE CONCAT('%','{$search}','%')) + WHERE + ss_price_mou.T_PriceT_PriceHeaderID = {$setup['Mgm_McuT_PriceHeaderID']} + AND is_packet = 'N' + AND price.T_PriceIsCito = 'N' + ORDER BY T_TestSasCode ASC + limit $number_limit offset $number_offset + "; + //echo $query; + $rows = $this->db_onedev->query($query)->result_array(); + if ($rows) { + foreach ($rows as $kp => $vp) { + $rows[$kp]['nat_test'] = json_decode($vp['nat_test']); + } + } + $result = array( + "total" => $tot_page, + "records" => $rows, + ); + $this->sys_ok($result); + exit; + } + + function savepreregisterpatient() + { + $prm = $this->sys_input; + $userid = $this->sys_user["M_UserID"]; + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + $v = $prm['selected_patient']; + $religion = $v['selected_religion']; + + $religionCode = $religion['code']; + $religionSystem = $religion['code_system']; + $setup = $prm['setup']; + $antrian = $prm['antrian'] ? $prm['antrian'] : ''; + + $pdob = date('Y-m-d', strtotime($v['Mcu_PreregisterPatientsDOB'])); + + $Mcu_PreregisterPatientsTests = ''; + $packettests = array(); + if (count($prm['selected_packets']) > 0) { + foreach ($prm['selected_packets'] as $kp => $vp) { + array_push($packettests, $vp['code']); + } + } + if (count($prm['selected_tests']) > 0) { + foreach ($prm['selected_tests'] as $kt => $vt) { + + // jika test diambil dari dalam paket opsional + if (!empty($vt['T_PacketDetailT_PacketID'])) { + $code = $vt['code'] . "_" . $vt['T_PacketDetailT_PacketID'] . "-opt"; + array_push($packettests, $code); + } else { + array_push($packettests, $vt['code']); + } + } + } + if (count($packettests) > 0) { + $Mcu_PreregisterPatientsTests = join(',', $packettests); + } + + $sql = "SELECT * FROM m_patient WHERE M_PatientID = ?"; + $rows = $this->db_onedev->query($sql, [$v['Mcu_PreregisterPatientsM_PatientID']]); + if (!$rows) { + $message = $this->db_onedev->error(); + $message['qry'] = $this->db_onedev->last_query(); + $this->sys_error($message); + exit; + } + $dataPatientBefore = $rows->row_array(); + + /*ada permintaan tdk bisa update nama pasien dari menu preregister 210525 (diki)*/ + $query = " UPDATE mcu_preregister_patients SET + Mcu_PreregisterPatientsM_PatientID = ?, + Mcu_PreregisterPatientsKTP = ?, + Mcu_PreregisterPatientsNIP = ?, + Mcu_PreregisterPatientsPatientPrefix = ?, + Mcu_PreregisterPatientsPatientSuffix = ?, + Mcu_PreregisterPatientsEmail = ?, + Mcu_PreregisterPatientsHp = ?, + Mcu_PreregisterPatientsDOB = ?, + Mcu_PreregisterPatientsPosisi = ?, + Mcu_PreregisterPatientsDivisi = ?, + Mcu_PreregisterPatientsJob = ?, + Mcu_PreregisterPatientsLocation = ?, + Mcu_PreregisterPatientsGender = ?, + Mcu_PreregisterPatientsM_TitleID = ?, + Mcu_PreregisterPatientsCorporateCode = ?, + Mcu_PreregisterPatientsDepartment = ?, + Mcu_PreregisterPatientsReligion = ?, + Mcu_PreregisterPatientsOrders = ?, + Mcu_PreregisterPatientsUserID = ?, + Mcu_PreregisterPatientsLastUpdated = NOW() + WHERE + Mcu_PreregisterPatientsID = ?"; + //echo $query; + $rows = $this->db_onedev->query($query, [ + $v['Mcu_PreregisterPatientsM_PatientID'], + $v['Mcu_PreregisterPatientsKTP'], + $v['Mcu_PreregisterPatientsNIP'], + $v['Mcu_PreregisterPatientsPatientPrefix'], + $v['Mcu_PreregisterPatientsPatientSuffix'], + $v['Mcu_PreregisterPatientsEmail'], + $v['Mcu_PreregisterPatientsHp'], + $pdob, + $v['Mcu_PreregisterPatientsPosisi'], + $v['Mcu_PreregisterPatientsDivisi'], + $v['Mcu_PreregisterPatientsJob'], + $v['Mcu_PreregisterPatientsLocation'], + $v['Mcu_PreregisterPatientsGender'], + $v['Mcu_PreregisterPatientsM_TitleID'], + $v['Mcu_PreregisterPatientsCorporateCode'], + $v['Mcu_PreregisterPatientsDepartment'], + $religionCode, + $Mcu_PreregisterPatientsTests, + $userid, + $v['Mcu_PreregisterPatientsID'] + ]); + if (!$rows) { + $message = $this->db_onedev->error(); + $message['qry'] = $this->db_onedev->last_query(); + $this->sys_error($message); + exit; + } + + $sql_ktp = ''; + if (isset($v['Mcu_PreregisterPatientsKTP']) && $v['Mcu_PreregisterPatientsKTP'] != '') { + $sql_ktp = "M_PatientIdentifierCode = 'NNIDN', M_PatientIdentifierSystem='http://terminology.hl7.org/CodeSystem/v2-0203', M_PatientIdentifierValue = '{$v['Mcu_PreregisterPatientsKTP']}',"; + //echo $sql_ktp; + } else { + $sql_ktp = "M_PatientIdentifierCode = '', M_PatientIdentifierSystem='', M_PatientIdentifierValue = '',"; + } + $sql = "UPDATE m_patient SET + $sql_ktp + M_PatientDOB = ?, + M_PatientM_TitleID = ?, + M_PatientNIP = ?, + M_PatientGender = ?, + M_PatientPrefix = ?, + M_PatientName = ?, + M_PatientSuffix = ?, + M_PatientEmail = ?, + M_PatientHP = ?, + M_PatientDivisi = ?, + M_PatientPosisi = ?, + M_PatientLocation = ?, + M_PatientJob = ?, + M_PatientDepartement = ?, + M_PatientReligionCode = ?, + M_PatientReligionSystem = ?, + M_PatientAddressRegionalCd = ?, + M_PatientAddressState = ?, + M_PatientAddressCity = ?, + M_PatientAddressDistrict = ?, + M_PatientAddressVillage = ?, + M_PatientAddress = ?, + M_PatientLastUpdatedUserID = ?, + M_PatientLastUpdated = NOW() + WHERE + M_PatientID = ? + "; + //echo $sql; + $qry = $this->db_onedev->query($sql, [ + $pdob, + $v['Mcu_PreregisterPatientsM_TitleID'], + $v['Mcu_PreregisterPatientsNIP'], + $v['Mcu_PreregisterPatientsGender'], + $v['Mcu_PreregisterPatientsPatientPrefix'], + $v['Mcu_PreregisterPatientsPatientName'], + $v['Mcu_PreregisterPatientsPatientSuffix'], + $v['Mcu_PreregisterPatientsEmail'], + $v['Mcu_PreregisterPatientsHp'], + $v['Mcu_PreregisterPatientsDivisi'], + $v['Mcu_PreregisterPatientsPosisi'], + $v['Mcu_PreregisterPatientsLocation'], + $v['Mcu_PreregisterPatientsJob'], + $v['Mcu_PreregisterPatientsDepartment'], + $religionCode, + $religionSystem, + $prm['M_PatientAddressRegionalCd'], + $prm['M_PatientAddressState'], + $prm['M_PatientAddressCity'], + $prm['M_PatientAddressDistrict'], + $prm['M_PatientAddressVillage'], + $prm['M_PatientAddress'], + $userid, + $v['Mcu_PreregisterPatientsM_PatientID'] + ]); + if (!$qry) { + $message = $this->db_onedev->error(); + $message['qry'] = $this->db_onedev->last_query(); + $this->sys_error($message); + exit; + } + //echo $this->db_onedev->last_query(); + + if ($antrian != '' && $antrian != null) { + $sql = "INSERT INTO preregister_antrian( + PreregisterAntrianMcu_PreregisterPatientsID, + PreregisterAntrianMgm_McuID, + PreregisterAntrianM_PatientID, + PreregisterAntrianNumber, + PreregisterAntrianUserID, + PreregisterAntrianCreated + ) VALUES(?,?,?,?,?,NOW())"; + $qry = $this->db_onedev->query($sql, [ + $v['Mcu_PreregisterPatientsID'], + $v['Mcu_PreregisterPatientsMgm_McuID'], + $v['Mcu_PreregisterPatientsM_PatientID'], + $antrian, + $userid + ]); + if (!$qry) { + $message = $this->db_onedev->error(); + $message['qry'] = $this->db_onedev->last_query(); + $this->sys_error($message); + exit; + } + } + $sql = "SELECT * FROM m_patient WHERE M_PatientID = ?"; + $rows = $this->db_onedev->query($sql, [$v['Mcu_PreregisterPatientsM_PatientID']]); + if (!$rows) { + $message = $this->db_onedev->error(); + $message['qry'] = $this->db_onedev->last_query(); + $this->sys_error($message); + exit; + } + $dataPatientAfter = $rows->row_array(); + $sql = "INSERT INTO cpone_log.log_patient( + Log_PatientM_PatientID, + Log_PatientDate, + Log_PatientCode, + Log_PatientJsonBefore, + Log_PatientJsonAfter, + Log_PatientUserID) + VALUES (?, NOW(), 'EDIT', ?, ?, ?)"; + $rows = $this->db_onedev->query($sql, [ + $v['Mcu_PreregisterPatientsM_PatientID'], + json_encode($dataPatientBefore), + json_encode($dataPatientAfter), + $userid + ]); + if (!$rows) { + $message = $this->db_onedev->error(); + $message['qry'] = $this->db_onedev->last_query(); + $this->sys_error($message); + exit; + } + + $result = array( + "msg" => "ok", + "total" => 1, + "records" => $prm + ); + + $this->sys_ok($result); + exit; + } + + function checkpatientbynoid($noKtp, $pid, $corporateid) + { + // $prm = $this->sys_input; + // $userid = $this->sys_user["M_UserID"]; + // if (!$this->isLogin) { + // $this->sys_error("Invalid Token"); + // exit; + // } + // $noKtp = $prm['nik']; + // $pid = $prm['pid']; + // $corporateid = $prm['corporateid']; + $sql = "SELECT * FROM m_patient + WHERE M_PatientIdentifierCode = 'NNIDN' + AND M_PatientIdentifierValue = $noKtp + AND M_PatientIsActive = 'Y' + AND M_PatientID <> $pid + AND M_PatientRegisteredByCorporateID = $corporateid"; + $qry = $this->db_onedev->query($sql); + if (!$qry) { + $message = $this->db_onedev->error(); + $this->sys_error($message); + $message['qry'] = $this->db_onedev->last_query(); + exit; + } + $data = $qry->result_array(); + if (count($data) > 0) { + $rst = [ + + "msg" => "errpatient", + "msg_dialog" => "Pasien memiliki NIK sama dengan pasien berikut", + "data" => $data, + "total" => count($data) + ]; + $this->sys_ok($rst); + exit; + } + } + + function checkpatientbynamedob($name, $dob, $pid, $corporateid) + { + // $prm = $this->sys_input; + // $userid = $this->sys_user["M_UserID"]; + // if (!$this->isLogin) { + // $this->sys_error("Invalid Token"); + // exit; + // } + // $name = $prm['name']; + // $dob = $prm['dob']; + // $pid = $prm['pid']; + // $corporateid = $prm['corporateid']; + $sql = "SELECT * FROM m_patient + WHERE LOWER(M_PatientName)=LOWER(?) + AND M_PatientDOB= '{$dob}' + AND M_PatientIsActive = 'Y' + AND M_PatientID <> $pid + AND M_PatientRegisteredByCorporateID = $corporateid"; + $qry = $this->db_onedev->query($sql, [$name]); + if (!$qry) { + $message = $this->db_onedev->error(); + $message['qry'] = $this->db_onedev->last_query(); + $this->sys_error($message); + exit; + } + // echo $sql; + $data = $qry->result_array(); + if (count($data) > 0) { + $rst = [ + "msg" => "errpatient", + "msg_dialog" => "Pasien memiliki Nama dan Tanggal Lahir sama dengan pasien berikut", + + "data" => $data, + "total" => count($data) + ]; + $this->sys_ok($rst); + exit; + } + } + + function checkpatientbynip($nip, $pid, $corporateid) + { + // $prm = $this->sys_input; + // $userid = $this->sys_user["M_UserID"]; + // if (!$this->isLogin) { + // $this->sys_error("Invalid Token"); + // exit; + // } + // $name = $prm['name']; + // $dob = $prm['dob']; + // $pid = $prm['pid']; + // $corporateid = $prm['corporateid']; + $sql = "SELECT * FROM m_patient + WHERE M_PatientNIP= '{$nip}' + AND M_PatientIsActive = 'Y' + AND M_PatientID <> $pid + AND M_PatientRegisteredByCorporateID = $corporateid"; + $qry = $this->db_onedev->query($sql, []); + if (!$qry) { + $message = $this->db_onedev->error(); + $this->sys_error($message); + exit; + } + // echo $sql; + $data = $qry->result_array(); + if (count($data) > 0) { + $rst = [ + "msg" => "errpatient", + "msg_dialog" => "Pasien memiliki NIP sama dengan pasien berikut", + + "data" => $data, + "total" => count($data) + ]; + $this->sys_ok($rst); + exit; + } + } + + function getDetailPacketByID() + { + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + + $userid = $this->sys_user["M_UserID"]; + $prm = $this->sys_input; + + $T_PacketID = $prm['T_PacketID']; + + // SELECT test.T_TestID as id, test.T_TestName as name, test.T_TestSasCode as code, nat_test,ss_price_mou.T_PriceTotal as price + $sql = "SELECT + T_PacketName, + T_PacketSasCode, + pd.T_PacketDetailID, + pd.T_PacketDetailT_PacketID, + pd.T_PacketDetailOriginalPrice, + pd.T_PacketDetailPrice as price, + T_PacketDetailPrice, + t.T_TestID as id, + t.T_TestParentT_TestID, + t.T_TestSasCode as code, + t.T_TestName as name, + t.T_TestNat_TestID, + T_PacketPrice, + T_PacketOriginalPrice + FROM t_packetdetail as pd + JOIN t_packet ON pd.T_PacketDetailT_PacketID = T_PacketID + JOIN t_test as t ON pd.T_PacketDetailT_TestID = t.T_TestID + AND t.T_TestIsActive = 'Y' + AND pd.T_PacketDetailIsActive = 'Y' + AND pd.T_PacketDetailT_PacketID = $T_PacketID"; + + $qry = $this->db_onedev->query($sql); + if (!$qry) { + $this->sys_error_db("list packet", $this->db_onedev); + exit; + } + + $rows = $qry->result_array(); + + foreach ($rows as $key => $obj) { + $sql_nattest = "WITH RECURSIVE TestNatTest AS ( + SELECT + T_TestID, + T_TestCode, + T_TestSasCode, + T_TestName, + T_TestNat_TestID + FROM t_test + WHERE T_TestIsActive = 'Y' AND T_TestParentT_TestID = ? + UNION ALL + SELECT + t.T_TestID, + t.T_TestCode, + t.T_TestSasCode, + t.T_TestName, + t.T_TestNat_TestID + FROM t_test t + INNER JOIN TestNatTest tnt ON t.T_TestParentT_TestID = tnt.T_TestID + WHERE T_TestIsActive = 'Y' + ) + + SELECT + T_TestID, + T_TestNat_TestID + FROM TestNatTest + ORDER BY T_TestNat_TestID"; + $que_nattest = $this->db_onedev->query($sql_nattest, [$obj['T_TestID']]); + if (!$que_nattest) { + $this->sys_error_db("[Error] get nat_test per pemeriksaan"); + exit; + } + $data = $que_nattest->result_array(); + $nattest = array_column($data, 'T_TestNat_TestID'); + array_unshift($nattest, $obj['T_TestNat_TestID']); + + $rows[$key]['nat_test'] = $nattest; + } + + $result = array( + // "total" => $tot_page, + "records" => $rows, + ); + $this->sys_ok($result); + exit; + } + function getpreregistermenu() + { + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + + $sql = "SELECT S_MenuUrl as url FROM s_menu + WHERE S_MenuName = 'Registration (Preregister)' LIMIT 1"; + $qry = $this->db_onedev->query($sql); + if (!$qry) { + $message = $this->db_onedev->error(); + $this->sys_error($message); + exit; + } + $data = $qry->row(); + + $this->sys_ok($data); + exit; + } + function lookup_barcodes() + { + try { + $prm = $this->sys_input; + //# cek token valid + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + /*$sql = "SELECT T_BarcodeLabID as id, + 'barcode' as type, + T_SampleTypeID, + T_BarcodeLabID, + T_BarcodeLabBarcode, + T_BarcodeLabT_OrderHeaderID as orderid, + T_BarcodeLabCounter, + T_SampleTypeName, + 'N' as chex + FROM t_barcodelab + JOIN t_sampletype ON T_BarcodeLabT_SampleTypeID = T_SampleTypeID + JOIN t_bahan ON T_SampleTypeT_BahanID = T_BahanID + JOIN t_samplestation ON T_BahanT_SampleStationID = T_SampleStationID AND T_SampleStationIsNonLab = '' + WHERE + T_BarcodeLabT_OrderHeaderID = {$prm['T_OrderHeaderID']} AND T_BarcodeLabIsActive = 'Y' + UNION + SELECT T_TestID as id, + 'nonlab' as type, + T_OrderDetailID as T_SampleTypeID, + '' as T_BarcodeLabID, + '-' as T_BarcodeLabBarcode, + T_OrderHeaderID as order_id, + 0 as T_BarcodeLabCounter, + T_TestName as T_SampleTypeName, + 'N' as chex + FROM t_orderheader + JOIN t_orderdetail ON T_OrderDetailT_OrderHeaderID = T_OrderHeaderID AND T_OrderDetailIsActive = 'Y' + JOIN t_test ON T_OrderDetailT_TestID = T_TestID AND T_TestIsResult = 'Y' + JOIN group_resultdetail ON Group_ResultDetailT_TestID = T_TestID + JOIN group_result ON Group_ResultDetailGroup_ResultID = Group_ResultID AND Group_ResultName <> 'lab' + WHERE + T_OrderHeaderID = {$prm['T_OrderHeaderID']} + GROUP BY T_TestID + + ";*/ + $sql = "SELECT T_BarcodeLabID as id, + 'barcode' as type, + T_SampleTypeID, + T_BarcodeLabID, + T_BarcodeLabBarcode, + T_BarcodeLabT_OrderHeaderID as orderid, + T_BarcodeLabCounter, + T_SampleTypeName, + 'N' as chex + FROM t_barcodelab + JOIN t_sampletype ON T_BarcodeLabT_SampleTypeID = T_SampleTypeID + JOIN t_bahan ON T_SampleTypeT_BahanID = T_BahanID + JOIN t_samplestation ON T_BahanT_SampleStationID = T_SampleStationID AND T_SampleStationIsNonLab = '' + WHERE + T_BarcodeLabT_OrderHeaderID = {$prm['T_OrderHeaderID']} AND T_BarcodeLabIsActive = 'Y' + UNION + SELECT T_TestID as id, + 'nonlab' as type, + T_OrderDetailID as T_SampleTypeID, + '' as T_BarcodeLabID, + '-' as T_BarcodeLabBarcode, + T_OrderHeaderID as order_id, + 0 as T_BarcodeLabCounter, + T_TestName as T_SampleTypeName, + 'N' as chex + FROM t_orderheader + JOIN t_orderdetail ON T_OrderDetailT_OrderHeaderID = T_OrderHeaderID AND T_OrderDetailIsActive = 'Y' + JOIN t_test ON T_OrderDetailT_TestID = T_TestID AND T_TestIsResult = 'Y' + JOIN group_resultdetail ON Group_ResultDetailT_TestID = T_TestID + JOIN group_result ON Group_ResultDetailGroup_ResultID = Group_ResultID AND + ( Group_ResultName <> 'lab' AND Group_ResultName <> 'Papsmear') AND + Group_ResultFlagNonLab = 'Y' + WHERE + T_OrderHeaderID = {$prm['T_OrderHeaderID']} + GROUP BY T_TestID + UNION + SELECT T_TestID as id, + 'nonlab' as type, + T_OrderDetailID as T_SampleTypeID, + '' as T_BarcodeLabID, + '-' as T_BarcodeLabBarcode, + T_OrderHeaderID as order_id, + 0 as T_BarcodeLabCounter, + T_TestName as T_SampleTypeName, + 'N' as chex + FROM t_orderheader + JOIN t_orderdetail ON T_OrderDetailT_OrderHeaderID = T_OrderHeaderID AND T_OrderDetailIsActive = 'Y' + JOIN t_test ON T_OrderDetailT_TestID = T_TestID AND T_TestIsResult = 'Y' + JOIN group_resultdetail ON Group_ResultDetailT_TestID = T_TestID + JOIN group_result ON Group_ResultDetailGroup_ResultID = Group_ResultID AND ( Group_ResultName = 'Papsmear') + WHERE + T_OrderHeaderID = {$prm['T_OrderHeaderID']} + GROUP BY Group_ResultID + + "; + //echo $sql; + $query = $this->db_onedev->query($sql); + if (!$query) { + $message = $this->db_onedev->error(); + $message['qry'] = $this->db_onedev->last_query(); + $this->sys_error($message); + exit; + } + $rows = $query->result_array(); + $rst = []; + if ($rows) { + foreach ($rows as $k => $v) { + $v['chex'] = false; + // if ($v['chex'] == 'N') + // $rows[$k]['chex'] = false; + // else + // $rows[$k]['chex'] = true; + $v['count'] = 1; + $rst[] = $v; + // if ($v['type'] == 'barcode' && $v['T_SampleTypeID'] == 48) { + // $rst[] = $v; + // } + } + } + $result = array("total" => 0, "records" => $rst); + $this->sys_ok($result); + } catch (Exception $exc) { + $message = $exc->getMessage(); + $this->sys_error($message); + } + } + function getcorporate() + { + try { + $prm = $this->sys_input; + //# cek token valid + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + $code = $prm['code']; + + + $sql = "SELECT + CorporateID, + CorporateCode, + CorporateName, + 'Y' AS CorporateDefault + FROM corporate + WHERE (CorporateID = ?);"; + //echo $sql; + $query = $this->db_onedev->query($sql, [$code]); + if (!$query) { + $message = $this->db_onedev->error(); + $message['qry'] = $this->db_onedev->last_query(); + $this->sys_error($message); + exit; + } + $corporate = $query->row_array(); + $sql = "SELECT + CorporateID, + CorporateCode, + CorporateName, + 'N' AS CorporateDefault + FROM corporate_relation + JOIN corporate ON CorporateRelationCorporateRelationID = CorporateID + AND CorporateRelationIsActive = 'Y' + AND CorporateRelationCorporateID = ?"; + //echo $sql; + $query = $this->db_onedev->query($sql, [$corporate['CorporateID']]); + if (!$query) { + $message = $this->db_onedev->error(); + $message['qry'] = $this->db_onedev->last_query(); + $this->sys_error($message); + exit; + } + $relation = $query->result_array(); + // $rows = [$corporate,...$relation] + $rows = [$corporate]; + for ($i = 0; $i < count($relation); $i++) { + array_push($rows, $relation[$i]); + } + $result = array("records" => $rows); + $this->sys_ok($result); + } catch (Exception $exc) { + $message = $exc->getMessage(); + $this->sys_error($message); + } + } + function search_patient_form() + { + $prm = $this->sys_input; + $name = $prm['name']; + $dob = $prm['dob']; + $nik = $prm['nik']; + $ktp = $prm['ktp']; + $add_where = ''; + // M_PatientIdentifierCode varchar(50) [NNIDN] + // M_PatientIdentifierSystem varchar(100) [http://terminology.hl7.org/CodeSystem/v2-0203] + // M_PatientIdentifierCode varchar(50) [NNIDN] + if ($nik != '') { + $add_where .= " AND M_PatientNIP = '{$nik}'"; + } + if ($ktp != '') { + $add_where .= " AND M_PatientIdentifierValue = '{$nik}' AND M_PatientIdentifierCode = 'NNIDN'"; + } + $setup = $prm['setup']; + $join_company = ""; + if (isset($prm['company']) && intval($prm['company']) > 0) { + $join_company = "JOIN t_orderheader ON T_OrderHeaderM_PatientID = M_PatientID AND + T_OrderHeaderIsActive = 'Y' AND + T_OrderHeaderM_CompanyID = {$prm['company']}"; + } + + $sql = "SELECT COUNT(*) as total + FROM ( + SELECT * + FROM m_patient + $join_company + LEFT JOIN m_title ON M_PatientM_TitleID = M_TitleID + WHERE + M_PatientIsActive = 'Y' + AND M_PatientRegisteredByCorporateID = {$setup['Mgm_McuCorporateID']} + AND M_PatientName LIKE CONCAT('%',?,'%') AND + (DATE_FORMAT(M_PatientDOB, '%d-%m-%Y') LIKE '%{$dob}%' and M_PatientDOB IS NOT NULL) + {$add_where} + GROUP BY M_PatientID + ) x + "; + //echo $sql; + $qry = $this->db_onedev->query($sql, [$name]); + if (!$qry) { + $message = $this->db_onedev->error(); + $message['qry'] = $this->db_onedev->last_query(); + $this->sys_error($message); + exit; + } + $countx = $qry->row()->total; + $sql = "SELECT *, DATE_FORMAT(M_PatientDOB, '%d-%m-%Y') as dob_ina, + M_PatientNoReg as Mcu_PreregisterPatientsPID, + M_PatientIdentifierValue as Mcu_PreregisterPatientsKTP, + M_PatientID as Mcu_PreregisterPatientsM_PatientID, + M_TitleID as Mcu_PreregisterPatientsM_TitleID, + M_PatientName as Mcu_PreregisterPatientsPatientName, + M_PatientGender as Mcu_PreregisterPatientsGender, + M_PatientDOB as Mcu_PreregisterPatientsDOB, + IFNULL(M_PatientReligionCode,0) as Mcu_PreregisterPatientsReligion, + M_PatientEmail as Mcu_PreregisterDetailsEmail, + M_PatientHP as Mcu_PreregisterDetailsHp, + M_PatientEmail as Mcu_PreregisterPatientsEmail, + M_PatientHP as Mcu_PreregisterPatientsHp, + M_PatientPosisi as Mcu_PreregisterPatientsPosisi, + M_PatientDivisi as Mcu_PreregisterPatientsDivisi, + M_PatientJob as Mcu_PreregisterPatientsJob, + M_PatientDepartement as Mcu_PreregisterPatientsDepartment + FROM m_patient + $join_company + LEFT join m_title on M_PatientM_TitleID = M_TitleID + WHERE + M_PatientRegisteredByCorporateID = {$setup['Mgm_McuCorporateID']} + AND M_PatientIsActive = 'Y' AND + M_PatientName LIKE CONCAT('%',?,'%') AND + (DATE_FORMAT(M_PatientDOB, '%d-%m-%Y') LIKE '%{$dob}%' and M_PatientDOB IS NOT NULL) + {$add_where} + GROUP BY M_PatientID + LIMIT 10 OFFSET 0 + "; + + //echo $sql; + $qry = $this->db_onedev->query($sql, [$name]); + if (!$qry) { + $message = $this->db_onedev->error(); + $message['qry'] = $this->db_onedev->last_query(); + $this->sys_error($message); + exit; + } + $rows = $qry->result_array(); + $result = array( + "total" => $countx, + "records" => $rows + ); + $this->sys_ok($result); + exit; + } + + function getdetailcorporate() + { + try { + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + + $prm = $this->sys_input; + + $sql = "SELECT corporate.*, IFNULL(display, '') as countryName + FROM corporate + LEFT JOIN terminology ON attribute_path = 'Address.country' AND code = CorporateAddressCountry + WHERE + CorporateID = ?"; + $qry = $this->db_onedev->query($sql, array($prm['Mgm_McuCorporateID'])); + + if (!$qry) { + $this->sys_error_db("search corporate select error", $this->db_onedev); + exit; + } + + $row = $qry->row_array(); + if (!$row) { + # code... + } + $result = array( + "records" => $row + ); + + $this->sys_ok($result); + } catch (Exception $exc) { + $message = $exc->getMessage(); + $this->sys_error($message); + } + } + + public function searchwilayah() + { + try { + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + + $prm = $this->sys_input; + $search = $prm['search']; + + $sql = "SELECT + r.regional_cd, + r.regional_cd AS id, + r.regional_nm, + r.full_name AS text_nm, + r.pro_cd, IFNULL(pro.regional_nm,'') AS pro_nm, + r.kab_cd, IFNULL(kab.regional_nm,'') AS kab_nm, + r.kec_cd, IFNULL(kec.regional_nm,'') AS kec_nm, + r.kel_cd, IFNULL(kel.regional_nm,'') AS kel_nm, + r.status_cd, r.old_nm + FROM regional r + LEFT JOIN regional pro ON CONCAT(r.pro_cd, REPEAT('0', 8)) = pro.regional_cd + LEFT JOIN regional kab ON CONCAT(r.pro_cd, r.kab_cd, REPEAT('0', 6)) = kab.regional_cd + LEFT JOIN regional kec ON CONCAT(r.pro_cd, r.kab_cd, r.kec_cd, REPEAT('0', 3)) = kec.regional_cd + LEFT JOIN regional kel ON CONCAT(r.pro_cd, r.kab_cd, r.kec_cd, r.kel_cd) = kel.regional_cd + WHERE + r.full_name LIKE CONCAT('%','{$search}','%') + LIMIT 100 + "; + + $qry = $this->db_onedev->query($sql); + + if (!$qry) { + $this->sys_error_db("search wilayah select error", $this->db_onedev); + exit; + } + + $rows = $qry->result_array(); + + $result = array( + "records" => $rows, + "sql" => $this->db_onedev->last_query() + ); + + $this->sys_ok($result); + } catch (Exception $exc) { + $message = $exc->getMessage(); + $this->sys_error($message); + } + } + + public function getCountryTerminology() + { + try { + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + + $prm = $this->sys_input; + + $sql = "SELECT + code, + display + FROM + terminology + WHERE status_cd = 'normal' + AND attribute_path = 'Address.country' + AND resource_type = 'Address' + AND `code` = 'ID' + AND code_system = 'urn:iso:std:iso:3166' + "; + + $qry = $this->db_onedev->query($sql); + + if (!$qry) { + $this->sys_error_db("terminology select error", $this->db_onedev); + exit; + } + + $rows = $qry->result_array(); + + $result = array( + "records" => $rows, + "sql" => $this->db_onedev->last_query() + ); + + $this->sys_ok($result); + } catch (Exception $exc) { + $message = $exc->getMessage(); + $this->sys_error($message); + } + } + + public function getCountryTerminologySelected() + { + try { + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + + $prm = $this->sys_input; + + $sql = "SELECT + resource_type, + attribute_path, + code, + parent_code, + display, + display_en, + code_system, + order_no, + ft_index, + use_ind, + description, + status_cd, + created_dttm, + created_user_id, + updated_dttm, + updated_user_id, + nullified_dttm, + nullified_user_id + FROM + terminology + WHERE status_cd = 'normal' + AND attribute_path = 'Address.country' + AND resource_type = 'Address' + AND code_system = 'urn:iso:std:iso:3166' + AND + "; + + $qry = $this->db_onedev->query($sql); + + if (!$qry) { + $this->sys_error_db("terminology select error", $this->db_onedev); + exit; + } + + $rows = $qry->result_array(); + + $result = array( + "records" => $rows, + "sql" => $this->db_onedev->last_query() + ); + + $this->sys_ok($result); + } catch (Exception $exc) { + $message = $exc->getMessage(); + $this->sys_error($message); + } + } +} diff --git a/scripts/sql/2026-05-11_create_sp_refresh_mcu_participant_daily_by_preregister.sql b/scripts/sql/2026-05-11_create_sp_refresh_mcu_participant_daily_by_preregister.sql new file mode 100644 index 0000000..854b4f0 --- /dev/null +++ b/scripts/sql/2026-05-11_create_sp_refresh_mcu_participant_daily_by_preregister.sql @@ -0,0 +1,92 @@ +DROP PROCEDURE IF EXISTS cpone.sp_refresh_mcu_participant_daily_by_preregister; +DELIMITER $$ +CREATE PROCEDURE cpone.sp_refresh_mcu_participant_daily_by_preregister( + IN p_preregister_id INT +) +main_block: BEGIN + DECLARE v_mgm_mcuid INT DEFAULT 0; + DECLARE v_schedule_date DATE DEFAULT NULL; + DECLARE v_participant_daily_id INT DEFAULT 0; + DECLARE v_mcu_patient_id INT DEFAULT 0; + DECLARE v_total_participant INT DEFAULT 0; + + SELECT + pp.Mcu_PreregisterPatientsMgm_McuID, + pd.Mcu_PreregisterDateCheckinSchedule + INTO v_mgm_mcuid, v_schedule_date + FROM cpone.mcu_preregister_patients pp + JOIN cpone.mcu_preregister_date pd + ON pd.Mcu_PreregisterDateMcu_PreregisterPatientsID = pp.Mcu_PreregisterPatientsID + AND pd.Mcu_PreregisterDateIsActive = 'Y' + WHERE pp.Mcu_PreregisterPatientsID = p_preregister_id + LIMIT 1; + + IF v_mgm_mcuid = 0 OR v_schedule_date IS NULL THEN + SELECT 0 AS participant_daily_id, 0 AS total_participant, 0 AS total_details; + LEAVE main_block; + END IF; + + START TRANSACTION; + + INSERT INTO cpone_dashboard.mcu_patient_schedule ( + Mcu_PatientSchedulePreregisterID, + Mcu_PatientScheduleDate, + Mcu_PatientScheduleIsActive, + Mcu_PatientScheduleSyncedAt + ) VALUES (p_preregister_id, v_schedule_date, 'Y', NOW()) + ON DUPLICATE KEY UPDATE + Mcu_PatientScheduleIsActive = 'Y', + Mcu_PatientScheduleSyncedAt = NOW(); + + SELECT COUNT(DISTINCT pp.Mcu_PreregisterPatientsID) + INTO v_total_participant + FROM cpone.mcu_preregister_patients pp + JOIN cpone.mcu_preregister_date pd + ON pd.Mcu_PreregisterDateMcu_PreregisterPatientsID = pp.Mcu_PreregisterPatientsID + AND pd.Mcu_PreregisterDateCheckinSchedule = v_schedule_date + AND pd.Mcu_PreregisterDateIsActive = 'Y' + WHERE pp.Mcu_PreregisterPatientsMgm_McuID = v_mgm_mcuid + AND pp.Mcu_PreregisterPatientsIsActive = 'Y'; + + INSERT INTO cpone_dashboard.mcu_participant_daily ( + Mcu_ParticipantDailyMcuID, + Mcu_ParticipantDailyDate, + Mcu_ParticipantDailyTotal, + Mcu_ParticipantDailyIsActive + ) VALUES (v_mgm_mcuid, v_schedule_date, v_total_participant, 'Y') + ON DUPLICATE KEY UPDATE + Mcu_ParticipantDailyID = LAST_INSERT_ID(Mcu_ParticipantDailyID), + Mcu_ParticipantDailyTotal = v_total_participant, + Mcu_ParticipantDailyIsActive = 'Y'; + + SET v_participant_daily_id = LAST_INSERT_ID(); + + SELECT Mcu_PatientID + INTO v_mcu_patient_id + FROM cpone_dashboard.mcu_patient + WHERE Mcu_PatientPreregisterID = p_preregister_id + AND Mcu_PatientMcuID = v_mgm_mcuid + LIMIT 1; + + IF v_participant_daily_id > 0 AND v_mcu_patient_id > 0 THEN + INSERT INTO cpone_dashboard.mcu_participant_daily_details ( + Mcu_ParticipantDailyDetailsParticipantDailyID, + Mcu_ParticipantDailyDetailsMcu_PatientID, + Mcu_ParticipantDailyDetailsIsActive + ) VALUES (v_participant_daily_id, v_mcu_patient_id, 'Y') + ON DUPLICATE KEY UPDATE + Mcu_ParticipantDailyDetailsIsActive = 'Y'; + END IF; + + COMMIT; + + SELECT + v_participant_daily_id AS participant_daily_id, + v_total_participant AS total_participant, + ( + SELECT COUNT(*) + FROM cpone_dashboard.mcu_participant_daily_details d + WHERE d.Mcu_ParticipantDailyDetailsParticipantDailyID = v_participant_daily_id + ) AS total_details; +END main_block$$ +DELIMITER ;