Files
2026-04-15 15:23:57 +07:00

209 lines
6.9 KiB
PHP

<?php
class Bundle extends MY_Controller
{
var $db;
var $load;
var $satusehat;
function __construct()
{
parent::__construct();
$this->load->library('Satusehat');
}
function createbundle()
{
try {
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$userid = $this->sys_user["M_UserID"];
$prm = $this->sys_input;
$orderheaderID = $prm['orderheaderID'];
$result["resourceType"] = "Bundle";
$result["type"] = "transaction";
$entry = [];
$sql = "SELECT *
FROM one_health.encounter
WHERE
EncounterT_orderHeaderID = ? AND EncounterIsActive = 'Y'
LIMIT 1";
$qry = $this->db->query($sql, [$orderheaderID]);
if (!$qry) {
$last_qry = $this->db->last_query();
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
$rst_encounter = $qry->row_array();
$entry[] = json_decode($rst_encounter['EncounterJSON']);
$service_requests = [];
$sql = "SELECT *
FROM one_health.service_request
WHERE
ServiceRequestT_OrderHeaderID = ? AND ServiceRequestIsActive = 'Y'";
$qry = $this->db->query($sql, [$orderheaderID]);
if (!$qry) {
$last_qry = $this->db->last_query();
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
$rst_service_request = $qry->result_array();
if($rst_service_request){
foreach ($rst_service_request as $key => $value) {
$entry[] = json_decode($value['ServiceRequestJSON']);
}
}
$sql = "SELECT *
FROM one_health.specimen
WHERE
SpecimentT_OrderHeaderID = ? AND SpecimenIsActive = 'Y'";
$qry = $this->db->query($sql, [$orderheaderID]);
if (!$qry) {
$last_qry = $this->db->last_query();
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
$rst_specimen = $qry->result_array();
if($rst_specimen){
foreach ($rst_specimen as $key => $value) {
$entry[] = json_decode($value['SpecimenJSON']);
}
}
$sql = "SELECT *
FROM one_health.observation
WHERE
observationT_OrderHeaderID = ? AND observationIsActive = 'Y'";
$qry = $this->db->query($sql, [$orderheaderID]);
if (!$qry) {
$last_qry = $this->db->last_query();
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
$rst_observation = $qry->result_array();
if($rst_observation){
foreach ($rst_observation as $key => $value) {
$entry[] = json_decode($value['observationJSON']);
}
}
$sql = "SELECT *
FROM one_health.diagnostic_report
WHERE
DiagnosticReportT_orderHeaderID = {$orderheaderID} AND DiagnosticReportIsActive = 'Y'";
$qry = $this->db->query($sql);
//echo $sql;
if (!$qry) {
$last_qry = $this->db->last_query();
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
$rst_diagnosticreport = $qry->row_array();
//print_r($rst_diagnosticreport);
$entry[] = json_decode($rst_diagnosticreport['DiagnosticReportJSON']);
//print_r(json_decode($rst_diagnosticreport['DiagnosticReportJSON']));
$result["entry"] = $entry;
$sql = "SELECT COUNT(*) as xcount
FROM one_health.bundle
WHERE
BundleT_orderHeaderID = ? AND BundleIsActive = 'Y'
";
$qry = $this->db->query($sql, [$orderheaderID]);
if (!$qry) {
$last_qry = $this->db->last_query();
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
$rst_count = $qry->row_array();
if($rst_count['xcount'] > 0){
$sql = "UPDATE one_health.bundle SET
BundleResponseJSON = ?
WHERE
BundleT_orderHeaderID = ? AND
BundleIsActive = 'Y'";
$qry = $this->db->query($sql, [json_encode($result),$orderheaderID]);
if (!$qry) {
$last_qry = $this->db->last_query();
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
}else{
$sql = "INSERT INTO one_health.bundle (
BundleJSON,
BundleT_orderHeaderID,
BundleCreated,
BundleUserID
)
VALUES(
?,?,NOW(),?
)";
$qry = $this->db->query($sql, [json_encode($result),$orderheaderID,$userid]);
if (!$qry) {
$last_qry = $this->db->last_query();
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error);
exit;
}
}
echo json_encode($result);
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
}