Files
BE_CPONE/application/libraries/Apilis.php--300825
2026-04-27 10:26:26 +07:00

325 lines
10 KiB
Plaintext

<?php
defined("BASEPATH") or exit("No direct script access allowed");
class Apilis
{
function __construct()
{
$CI = &get_instance();
$this->db_smartone = $CI->load->database("default", true);
$this->db_onedev = $CI->load->database("default", true);
}
function clean_mysqli_connection($dbc)
{
while (mysqli_more_results($dbc)) {
if (mysqli_next_result($dbc)) {
$result = mysqli_use_result($dbc);
if (get_class($result) == 'mysqli_stmt') {
mysqli_stmt_free_result($result);
} else {
unset($result);
}
}
}
}
function GetOrderDetail($nolab)
{
//$param = $this->param();
//$nolab = $param["orderNumber"];
$debug=true;
$sql = "SELECT
M_BranchCodeBridging AS outletId,
T_OrderHeaderLabNumber as orderNumber,
REPLACE(T_OrderHeaderDate, '-', '/') AS orderDT,
T_OrderHeaderLabNumber AS visitNumber,
M_PatientNoreg AS patientId,
M_PatientName AS patientName,
IF(LOWER(M_PatientGender) = 'male', 'L', 'P') AS sexId,
IF(LOWER(M_PatientGender) = 'male', 'Male', 'Female') AS sexName,
REPLACE(M_PatientDOB, '-', '/') AS dob,
IFNULL(M_TitleName,'') as titleName,
'' AS pob,
M_PatientAddress AS addressName,
'' AS cityId,
M_PatientAddressCity AS cityName,
M_PatientHp as mobileNumber,
'' AS phoneNumber,
'' AS faxNumber,
M_PatientEmail as email,
M_PatientIdentifierValue as idNIK,
'' as idSIM,
'' as idPassport,
'CpOne001' AS physicianId,
'Dokter Laboratorium' AS physicianName,
'' as isCito,
'' as diagnoseId,
'' as diagnoseName,
M_BranchCode AS guarantorId,
M_BranchName AS guarantorName,
'Cp1' AS agreementId,
'CpOne' AS agreementName,
'' as serviceUnitId,
'' as serviceUnitName,
'' as wardPoliId,
'' as wardPoliName,
'' as roomId,
'' as roomName,
CorporateID as bedId,
CorporateName as bedName,
'' as classId,
'' as className,
1 as regUserId,
'admin' as regUserName,
RIGHT(M_BranchCode, 6) AS BranchCode,
M_BranchName AS BranchName,
FALSE AS ReceivedFlag,
NULL AS LabRegNo,
NULL AS ReceivedDateTime,
IFNULL(Nat_TestMapCode, 'Un-Map') AS ItemCode,
T_TestName AS ItemName
FROM
t_orderheader
JOIN
m_branch ON T_OrderHeaderLabNumber = ?
AND T_OrderHeaderIsActive = 'Y'
AND T_OrderHeaderM_BranchID = M_BranchID
AND M_BranchIsActive = 'Y'
JOIN
corporate ON T_OrderHeaderCorporateID = CorporateID
AND CorporateIsActive = 'Y'
JOIN
m_patient ON T_OrderHeaderM_PatientID = M_PatientID
JOIN
t_orderdetail ON T_OrderHeaderID = T_OrderDetailT_OrderHeaderID
AND T_OrderDetailIsActive = 'Y'
JOIN
t_test ON T_OrderDetailT_TestID = T_TestID
AND T_TestIsPrice = 'Y'
AND T_TestNat_GroupID = 1
LEFT JOIN
nat_testmap ON T_TestNat_TestID = Nat_TestMapNat_TestID
AND Nat_TestMapIsActive = 'Y'
LEFT JOIN
m_title ON M_PatientM_TitleID = M_TitleID
ORDER BY
T_OrderHeaderLabNumber, Nat_TestMapCode;";
$qry = $this->db_onedev->query($sql, [$nolab]);
//echo $this->db_onedev->last_query();
if (!$qry) {
echo json_encode([
"Status" => ["OK" => false, "Code" => 0, "Messages" => "Get Order Detail issue [Order004]"],
"orderItemList" => [],
]);
exit;
}
$rows = $qry->result_array();
//echo json_encode($rows);
//echo count($rows);
if($debug){
$q_las = $this->db_onedev->last_query();
$sql = "INSERT INTO debug_log (DebugLogCode, DebugLogRefID, DebugLogMessage)
VALUES ('QUERYPO',?,?)";
$qry = $this->db_onedev->query($sql,[$nolab,$q_las]);
//echo $this->db_onedev->last_query();
}
if(count($rows) > 0){
$result = [];
foreach ($rows as $idx => $r) {
if ($idx == 0) {
$result = $r;
unset($result["ItemCode"]);
unset($result["ItemName"]);
$result["orderItemList"] = [];
$result["ReceivedFlag"] = (bool) false;
}
$result["orderItemList"][] = [
"OrderItemId" => $r["ItemCode"],
"OrderItemName" => $r["ItemName"]
];
}
if($debug){
$sql = "INSERT INTO debug_log (DebugLogCode, DebugLogRefID, DebugLogMessage)
VALUES ('RSTDT',?,?)";
$qry = $this->db_onedev->query($sql,[$nolab,json_encode($result)]);
}
//echo json_encode($result);
if(count($result) > 0){
$rst = $this->send_order($result);
}
return true;
}else{
return false;
}
}
function send_order($params)
{
$this->insert_api_push("new",$params,'');
$sql = "SELECT S_SystemsAPiPushUrl as urlx
FROM conf_systems
WHERE S_SystemsIsActive = 'Y'
LIMIT 1";
$qry = $this->db_onedev->query($sql);
// URL endpoint yang ingin dikirimkan POST request
$url = $qry->row()->urlx;
if($debug){
$sql = "INSERT INTO debug_log (DebugLogCode, DebugLogRefID, DebugLogMessage)
VALUES ('QUERYSYS',?,?)";
$qry = $this->db_onedev->query($sql,[$nolab,json_encode($url)]);
}
// Inisialisasi CURL
$ch = curl_init($url);
// Mengatur CURL untuk mengirim POST request dengan JSON
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Accept: application/json'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
// Eksekusi CURL dan mendapatkan respons dari server
$response = curl_exec($ch);
if($debug){
$sql = "INSERT INTO debug_log (DebugLogCode, DebugLogRefID, DebugLogMessage)
VALUES ('RSPN',?,?)";
$qry = $this->db_onedev->query($sql,[$nolab,json_encode($response)]);
}
// Cek apakah ada error saat eksekusi CURL
/*if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
$response = curl_error($ch);
}*/
// Tutup CURL
curl_close($ch);
// print_r($response);
$this->insert_api_push("new",$params,$response);
$rst = json_decode($response);
$rst_rspn = stripslashes(json_encode($rst));
return $rst;
}
function insert_api_push($type,$params,$response){
//echo $response;
$rst = json_decode($response);
//print_r($rst);
//echo $rst->message;
$rst_save = stripslashes(json_encode($rst));
$res = json_encode($response);
$save_param = $params;
$nolab_lis = '';
$message = '';
if($response && $rst){
$nolab_lis = $rst->orderNumber?$rst->orderNumber:"Kosong";
$message = $rst->message;
}
$sql = "INSERT INTO api_push (
Api_PushDateTime,
Api_PushT_OrderHeaderLabNumber,
Api_PushParams,
Api_PushResponse,
Api_PushRetry,
Api_PushLISLabNumber,
Api_PushStatus,
Api_PushCreated
)
VALUES(NOW(),?,?,?,?,?,?,NOW())
ON DUPLICATE KEY
UPDATE Api_PushLastUpdated = NOW(), Api_PushParams = ?, Api_PushResponse = ?, Api_PushRetry = IF('{$type}' = 'retry', Api_PushRetry + 1,Api_PushRetry), Api_PushStatus = ?";
$qry = $this->db_onedev->query($sql, [$params['orderNumber'], stripslashes(json_encode($save_param)), $rst_save, 1, $nolab_lis, $message,stripslashes(json_encode($save_param)), $rst_save, $message]);
if($debug){
$sql = "INSERT INTO debug_log (DebugLogCode, DebugLogRefID, Debug_LogMessage)
VALUES ('QRYPUSH',?,?)";
$qry = $this->db_onedev->query($sql,[$nolab_lis,$this->db_onedev->last_query()]);
}
//echo $this->db_onedev->last_query();
//exit;
return true;
}
function generate_kelainan_nonlab($orderID,$userID){
$sql = "SELECT So_ResultEntryID, T_OrderHeaderID, T_TestNat_TestID, Mcu_SummaryNonlabID, Mcu_SummaryNonlabMcu_KelainanID, Mcu_SummaryNonlabID
FROM so_resultentry_category_result
JOIN so_resultentry ON So_ResultEntryCategoryResultSo_ResultEntryID = So_ResultEntryID AND
So_ResultEntryStatus <> 'NEW' AND So_ResultEntryIsActive = 'Y'
JOIN t_orderheader ON So_ResultEntryT_OrderHeaderID = T_OrderHeaderID AND T_OrderHeaderIsActive = 'Y' AND
T_OrderHeaderID = ?
JOIN t_orderdetail ON So_ResultEntryT_OrderDetailID = T_OrderDetailID AND T_OrderDetailIsActive = 'Y'
JOIN t_test ON T_OrderDetailT_TestID = T_TestID
JOIN mcu_summarynonlab ON Mcu_SummaryNonlabNat_TestID = T_TestNat_TestID AND
Mcu_SummaryNonlabIsActive = 'Y' AND So_ResultEntryCategoryNonlabConclusionDetailID IN (Mcu_SummaryNonlabConclusionDetailIDs)
WHERE
So_ResultEntryCategoryResultIsActive = 'Y'";
$qry = $this->db_onedev->query($sql,array($orderID));
if (!$qry) {
echo $this->db_onedev->last_query();
echo json_encode([
"status" => "ERR",
"message" => "Error get order | " . $this->db_onedev->error()["message"]
]);
exit;
}
$rows = $qry->result_array();
$sql = "UPDATE t_kelainan_nonlab SET
T_KelainanNonLabIsActive = 'N'
WHERE
T_KelainanNonLabT_OrderHeaderID = ? AND
T_KelainanNonLabIsActive = 'Y'
";
$qry = $this->db_onedev->query($sql,array($orderID));
foreach ($rows as $key => $value) {
$sql = "INSERT INTO t_kelainan_nonlab (
T_KelainanNonLabSo_ResultEntryID,
T_KelainanNonLabT_OrderHeaderID,
T_KelainanNonLabNat_TestID,
T_KelainanNonLabMcu_SummaryNonlabID,
T_KelainanNonLabCreatedUserID,
T_KelainanLabNonCreated
) VALUES(?,?,?,?,?,NOW())";
$qry = $this->db_onedev->query($sql,[
$value['So_ResultEntryID'],
$value['T_OrderHeaderID'],
$value['T_TestNat_TestID'],
$value['Mcu_SummaryNonlabID'],
$userID
]);
}
}
}