From 2144e00b31be5f8f3b8c62361dac51ac2f7b7a23 Mon Sep 17 00:00:00 2001 From: "sas.fajri" Date: Wed, 29 Apr 2026 13:13:18 +0700 Subject: [PATCH] Add MCU dashboard station progress sync --- .../sampling-lab-mobile-cpone-v14/Patient.php | 86 +++++--- application/libraries/Mcudashboard.php | 185 ++++++++++++++++++ 2 files changed, 248 insertions(+), 23 deletions(-) create mode 100644 application/libraries/Mcudashboard.php diff --git a/application/controllers/mockup/sampling-lab-mobile-cpone-v14/Patient.php b/application/controllers/mockup/sampling-lab-mobile-cpone-v14/Patient.php index ec093ce..b67d9f8 100644 --- a/application/controllers/mockup/sampling-lab-mobile-cpone-v14/Patient.php +++ b/application/controllers/mockup/sampling-lab-mobile-cpone-v14/Patient.php @@ -7,13 +7,20 @@ class Patient extends MY_Controller { echo "Patient API"; } - public function __construct() - { - parent::__construct(); - $this->db_onedev = $this->load->database("onedev", true); - $this->load->library('Nonlabtemplate'); - $this->IP_SOCKET_IO = "127.0.0.1"; - } + public function __construct() + { + parent::__construct(); + $this->db_onedev = $this->load->database("onedev", true); + $this->load->library('Nonlabtemplate'); + $this->load->library('Mcudashboard'); + $this->IP_SOCKET_IO = "127.0.0.1"; + } + + private function sync_station_progress($orderSampleID = 0, $samplingSoID = 0) + { + $sync = $this->mcudashboard->upsert_station_progress(intval($orderSampleID), intval($samplingSoID)); + return $sync; + } function getstations() { @@ -895,11 +902,23 @@ class Patient extends MY_Controller T_OrderSampleT_BarcodeLabID = {$v->T_BarcodeLabID} "; - //echo $sql; - $this->db_onedev->query($sql); - $sql = "INSERT INTO sample_by_step( - SampleByStepM_StatusSampleCode, - SampleByStepT_OrderHeaderID, + //echo $sql; + $this->db_onedev->query($sql); + $sqlProgress = "SELECT T_OrderSampleID + FROM t_ordersample + WHERE T_OrderSampleT_OrderHeaderID = {$orderid} AND + T_OrderSampleT_SampleTypeID = {$v->T_SampleTypeID} AND + T_OrderSampleT_BarcodeLabID = {$v->T_BarcodeLabID} AND + T_OrderSampleIsActive = 'Y' + ORDER BY T_OrderSampleID DESC + LIMIT 1"; + $qProgress = $this->db_onedev->query($sqlProgress); + if ($qProgress && $qProgress->num_rows() > 0) { + $this->sync_station_progress(intval($qProgress->row()->T_OrderSampleID), 0); + } + $sql = "INSERT INTO sample_by_step( + SampleByStepM_StatusSampleCode, + SampleByStepT_OrderHeaderID, SampleByStepT_BarcodeLabID, SampleByStepRequirementStatus, SampleByStepRequirements, @@ -941,7 +960,10 @@ class Patient extends MY_Controller $sql = "SELECT * FROM t_ordersample WHERE T_OrderSampleT_BarcodeLabID = {$barcodelabid} AND T_OrderSampleIsActive = 'Y' ORDER BY T_OrderSampleID DESC LIMIT 1"; - $dt_sampleorder = $this->db_onedev->query($sql)->row(); + $dt_sampleorder = $this->db_onedev->query($sql)->row(); + if (!empty($dt_sampleorder) && isset($dt_sampleorder->T_OrderSampleID)) { + $this->sync_station_progress(intval($dt_sampleorder->T_OrderSampleID), 0); + } $sql = "SELECT * FROM t_sampletype WHERE T_SampleTypeID = {$sampletypeid}"; $dt_sampletype = $this->db_onedev->query($sql)->row(); @@ -1828,8 +1850,8 @@ class Patient extends MY_Controller {$userid} ) "; //echo $sql; - } - } else { + } + } else { $sql = "UPDATE t_samplingso SET T_SamplingSoProcessDate = CURDATE(), T_SamplingSoProcessTime = CURTIME(), @@ -1840,12 +1862,27 @@ class Patient extends MY_Controller T_SamplingSoLastUpdated = NOW() WHERE T_SamplingSoID = {$v->samplingso_id}"; - //echo $sql; - } - $this->db_onedev->query($sql); - } - //$this->broadcast("specimen-col-process"); - } + //echo $sql; + } + $this->db_onedev->query($sql); + if ($v->samplingso_id == 0) { + $sqlSync = "SELECT T_SamplingSoID + FROM t_samplingso + WHERE T_SamplingSoT_OrderHeaderID = {$orderid} AND + T_SamplingSoT_TestID = {$v->test_id} AND + T_SamplingSoIsActive = 'Y' + ORDER BY T_SamplingSoID DESC + LIMIT 1"; + $qSync = $this->db_onedev->query($sqlSync); + if ($qSync && $qSync->num_rows() > 0) { + $this->sync_station_progress(0, intval($qSync->row()->T_SamplingSoID)); + } + } else { + $this->sync_station_progress(0, intval($v->samplingso_id)); + } + } + //$this->broadcast("specimen-col-process"); + } } $isdone = "X"; @@ -1877,8 +1914,11 @@ class Patient extends MY_Controller $this->sys_error($message); exit; } - $samplingso = $query->result(); - //print_r($samplingso); + $samplingso = $query->result(); + foreach ($samplingso as $syncRow) { + $this->sync_station_progress(0, intval($syncRow->T_SamplingSoID)); + } + //print_r($samplingso); //echo $samplingso[0]->T_SamplingSoID; // print_r($samplingso['T_SamplingSoID']); //print_r($samplingso); diff --git a/application/libraries/Mcudashboard.php b/application/libraries/Mcudashboard.php new file mode 100644 index 0000000..678915a --- /dev/null +++ b/application/libraries/Mcudashboard.php @@ -0,0 +1,185 @@ +db_onedev = $CI->load->database("onedev", true); + $this->db_dashboard = $CI->load->database("cpone_dashboard", 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"); + } + + 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); + } +}