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 index 854b4f0..6f7026c 100644 --- 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 @@ -26,8 +26,17 @@ main_block: BEGIN LEAVE main_block; END IF; + -- Lookup mcu_patient_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; + START TRANSACTION; + -- Upsert mcu_patient_schedule untuk tanggal aktif INSERT INTO cpone_dashboard.mcu_patient_schedule ( Mcu_PatientSchedulePreregisterID, Mcu_PatientScheduleDate, @@ -38,6 +47,7 @@ main_block: BEGIN Mcu_PatientScheduleIsActive = 'Y', Mcu_PatientScheduleSyncedAt = NOW(); + -- Hitung ulang total participant untuk MCU + tanggal aktif SELECT COUNT(DISTINCT pp.Mcu_PreregisterPatientsID) INTO v_total_participant FROM cpone.mcu_preregister_patients pp @@ -48,6 +58,7 @@ main_block: BEGIN WHERE pp.Mcu_PreregisterPatientsMgm_McuID = v_mgm_mcuid AND pp.Mcu_PreregisterPatientsIsActive = 'Y'; + -- Upsert mcu_participant_daily dan ambil ID-nya INSERT INTO cpone_dashboard.mcu_participant_daily ( Mcu_ParticipantDailyMcuID, Mcu_ParticipantDailyDate, @@ -61,25 +72,103 @@ main_block: BEGIN 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; - + -- Upsert mcu_participant_daily_details untuk pasien ini (delete + insert) IF v_participant_daily_id > 0 AND v_mcu_patient_id > 0 THEN + DELETE FROM cpone_dashboard.mcu_participant_daily_details + WHERE Mcu_ParticipantDailyDetailsParticipantDailyID = v_participant_daily_id + AND Mcu_ParticipantDailyDetailsMcu_PatientID = v_mcu_patient_id; + 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'; + ) VALUES (v_participant_daily_id, v_mcu_patient_id, 'Y'); END IF; COMMIT; + -- Cleanup tanggal lama yang berbeda dari tanggal aktif dan belum checkin + cleanup_block: BEGIN + DECLARE v_old_date DATE; + DECLARE v_old_daily_id INT DEFAULT 0; + DECLARE v_old_total INT DEFAULT 0; + DECLARE v_checkin_count INT DEFAULT 0; + DECLARE done INT DEFAULT 0; + DECLARE cur_old_dates CURSOR FOR + SELECT Mcu_PatientScheduleDate + FROM cpone_dashboard.mcu_patient_schedule + WHERE Mcu_PatientSchedulePreregisterID = p_preregister_id + AND Mcu_PatientScheduleDate != v_schedule_date; + DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; + + OPEN cur_old_dates; + old_date_loop: LOOP + SET done = 0; + FETCH cur_old_dates INTO v_old_date; + IF done = 1 THEN LEAVE old_date_loop; END IF; + + -- Cek apakah sudah checkin di tanggal lama + SELECT COUNT(*) + INTO v_checkin_count + FROM cpone_dashboard.mcu_checkinout + WHERE Mcu_CheckinoutPreregisterID = p_preregister_id + AND Mcu_CheckinoutDate = v_old_date + AND Mcu_CheckinoutIsActive = 'Y'; + + IF v_checkin_count = 0 THEN + START TRANSACTION; + + -- Ambil daily_id untuk tanggal lama + SET v_old_daily_id = 0; + SELECT Mcu_ParticipantDailyID + INTO v_old_daily_id + FROM cpone_dashboard.mcu_participant_daily + WHERE Mcu_ParticipantDailyMcuID = v_mgm_mcuid + AND Mcu_ParticipantDailyDate = v_old_date + LIMIT 1; + + -- Hapus detail pasien ini dari tanggal lama + IF v_old_daily_id > 0 AND v_mcu_patient_id > 0 THEN + DELETE FROM cpone_dashboard.mcu_participant_daily_details + WHERE Mcu_ParticipantDailyDetailsParticipantDailyID = v_old_daily_id + AND Mcu_ParticipantDailyDetailsMcu_PatientID = v_mcu_patient_id; + END IF; + + -- Hitung sisa total untuk tanggal lama + SELECT COUNT(DISTINCT s.Mcu_PatientSchedulePreregisterID) + INTO v_old_total + FROM cpone_dashboard.mcu_patient_schedule s + JOIN cpone.mcu_preregister_patients pp + ON pp.Mcu_PreregisterPatientsID = s.Mcu_PatientSchedulePreregisterID + WHERE s.Mcu_PatientScheduleDate = v_old_date + AND s.Mcu_PatientSchedulePreregisterID != p_preregister_id + AND s.Mcu_PatientScheduleIsActive = 'Y' + AND pp.Mcu_PreregisterPatientsMgm_McuID = v_mgm_mcuid + AND pp.Mcu_PreregisterPatientsIsActive = 'Y'; + + IF v_old_total > 0 THEN + UPDATE cpone_dashboard.mcu_participant_daily + SET Mcu_ParticipantDailyTotal = v_old_total + WHERE Mcu_ParticipantDailyMcuID = v_mgm_mcuid + AND Mcu_ParticipantDailyDate = v_old_date; + ELSE + DELETE FROM cpone_dashboard.mcu_participant_daily + WHERE Mcu_ParticipantDailyMcuID = v_mgm_mcuid + AND Mcu_ParticipantDailyDate = v_old_date; + END IF; + + -- Hapus dari mcu_patient_schedule untuk tanggal lama + DELETE FROM cpone_dashboard.mcu_patient_schedule + WHERE Mcu_PatientSchedulePreregisterID = p_preregister_id + AND Mcu_PatientScheduleDate = v_old_date; + + COMMIT; + END IF; + + END LOOP old_date_loop; + CLOSE cur_old_dates; + END cleanup_block; + SELECT v_participant_daily_id AS participant_daily_id, v_total_participant AS total_participant,