Files
2026-04-27 10:26:26 +07:00

434 lines
17 KiB
PHP

<?php
class Encounter extends MY_Controller
{
var $db;
var $load;
var $satusehat;
function __construct()
{
parent::__construct();
$this->load->library('Satusehat');
}
function index()
{
echo "Satu Sehat Encounter";
}
function getencounter()
{
try {
$prm = $this->sys_input;
$orderheaderID = $prm['orderheaderID'];
//cek db one_healt encounter sudah ada atau belum
$sql = "SELECT * FROM one_health.encounter
WHERE EncounterT_orderHeaderID = ?
AND EncounterIsActive = 'Y'";
$qry = $this->db->query($sql, [$orderheaderID]);
$last_qry = $this->db->last_query();
if (!$qry) {
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
$result_cek = $qry->result_array();
if (count($result_cek) == 0) {
//blm ada data di one_helath makan diinsertkan
$encounter_uuid = $this->satusehat->gen_uuid();
//cek db one_hs eapakah order home service atau tidak
$sql = "SELECT T_OrderT_OrderHeaderID FROM one_hs.t_order
WHERE T_OrderT_OrderHeaderID = ?;";
$qry = $this->db->query($sql, [$orderheaderID]);
$last_qry = $this->db->last_query();
if (!$qry) {
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
$cek_hs = $qry->result_array();
$codeClass = "AMB";
$codeDisplay = "ambulatory";
if (count($cek_hs) > 0) {
$codeClass = "HH";
$codeDisplay = "home health";
}
$encounter = array(
"fullUrl" => "urn:uuid:{$encounter_uuid}",
"resource" => [
"resourceType" => "Encounter",
"status" => "finished",
"class" => [
"system" => "http://terminology.hl7.org/CodeSystem/v3-ActCode",
"code" => $codeClass,
"display" => $codeDisplay
],
]
);
//get organization
$sql = "SELECT organizationID
FROM one_health.organization
WHERE organizationIsActive = 'Y'";
$qry = $this->db->query($sql, []);
$last_qry = $this->db->last_query();
if (!$qry) {
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
$organizationID = $qry->result_array()[0]['organizationID'];
$organization = [
"reference" => "Organization/$organizationID"
];
//get subject
//subject reference
$sql = "SELECT
T_OrderHeaderCreated AS T_OrderHeaderCreated,
T_OrderHeaderLabNumber,
T_OrderHeaderM_PatientID,
OHPatientMapIHSNumber,
T_OrderHeaderDiagnose,
T_OrderHeaderPjM_DoctorID,
T_OrderHeaderSenderM_DoctorID,
OHDoctorMapIHSNumber,
M_PatientName,
M_DoctorName
FROM t_orderheader
JOIN m_patient
ON T_OrderHeaderM_PatientID = M_PatientID
AND T_OrderHeaderID = ?
AND T_OrderHeaderIsActive = 'Y'
AND M_PatientIsActive = 'Y'
JOIN m_doctor
ON T_OrderHeaderPjM_DoctorID = M_DoctorID
AND M_DoctorIsActive ='Y'
LEFT JOIN one_health.oh_doctor_map
ON T_OrderHeaderPjM_DoctorID = OHDoctorMapM_DoctorID
AND OHDoctorMapIsActive = 'Y'
LEFT JOIN one_health.oh_patient_map
ON T_OrderHeaderM_PatientID = OHPatientMapM_PatientID
AND OHPatientMapIsActive = 'Y'";
$qry = $this->db->query($sql, [$orderheaderID]);
$last_qry = $this->db->last_query();
if (!$qry) {
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
$dataBisone = $qry->result_array()[0];
$subject = array(
"reference" => "Patient/{$dataBisone['OHPatientMapIHSNumber']}",
"display" => $dataBisone['M_PatientName']
);
$participant = [
[
"type" => [
[
"coding" => [
[
"system" => "http://terminology.hl7.org/CodeSystem/v3-ParticipationType",
"code" => "ATND",
"display" => "attender"
]
]
]
],
"individual" => [
"reference" => "Practitioner/{$dataBisone['OHDoctorMapIHSNumber']}",
"display" => $dataBisone['M_DoctorName']
]
]
];
//get period end
$sql = "SELECT
Result_ProcessToOfficeSendTime AS Result_ProcessToOfficeSendTime
FROM result_processtooffice
WHERE Result_ProcessToOfficeT_OrderHeaderID = ?
AND Result_ProcessToOfficeIsActive = 'Y'";
$qry = $this->db->query($sql, [$orderheaderID]);
$last_qry = $this->db->last_query();
if (!$qry) {
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
$periodEnd = $qry->result_array()[0]['Result_ProcessToOfficeSendTime'];
$period = [
"start" => date('c', strtotime($dataBisone['T_OrderHeaderCreated'])),
"end" =>date('c', strtotime($periodEnd))
];
//get location
$sql = "SELECT LocationID,
LocationName
FROM one_health.location
WHERE
LocationType = 'ENCOUNTER_LAB'
AND LocationIsActive = 'Y'";
$qry = $this->db->query($sql, []);
$last_qry = $this->db->last_query();
if (!$qry) {
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
$locationData = $qry->result_array()[0];
$location = [
[
"location" => [
"reference" => "Location/{$locationData['LocationID']}",
"display" => $locationData['LocationName']
]
]
];
//diagnosis
$diagnosis = [
[
"condition" => [
"reference" => "urn:uuid:{$this->satusehat->gen_uuid()}",
"display" => $dataBisone['T_OrderHeaderDiagnose']
],
"use" => [
"coding" => []
],
"rank" => 1
]
];
//contoh isi diagnosisi->use->coidng
// [
// "system" => "http://terminology.hl7.org/CodeSystem/diagnosis-role",
// "code" => "DD",
// "display" => "Discharge diagnosis"
// ]
//status history
//arrived
$sql = "SELECT
T_OrderHeaderAddonFoTimeStart AS `T_OrderHeaderAddonFoTimeStart`,
T_OrderHeaderAddonFoTimeEnd AS `T_OrderHeaderAddonFoTimeEnd`
FROM
`t_orderheaderaddon`
WHERE
`T_OrderHeaderAddOnT_OrderHeaderID` = ?
AND `T_OrderHeaderAddOnIsActive` = 'Y' ";
$qry = $this->db->query($sql, [$orderheaderID]);
$last_qry = $this->db->last_query();
if (!$qry) {
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
$arrivedData = $qry->result_array()[0];
$arrived = [
"status" => "arrived",
"period" => [
"start" => date('c', strtotime($arrivedData['T_OrderHeaderAddonFoTimeStart'])),
"end" => date('c', strtotime($arrivedData['T_OrderHeaderAddonFoTimeEnd']))
]
];
// inprogress
$sql = "SELECT
T_OrderSampleProcessingDate,
T_OrderSampleProcessingTime
FROM `t_ordersample`
WHERE `T_OrderSampleT_OrderHeaderID` = ?
AND `T_OrderSampleIsActive` = 'Y' ";
$qry = $this->db->query($sql, [$orderheaderID]);
$last_qry = $this->db->last_query();
if (!$qry) {
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
$inProgressDataStart = $qry->result_array()[0];
$sql = "SELECT
T_OrderDetailVerDate
FROM `t_orderdetail`
WHERE `T_OrderDetailT_OrderHeaderID` = ?
AND T_OrderDetailT_TestIsResult = 'Y'
AND `T_OrderDetailIsActive` = 'Y'
ORDER BY T_OrderDetailVerDate ASC
LIMIT 1";
$qry = $this->db->query($sql, [$orderheaderID]);
$last_qry = $this->db->last_query();
if (!$qry) {
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
$inProgressDataEnd = $qry->row()->T_OrderDetailVerDate;
$inProgress = [
"status" => "in-progress",
"period" => [
"start" => date('c', strtotime($inProgressDataStart['T_OrderSampleProcessingDate']." ".$inProgressDataStart['T_OrderSampleProcessingTime'])) ,
"end" => date('c', strtotime($inProgressDataEnd))
]
];
//finished
$sql = "SELECT
Result_ProcessToOfficeSendTime AS `Result_ProcessToOfficeSendTime`
FROM `result_processtooffice`
WHERE `Result_ProcessToOfficeT_OrderHeaderID` = ?
AND `Result_ProcessToOfficeIsActive` = 'Y'";
$qry = $this->db->query($sql, [$orderheaderID]);
$last_qry = $this->db->last_query();
if (!$qry) {
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
$finishedData = $qry->result_array();
$finished = [
"status" => "finished",
"period" => [
"start" => date('c', strtotime($finishedData[0]['Result_ProcessToOfficeSendTime'])),
"end" => date('c', strtotime($finishedData[count($finishedData) - 1]['Result_ProcessToOfficeSendTime']))
]
];
$identifier = [[
"system" => "http://sys-ids.kemkes.go.id/encounter/$organizationID",
"value" => $dataBisone['T_OrderHeaderLabNumber']
]];
$statusHistory = [$arrived, $inProgress, $finished];
$encounter['resource']['subject'] = $subject;
$encounter['resource']['participant'] = $participant;
$encounter['resource']['period'] = $period;
$encounter['resource']['location'] = $location;
//$encounter['resource']['diagnosis'] = $diagnosis;
$encounter['resource']['statusHistory'] = $statusHistory;
$encounter['resource']['serviceProvider'] = $organization;
$encounter['resource']['identifier'] = $identifier;
$encounter['request'] = [
"method" => "POST",
"url" => "Encounter"
];
//insert json
$sql = "INSERT INTO one_health.encounter
(EncounterUUID,
EncounterJSON,
EncounterT_orderHeaderID,
EncounterCreated)
VALUES
(?,?,?,NOW())";
$qry = $this->db->query($sql, [$encounter_uuid, json_encode($encounter), $orderheaderID]);
$last_qry = $this->db->last_query();
if (!$qry) {
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
// $this->sys_ok($dataBisone);
// $this->sys_ok($encounter);
echo json_encode($encounter);
} else {
//sudah ada data di one_helath makan langsung di returnkan jsonya ;
echo $result_cek[0]['EncounterJSON'];
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function updatediagnose()
{
try {
$prm = $this->sys_input;
$orderheaderID = $prm['orderheaderID'];
$system = $prm['system'];
$code = $prm['code'];
$display = $prm['display'];
$sql = "SELECT * FROM one_health.encounter
WHERE EncounterT_orderHeaderID = ?
AND EncounterIsActive = 'Y'";
$qry = $this->db->query($sql, [$orderheaderID]);
$last_qry = $this->db->last_query();
if (!$qry) {
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
$result = $qry->result_array()[0];
$json = $result['EncounterJSON'];
$data = (array)json_decode($json, true);
$data['resource']['diagnosis'][0]['use']['coding'][] = [
"system" => "http://terminology.hl7.org/CodeSystem/diagnosis-role",
"code" => "DD",
"display" => "Discharge diagnosis"
];
echo (json_encode($data));
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
}