diff --git a/scripts/send_email.php b/scripts/send_email.php index a127fa13..878d2692 100755 --- a/scripts/send_email.php +++ b/scripts/send_email.php @@ -16,6 +16,7 @@ define('DB_HOST', 'localhost'); define('DB_USER', 'root'); define('DB_PASS', 'sasone102938'); define('DB_NAME', 'one_lab'); +define('DB_LOG_NAME', 'one_lab_log'); define('SMTP_PORT', 587); define('EMAIL_SUBJECT', 'Hasil Pemeriksaan Laboratorium'); @@ -217,6 +218,12 @@ try { DB_PASS, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION] ); + $pdo_log = new PDO( + 'mysql:host=' . DB_HOST . ';dbname=' . DB_LOG_NAME . ';charset=utf8', + DB_USER, + DB_PASS, + [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION] + ); } catch (PDOException $e) { log_msg('DB connect failed: ' . $e->getMessage()); exit(1); @@ -247,6 +254,7 @@ $where_id = $only_id ? 'AND e.T_SendEmailID = ' . $only_id : ''; $sql = " SELECT e.T_SendEmailID, + e.T_SendEmailT_OrderHeaderID, e.T_SendEmailRecepient, e.T_SendEmailRecepientType, e.T_SendEmailPatientName, @@ -270,13 +278,14 @@ $rows = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC); log_msg('Found ' . count($rows) . ' pending email(s)' . ($dry_run ? ' [DRY RUN]' : '')); foreach ($rows as $row) { - $id = (int) $row['T_SendEmailID']; - $recipient = $row['T_SendEmailRecepient']; - $rec_type = $row['T_SendEmailRecepientType']; - $body_html = $row['T_SendEmailNarratives'] ?? ''; - $dob = $row['M_PatientDOB'] ?? ''; - $password = str_replace('-', '', $dob); - $reports = json_decode($row['T_SendEmailReports'] ?? '[]', true) ?: []; + $id = (int) $row['T_SendEmailID']; + $order_id = (int) $row['T_SendEmailT_OrderHeaderID']; + $recipient = $row['T_SendEmailRecepient']; + $rec_type = $row['T_SendEmailRecepientType']; + $body_html = $row['T_SendEmailNarratives'] ?? ''; + $dob = $row['M_PatientDOB'] ?? ''; + $password = str_replace('-', '', $dob); + $reports = json_decode($row['T_SendEmailReports'] ?? '[]', true) ?: []; log_msg("Processing ID {$id} → {$recipient} ({$rec_type})"); @@ -366,11 +375,23 @@ foreach ($rows as $row) { T_SendEmailLastUpdated = NOW() WHERE T_SendEmailID = ? ")->execute([$id]); - $pdo->prepare(" + $group_result_names = array_filter(array_column( + array_filter($reports, 'is_array'), + 'result' + )); + $pdo_log->prepare(" INSERT INTO t_send_email_log - (T_SendEmailLogT_SendEmailID, T_SendEmailLogRecepient, T_SendEmailLogStatus, T_SendEmailLogResponse, T_SendEmailLogCreated) - VALUES (?, ?, 'D', NULL, NOW()) - ")->execute([$id, $recipient]); + (T_SendEmailLogT_OrderHeaderID, T_SendEmailLogRecepient, + T_SendEmailLogStatus, T_SendEmailLogResponse, + T_SendEmailLogGroup_ResultName, T_SendEmailLogJson, + T_SendEmailLogCreated, T_SendEmailLogCreatedUserID) + VALUES (?, ?, 'D', NULL, ?, ?, NOW(), 0) + ")->execute([ + $order_id, + $recipient, + implode(', ', $group_result_names), + json_encode($row), + ]); } else { log_msg(" Error: {$err}"); $pdo->prepare("