From 8bbd9981f0d55f4be2792f3353d2d1ed22b7b31e Mon Sep 17 00:00:00 2001 From: "sas.fajri" Date: Wed, 29 Apr 2026 16:53:50 +0700 Subject: [PATCH] Add MCU dashboard file upload logging --- application/libraries/Mcudashboard.php | 205 ++++++++++++++++++ ...9_create_log_mcu_dashboard_file_upload.sql | 13 ++ 2 files changed, 218 insertions(+) create mode 100644 scripts/sql/2026-04-29_create_log_mcu_dashboard_file_upload.sql diff --git a/application/libraries/Mcudashboard.php b/application/libraries/Mcudashboard.php index a78fe2b..2d0a043 100644 --- a/application/libraries/Mcudashboard.php +++ b/application/libraries/Mcudashboard.php @@ -5,12 +5,14 @@ class Mcudashboard { private $db_onedev; private $db_dashboard; + private $db_log; public function __construct() { $CI = &get_instance(); $this->db_onedev = $CI->load->database("onedev", true); $this->db_dashboard = $CI->load->database("cpone_dashboard", true); + $this->db_log = $CI->load->database("cpone_log", true); } public function upsert_station_progress($T_OrderSampleID = 0, $T_SamplingSoID = 0) @@ -91,6 +93,130 @@ class Mcudashboard return array(true, "ok"); } + public function generate_dashboard_files($Mgm_McuID = 0, $Published_McuDasboardID = 0) + { + $Mgm_McuID = intval($Mgm_McuID); + $Published_McuDasboardID = intval($Published_McuDasboardID); + + if ($Mgm_McuID <= 0 && $Published_McuDasboardID <= 0) { + return array(false, "Mgm_McuID or Published_McuDasboardID is required"); + } + + $where = ""; + $params = array(); + if ($Published_McuDasboardID > 0) { + $where = "pmd.Published_McuDasboardID = ?"; + $params[] = $Published_McuDasboardID; + } else { + $where = "oh.T_OrderHeaderMgm_McuID = ?"; + $params[] = $Mgm_McuID; + } + + $sql = "SELECT + pmd.Published_McuDasboardID, + pmd.Published_McuDasboardT_OrderHeaderID, + oh.T_OrderHeaderMgm_McuID, + oh.T_OrderHeaderDate, + oh.T_OrderHeaderLabNumber, + mr.Mcu_ResumeID, + t.Mcu_ReportUrlTemplateUrl, + t.Mcu_ReportUrlTemplateParams + FROM published_mcu_dashboard pmd + JOIN t_orderheader oh ON oh.T_OrderHeaderID = pmd.Published_McuDasboardT_OrderHeaderID + AND oh.T_OrderHeaderIsActive = 'Y' + JOIN mcu_resume mr ON mr.Mcu_ResumeT_OrderHeaderID = oh.T_OrderHeaderID + AND mr.Mcu_ResumeIsActive = 'Y' + LEFT JOIN mgm_mcureport mcr ON mcr.Mgm_McuReportMgm_McuID = oh.T_OrderHeaderMgm_McuID + AND mcr.Mgm_McuReportIsActive = 'Y' + LEFT JOIN mcu_report_url_template t ON t.Mcu_ReportUrlTemplateID = mcr.Mgm_McuReportMcu_ReportUrlTemplateID + AND t.Mcu_ReportUrlTemplateIsActive = 'Y' + WHERE pmd.Published_McuDasboardIsActive = 'Y' + AND {$where} + ORDER BY pmd.Published_McuDasboardID ASC"; + $qry = $this->db_onedev->query($sql, $params); + if (!$qry) { + return array(false, $this->db_onedev->error()["message"]); + } + + $rows = $qry->result_array(); + if (count($rows) === 0) { + return array(true, array()); + } + + $baseDir = '/home/one/project/one/dashboard-files'; + if (!is_dir($baseDir)) { + @mkdir($baseDir, 0775, true); + } + + $results = array(); + foreach ($rows as $row) { + $templateUrl = trim((string)$row['Mcu_ReportUrlTemplateUrl']); + if ($templateUrl === '') { + $templateUrl = "/birt/run?__report=report/one/mcu/rpt_mcu_resume.rptdesign&__format=pdf&username={{username}}&PID={{PResumeID}}&tm={{PTimestamp}}"; + } + + $replacedUrl = $this->build_report_url($templateUrl, $row); + $pdfBytes = $this->download_url_bytes($replacedUrl); + if ($pdfBytes === false || strlen($pdfBytes) === 0) { + $result = array( + "Published_McuDasboardID" => intval($row['Published_McuDasboardID']), + "status" => "ERR", + "message" => "download pdf failed", + "url" => $replacedUrl + ); + $results[] = $result; + $this->insert_upload_log($row, $result); + continue; + } + + $orderDate = date('Y-m-d', strtotime($row['T_OrderHeaderDate'])); + $yyyy = date('Y', strtotime($orderDate)); + $mm = date('m', strtotime($orderDate)); + $subDir = $baseDir . '/' . $yyyy . '/' . $mm; + if (!is_dir($subDir)) { + @mkdir($subDir, 0775, true); + } + + $labNumber = preg_replace('/[^A-Za-z0-9_-]/', '', (string)$row['T_OrderHeaderLabNumber']); + if ($labNumber === '') { + $labNumber = 'unknown'; + } + $fileName = $labNumber . '_resume_individu.pdf'; + $fullPath = $subDir . '/' . $fileName; + $relativePath = $yyyy . '/' . $mm . '/' . $fileName; + + $okWrite = @file_put_contents($fullPath, $pdfBytes); + if ($okWrite === false) { + $result = array( + "Published_McuDasboardID" => intval($row['Published_McuDasboardID']), + "status" => "ERR", + "message" => "write file failed", + "path" => $fullPath + ); + $results[] = $result; + $this->insert_upload_log($row, $result); + continue; + } + + $sqlUpdate = "UPDATE published_mcu_dashboard + SET Published_McuDasboardFileUrl = ?, + Published_McuDasboardLastUpdated = NOW() + WHERE Published_McuDasboardID = ?"; + $this->db_onedev->query($sqlUpdate, array($relativePath, intval($row['Published_McuDasboardID']))); + + $result = array( + "Published_McuDasboardID" => intval($row['Published_McuDasboardID']), + "status" => "OK", + "file" => $relativePath, + "url" => $replacedUrl + ); + $results[] = $result; + $this->insert_upload_log($row, $result); + } + + return array(true, $results); + } + private function upsert_from_lab($orderSampleID) { $sql = "SELECT @@ -244,4 +370,83 @@ class Mcudashboard } return intval($qry->row()->Mcu_PatientPreregisterID); } + + private function build_report_url($templateUrl, $row) + { + $timestamp = date('ymdHis'); + $replacements = array( + '{{username}}' => 'admin', + '{{PResumeID}}' => intval($row['Mcu_ResumeID']), + '{{PTimestamp}}' => $timestamp, + '{{POrderID}}' => intval($row['Published_McuDasboardT_OrderHeaderID']), + '{{PReIDHaji}}' => intval($row['Mcu_ResumeID']), + '{{PBg}}' => '', + '{{PBgSuffix}}' => '', + '{{PLabNumber}}' => (string)$row['T_OrderHeaderLabNumber'], + '{{PLabelType}}' => 'departement', + '{{PStartDate}}' => date('Y-m-d', strtotime($row['T_OrderHeaderDate'])), + '{{PEndDate}}' => date('Y-m-d', strtotime($row['T_OrderHeaderDate'])), + '{{PMcuID}}' => intval($row['T_OrderHeaderMgm_McuID']) + ); + + $urlPath = strtr($templateUrl, $replacements); + if (strpos($urlPath, 'http://') === 0 || strpos($urlPath, 'https://') === 0) { + return $urlPath; + } + return 'https://devcpone.aplikasi.web.id' . $urlPath; + } + + private function download_url_bytes($url) + { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); + curl_setopt($ch, CURLOPT_TIMEOUT, 90); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + $resp = curl_exec($ch); + $httpCode = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE)); + curl_close($ch); + + if ($resp === false || $httpCode < 200 || $httpCode >= 300) { + return false; + } + return $resp; + } + + private function insert_upload_log($row, $response) + { + if (!$this->db_log) { + return; + } + + $payload = array( + "Mgm_McuID" => intval($row['T_OrderHeaderMgm_McuID']), + "Published_McuDasboardID" => intval($row['Published_McuDasboardID']), + "T_OrderHeaderID" => intval($row['Published_McuDasboardT_OrderHeaderID']), + "Mcu_ResumeID" => intval($row['Mcu_ResumeID']), + "ReportTemplateUrl" => (string)$row['Mcu_ReportUrlTemplateUrl'], + "ReportTemplateParams" => (string)$row['Mcu_ReportUrlTemplateParams'], + "T_OrderHeaderLabNumber" => (string)$row['T_OrderHeaderLabNumber'], + "T_OrderHeaderDate" => (string)$row['T_OrderHeaderDate'] + ); + + $status = isset($response['status']) ? (string)$response['status'] : 'UNK'; + $sql = "INSERT INTO log_mcu_dashboard_file_upload ( + Log_McuDashboardFileUploadMgm_McuID, + Log_McuDashboardFileUploadPublished_McuDasboardID, + Log_McuDashboardFileUploadPayload, + Log_McuDashboardFileUploadResponse, + Log_McuDashboardFileUploadStatus + ) VALUES (?, ?, ?, ?, ?)"; + $this->db_log->query($sql, array( + intval($row['T_OrderHeaderMgm_McuID']), + intval($row['Published_McuDasboardID']), + json_encode($payload), + json_encode($response), + $status + )); + } } diff --git a/scripts/sql/2026-04-29_create_log_mcu_dashboard_file_upload.sql b/scripts/sql/2026-04-29_create_log_mcu_dashboard_file_upload.sql new file mode 100644 index 0000000..3754841 --- /dev/null +++ b/scripts/sql/2026-04-29_create_log_mcu_dashboard_file_upload.sql @@ -0,0 +1,13 @@ +CREATE TABLE IF NOT EXISTS `log_mcu_dashboard_file_upload` ( + `Log_McuDashboardFileUploadID` bigint NOT NULL AUTO_INCREMENT, + `Log_McuDashboardFileUploadMgm_McuID` int NOT NULL DEFAULT '0', + `Log_McuDashboardFileUploadPublished_McuDasboardID` int NOT NULL DEFAULT '0', + `Log_McuDashboardFileUploadPayload` longtext, + `Log_McuDashboardFileUploadResponse` longtext, + `Log_McuDashboardFileUploadStatus` varchar(10) NOT NULL DEFAULT 'OK', + `Log_McuDashboardFileUploadCreated` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`Log_McuDashboardFileUploadID`), + KEY `idx_mgm_mcuid` (`Log_McuDashboardFileUploadMgm_McuID`), + KEY `idx_published_id` (`Log_McuDashboardFileUploadPublished_McuDasboardID`), + KEY `idx_created` (`Log_McuDashboardFileUploadCreated`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;