Use notification email template config

This commit is contained in:
sas.fajri
2026-05-05 10:20:13 +07:00
parent 8e4a9f50b5
commit 9f2ee1cc79
2 changed files with 199 additions and 1 deletions

View File

@@ -13,7 +13,7 @@ class SendEmailPreregister extends MY_Controller
echo "SEND EMAIL API";
}
public function RegisterEmailNotif()
public function RegisterEmailNotif_BAK_050526()
{
try {
$data_send_patients = [];
@@ -287,4 +287,162 @@ class SendEmailPreregister extends MY_Controller
exit;
}
}
public function RegisterEmailNotif()
{
try {
$data_send_patients = [];
$sql_patients = "SELECT
pp.Mcu_PreregisterPatientsID AS preregister_id,
pp.Mcu_PreregisterPatientsPatientName AS nama_pasien,
pp.Mcu_PreregisterPatientsEmail AS email_pasien,
pp.Mcu_PreregisterPatientsT_OrderHeaderID AS orderid,
oh.T_OrderHeaderLabNumber AS ordernumber,
mpd.Mcu_ParticipantDailyDate AS tanggal_mcu
FROM mgm_mcu mm
JOIN cpone_dashboard.mcu_participant_daily mpd
ON mpd.Mcu_ParticipantDailyMcuID = mm.Mgm_McuID
AND mpd.Mcu_ParticipantDailyDate = DATE_ADD(CURDATE(), INTERVAL 1 DAY)
AND mpd.Mcu_ParticipantDailyIsActive = 'Y'
JOIN cpone_dashboard.mcu_participant_daily_details mpdd
ON mpdd.Mcu_ParticipantDailyDetailsParticipantDailyID = mpd.Mcu_ParticipantDailyID
AND mpdd.Mcu_ParticipantDailyDetailsIsActive = 'Y'
JOIN cpone_dashboard.mcu_patient mp
ON mp.Mcu_PatientID = mpdd.Mcu_ParticipantDailyDetailsMcu_PatientID
AND mp.Mcu_PatientIsActive = 'Y'
JOIN mcu_preregister_patients pp
ON pp.Mcu_PreregisterPatientsID = mp.Mcu_PatientPreregisterID
AND pp.Mcu_PreregisterPatientsIsActive = 'Y'
AND pp.Mcu_PreregisterPatientsMgm_McuID = mm.Mgm_McuID
LEFT JOIN t_orderheader oh
ON oh.T_OrderHeaderID = pp.Mcu_PreregisterPatientsT_OrderHeaderID
WHERE mm.Mgm_McuNotifByEmail = 'Y'
AND LENGTH(TRIM(IFNULL(pp.Mcu_PreregisterPatientsEmail, ''))) > 0";
$que_patients = $this->db->query($sql_patients);
if (!$que_patients) {
throw new Exception('[Error] failed get data patients');
}
$data_send_patients = $que_patients->result_array();
$sql_notif_config = "SELECT
Email_ConfigNofificationTemplate
FROM email_config_nofification
WHERE Email_ConfigNofificationIsActive = 'Y'
ORDER BY Email_ConfigNofificationID DESC
LIMIT 1";
$que_notif_config = $this->db->query($sql_notif_config);
if (!$que_notif_config || $que_notif_config->num_rows() == 0) {
throw new Exception('[Error] failed get notif email template config');
}
$notifConfig = $que_notif_config->row_array();
$templateBody = $notifConfig['Email_ConfigNofificationTemplate'];
$this->db->trans_begin();
foreach ($data_send_patients as $patient) {
$sql_config_email = "SELECT * FROM m_emailconfig WHERE M_EmailConfigType = 'R'";
$que_config_email = $this->db->query($sql_config_email, []);
if (!$que_config_email) {
$this->db->trans_rollback();
throw new Exception('[Error] failed to get email config');
}
$emailconfig = $que_config_email->row_array();
$tanggalMCUTs = strtotime($patient['tanggal_mcu']);
$hariIndonesia = [
'Sunday' => 'Minggu',
'Monday' => 'Senin',
'Tuesday' => 'Selasa',
'Wednesday' => 'Rabu',
'Thursday' => 'Kamis',
'Friday' => 'Jumat',
'Saturday' => 'Sabtu'
];
$dayEn = date('l', $tanggalMCUTs);
$dayId = isset($hariIndonesia[$dayEn]) ? $hariIndonesia[$dayEn] : $dayEn;
$tanggalMCU = $dayId . ', ' . date('d-m-Y', $tanggalMCUTs);
$body = str_replace(
['{PASIEN}', '{TANGGAL_MCU}'],
[$patient['nama_pasien'], $tanggalMCU],
$templateBody
);
$subjectPrefix = empty($patient['ordernumber']) ? '' : ($patient['ordernumber'] . ' ');
$subject = $subjectPrefix . $patient['nama_pasien'];
$recipient = json_encode([
["name" => $patient['nama_pasien'], "email" => $patient['email_pasien']]
]);
$sendCC = json_encode([
["name" => $emailconfig['M_EmailConfigCc'], "email" => $emailconfig['M_EmailConfigCc']]
]);
$sql_cek_outbox = "SELECT PreregisterEmailOutboxID
FROM preregister_email_outbox
WHERE PreregisterEmailOutboxReffID = ?
AND PreregisterEmailOutboxRecipientType = 'P'
AND PreregisterEmailOutboxIsActive = 'Y'
AND PreregisterEmailOutboxType = 'PREREGIST'";
$que_cek_outbox = $this->db->query($sql_cek_outbox, [$patient['preregister_id']]);
if (!$que_cek_outbox) {
$this->db->trans_rollback();
throw new Exception('[Error] failed check existing outbox data');
}
$cek_outbox = $que_cek_outbox->result_array();
if (count($cek_outbox) == 0) {
$sql_insert_outbox = "INSERT INTO preregister_email_outbox (
PreregisterEmailOutboxSubject,
PreregisterEmailOutboxRecipient,
PreregisterEmailOutboxRecipientType,
PreregisterEmailOutboxCC,
PreregisterEmailOutboxIsHtml,
PreregisterEmailOutboxBody,
PreregisterEmailOutboxType,
PreregisterEmailOutboxReffID
) VALUES (?,?,'P',?,'Y',?,?,?)";
$que_insert_outbox = $this->db->query($sql_insert_outbox, [
$subject,
$recipient,
$sendCC,
$body,
'PREREGIST',
$patient['preregister_id']
]);
if (!$que_insert_outbox) {
$this->db->trans_rollback();
throw new Exception('[Error] failed insert email outbox');
}
} else {
$sql_update_outbox = "UPDATE preregister_email_outbox SET
PreregisterEmailOutboxSubject = ?,
PreregisterEmailOutboxRecipient = ?,
PreregisterEmailOutboxCC = ?,
PreregisterEmailOutboxBody = ?,
PreregisterEmailOutboxIsSent = 'R',
PreregisterEmailOutboxRetry = 0,
PreregisterEmailOutboxLastUpdated = NOW()
WHERE PreregisterEmailOutboxID = ?";
$que_update_outbox = $this->db->query($sql_update_outbox, [
$subject,
$recipient,
$sendCC,
$body,
$cek_outbox[0]['PreregisterEmailOutboxID']
]);
if (!$que_update_outbox) {
$this->db->trans_rollback();
throw new Exception('[Error] failed update outbox');
}
}
}
$this->db->trans_commit();
$total_email_sent = count($data_send_patients);
$this->sys_ok($total_email_sent);
} catch (Exception $e) {
$msg = $e->getMessage();
$this->sys_error_db($msg);
exit;
}
}
}

View File

@@ -0,0 +1,40 @@
CREATE TABLE IF NOT EXISTS `cpone`.`email_config_nofification` (
`Email_ConfigNofificationID` int NOT NULL AUTO_INCREMENT,
`Email_ConfigNofificationTemplate` text NOT NULL,
`Email_ConfigNofificationSender` varchar(100) NOT NULL,
`Email_ConfigNofificationUsername` varchar(100) NOT NULL,
`Email_ConfigNofificationPassword` varchar(100) NOT NULL,
`Email_ConfigNofificationServer` varchar(100) NOT NULL,
`Email_ConfigNofificationCreated` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`Email_ConfigNofificationCreatedUserID` int NOT NULL DEFAULT '0',
`Email_ConfigNofificationLastUpdated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`Email_ConfigNofificationLastUpdatedUserID` int NOT NULL DEFAULT '0',
`Email_ConfigNofificationIsActive` char(1) NOT NULL DEFAULT 'Y',
PRIMARY KEY (`Email_ConfigNofificationID`),
KEY `idx_email_config_nofification_active` (`Email_ConfigNofificationIsActive`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
INSERT INTO `cpone`.`email_config_nofification` (
`Email_ConfigNofificationTemplate`,
`Email_ConfigNofificationSender`,
`Email_ConfigNofificationUsername`,
`Email_ConfigNofificationPassword`,
`Email_ConfigNofificationServer`,
`Email_ConfigNofificationCreatedUserID`,
`Email_ConfigNofificationLastUpdatedUserID`,
`Email_ConfigNofificationIsActive`
)
SELECT
'Yth {PASIEN},\n<p></p>\n<p>Melalui email ini, kami ingin mengingatkan kembali jadwal pemeriksaan kesehatan (*Medical Check-Up*) Anda yang akan dilaksanakan pada:</p>\n<p><strong>Hari/Tanggal : {TANGGAL_MCU}</strong><br><strong>Lokasi : Laboratorium Klinik <span style=\"font-weight:bold;color:blue\">WESTERINDO</span></strong></p>\n<p>Mohon kehadirannya tepat waktu dan pastikan Anda telah mengikuti instruksi persiapan (seperti puasa jika diperlukan) sebelum melakukan pemeriksaan.</p>\n<p>MOHON TIDAK DI-REPLY.</p>\nHormat kami,\n<p>Laboratorium Klinik <span style=\"font-weight:bold;color:blue\">WESTERINDO</span></p>',
'',
'',
'',
'',
0,
0,
'Y'
WHERE NOT EXISTS (
SELECT 1
FROM `cpone`.`email_config_nofification`
WHERE `Email_ConfigNofificationIsActive` = 'Y'
);