diff --git a/application/controllers/mockup/fo/resultemailv7/Done.php b/application/controllers/mockup/fo/resultemailv7/Done.php index ab022842..6aa04e5f 100644 --- a/application/controllers/mockup/fo/resultemailv7/Done.php +++ b/application/controllers/mockup/fo/resultemailv7/Done.php @@ -256,8 +256,7 @@ class Done extends MY_Controller } $type = isset($prm['type']) ? $prm['type'] : 'S'; - $reports = isset($prm['reports']) ? json_encode($prm['reports']) : '[]'; - + $email_config = isset($prm['email_config']) ? $prm['email_config'] : []; if (is_string($email_config)) { $email_config = json_decode($email_config, true); @@ -267,15 +266,30 @@ class Done extends MY_Controller $status = isset($prm['status']) ? $prm['status'] : 'S'; - // Extract result names from reports for log - $group_result_names = []; - if (isset($prm['reports']) && is_array($prm['reports'])) { - foreach ($prm['reports'] as $report) { - if (isset($report['result'])) { - $group_result_names[] = $report['result']; - } + // Build reports dari qr_printout: [{"id":"1","url":"...","result":"LAB"}, ...] + $sql_qr = "SELECT + QR_PrintOutID AS id, + QR_PrintOutReportURLElectronic AS url, + QR_PrintOutGroup_ResultName AS result + FROM qr_printout + WHERE QR_PrintOutT_OrderHeaderID = ? + AND QR_PrintOutIsActive = 1 + ORDER BY QR_PrintOutID ASC"; + $qr_rows = $this->db_onedev->query($sql_qr, [$order_header_id]); + $reports_arr = []; + if ($qr_rows && $qr_rows->num_rows() > 0) { + foreach ($qr_rows->result_array() as $idx => $qr) { + $reports_arr[] = [ + 'id' => (string)($idx + 1), + 'url' => $qr['url'], + 'result' => $qr['result'], + ]; } } + $reports = json_encode($reports_arr); + + // Extract result names for log + $group_result_names = array_column($reports_arr, 'result'); $group_result_name_str = implode(', ', $group_result_names); $payload_json = json_encode($prm); diff --git a/scripts/send_email.php b/scripts/send_email.php index d793638d..a127fa13 100755 --- a/scripts/send_email.php +++ b/scripts/send_email.php @@ -283,8 +283,21 @@ foreach ($rows as $row) { $attachments = []; $tmp_files = []; - foreach ($reports as $idx => $url) { - log_msg(" Downloading attachment " . ($idx + 1) . ": {$url}"); + foreach ($reports as $idx => $entry) { + // Support both formats: + // old: ["http://..."] + // new: [{"id":"1","url":"http://...","result":"LAB"}] + if (is_array($entry)) { + $url = $entry['url'] ?? ''; + $result = $entry['result'] ?? ('hasil_lab_' . ($idx + 1)); + } else { + $url = (string) $entry; + $result = 'hasil_lab_' . ($idx + 1); + } + + if (empty($url)) continue; + + log_msg(" Downloading attachment " . ($idx + 1) . " [{$result}]: {$url}"); $pdf = download_pdf($url); if ($pdf === false) { @@ -313,9 +326,10 @@ foreach ($rows as $row) { } } + $safe_result = preg_replace('/[^a-zA-Z0-9_\-]/', '_', $result); $attachments[] = [ 'path' => $final, - 'name' => 'hasil_lab_' . ($idx + 1) . '.pdf', + 'name' => $safe_result . '.pdf', ]; }