From 8e4a9f50b5ef900e56727495d43315c63b926f4f Mon Sep 17 00:00:00 2001 From: "sas.fajri" Date: Tue, 5 May 2026 09:41:04 +0700 Subject: [PATCH] Add MCU participant daily refresh SP --- .../cpone/mcuoffline/Preregisterv3.php | 18 ++++ ...reate_sp_refresh_mcu_participant_daily.sql | 82 +++++++++++++++++++ ...026-05-05_create_sp_upsert_mcu_patient.sql | 57 +++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 scripts/sql/2026-05-05_create_sp_refresh_mcu_participant_daily.sql create mode 100644 scripts/sql/2026-05-05_create_sp_upsert_mcu_patient.sql diff --git a/application/controllers/cpone/mcuoffline/Preregisterv3.php b/application/controllers/cpone/mcuoffline/Preregisterv3.php index 0e58727..2e6ab86 100644 --- a/application/controllers/cpone/mcuoffline/Preregisterv3.php +++ b/application/controllers/cpone/mcuoffline/Preregisterv3.php @@ -569,6 +569,24 @@ class Preregisterv3 extends MY_Controller $this->sys_error("update mcu_preregister_patients : ".$last_qry); exit; } + $qry_sync_patient = $this->db->query( + "CALL cpone.sp_upsert_mcu_patient_by_preregister_id(?)", + array($last_id_x) + ); + if (!$qry_sync_patient) { + $last_qry = $this->db->last_query(); + $this->db->trans_rollback(); + $this->sys_error("call sp_upsert_mcu_patient_by_preregister_id : " . $last_qry); + exit; + } + if (isset($this->db->conn_id) && $this->db->conn_id instanceof mysqli) { + while ($this->db->conn_id->more_results() && $this->db->conn_id->next_result()) { + $dummyResult = $this->db->conn_id->store_result(); + if ($dummyResult instanceof mysqli_result) { + $dummyResult->free(); + } + } + } } } } diff --git a/scripts/sql/2026-05-05_create_sp_refresh_mcu_participant_daily.sql b/scripts/sql/2026-05-05_create_sp_refresh_mcu_participant_daily.sql new file mode 100644 index 0000000..d075e50 --- /dev/null +++ b/scripts/sql/2026-05-05_create_sp_refresh_mcu_participant_daily.sql @@ -0,0 +1,82 @@ +DROP PROCEDURE IF EXISTS cpone.sp_refresh_mcu_participant_daily_by_mcu_date; +DELIMITER $$ +CREATE PROCEDURE cpone.sp_refresh_mcu_participant_daily_by_mcu_date( + IN p_mgm_mcuid INT, + IN p_schedule_date DATE +) +BEGIN + DECLARE v_participant_daily_id INT DEFAULT 0; + + START TRANSACTION; + + DELETE d + FROM cpone_dashboard.mcu_participant_daily_details d + INNER JOIN cpone_dashboard.mcu_participant_daily h + ON h.Mcu_ParticipantDailyID = d.Mcu_ParticipantDailyDetailsParticipantDailyID + WHERE h.Mcu_ParticipantDailyMcuID = p_mgm_mcuid + AND h.Mcu_ParticipantDailyDate = p_schedule_date; + + DELETE FROM cpone_dashboard.mcu_participant_daily + WHERE Mcu_ParticipantDailyMcuID = p_mgm_mcuid + AND Mcu_ParticipantDailyDate = p_schedule_date; + + INSERT INTO cpone_dashboard.mcu_participant_daily ( + Mcu_ParticipantDailyMcuID, + Mcu_ParticipantDailyDate, + Mcu_ParticipantDailyTotal, + Mcu_ParticipantDailyIsActive + ) + SELECT + p_mgm_mcuid, + p_schedule_date, + COUNT(DISTINCT pp.Mcu_PreregisterPatientsID) AS total_participant, + 'Y' AS Mcu_ParticipantDailyIsActive + FROM cpone.mcu_preregister_patients pp + INNER JOIN cpone.mcu_preregister_date pd + ON pd.Mcu_PreregisterDateMcu_PreregisterPatientsID = pp.Mcu_PreregisterPatientsID + AND pd.Mcu_PreregisterDateCheckinSchedule = p_schedule_date + AND pd.Mcu_PreregisterDateIsActive = 'Y' + WHERE pp.Mcu_PreregisterPatientsMgm_McuID = p_mgm_mcuid + AND pp.Mcu_PreregisterPatientsIsActive = 'Y' + HAVING COUNT(DISTINCT pp.Mcu_PreregisterPatientsID) > 0; + + SET v_participant_daily_id = LAST_INSERT_ID(); + + IF v_participant_daily_id > 0 THEN + INSERT INTO cpone_dashboard.mcu_participant_daily_details ( + Mcu_ParticipantDailyDetailsParticipantDailyID, + Mcu_ParticipantDailyDetailsMcu_PatientID, + Mcu_ParticipantDailyDetailsIsActive + ) + SELECT + v_participant_daily_id, + mp.Mcu_PatientID, + 'Y' + FROM cpone.mcu_preregister_patients pp + INNER JOIN cpone.mcu_preregister_date pd + ON pd.Mcu_PreregisterDateMcu_PreregisterPatientsID = pp.Mcu_PreregisterPatientsID + AND pd.Mcu_PreregisterDateCheckinSchedule = p_schedule_date + AND pd.Mcu_PreregisterDateIsActive = 'Y' + INNER JOIN cpone_dashboard.mcu_patient mp + ON mp.Mcu_PatientPreregisterID = pp.Mcu_PreregisterPatientsID + AND mp.Mcu_PatientMcuID = p_mgm_mcuid + WHERE pp.Mcu_PreregisterPatientsMgm_McuID = p_mgm_mcuid + AND pp.Mcu_PreregisterPatientsIsActive = 'Y'; + END IF; + + COMMIT; + + SELECT + v_participant_daily_id AS participant_daily_id, + ( + SELECT IFNULL(MAX(h.Mcu_ParticipantDailyTotal), 0) + FROM cpone_dashboard.mcu_participant_daily h + WHERE h.Mcu_ParticipantDailyID = v_participant_daily_id + ) 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$$ +DELIMITER ; diff --git a/scripts/sql/2026-05-05_create_sp_upsert_mcu_patient.sql b/scripts/sql/2026-05-05_create_sp_upsert_mcu_patient.sql new file mode 100644 index 0000000..f1c155a --- /dev/null +++ b/scripts/sql/2026-05-05_create_sp_upsert_mcu_patient.sql @@ -0,0 +1,57 @@ +DROP PROCEDURE IF EXISTS cpone.sp_upsert_mcu_patient_by_preregister_id; +DELIMITER $$ +CREATE PROCEDURE cpone.sp_upsert_mcu_patient_by_preregister_id(IN p_preregister_id INT) +BEGIN + INSERT INTO cpone_dashboard.mcu_patient ( + Mcu_PatientPreregisterID, + Mcu_PatientMcuID, + Mcu_PatientName, + Mcu_PatientNIP, + Mcu_PatientGender, + Mcu_PatientDOB, + Mcu_PatientDepartment, + Mcu_PatientDivision, + Mcu_PatientPosisi, + Mcu_PatientIsRegistered, + Mcu_PatientOrderID, + Mcu_PatientIsActive, + Mcu_PatientSyncedAt + ) + SELECT + pp.Mcu_PreregisterPatientsID, + pp.Mcu_PreregisterPatientsMgm_McuID, + pp.Mcu_PreregisterPatientsPatientName, + pp.Mcu_PreregisterPatientsNIP, + pp.Mcu_PreregisterPatientsGender, + pp.Mcu_PreregisterPatientsDOB, + pp.Mcu_PreregisterPatientsDepartment, + pp.Mcu_PreregisterPatientsDivisi, + pp.Mcu_PreregisterPatientsPosisi, + pp.Mcu_PreregisterPatientsIsRegistered, + CASE + WHEN pp.Mcu_PreregisterPatientsIsRegistered = 'Y' + AND IFNULL(pp.Mcu_PreregisterPatientsT_OrderHeaderID, 0) > 0 + THEN pp.Mcu_PreregisterPatientsT_OrderHeaderID + ELSE NULL + END AS Mcu_PatientOrderID, + pp.Mcu_PreregisterPatientsIsActive AS Mcu_PatientIsActive, + NOW() AS Mcu_PatientSyncedAt + FROM cpone.mcu_preregister_patients pp + WHERE pp.Mcu_PreregisterPatientsID = p_preregister_id + ON DUPLICATE KEY UPDATE + Mcu_PatientMcuID = VALUES(Mcu_PatientMcuID), + Mcu_PatientName = VALUES(Mcu_PatientName), + Mcu_PatientNIP = VALUES(Mcu_PatientNIP), + Mcu_PatientGender = VALUES(Mcu_PatientGender), + Mcu_PatientDOB = VALUES(Mcu_PatientDOB), + Mcu_PatientDepartment = VALUES(Mcu_PatientDepartment), + Mcu_PatientDivision = VALUES(Mcu_PatientDivision), + Mcu_PatientPosisi = VALUES(Mcu_PatientPosisi), + Mcu_PatientIsRegistered = VALUES(Mcu_PatientIsRegistered), + Mcu_PatientOrderID = VALUES(Mcu_PatientOrderID), + Mcu_PatientIsActive = VALUES(Mcu_PatientIsActive), + Mcu_PatientSyncedAt = NOW(); + + SELECT ROW_COUNT() AS affected_rows; +END$$ +DELIMITER ;