453 lines
19 KiB
PHP
453 lines
19 KiB
PHP
<?php
|
|
defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
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)
|
|
{
|
|
$T_OrderSampleID = intval($T_OrderSampleID);
|
|
$T_SamplingSoID = intval($T_SamplingSoID);
|
|
|
|
if ($T_SamplingSoID === 0 && $T_OrderSampleID > 0) {
|
|
return $this->upsert_from_lab($T_OrderSampleID);
|
|
}
|
|
|
|
if ($T_OrderSampleID === 0 && $T_SamplingSoID > 0) {
|
|
return $this->upsert_from_nonlab($T_SamplingSoID);
|
|
}
|
|
|
|
return array(false, "invalid source argument");
|
|
}
|
|
|
|
public function upsert_patient_resume_status($Mcu_ResumeID = 0)
|
|
{
|
|
$Mcu_ResumeID = intval($Mcu_ResumeID);
|
|
if ($Mcu_ResumeID <= 0) {
|
|
return array(false, "invalid Mcu_ResumeID");
|
|
}
|
|
|
|
$sql = "SELECT
|
|
mr.Mcu_ResumeID,
|
|
mr.Mcu_ResumeStatus,
|
|
mr.Mcu_ResumeValidation,
|
|
oh.T_OrderHeaderID,
|
|
oh.T_OrderHeaderMgm_McuID AS mcu_id,
|
|
pp.Mcu_PreregisterPatientsID AS preregister_id,
|
|
IFNULL(pmd.Published_McuDasboardStatus, 'N') AS published_status
|
|
FROM mcu_resume mr
|
|
JOIN t_orderheader oh ON oh.T_OrderHeaderID = mr.Mcu_ResumeT_OrderHeaderID
|
|
AND oh.T_OrderHeaderIsActive = 'Y'
|
|
JOIN mcu_preregister_patients pp ON pp.Mcu_PreregisterPatientsT_OrderHeaderID = oh.T_OrderHeaderID
|
|
AND pp.Mcu_PreregisterPatientsIsActive = 'Y'
|
|
LEFT JOIN published_mcu_dashboard pmd ON pmd.Published_McuDasboardT_OrderHeaderID = oh.T_OrderHeaderID
|
|
AND pmd.Published_McuDasboardIsActive = 'Y'
|
|
WHERE mr.Mcu_ResumeID = ? AND mr.Mcu_ResumeIsActive = 'Y'
|
|
LIMIT 1";
|
|
$qry = $this->db_onedev->query($sql, array($Mcu_ResumeID));
|
|
if (!$qry) {
|
|
return array(false, $this->db_onedev->error()["message"]);
|
|
}
|
|
if ($qry->num_rows() === 0) {
|
|
return array(false, "resume not found");
|
|
}
|
|
|
|
$row = $qry->row_array();
|
|
|
|
$sql = "INSERT INTO mcu_patient_resume_status (
|
|
Mcu_PatientResumeStatusPreregisterID,
|
|
Mcu_PatientResumeStatusMcuID,
|
|
Mcu_PatientResumeStatusStatus,
|
|
Mcu_PatientResumeStatusValidated,
|
|
Mcu_PatientResumeStatusPublished,
|
|
Mcu_PatientResumeSyncedAt
|
|
) VALUES (?, ?, ?, ?, ?, NOW())
|
|
ON DUPLICATE KEY UPDATE
|
|
Mcu_PatientResumeStatusStatus = VALUES(Mcu_PatientResumeStatusStatus),
|
|
Mcu_PatientResumeStatusValidated = VALUES(Mcu_PatientResumeStatusValidated),
|
|
Mcu_PatientResumeStatusPublished = VALUES(Mcu_PatientResumeStatusPublished),
|
|
Mcu_PatientResumeSyncedAt = NOW()";
|
|
$ok = $this->db_dashboard->query($sql, array(
|
|
intval($row['preregister_id']),
|
|
intval($row['mcu_id']),
|
|
$row['Mcu_ResumeStatus'],
|
|
$row['Mcu_ResumeValidation'],
|
|
$row['published_status']
|
|
));
|
|
|
|
if (!$ok) {
|
|
return array(false, $this->db_dashboard->error()["message"]);
|
|
}
|
|
|
|
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
|
|
os.T_OrderSampleID,
|
|
os.T_OrderSampleT_OrderHeaderID AS order_id,
|
|
oh.T_OrderHeaderMgm_McuID AS mcu_id,
|
|
os.T_OrderSampleT_SampleStationID AS station_id,
|
|
ss.T_SampleStationName AS station_name,
|
|
os.T_OrderSampleSamplingDate AS sampling_date,
|
|
os.T_OrderSampleSamplingTime AS sampling_time,
|
|
os.T_OrderSampleReceiveDate AS receive_date,
|
|
os.T_OrderSampleReceiveTime AS receive_time
|
|
FROM t_ordersample os
|
|
JOIN t_orderheader oh ON oh.T_OrderHeaderID = os.T_OrderSampleT_OrderHeaderID
|
|
LEFT JOIN t_samplestation ss ON ss.T_SampleStationID = os.T_OrderSampleT_SampleStationID
|
|
WHERE os.T_OrderSampleID = ? AND os.T_OrderSampleIsActive = 'Y'
|
|
LIMIT 1";
|
|
$qry = $this->db_onedev->query($sql, array($orderSampleID));
|
|
if (!$qry) {
|
|
return array(false, $this->db_onedev->error()["message"]);
|
|
}
|
|
if ($qry->num_rows() === 0) {
|
|
return array(false, "ordersample not found");
|
|
}
|
|
$row = $qry->row_array();
|
|
|
|
$preregister_id = $this->get_preregister_id($row['order_id']);
|
|
$sampling_at = $this->combine_datetime($row['sampling_date'], $row['sampling_time']);
|
|
$receive_at = $this->combine_datetime($row['receive_date'], $row['receive_time']);
|
|
|
|
$sql = "INSERT INTO mcu_station_progress (
|
|
Mcu_StationProgressOrderID,
|
|
Mcu_StationProgressPreregisterID,
|
|
Mcu_StationProgressMcuID,
|
|
Mcu_StationProgressStationID,
|
|
Mcu_StationProgressStationName,
|
|
Mcu_StationProgressSource,
|
|
Mcu_StationProgressCheckinDate,
|
|
Mcu_StationProgressSamplingAt,
|
|
Mcu_StationProgressReceiveAt,
|
|
Mcu_StationProgressSyncedAt
|
|
) VALUES (?, ?, ?, ?, ?, 'lab', CURDATE(), ?, ?, NOW())
|
|
ON DUPLICATE KEY UPDATE
|
|
Mcu_StationProgressPreregisterID = VALUES(Mcu_StationProgressPreregisterID),
|
|
Mcu_StationProgressMcuID = VALUES(Mcu_StationProgressMcuID),
|
|
Mcu_StationProgressStationName = VALUES(Mcu_StationProgressStationName),
|
|
Mcu_StationProgressCheckinDate = CURDATE(),
|
|
Mcu_StationProgressSamplingAt = VALUES(Mcu_StationProgressSamplingAt),
|
|
Mcu_StationProgressReceiveAt = VALUES(Mcu_StationProgressReceiveAt),
|
|
Mcu_StationProgressSyncedAt = NOW()";
|
|
$ok = $this->db_dashboard->query($sql, array(
|
|
intval($row['order_id']),
|
|
$preregister_id,
|
|
intval($row['mcu_id']),
|
|
intval($row['station_id']),
|
|
$row['station_name'],
|
|
$sampling_at,
|
|
$receive_at
|
|
));
|
|
|
|
if (!$ok) {
|
|
return array(false, $this->db_dashboard->error()["message"]);
|
|
}
|
|
|
|
return array(true, "ok");
|
|
}
|
|
|
|
private function upsert_from_nonlab($samplingSoID)
|
|
{
|
|
$sql = "SELECT
|
|
so.T_SamplingSoID,
|
|
so.T_SamplingSoT_OrderHeaderID AS order_id,
|
|
oh.T_OrderHeaderMgm_McuID AS mcu_id,
|
|
so.T_SamplingSoT_SampleStationID AS station_id,
|
|
ss.T_SampleStationName AS station_name,
|
|
so.T_SamplingSoProcessDate AS process_date,
|
|
so.T_SamplingSoProcessTime AS process_time,
|
|
so.T_SamplingSoDoneDate AS done_date,
|
|
so.T_SamplingSoDoneTime AS done_time
|
|
FROM t_samplingso so
|
|
JOIN t_orderheader oh ON oh.T_OrderHeaderID = so.T_SamplingSoT_OrderHeaderID
|
|
LEFT JOIN t_samplestation ss ON ss.T_SampleStationID = so.T_SamplingSoT_SampleStationID
|
|
WHERE so.T_SamplingSoID = ? AND so.T_SamplingSoIsActive = 'Y'
|
|
LIMIT 1";
|
|
$qry = $this->db_onedev->query($sql, array($samplingSoID));
|
|
if (!$qry) {
|
|
return array(false, $this->db_onedev->error()["message"]);
|
|
}
|
|
if ($qry->num_rows() === 0) {
|
|
return array(false, "samplingso not found");
|
|
}
|
|
$row = $qry->row_array();
|
|
|
|
$preregister_id = $this->get_preregister_id($row['order_id']);
|
|
$process_at = $this->combine_datetime($row['process_date'], $row['process_time']);
|
|
$done_at = $this->combine_datetime($row['done_date'], $row['done_time']);
|
|
|
|
$sql = "INSERT INTO mcu_station_progress (
|
|
Mcu_StationProgressOrderID,
|
|
Mcu_StationProgressPreregisterID,
|
|
Mcu_StationProgressMcuID,
|
|
Mcu_StationProgressStationID,
|
|
Mcu_StationProgressStationName,
|
|
Mcu_StationProgressSource,
|
|
Mcu_StationProgressProcessAt,
|
|
Mcu_StationProgressDoneAt,
|
|
Mcu_StationProgressSyncedAt
|
|
) VALUES (?, ?, ?, ?, ?, 'nonlab', ?, ?, NOW())
|
|
ON DUPLICATE KEY UPDATE
|
|
Mcu_StationProgressPreregisterID = VALUES(Mcu_StationProgressPreregisterID),
|
|
Mcu_StationProgressMcuID = VALUES(Mcu_StationProgressMcuID),
|
|
Mcu_StationProgressStationName = VALUES(Mcu_StationProgressStationName),
|
|
Mcu_StationProgressProcessAt = VALUES(Mcu_StationProgressProcessAt),
|
|
Mcu_StationProgressDoneAt = VALUES(Mcu_StationProgressDoneAt),
|
|
Mcu_StationProgressSyncedAt = NOW()";
|
|
$ok = $this->db_dashboard->query($sql, array(
|
|
intval($row['order_id']),
|
|
$preregister_id,
|
|
intval($row['mcu_id']),
|
|
intval($row['station_id']),
|
|
$row['station_name'],
|
|
$process_at,
|
|
$done_at
|
|
));
|
|
|
|
if (!$ok) {
|
|
return array(false, $this->db_dashboard->error()["message"]);
|
|
}
|
|
|
|
return array(true, "ok");
|
|
}
|
|
|
|
private function combine_datetime($dateValue, $timeValue)
|
|
{
|
|
if (empty($dateValue) || empty($timeValue) || $dateValue === '0000-00-00') {
|
|
return null;
|
|
}
|
|
return $dateValue . ' ' . $timeValue;
|
|
}
|
|
|
|
private function get_preregister_id($orderID)
|
|
{
|
|
$sql = "SELECT Mcu_PatientPreregisterID
|
|
FROM mcu_patient
|
|
WHERE Mcu_PatientOrderID = ?
|
|
ORDER BY Mcu_PatientID DESC
|
|
LIMIT 1";
|
|
$qry = $this->db_dashboard->query($sql, array(intval($orderID)));
|
|
if (!$qry || $qry->num_rows() === 0) {
|
|
return 0;
|
|
}
|
|
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
|
|
));
|
|
}
|
|
}
|