Add MCU participant daily refresh SP

This commit is contained in:
sas.fajri
2026-05-05 09:41:04 +07:00
parent 62254ff40c
commit 8e4a9f50b5
3 changed files with 157 additions and 0 deletions

View File

@@ -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();
}
}
}
}
}
}

View File

@@ -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 ;

View File

@@ -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 ;