Add MCU dashboard station progress sync
This commit is contained in:
185
application/libraries/Mcudashboard.php
Normal file
185
application/libraries/Mcudashboard.php
Normal file
@@ -0,0 +1,185 @@
|
||||
<?php
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
class Mcudashboard
|
||||
{
|
||||
private $db_onedev;
|
||||
private $db_dashboard;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$CI = &get_instance();
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user