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

414 lines
11 KiB
PHP

<?php
class Clinic_condition extends MY_Controller
{
function __construct()
{
parent::__construct();
}
function status($date = "", $edate = "", $json = "")
{
if ($date == "") {
$date = date("Y-m-d");
}
if ($edate == "") {
$edate = date("Y-m-d");
}
$sdate = "$date 00:00:00";
$edate = "$edate 23:59:59";
$sql = "select
date(oneOrderDate) oneOrderDate,
diagnoseType,
diagnoseStatus, count(*) Jumlah
from one_health_clinic.one_order
join m_branch on M_BranchIsActive = 'Y'
and M_BranchIsDefault = 'Y'
and oneOrderDate >= ?
and oneOrderDate <= ?
join one_health_clinic.diagnose
on oneOrderID = diagnoseOneOrderID
group by date(oneOrderDate),diagnoseType,diagnoseStatus";
$qry = $this->db->query($sql, [$sdate, $edate]);
$this->error_check($qry, "get status");
$rows = $qry->result_array();
$arr_code = [];
$result = [];
$total_order = 0;
$dt_total = [];
$dt_primer = [];
$dt_sekunder = [];
$dt_submitted = [];
foreach ($rows as $r) {
$total_order += $r["Jumlah"];
$rdate = $r["oneOrderDate"];
if (!isset($dt_total[$rdate])) {
$dt_total[$rdate] = 0;
}
$dt_total[$rdate] += $r["Jumlah"];
if (!isset($dt_primer[$rdate])) {
$dt_sekunder[$rdate] = 0;
}
if (!isset($dt_sekunder[$rdate])) {
$dt_sekunder[$rdate] = 0;
}
if (!isset($dt_submitted[$rdate])) {
$dt_submitted[$rdate] = 0;
}
if ($r["diagnoseType"] == "primary") {
$dt_primer[$rdate] += $r["Jumlah"];
} else {
$dt_sekunder[$rdate] += $r["Jumlah"];
}
if ($r["diagnoseStatus"] == "Sent") {
$dt_submitted[$rdate] += $r["Jumlah"];
}
}
if ($json != "") {
Header("Content-Type: application/json");
echo json_encode(["status" => "OK", "data" => $rows, "satu_sehat" => $satu_sehat, "total" => $total_order]);
exit;
}
$total_order = 0;
$result = [];
$rec_no_pj = [];
foreach ($dt_total as $rdate => $r) {
$result[] = [
"Date" => $rdate,
"Primary" => $dt_primer[$rdate],
"Secondary" => $dt_sekunder[$rdate],
"Total" => $dt_total[$rdate],
"Submitted" => $dt_submitted[$rdate],
];
}
$month = date("F", strtotime($sdate));
echo "<H3 class='pe-status'>Status Condition </H3>";
echo "<H4 class='pe-status'>Bulan: $month </H4>";
// echo "<H4 class='pe-status'>* : order , $pre_s * $post_f : w/o sample, Percentage : Submitted / Total Order ( sample only tidak di hitung) </H4>";
$this->print_table($result, array_keys($result[0]));
}
function submit($sdate, $edate = "", $debug = "")
{
if ($edate == "") {
$edate = $sdate;
}
$sdate = "$sdate 00:00:00";
$edate = "$edate 23:59:59";
$sql = "select
oneOrderID, oneOrderDate, oneOrderOrderNumber,
oneOrderM_PatientName,oneOrderEncounterID,
oneOrderM_PatientIhsID,
diagnose.*
from one_health_clinic.diagnose
join one_health_clinic.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");
$this->satu_sehat->load_clinic();
header("Content-Type: text/plain");
$service = "/Condition";
$sql_u = "update one_health_clinic.diagnose set
diagnoseReplay = ?, diagnoseSent = now(), diagnoseStatus =?
where diagnoseID = ?";
$idx = 0;
foreach ($rows as $r) {
$number = $r["oneOrderOrderNumber"];
$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"];
echo "Diagnose alreay found. $resp_icd10 | $payload_icd10";
$j_condition = json_encode($o_resp["entry"][0]);
echo $j_condition;
exit;
$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());
}
$idx++;
if ($idx >= 10) {
sleep(2);
$idx = 0;
}
}
}
// diagnose
/*
create table diagnose(
diagnoseID int not null auto_increment primary key,
diagnoseOneOrderID int,
diagnoseType varchar(50) default 'primer',
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)
)
*/
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 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";
$sql_i = "";
$sql = "select
concat('Kunjungan ', oneOrderM_PatientName, ' on ', oneOrderDate)
encounterDisplay,
oneOrderM_PatientIhsID,
oneOrderM_PatientName,
oneOrderID,
oneOrderEncounterID,
orderDiagnoseIcd10Primary ,
orderDiagnoseIcd10PrimaryText,
orderDiagnoseIcd10Sekunder,
orderDiagnoseIcd10SekunderText,
orderDiagnoseIcd10Created,
orderDiagnoseIcd10LastUpdated
from one_health_clinic.one_order
join one_klinik.order_diagnose_icd10
on oneOrderOrderID = orderDiagnoseIcd10OrderID
and oneOrderStatus = 'SubOK'
and oneOrderDate >= ? and oneOrderDate <= ?
where orderDiagnoseIcd10Primary <> ''
";
$qry = $this->db->query($sql, [$sdate, $edate]);
$this->error_check($qry, "get order");
$rows = $qry->result_array();
$sql_i = "insert into one_health_clinic.diagnose (
diagnoseOneOrderID, diagnoseType,diagnoseJson) values (?,?,?)
on duplicate key
update diagnoseJson = ?";
$ctr_primay = 0;
$ctr_secondary = 0;
foreach ($rows as $r) {
$json = $this->condition(
$r["oneOrderEncounterID"],
$r["encounterDisplay"],
$r["oneOrderM_PatientIhsID"],
$r["oneOrderM_PatientName"],
$r["orderDiagnoseIcd10Primary"],
$r["orderDiagnoseIcd10PrimaryText"],
$r["orderDiagnoseIcd10LastUpdated"]
);
$qry = $this->db->query($sql_i, [
$r["oneOrderID"],
"primary", $json, $json
]);
$this->error_check($qry, "insert primary");
$ctr_primay++;
if ($r["orderDiagnoseIcd10Sekunder"] != "") {
$json = $this->condition(
$r["oneOrderEncounterID"],
$r["encounterDisplay"],
$r["oneOrderM_PatientIhsID"],
$r["oneOrderM_PatientName"],
$r["orderDiagnoseIcd10Sekunder"],
$r["orderDiagnoseIcd10SekunderText"],
$r["orderDiagnoseIcd10LastUpdated"]
);
$qry = $this->db->query($sql_i, [
$r["oneOrderID"],
"secondary", $json, $json
]);
$this->error_check($qry, "insert secondary");
$ctr_secondary++;
}
}
echo "Date $sdate - $edate \n";
echo "Diagnose Primary $ctr_primay , Secondary $ctr_secondary\n";
}
//helper
//
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;
}
}