Files
BE_IBL/application/controllers/tools/satu_sehat/Condition.php
2026-04-15 15:23:57 +07:00

360 lines
9.7 KiB
PHP

<?php
class Condition extends MY_Controller {
function __construct() {
parent::__construct();
}
function run($date = "", $edate = "")
{
header("Content-Type: text/plain");
if ($date == "") {
$date = date("Y-m-d");
}
if ($edate == "") {
$edate = $date;
}
$sdate = "$date 00:00:00";
$edate = "$edate 23:59:59";
$z08Code = "Z00.8";
$z08Display = "Other general examinations";
$sql = "select
concat('Kunjungan ', oneOrderM_PatientName, ' on ', oneOrderDate)
encounterDisplay,
oneOrderM_PatientIhsID,
oneOrderM_PatientName,
oneOrderID,
oneOrderEncounterID,
T_OrderHeaderDiagnose,
if (M_MouM_OmzetTypeID = 1, 'Y','N') isAPS,
if (M_MouM_OmzetTypeID = 2, 'Y','N') isDokter,
if (M_MouM_OmzetTypeID = 3, 'Y','N') isPerusahaan,
T_OrderHeaderDate,
ifnull(T_OrderHeaderAddonFoTimeEnd,T_OrderHeaderDate + interval 5 minute)
T_OrderHeaderAddonFoTimeEnd
from one_health.one_order
join t_orderheader
on oneOrderT_OrderHeaderID = T_OrderHeaderID
and T_OrderHeaderIsActive = 'Y'
and oneOrderStatus = 'SubOK'
and oneOrderDate >= ? and oneOrderDate <= ?
join m_mou on T_OrderHeaderM_MouID = M_MouID
join t_orderheaderaddon
on T_OrderHeaderID = T_OrderHeaderAddonT_OrderHeaderID
";
$qry = $this->db->query($sql, [$sdate, $edate]);
$this->error_check($qry, "get order");
$rows = $qry->result_array();
$sql_i = "insert into one_health.diagnose (
diagnoseOneOrderID, diagnoseType,
diagnoseText, diagnoseIsAPS, diagnoseIsPerusahaan,
diagnoseIsDokter, diagnoseJson, diagnoseStatus)
values (?,?,
?,?,?,
?, ?,? )
on duplicate key
update diagnoseText = ? , diagnoseJson = ?
";
$ctr_primay = 0;
foreach ($rows as $r) {
if ($r["isAPS"] == "Y" && $r["T_OrderHeaderDiagnose"] == "") {
$json = $this->condition(
$r["oneOrderEncounterID"],
$r["encounterDisplay"],
$r["oneOrderM_PatientIhsID"],
$r["oneOrderM_PatientName"],
$z08Code,
$z08Display,
$r["T_OrderHeaderAddonFoTimeEnd"]
);
$status = "New";
} else {
$json = "";
$status = "Hold";
}
$qry = $this->db->query($sql_i, [
$r["oneOrderID"],
"primary",
$r["T_OrderHeaderDiagnose"],
$r["isAPS"],
$r["isPerusahaan"],
$r["isDokter"],
$json,
$status,
$r["T_OrderHeaderDiagnose"],
$json
]);
$this->error_check($qry, "insert primary");
$ctr_primay++;
}
echo "Date $sdate - $edate \n";
echo "Diagnose Primary $ctr_primay \n";
}
/*function submit($sdate, $edate = "", $xid = "", $debug = "")*/
function submit($xid)
{
if ($edate == "") {
$edate = $sdate;
}
$sdate = "$sdate 00:00:00";
$edate = "$edate 23:59:59";
if($xid != "") {
$sql = "select
oneOrderID, oneOrderDate, oneOrderT_OrderHeaderLabNumber,
oneOrderM_PatientName,oneOrderEncounterID,
oneOrderM_PatientIhsID,
diagnose.*
from one_health.diagnose
join one_health.one_order
on diagnoseOneOrderID = oneOrderID
and diagnoseStatus = 'New'
and diagnoseOneOrderID = ?
";
$qry = $this->db->query($sql, [$xid]);
} else {
$sql = "select
oneOrderID, oneOrderDate, oneOrderT_OrderHeaderLabNumber,
oneOrderM_PatientName,oneOrderEncounterID,
oneOrderM_PatientIhsID,
diagnose.*
from one_health.diagnose
join one_health.one_order
on diagnoseOneOrderID = oneOrderID
and diagnoseStatus = 'New'
and oneOrderDate >= ? and oneOrderDate <= ?
";
$qry = $this->db->query($sql, [$sdate, $edate]);
}
if (!$qry) {
echo $this->db->last_query();
exit;
}
$rows = $this->error_check($qry, "get diagnose New", true);
if (count($rows) == 0) {
echo "\t No Diagnose \n";
}
if ($debug != "") {
$xrows = [];
$xrows[] = $rows[0];
$rows = $xrows;
}
$this->load->library("Satu_sehat");
header("Content-Type: text/plain");
$service = "/Condition";
$sql_u = "update one_health.diagnose set
diagnoseReplay = ?, diagnoseSent = now(), diagnoseStatus =?
where diagnoseID = ?";
$idx = 0;
foreach ($rows as $r) {
$number = $r["oneOrderT_OrderHeaderLabNumber"];
$patient = $r["oneOrderM_PatientName"];
$subject = $r["oneOrderM_PatientIhsID"];
$payload = json_decode($r["diagnoseJson"], true);
$date = $r["oneOrderDate"];
$id = $r["diagnoseID"];
$type = $r["diagnoseType"];
$encounterID = $r["oneOrderEncounterID"];
$get_service = $service . "?subject=" . $subject
. "&encounter=" . $encounterID;
$resp = $this->satu_sehat->ss_get($get_service);
echo "Submitting Diagnose {$type} for $number - $patient $date \n";
// echo "Check Service : $get_service\n";
$o_resp = $this->objToArray($resp);
// print_r($payload);
if ($o_resp["total"] > 0) {
$resp_icd10 = $o_resp["entry"][0]["resource"]["code"]["coding"][0]["code"];
$payload_icd10 = $payload["code"]["coding"][0]["code"];
if ($resp_icd10 == $payload_icd10) {
echo "Diagnose alreay found. $resp_icd10 | $payload_icd10";
$j_condition = json_encode($o_resp["entry"][0]);
echo $j_condition;
$qry = $this->db->query($sql_u, [$j_condition, "Sent", $id]);
if (!$qry) {
echo $this->db->last_query();
exit;
}
continue;
}
}
$resp = $this->satu_sehat->ss_post($service, $payload);
$o_resp = $this->objToArray($resp);
// print_r($o_resp);
$j_resp = json_encode($o_resp);
// echo "Response : \n";
$qry = $this->db->query($sql_u, [$j_resp, "Sent", $id]);
if (!$qry) {
echo $this->db->last_query();
echo "\n";
print_r($this->db->error());
} else {
echo "Uploaded ...";
}
$idx++;
if ($idx >= 10) {
sleep(2);
$idx = 0;
}
}
}
function condition(
$encounterId,
$encounterDisplay,
$patientIhs,
$patientName,
$icdxCode,
$icdxName,
$theDate
) {
$orgDate = $theDate;
$theDate = substr($orgDate, 0, 10) . "T" . substr($orgDate, 11, 8) . "+07:00";
$param = [
"category" => [
[
"coding" => [
[
"code" => "encounter-diagnosis",
"display" => "Encounter Diagnosis",
"system" => "http://terminology.hl7.org/CodeSystem/condition-category"
]
]
]
],
"clinicalStatus" => [
"coding" => [
[
"code" => "active",
"display" => "Active",
"system" => "http://terminology.hl7.org/CodeSystem/condition-clinical"
]
]
],
"code" => [
"coding" => [
[
"code" => "$icdxCode",
"display" => "$icdxName",
"system" => "http://hl7.org/fhir/sid/icd-10"
]
]
],
"encounter" => [
"display" => "$encounterDisplay",
"reference" => "Encounter/$encounterId"
],
"onsetDateTime" => "$theDate",
"recordedDate" => "$theDate",
"resourceType" => "Condition",
"subject" => [
"display" => "$patientName",
"reference" => "Patient/$patientIhs"
]
];
return json_encode($param, JSON_PRETTY_PRINT);
}
function error_check($qry, $stage, $check_empty = false)
{
if (!$qry) {
echo json_encode(
[
"status" => "ERR",
"stage" => $stage,
"message" => $this->db->error(),
"sql" => $this->db->last_query()
]
);
exit;
}
if ($check_empty) {
$rows = $qry->result_array();
if (count($rows) == 0) {
echo json_encode(
[
"status" => "ERR",
"stage" => $stage,
"message" => "No record found",
"sql" => $this->db->last_query()
]
);
exit;
}
return $rows;
}
}
function print_table($rows, $keys)
{
$this->print_table_style();
echo "<table class='pe-status'>";
echo "<tr>";
foreach ($keys as $k) {
echo "<td>$k</td>";
}
echo "</tr>\n";
foreach ($rows as $r) {
echo "<tr>";
foreach ($keys as $k) {
echo "<td>" . $r[$k] . "</td>";
}
echo "</tr>";
}
echo "</table>";
}
function print_table_style()
{
echo "
<style>
.pe-status th, .pe-status td {
padding: 15px;
text-align: left;
}
.pe-status tr:nth-child(even) {background-color: #f2f2f2;}
table.pe-status {
border: solid 1px ;
min-width:600px;
}
</style>
";
}
protected function objToArray($obj)
{
if (!is_object($obj) && !is_array($obj)) {
return $obj;
}
foreach ($obj as $key => $value) {
$arr[$key] = $this->objToArray($value);
}
return $arr;
}
}
/*
create table diagnose(
diagnoseID int not null auto_increment primary key,
diagnoseOneOrderID int,
diagnoseType varchar(50) default 'awal',
diagnoseText text default '',
diagnoseIsAPS varchar(1) default 'N',
diagnoseIsDokter varchar(1) default 'N',
diagnoseIsPerusahaan varchar(1) default 'N',
diagnoseJson text,
diagnoseReplay text,
diagnoseStatus varchar(10) default 'New' comment 'New/Sent/ERR',
diagnoseIsActive varchar(1) default 'Y',
diagnoseCreated datetime default current_timestamp(),
key(diagnoseOneOrderID),
key(diagnoseType),
key(diagnoseIsAPS),
key(diagnoseIsPerusahaan)
)
*/