278 lines
12 KiB
PHP
278 lines
12 KiB
PHP
<?php
|
|
class Xferbranch_v4 extends CI_Controller
|
|
{
|
|
function __construct() {
|
|
parent::__construct();
|
|
$this->db = $this->load->database("onedev", true);
|
|
}
|
|
function index() {
|
|
echo "Xfer Branch API";
|
|
}
|
|
|
|
function batch() {
|
|
$this->fix();
|
|
|
|
$sql = "select T_RefDeliveryOrderID, T_RefDeliveryOrderNumber,
|
|
M_BranchName, M_BranchIPAddress
|
|
from t_ref_deliveryorder
|
|
join m_branch on T_RefDeliveryOrderM_DestinationID = M_BranchID
|
|
where T_RefDeliveryOrderIsActive = 'Y' and
|
|
(T_RefDeliveryOrderIsConfirm = 'Y' or T_RefDeliveryOrderIsConfirm = 'P')
|
|
and T_RefDeliveryOrderLastUpdated + interval 3 minute < now()
|
|
limit 0,50
|
|
";
|
|
$qry = $this->db->query($sql);
|
|
$tot_upload = 0;
|
|
if ($qry) {
|
|
$rows = $qry->result_array();
|
|
foreach($rows as $r) {
|
|
$date = date("Y-m-d H:i:s ");
|
|
$branch = $r["M_BranchName"];
|
|
$ip_address = $r["M_BranchIPAddress"];
|
|
$number = $r["T_RefDeliveryOrderNumber"];
|
|
|
|
if ( $this->do($r["T_RefDeliveryOrderID"])) {
|
|
echo "$date [OK] uploaded {$number} to {$branch} @ $ip_address\n";
|
|
} else {
|
|
echo "$date [ERR] uploaded {$number} to {$branch} @ $ip_address\n";
|
|
}
|
|
$tot_upload++;
|
|
}
|
|
}
|
|
if ($tot_upload == 0 ) {
|
|
$date = date("Y-m-d H:i:s ");
|
|
echo "$date [OK] No upload data\n";
|
|
}
|
|
}
|
|
|
|
function do($deliveryID) {
|
|
$sql = "select distinct
|
|
T_RefDeliveryOrderID,
|
|
T_RefDeliveryOrderDate,
|
|
T_RefDeliveryOrderNumber,
|
|
T_RefDeliveryOrderDetailID,
|
|
T_RefDeliveryOrderDetailT_OrderDetailID T_OrderDetailID,
|
|
ob.M_BranchCode OriginM_BranchCode,
|
|
db.M_BranchCode DestinationM_BranchCode, db.M_BranchIPAddress DestinationIPAddress,
|
|
T_OrderHeaderID, T_OrderHeaderLabNumber,
|
|
T_OrderHeaderM_PatientID,
|
|
Concat(M_TitleName, ' ',M_PatientName) M_PatientName,
|
|
if( T_OrderHeaderFoNote <> '' or T_OrderHeaderFoNote is null,
|
|
left(concat(T_OrderHeaderFoNote,' (', sfo.M_StaffName , ')' ),255) , '') FoNote,
|
|
if( T_OrderHeaderVerificationNote <> '' or T_OrderHeaderVerificationNote is null,
|
|
concat(T_OrderHeaderVerificationNote,' (', sver.M_StaffName , ')' ), '' ) VerificationNote,
|
|
if( T_OrderHeaderSamplingNote <> '' or T_OrderHeaderSamplingNote is null ,
|
|
concat(T_OrderHeaderSamplingNote,' (', ssam.M_StaffName , ')' ), '') SamplingNote
|
|
from
|
|
t_ref_deliveryorder_detail
|
|
join t_ref_deliveryorder on T_RefDeliveryOrderDetailT_RefDeliveryOrderID = T_RefDeliveryOrderID
|
|
and T_RefDeliveryOrderIsConfirm <> 'N'
|
|
join m_branch ob on T_RefDeliveryOrderM_BranchID = ob.M_BranchID
|
|
join m_branch db on T_RefDeliveryOrderM_DestinationID = db.M_BranchID
|
|
join t_orderheader on T_RefDeliveryOrderDetailT_OrderHeaderID = T_OrderHeaderID
|
|
join m_patient on T_OrderHeaderM_PatientID = M_PatientID
|
|
join m_title on M_PatientM_TitleID = M_TitleID
|
|
left join m_user ufo on T_OrderHeaderFoNoteM_UserID = ufo.M_UserID
|
|
left join m_staff sfo on ufo.M_UserM_StaffID = sfo.M_StaffID
|
|
left join m_user uver on T_OrderHeaderVerificationNoteM_UserID = uver.M_UserID
|
|
left join m_staff sver on uver.M_UserM_StaffID = sver.M_StaffID
|
|
left join m_user usam on T_OrderHeaderVerificationNoteM_UserID = usam.M_UserID
|
|
left join m_staff ssam on usam.M_UserM_StaffID = ssam.M_StaffID
|
|
where T_RefDeliveryOrderID = ?
|
|
and T_RefDeliveryOrderDetailIsConfirm = 'Y'
|
|
";
|
|
$qry = $this->db->query($sql, array($deliveryID) );
|
|
$rows = array();
|
|
$branch_ip_address = "";
|
|
if($qry) {
|
|
$rows = $qry->result_array();
|
|
|
|
if (count($rows) == 0 ) {
|
|
echo "No Detail Records. => ";
|
|
$this->db->query("update t_ref_deliveryorder set T_RefDeliveryOrderIsConfirm = 'Z' where T_RefDeliveryOrderID = ?", array($deliveryID));
|
|
return false;
|
|
}
|
|
$sqlp = "select * from m_patient where M_PatientID = ?";
|
|
$sqlpa = "select * from m_patientaddress where M_PatientAddressM_PatientID = ?";
|
|
$sqlpt = "select * from m_title where M_TitleID = ?";
|
|
|
|
$sqlt = "select
|
|
T_OrderDetailT_TestID T_TestID,
|
|
T_OrderDetailT_TestName T_TestName,
|
|
T_OrderDetailT_TestSasCode T_TestSasCode,
|
|
T_OrderPromiseDateTime,
|
|
T_RefDeliveryOrderDetailT_BarcodeLabBarcode T_BarcodeLabBarcode,
|
|
T_OrderDetailID
|
|
from
|
|
t_ref_deliveryorder_detail
|
|
join t_orderdetail on T_RefDeliveryOrderDetailT_OrderDetailID = T_OrderDetailID
|
|
and T_RefDeliveryOrderDetailT_OrderDetailID = ?
|
|
and T_RefDeliveryOrderDetailIsActive = 'Y'
|
|
and T_RefDeliveryOrderDetailIsConfirm = 'Y'
|
|
left join t_orderpromise on T_OrderDetailT_OrderPromiseID = T_OrderPromiseID
|
|
";
|
|
$sqlct = "select
|
|
T_OrderDetailT_OrderHeaderID T_OrderHeaderID,
|
|
T_OrderDetailT_TestID T_TestID,
|
|
T_OrderDetailT_TestName T_TestName,
|
|
T_OrderPromiseDateTime,
|
|
T_OrderDetailID
|
|
from
|
|
t_ref_deliveryorder_child
|
|
join t_orderdetail on T_RefDeliveryOrderChildT_OrderDetailID = T_OrderDetailID
|
|
and T_RefDeliveryOrderChildT_RefDeliveryOrderDetailID= ?
|
|
left join t_orderpromise on T_OrderDetailT_OrderPromiseID = T_OrderPromiseID";
|
|
|
|
$arr_price_test = array();
|
|
foreach($rows as $idx => $r) {
|
|
//patient & address
|
|
$branch_ip_address = $r["DestinationIPAddress"];
|
|
$patientID = $r["T_OrderHeaderM_PatientID"];
|
|
$headerID = $r["T_OrderHeaderID"];
|
|
$detailID= $r["T_OrderDetailID"];
|
|
$deliveryDetailID = $r["T_RefDeliveryOrderDetailID"];
|
|
$qryp = $this->db->query($sqlp, array($patientID));
|
|
$patient = array();
|
|
if ($qryp) {
|
|
$rowsp = $qryp->result_array();
|
|
if( count($rowsp) > 0 ) {
|
|
$patient = $rowsp[0];
|
|
$titleID = $patient["M_PatientM_TitleID"];
|
|
$qrypa = $this->db->query($sqlpa, array($patientID));
|
|
if ($qrypa) {
|
|
$patient["address"] = $qrypa->result_array();
|
|
}
|
|
$qrypt = $this->db->query($sqlpt, array($titleID));
|
|
if ($qrypt) {
|
|
$rowspt = $qrypt->result_array();
|
|
if (count($rowspt) > 0 ) $patient["title"] = $rowspt[0];
|
|
}
|
|
}
|
|
}
|
|
$rows[$idx]["patient"] = $patient;
|
|
// test
|
|
$qryt = $this->db->query($sqlt,array($detailID));
|
|
$arr_test = array();
|
|
|
|
if ($qryt) {
|
|
$rowst = $qryt->result_array();
|
|
if (! isset($arr_price_test[$headerID])) {
|
|
$arr_price_test[$headerID] = array();
|
|
}
|
|
foreach($rowst as $r ) {
|
|
$sasCode = substr($r["T_TestSasCode"],0,8);
|
|
$curSasCode = $r["T_TestSasCode"];
|
|
if ( ! isset($arr_price_test[$headerID][$sasCode]) ) {
|
|
if(strlen($curSasCode) == 8) $arr_price_test[$headerID][$sasCode] = true;
|
|
$rows[$idx]["test"][] = $r;
|
|
$arr_test[]=$r["T_TestID"];
|
|
}
|
|
}
|
|
}
|
|
// $child_test
|
|
$qryct = $this->db->query($sqlct,array($deliveryDetailID));
|
|
if ($qryct) {
|
|
$rowsct = $qryct->result_array();
|
|
$rows[$idx]["child_test"] = $rowsct;
|
|
}
|
|
|
|
$reqs = array();
|
|
if (count($arr_test) > 0 ) {
|
|
$sqlr = "select * from t_orderreq where T_OrderReqT_OrderHeaderID = ?";
|
|
$qryr = $this->db->query($sqlr, array($headerID));
|
|
if ($qryr) {
|
|
$rowsr = $qryr->result_array();
|
|
foreach($rowsr as $r) {
|
|
$a_test_r = json_decode($r["T_OrderReqT_TestID"]);
|
|
$a_x = array_intersect($arr_test,$a_test_r);
|
|
if( count($a_x) > 0 ) $reqs[] = $r;
|
|
}
|
|
}
|
|
}
|
|
$rows[$idx]["requirements"] = $reqs;
|
|
}
|
|
$o_rows=$rows;
|
|
$rows = array();
|
|
foreach($o_rows as $r) {
|
|
if (isset($r["test"]) ) {
|
|
$rows[] = $r;
|
|
}
|
|
}
|
|
$param = json_encode($rows);
|
|
|
|
$url = "http://$branch_ip_address/one-api/tools/incomingref_v4/receive";
|
|
|
|
$result = $this->post($url,$param);
|
|
echo "to $url \nresponse : $result\n";
|
|
$result = json_decode($result,true);
|
|
if ($result["status"] == "OK" ) {
|
|
$sqlu = "update t_ref_deliveryorder set T_RefDeliveryOrderIsConfirm = 'S'
|
|
where T_RefDeliveryOrderID = ?";
|
|
$this->db->query($sqlu, array($deliveryID));
|
|
return true;
|
|
} else {
|
|
print_r($result);
|
|
}
|
|
} else {
|
|
print_r($this->db->error());
|
|
}
|
|
return false;
|
|
}
|
|
public function fix()
|
|
{
|
|
$sql = "SELECT
|
|
T_RefDeliveryOrderID,
|
|
T_RefDeliveryOrderDetailT_WorklistRefConfirmDetailID,
|
|
T_RefDeliveryOrderDate,
|
|
T_RefDeliveryOrderDetailID,
|
|
T_RefDeliveryOrderDetailT_OrderHeaderID,
|
|
T_RefDeliveryOrderDetailT_OrderDetailID,
|
|
T_RefDeliveryOrderDetailT_TestID,
|
|
T_RefDeliveryOrderDetailT_BarcodeLabBarcode,
|
|
T_BarcodeLabBarcode
|
|
|
|
FROM
|
|
t_ref_deliveryorder_detail
|
|
JOIN t_ref_deliveryorder ON T_RefDeliveryOrderID = T_RefDeliveryOrderDetailT_RefDeliveryOrderID AND T_RefDeliveryOrderIsActive = 'Y' AND T_RefDeliveryOrderIsConfirm = 'N'
|
|
JOIN t_test ON T_TestID = T_RefDeliveryOrderDetailT_TestID
|
|
JOIN t_barcodelab ON T_BarcodeLabT_OrderHeaderID = T_RefDeliveryOrderDetailT_OrderHeaderID
|
|
AND T_BarcodeLabT_SampleTypeID = T_TestT_SampleTypeID
|
|
AND T_BarcodeLabIsActive = 'Y'
|
|
WHERE T_RefDeliveryOrderDetailIsActive = 'Y' AND
|
|
T_RefDeliveryOrderDetailT_BarcodeLabBarcode <> T_BarcodeLabBarcode AND
|
|
T_RefDeliveryOrderDate >= '2024-08-01'";
|
|
|
|
$qry = $this->db->query($sql);
|
|
$rows = $qry->result_array();
|
|
|
|
if (count($rows) > 0) {
|
|
foreach ($rows as $k => $v) {
|
|
$sql = "UPDATE t_ref_deliveryorder_detail
|
|
SET T_RefDeliveryOrderDetailT_BarcodeLabBarcode = '{$v['T_BarcodeLabBarcode']}'
|
|
WHERE T_RefDeliveryOrderDetailID = {$v['T_RefDeliveryOrderDetailID']}";
|
|
$qry = $this->db->query($sql);
|
|
|
|
$sql = "UPDATE t_worklist_ref_confirmdetail
|
|
SET T_WorklistRefConfirmDetailT_BarcodeLabBarcode = '{$v['T_BarcodeLabBarcode']}'
|
|
WHERE T_WorklistRefConfirmDetailID = {$v['T_RefDeliveryOrderDetailT_WorklistRefConfirmDetailID']}";
|
|
$qry = $this->db->query($sql);
|
|
}
|
|
}
|
|
}
|
|
function post($url,$data) {
|
|
$zdata = gzdeflate($data);
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $zdata);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
//curl_setopt($ch, CURLOPT_VERBOSE, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
|
'Content-Type: application/json',
|
|
'Content-Length: ' . strlen($zdata))
|
|
);
|
|
$result = curl_exec($ch);
|
|
//echo "RST : $result ";
|
|
return $result;
|
|
}
|
|
}
|