Update sp_refresh_mcu_participant_daily_by_mcu_date to cleanup old dates without checkin

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
sas.fajri
2026-05-11 15:11:50 +07:00
parent 5a3a73c773
commit 03496b59ff

View File

@@ -93,6 +93,106 @@ BEGIN
COMMIT;
-- Cleanup tanggal lain untuk pasien yang baru masuk ke p_schedule_date,
-- jika di tanggal lain itu belum checkin
cleanup_block: BEGIN
DECLARE v_old_date DATE;
DECLARE v_old_total INT DEFAULT 0;
DECLARE done INT DEFAULT 0;
DECLARE cur_old_dates CURSOR FOR
SELECT DISTINCT s.Mcu_PatientScheduleDate
FROM cpone_dashboard.mcu_patient_schedule s
JOIN cpone.mcu_preregister_patients pp
ON pp.Mcu_PreregisterPatientsID = s.Mcu_PatientSchedulePreregisterID
WHERE pp.Mcu_PreregisterPatientsMgm_McuID = p_mgm_mcuid
AND s.Mcu_PatientScheduleDate != p_schedule_date
AND s.Mcu_PatientSchedulePreregisterID IN (
SELECT Mcu_PatientSchedulePreregisterID
FROM cpone_dashboard.mcu_patient_schedule
WHERE Mcu_PatientScheduleDate = p_schedule_date
AND Mcu_PatientScheduleIsActive = 'Y'
);
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;
START TRANSACTION;
-- Hapus detail pasien yang pindah ke p_schedule_date dan belum checkin di v_old_date
DELETE d
FROM cpone_dashboard.mcu_participant_daily_details d
JOIN cpone_dashboard.mcu_participant_daily h
ON h.Mcu_ParticipantDailyID = d.Mcu_ParticipantDailyDetailsParticipantDailyID
JOIN cpone_dashboard.mcu_patient mp
ON mp.Mcu_PatientID = d.Mcu_ParticipantDailyDetailsMcu_PatientID
WHERE h.Mcu_ParticipantDailyMcuID = p_mgm_mcuid
AND h.Mcu_ParticipantDailyDate = v_old_date
AND mp.Mcu_PatientPreregisterID IN (
SELECT preregister_id FROM (
SELECT Mcu_PatientSchedulePreregisterID AS preregister_id
FROM cpone_dashboard.mcu_patient_schedule
WHERE Mcu_PatientScheduleDate = p_schedule_date
AND Mcu_PatientScheduleIsActive = 'Y'
) AS tmp
)
AND NOT EXISTS (
SELECT 1 FROM cpone_dashboard.mcu_checkinout c
WHERE c.Mcu_CheckinoutPreregisterID = mp.Mcu_PatientPreregisterID
AND c.Mcu_CheckinoutDate = v_old_date
AND c.Mcu_CheckinoutIsActive = 'Y'
);
-- Hapus dari mcu_patient_schedule untuk tanggal lama, pasien yang belum checkin
DELETE s
FROM cpone_dashboard.mcu_patient_schedule s
WHERE s.Mcu_PatientScheduleDate = v_old_date
AND s.Mcu_PatientSchedulePreregisterID IN (
SELECT preregister_id FROM (
SELECT Mcu_PatientSchedulePreregisterID AS preregister_id
FROM cpone_dashboard.mcu_patient_schedule
WHERE Mcu_PatientScheduleDate = p_schedule_date
AND Mcu_PatientScheduleIsActive = 'Y'
) AS tmp
)
AND NOT EXISTS (
SELECT 1 FROM cpone_dashboard.mcu_checkinout c
WHERE c.Mcu_CheckinoutPreregisterID = s.Mcu_PatientSchedulePreregisterID
AND c.Mcu_CheckinoutDate = v_old_date
AND c.Mcu_CheckinoutIsActive = 'Y'
);
-- Hitung sisa total untuk tanggal lama dan update/delete mcu_participant_daily
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_PatientScheduleIsActive = 'Y'
AND pp.Mcu_PreregisterPatientsMgm_McuID = p_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 = p_mgm_mcuid
AND Mcu_ParticipantDailyDate = v_old_date;
ELSE
DELETE FROM cpone_dashboard.mcu_participant_daily
WHERE Mcu_ParticipantDailyMcuID = p_mgm_mcuid
AND Mcu_ParticipantDailyDate = v_old_date;
END IF;
COMMIT;
END LOOP old_date_loop;
CLOSE cur_old_dates;
END cleanup_block;
SELECT
v_participant_daily_id AS participant_daily_id,
(