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

550 lines
17 KiB
PHP

<?php
class Encounter extends MY_Controller
{
function __construct()
{
parent::__construct();
}
function status01()
{
$branches = [
"matramam", "bonjer", "samanhudi", "ragunan",
"riau", "toha", "pajajaran", "cimahi",
"ngagel", "aditya", "mulyo", "parkus", "hrm"
];
$result = [];
echo "multi branch:<br/>";
print_r($branches);
foreach ($branches as $b) {
echo "$b\n";
$json = file_get_contents("/xtmp/$b.json");
print_r(json_decode($json));
echo "\n";
}
}
function devone()
{
$branches = [
"matraman", "bonjer", "samanhudi", "ragunan",
"riau", "toha", "pajajaran", "cimahi",
"ngagel", "aditya", "mulyo", "parkus", "hrm"
];
$result = [];
foreach ($branches as $b) {
$date = date("Y-m-d");
$url = "http://$b/one-api/tools/satu_sehat/encounter/status/$date/x";
$json = file_get_contents($url);
$resp = json_decode($json, true);
if ($resp["status"] != "OK") {
continue;
}
$data = $resp["data"];
$organization = $resp["satu_sehat"];
$blank_nik = 0;
$inv_nik = 0;
$non_ihs = 0;
$no_loc = 0;
$no_ihs_pj = 0;
$ready_encounter = 0;
$total_order = $resp["total"];
foreach ($data as $r) {
switch ($r["Status"]) {
case "InvNIK":
$inv_nik = $r["Jumlah"];
break;
case "NoLoc":
$no_loc = $r["Jumlah"];
break;
case "NonIHS":
$non_ihs = $r["Jumlah"];
break;
case "NonNIK":
$blank_nik = $r["Jumlah"];
break;
case "SubReady":
$ready_encounter = $r["Jumlah"];
break;
case "NoIHSPJ":
$no_ihs_pj = $r["Jumlah"];
break;
}
}
$percentage = round($ready_encounter / $total_order * 100, 2) . " %";
if ($organization == "N" || $no_loc > 0) {
$percentage = "***";
}
$result[] = [
"Cabang" => $b,
"Organization IHS" => $organization,
"Tanpa NIK" => $blank_nik,
"Invalid NIK" => $inv_nik,
"NIK belum terdaftar" => $non_ihs,
"Belum ada location" => $no_loc > 0 ? "***" : "0",
"Belum ada PJ" => $no_ihs_pj,
"Ready Encounter" => $ready_encounter,
"Total Order" => $total_order,
"Percentage" => $percentage
];
}
$this->print_table($result, array_keys($result[0]));
}
function status($date = "", $json = "")
{
$sql = "select * from one_health.client";
$qry = $this->db->query($sql);
$satu_sehat = "N";
if ($qry) {
$rows = $qry->result_array();
if (count($rows) > 0) $satu_sehat = "Y";
};
if ($date == "") {
$date = date("Y-m-d");
}
$sdate = "$date 00:00:01";
$edate = "$date 23:59:59";
$sql = "select
oneOrderStatus Status, count(*) Jumlah
, '' Note
from one_health.one_order
join m_branch on M_BranchIsActive = 'Y'
and M_BranchIsDefault = 'Y'
and oneOrderDate >= ?
and oneOrderDate <= ?
group by M_BranchCode,oneOrderStatus";
$qry = $this->db->query($sql, [$sdate, $edate]);
$this->error_check($qry, "get status");
$rows = $qry->result_array();
$arr_code = [];
$result = [];
$idx = 0;
$total_order = 0;
foreach ($rows as $r) {
$total_order += $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;
foreach ($rows as $idx => $r) {
$status = $r["Status"];
switch ($status) {
case "NonNIK":
$rows[$idx]["Note"] = "Tanpa NIK";
break;
case "InvNIK":
$rows[$idx]["Note"] = "Invalid NIK";
break;
case "NonIHS":
$rows[$idx]["Note"] = "NIK belum terdaftar IHS";
break;
case "SubReady":
$rows[$idx]["Note"] = "Ready Encounter";
break;
case "SubOK":
$rows[$idx]["Note"] = "Encounter OK";
break;
}
$total_order += $r["Jumlah"];
}
echo "<H3 style='margin-bottom:0px;'>Status Encounter </H3>";
echo "<H4 style='margin-top:0px;margin-bottom:0px'>Tanggal : $date</H4>";
echo "<H4 style='margin-top:0px;margin-bottom:0px'>Total Order: {$total_order} </H4></br>";
$this->print_table($rows, array_keys($rows[0]));
}
function submit()
{
$sql = "select one_order.*, branchName
from one_order join branch on oneOrderBranchID = branchID
where oneOrderStatus = 'SubReady'
";
$qry = $this->db->query($sql);
$rows = $this->error_check($qry, "get one_order SubReady", true);
$this->load->library("Satu_sehat");
header("Content-Type: text/plain");
$sql_u = "update one_order set oneOrderEncounterID = ?,
oneOrderJsonReply=?, oneOrderStatus='SubOK' where oneOrderID = ?";
//insert
foreach ($rows as $r) {
//get encounter by subject
$branchID = $r["oneOrderBranchID"];
$branchName = $r["branchName"];
$orderID = $r["oneOrderID"];
$patientIhsID = $r["oneOrderM_PatientIhsID"];
$nolab = $r["oneOrderT_OrderHeaderLabNumber"];
echo "checking $branchName $nolab \n";
$resp = $this->satusehat->encounter_by_subject($branchID, $patientIhsID);
list($encounterID, $jsonReply) = $this->parse_encounter($resp, $nolab);
if ($encounterID != "") {
echo "success : $encounterID \n";
$qry = $this->db->query($sql_u, [$encounterID, $jsonReply, $orderID]);
$this->error_check($qry, "update encounter\n");
continue;
}
//submit encounter
$h_date = $r["oneOrderDate"];
$patientName = $r["oneOrderM_PatientName"];
$doctorIhsID = $r["oneOrderM_DoctorIhsID"];
$doctorName = $r["oneOrderM_DoctorName"];
$locationID = $r["oneOrderLocationIhsID"];
$locationName = $r["oneOrderLocationName"];
list($encounterID, $message, $payload, $response) = $this->satusehat->encounter(
$branchID,
$h_date,
$patientIhsID,
$patientName,
$doctorIhsID,
$doctorName,
$locationID,
$locationName,
$nolab,
"+07:00"
);
if ($encounterID == "") {
echo "*** Error submit encounter : \n";
echo "$message \n";
}
$qry = $this->db->query($sql_u, [$encounterID, $response, $orderID]);
$this->error_check($qry, "update encounter new $encounterID\n");
echo "\t OK\n";
}
}
function parse_encounter($resp, $nolab)
{
$entries = $resp["entry"];
foreach ($entries as $e) {
$res = $e["resource"];
//get encounterID
$encounterID = $res["id"];
$r_nolab = $res["identifier"][0]["value"];
if ($r_nolab == $nolab) {
return [$encounterID, json_encode($res)];
}
}
return ["", ""];
}
function run($date = "")
{
header("Content-Type: text/plain");
if ($date == "") {
$date = date("Y-m-d");
}
$sdate = "$date 00:00:00";
$edate = "$date 23:59:59";
$sql_i = "insert ignore into one_health.one_order(
oneOrderDate,oneOrderT_OrderHeaderID,oneOrderT_OrderHeaderLabNumber, oneOrderT_OrderHeaderLabNumberExt,
oneOrderM_PatientID, oneOrderM_PatientName, oneOrderM_PatientNIK, oneOrderM_PatientIhsID,
oneOrderM_DoctorID, oneOrderM_DoctorName,oneOrderM_DoctorIhsID,
oneOrderLocationID, oneOrderLocationIhsID, oneOrderLocationName,
oneOrderStatus, oneOrderJsonPayLoad,oneOrderJsonReply,oneOrderEncounterID )
values(?,?,?,?, ?,?,?,?, ?,?,?, ?,?,?, ?,?,?,? )";
$sql = "select T_OrderHeaderDate, T_OrderHeaderID, T_OrderHeaderLabNumber,
T_OrderHeaderLabNumberExt, T_OrderHeaderM_PatientID, OHPatientMapIHSNumber,
fn_get_name(T_OrderHeaderM_PatientID) patient_name,
if(M_PatientM_IdTypeID = 1, M_PatientIDNumber, '') patientNIK,
T_OrderHeaderPjM_DoctorID, OHDoctorMapIHSNumber,
fn_get_doctor_fullname(T_OrderHeaderPjM_DoctorID) doctor_name,
LocationID, LocationLocalID,LocationDescription
from t_orderheader
join m_patient on T_OrderHeaderM_PatientID = M_PatientID
and T_OrderHeaderIsActive = 'Y'
and T_OrderHeaderDate >= ?
and T_OrderHeaderDate <= ?
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'
left join one_health.location on LocationIsActive = 'Y'
and LocationType ='ENCOUNTER_LAB'
";
$qry = $this->db->query($sql, [$sdate, $edate]);
$this->error_check($qry, "get order");
$rows = $qry->result_array();
$idx_encounter = 0;
$idx_order = 0;
$idx_blank_nik = 0;
$idx_invalid_nik = 0;
$idx_not_register_nik = 0;
$idx_no_pj = 0;
$idx_no_pj_ihs = 0;
$idx_no_loc = 0;
$this->load->library("Satu_sehat");
echo ">> Collect order at $date *** " . count($rows) . " \n";
$idx_ihs_counter = 0;
foreach ($rows as $r) {
$nolab_internal = $r["T_OrderHeaderLabNumber"];
$nolab = $r["T_OrderHeaderLabNumberExt"];
$h_id = $r["T_OrderHeaderID"];
$h_date = $r["T_OrderHeaderDate"];
$patientID = $r["T_OrderHeaderM_PatientID"];
$patientNIK = trim($r["patientNIK"]);
$patientIhsID = trim($r["OHPatientMapIHSNumber"]);
$patientName = $r["patient_name"];
$doctorID = $r["T_OrderHeaderPjM_DoctorID"];
$doctorName = $r["doctor_name"];
$doctorIhsID = $r["OHDoctorMapIHSNumber"];
$locationID = $r["LocationLocalID"];
$locationName = $r["LocationDescription"];
$locationIhsID = $r["LocationID"];
echo "\tLab No. $nolab_internal\n";
$idx_order++;
if ($patientNIK == "") {
echo "\tblank NIK \n";
$qry = $this->db->query($sql_i, [
$h_date, $h_id, $nolab_internal, $nolab,
$patientID, $patientName, $patientNIK, "",
$doctorID, $doctorName, $doctorIhsID,
$locationID, $locationIhsID, $locationName,
"NonNIK", "", "", ""
]);
$this->error_check($qry, "insert one_order NonNIK");
$idx_blank_nik++;
continue;
}
if (strlen($patientNIK) != 16) {
echo "\tInvalid NIK \n";
$qry = $this->db->query($sql_i, [
$h_date, $h_id, $nolab_internal, $nolab,
$patientID, $patientName, $patientNIK, "",
$doctorID, $doctorName, $doctorIhsID,
$locationID, $locationIhsID, $locationName,
"InvNIK", "", "", ""
]);
$this->error_check($qry, "insert one_order InvNIK");
$idx_invalid_nik++;
continue;
}
if ($patientIhsID == "") {
echo "\tMapping NIK $patientNIK\n";
$patientIhsID = $this->map_patient(
$patientID,
$patientNIK
);
if ($patientIhsID == "") {
//check patient IHS
echo "\tNon IHS Patient \n";
$qry = $this->db->query($sql_i, [
$h_date, $h_id, $nolab_internal, $nolab,
$patientID, $patientName, $patientNIK, "",
$doctorID, $doctorName, $doctorIhsID,
$locationID, $locationIhsID, $locationName,
"NonIHS", "", "", ""
]);
$this->error_check($qry, "insert one_order NonIHS");
$idx_not_register_nik++;
continue;
}
$idx_ihs_counter++;
if ($idx_ihs_counter == 6) {
$idx_ihs_counter = 0;
echo "\t*** Wait 2s\n";
sleep(2);
}
}
if ($doctorID == "") {
echo "\tNon PJ\n";
$qry = $this->db->query($sql_i, [
$h_date, $h_id, $nolab_internal, $nolab,
$patientID, $patientName, $patientNIK, $patientIhsID,
$doctorID, $doctorName, $doctorIhsID,
$locationID, $locationIhsID, $locationName,
"NonPJ", "", "", ""
]);
$this->error_check($qry, "insert one_order NonPJ");
$idx_no_pj++;
continue;
}
if ($doctorIhsID == "") {
echo "\tNon IHS PJ\n";
$qry = $this->db->query($sql_i, [
$h_date, $h_id, $nolab_internal, $nolab,
$patientID, $patientName, $patientNIK, $patientIhsID,
$doctorID, $doctorName, $doctorIhsID,
$locationID, $locationIhsID, $locationName,
"NoIHSPJ", "", "", ""
]);
$this->error_check($qry, "insert one_order NoIHSPJ");
$idx_no_pj_ihs++;
continue;
}
if ($locationIhsID == "") {
echo "\tNon Location IHS\n";
$qry = $this->db->query($sql_i, [
$h_date, $h_id, $nolab_internal, $nolab,
$patientID, $patientName, $patientNIK, $patientIhsID,
$doctorID, $doctorName, $doctorIhsID,
$locationID, $locationIhsID, $locationName,
"NoLoc", "", "", ""
]);
$this->error_check($qry, "insert one_order NoLoc");
$idx_no_loc++;
continue;
}
list($encounterID, $message, $payload, $response) = $this->satu_sehat->encounter(
$h_date,
$patientIhsID,
$patientName,
$doctorIhsID,
$doctorName,
$locationID,
$locationName,
$nolab,
"+07:00",
true
);
$qry = $this->db->query($sql_i, [
$h_date, $h_id, $nolab_internal, $nolab,
$patientID, $patientName, $patientNIK, "",
$doctorID, $doctorName, $doctorIhsID,
$locationID, $locationIhsID, $locationName,
"SubReady", $payload, "", ""
]);
$this->error_check($qry, "insert ignore one_order SubReady");
$idx_encounter++;
echo "\tReady Encounter\n";
}
echo "Tanggal\t\t\t\t: $date\n";
echo "Tot Order\t\t\t\t: $idx_order\n";
echo " Tanpa NIK\t\t\t\t: $idx_blank_nik\n";
echo " Invalid NIK\t\t\t\t: $idx_invalid_nik\n";
echo " Not register NIK\t\t\t: $idx_not_register_nik\n";
echo " PJ belum ada\t\t\t\t: $idx_no_pj\n";
echo " PJ belum terdaftar\t\t\t: $idx_no_pj_ihs\n";
echo "Siap Encounter\t\t\t\t: $idx_encounter\n";
}
function check_location($branchID)
{
$sql = "select * from location
where locationBranchID = ?
and locationIsActive='Y'
and locationType = 'ENCOUNTER_LAB'
order by locationID desc";
$qry = $this->db->query($sql, [$branchID]);
$this->error_check($qry, "check location map $branchID");
$rows = $qry->result_array();
if (count($rows) > 0) {
return [
$rows[0]["LocationLocalID"],
$rows[0]["LocationID"],
$rows[0]["LocationDescription"]
];
}
return ["", ""];
}
function check_pj($branchID)
{
$sql = "select * from map_doctor
where mapDoctorBranchID = ?
and mapDoctorIsActive='Y'
order by mapDoctorID desc";
$qry = $this->db->query($sql, [$branchID]);
$this->error_check($qry, "check doctor map $branchID");
$rows = $qry->result_array();
if (count($rows) > 0) {
return [
$rows[0]["mapDoctorM_DoctorID"], $rows[0]["mapDoctorName"],
$rows[0]["mapDoctorIhsID"]
];
}
return [0, "", ""];
}
function map_patient($patientID, $patientNIK)
{
$ihsID = $this->satu_sehat->search_patient_by_nik($patientNIK);
if ($ihsID != "") {
$sql = "insert into one_health.oh_patient_map(OHPatientMapM_PatientID,
OHPatientMapIHSNumber)
values(?,?)";
$qry = $this->db->query($sql, [$patientID, $ihsID]);
$this->error_check($qry, "insert oh_patient_map");
if (!$qry) return "";
}
return $ihsID;
}
//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>";
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>
th, td {
padding: 15px;
text-align: left;
}
tr:nth-child(even) {background-color: #f2f2f2;}
table {
border: solid 1px ;
min-width:600px;
}
</style>
";
}
}