330 lines
12 KiB
PHP
330 lines
12 KiB
PHP
<?php
|
|
|
|
class R_upload_result extends MY_Controller
|
|
{
|
|
var $db_onedev;
|
|
|
|
var $url_upload;
|
|
|
|
public function index()
|
|
{
|
|
echo "RE Patient API";
|
|
}
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->db_onedev = $this->load->database("onedev", true);
|
|
$this->db_log = $this->load->database("one_lab_log", true);
|
|
$this->url_upload = "https://devcpone.aplikasi.web.id/one-api/tools/result_westone/receive_result";
|
|
}
|
|
|
|
function autoUploadResult()
|
|
{
|
|
$sql = "SELECT DISTINCT T_OrderDetailT_OrderHeaderID
|
|
FROM t_orderdetail
|
|
WHERE
|
|
T_OrderDetailIsActive = 'Y' AND
|
|
T_OrderDetailValidation = 'Y' AND
|
|
T_OrderDetailDetailStatusUpload = 'R'";
|
|
|
|
$query = $this->db_onedev->query($sql);
|
|
$rows = $query->result_array();
|
|
if(count($rows) == 0)
|
|
{
|
|
echo json_encode(["status" => "OK", "message" => "No order detail to upload result"]);
|
|
exit;
|
|
}else{
|
|
$arr_response = [];
|
|
foreach($rows as $row){
|
|
$result = $this->doUploadResult($row['T_OrderDetailT_OrderHeaderID']);
|
|
$arr_response[] = $result;
|
|
}
|
|
|
|
echo json_encode($arr_response);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
function uploadResult()
|
|
{
|
|
$prm = $this->sys_input;
|
|
$new_order_id = $prm['T_OrderHeaderID'];
|
|
|
|
$result = $this->doUploadResult($new_order_id);
|
|
if($result['status'] == 'ERR'){
|
|
$this->sys_error($result['message']);
|
|
exit;
|
|
}
|
|
$this->sys_ok($result);
|
|
exit;
|
|
}
|
|
|
|
function doUploadResult($new_order_id){
|
|
$sql = "SELECT t_orderdetail.*, Log_DownloadT_OrderHeaderID as old_order_id,
|
|
userver.M_UserUsername as ver_user,
|
|
userval.M_UserUsername as val_user,
|
|
T_OrderHeaderLabNumber as lab_number,
|
|
IFNULL(userver.M_UserFullName, '') as ver_user_name,
|
|
IFNULL(userval.M_UserFullName, '') as val_user_name
|
|
FROM t_orderdetail
|
|
JOIN t_orderheader ON T_OrderDetailT_OrderHeaderID = T_OrderHeaderID
|
|
JOIN one_lab_log.log_download ON Log_DownloadT_OrderHeaderID = T_OrderDetailT_OrderHeaderID AND
|
|
Log_DownloadStatus = 'D' AND Log_DownloadT_OrderHeaderID <> 0
|
|
LEFT JOIN m_user userver ON T_OrderDetailValUserID = userver.M_UserID
|
|
LEFT JOIN m_user userval ON T_OrderDetailValUserID = userval.M_UserID
|
|
WHERE
|
|
T_OrderDetailT_OrderHeaderID = ? AND
|
|
T_OrderDetailIsActive = 'Y' AND
|
|
T_OrderDetailVerification = 'Y'";
|
|
$query = $this->db_onedev->query($sql, [$new_order_id]);
|
|
if (!$query)
|
|
{
|
|
return ["status" => "ERR", "message" => "Gagal select order detail"];
|
|
}
|
|
|
|
$arr_result = $query->result_array();
|
|
if(count($arr_result) == 0)
|
|
{
|
|
return ["status" => "ERR", "message" => "Belum ada hasil pemeriksaan yang bisa diupload, silahkan validasi terlebih dahulu"];
|
|
}
|
|
$old_order_id = $arr_result[0]['old_order_id'];
|
|
$lab_number = $arr_result[0]['lab_number'];
|
|
$user_id = $this->sys_user['M_UserID']?$this->sys_user['M_UserID']:0;
|
|
$url = $this->url_upload;
|
|
$log_id = -1;
|
|
|
|
$sql = "SELECT {$old_order_id} as CriticalValueT_OrderHeaderID,
|
|
T_OrderDetailT_OrderDetailOldID as CriticalValueT_OrderDetailID,
|
|
CriticalValueDescription,
|
|
CriticalValueCreated,
|
|
CriticalValueIsReported,
|
|
CriticalValueReportedM_UserID,
|
|
CriticalValueReportedDate,
|
|
CriticalValueReportedTo,
|
|
CriticalValueIsAdvised,
|
|
CriticalValueAdvisedDate,
|
|
CriticalValueAdvisedBy,
|
|
CriticalValueAdvisedNote,
|
|
CriticalValueAdvicedM_UserID,
|
|
CriticalValueIsActive
|
|
FROM critical_value
|
|
JOIN t_orderdetail ON T_OrderDetailID = CriticalValueT_OrderDetailID AND T_OrderDetailIsActive = 'Y'
|
|
WHERE
|
|
CriticalValueT_OrderHeaderID = ? AND CriticalValueIsActive = 'Y'";
|
|
$query = $this->db_onedev->query($sql, [$new_order_id]);
|
|
$dt_critical_value = $query->result_array();
|
|
|
|
$critical_values = [];
|
|
if(count($dt_critical_value) > 0)
|
|
{
|
|
$critical_values = $dt_critical_value;
|
|
}
|
|
$arr_upload = [
|
|
"order_id" => $old_order_id,
|
|
"lab_number" => $lab_number,
|
|
"results" => $arr_result,
|
|
"critical_values" => $critical_values
|
|
];
|
|
|
|
// Robust JSON encoding for data to avoid invalid UTF-8 issues
|
|
$json_upload = json_encode($arr_upload, JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE);
|
|
|
|
if ($json_upload === false) {
|
|
return ["status" => "ERR", "message" => "Gagal encode JSON payload: ".json_last_error_msg()];
|
|
}
|
|
|
|
$sql = "INSERT INTO log_uploadresult(
|
|
Log_UploadResultLocalT_OrderHeaderID,
|
|
Log_UploadResultT_OrderHeaderID,
|
|
Log_UploadResultT_OrderHeaderLabNumber,
|
|
Log_UploadResultUrl,
|
|
Log_UploadResultData,
|
|
Log_UploadResultCreatedUserID,
|
|
Log_UploadResultCreated
|
|
|
|
) VALUES (
|
|
?, ?, ?, ?, ?, ?,NOW()
|
|
)";
|
|
$query = $this->db_log->query($sql, [
|
|
$new_order_id,
|
|
$old_order_id,
|
|
$lab_number,
|
|
$url,
|
|
$json_upload,
|
|
$user_id
|
|
]);
|
|
if (!$query)
|
|
{
|
|
return ["status" => "ERR", "message" => "Gagal insert log upload result"];
|
|
}
|
|
$log_id = $this->db_log->insert_id();
|
|
|
|
// Compress data if larger than 1MB
|
|
$json_size = strlen($json_upload);
|
|
if ($json_size > (1024 * 1024)) { // 1MB
|
|
$compressed_data = gzcompress($json_upload, 9);
|
|
$md5 = md5($json_upload); // MD5 of original data
|
|
$param = json_encode([
|
|
"md5" => $md5,
|
|
"compressed" => true,
|
|
"data" => base64_encode($compressed_data)
|
|
], JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE);
|
|
error_log("Data compressed: " . round($json_size/1024/1024, 2) . "MB -> " . round(strlen($compressed_data)/1024/1024, 2) . "MB");
|
|
} else {
|
|
// Use the exact same JSON string for MD5 to ensure parity with receiver
|
|
$md5 = md5($json_upload);
|
|
$param = json_encode([
|
|
"md5" => $md5,
|
|
"data" => $arr_upload
|
|
], JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE);
|
|
}
|
|
if ($param === false) {
|
|
|
|
return ["status" => "ERR", "message" => "Gagal encode JSON payload: ".json_last_error_msg()];
|
|
}
|
|
|
|
$sql = "UPDATE log_uploadresult SET
|
|
Log_UploadResultStatus = 'P'
|
|
WHERE Log_UploadResultID = ?";
|
|
$query = $this->db_log->query($sql, [$log_id]);
|
|
if (!$query)
|
|
{
|
|
echo $this->db_log->last_query();
|
|
$this->sys_error_db("UPDATE LOG UPLOAD RESULT PROCESS", $this->db_log);
|
|
exit;
|
|
}
|
|
|
|
$response = $this->post($url, $param);
|
|
$j_response = json_decode($response, true);
|
|
$status = 'D';
|
|
$message = $response;
|
|
if (!$j_response) {
|
|
$status = 'E';
|
|
return ["status" => "ERR", "message" => "Error Json : $response"];
|
|
}
|
|
if ($j_response["status"] == "ERR") {
|
|
$status = 'E';
|
|
return ["status" => "ERR", "message" => $j_response["message"]];
|
|
}
|
|
$sql = "UPDATE log_uploadresult SET Log_UploadResultStatus = ?, Log_UploadResultResponse = ? WHERE Log_UploadResultID = ?";
|
|
$query = $this->db_log->query($sql, [$status, $message, $log_id]);
|
|
if (!$query)
|
|
{
|
|
return ["status" => "ERR", "message" => "Gagal update log upload result"];
|
|
}
|
|
if ($status == 'D')
|
|
{
|
|
$sql = "UPDATE t_orderdetail SET
|
|
T_OrderDetailDetailStatusUpload = 'D',
|
|
T_OrderDetailDetailTimeUpload = NOW()
|
|
WHERE
|
|
T_OrderDetailT_OrderHeaderID = ? AND
|
|
T_OrderDetailDetailStatusUpload = 'R'";
|
|
$query = $this->db_onedev->query($sql, [$new_order_id]);
|
|
if (!$query)
|
|
{
|
|
return ["status" => "ERR", "message" => "Gagal update order detail - lokal"];
|
|
}
|
|
|
|
return ["status" => "OK", "message" => "Order ".$lab_number." detail uploaded result"];
|
|
|
|
}
|
|
else
|
|
{
|
|
return ["status" => "ERR", "message" => $message];
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function get($url, $timeout = 60, $c_timeout = 5)
|
|
{
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $c_timeout);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
|
// Preserve POST + body on 301/302 redirects if any
|
|
if (defined('CURLOPT_POSTREDIR') && defined('CURL_REDIR_POST_ALL')) {
|
|
curl_setopt($ch, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
|
|
}
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
|
$result = curl_exec($ch);
|
|
$err_msg = curl_error($ch);
|
|
if ($err_msg != "") {
|
|
return json_encode(["status" => "ERR", "message" => $err_msg]);
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
function post($url, $data, $timeout = 180, $c_timeout = 5)
|
|
{
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $c_timeout);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
"Content-Type: application/json",
|
|
"Content-Length: " . strlen($data),
|
|
"Expect:"
|
|
]);
|
|
$result = curl_exec($ch);
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$err_msg = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
// Additional cURL diagnostics
|
|
if ($err_msg !== "") {
|
|
error_log("cURL error: ".$err_msg);
|
|
}
|
|
error_log("HTTP Code: ".$http_code);
|
|
error_log("Response head: ".substr((string)$result, 0, 1000));
|
|
|
|
if ($err_msg != "") {
|
|
return json_encode([
|
|
"status" => "ERR",
|
|
"message" => $err_msg,
|
|
"url" => $url,
|
|
"http_code" => $http_code,
|
|
"data" => json_decode($data, true),
|
|
]);
|
|
}
|
|
|
|
if ($http_code != 200) {
|
|
return json_encode([
|
|
"status" => "ERR",
|
|
"message" => "HTTP Error: " . $http_code,
|
|
"url" => $url,
|
|
"response" => $result
|
|
]);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
function insert_error($fn,$query,$data,$message)
|
|
{
|
|
$sql = "INSERT INTO log_error_uploadresult(
|
|
Log_ErrorUploadResultFn,
|
|
Log_ErrorUploadResultQuery,
|
|
Log_ErrorUploadResultData,
|
|
Log_ErrorUploadResultMessage,
|
|
Log_ErrorUploadResultCreated
|
|
) VALUES (?,?,?,?,NOW())";
|
|
$query = $this->db_log->query($sql, [$fn,$query,$data,$message]);
|
|
if(!$query)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|