Files
BE_CPONE/application/controllers/fix/Fix_rujukan_pasien.php
2026-04-27 10:26:26 +07:00

458 lines
15 KiB
PHP

<?php
class Fix_rujukan_pasien extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
//Create table mapping pasien result
public function db_error()
{
$date = date("Y-m-d H:i:s");
echo "$date Err : " . $this->db->error()['message'] . "\n";
echo "\t\t" . $this->db->last_query() . "\n";
exit;
}
public function list_rujukan($date = "")
{
if ($date == "") {
$date = date("Y-m-d");
}
$sql = "select T_OrderHeaderLabNumber,T_OrderHeaderAddOnLabNumberOrigin,
T_OrderHeaderID,
group_concat(distinct T_SampleTypeT_BahanID separator '.') BahanID
from t_orderheader
join t_ordersample on T_OrderHeaderID = T_OrderSampleT_OrderHeaderID
and T_OrderSampleIsActive = 'Y'
join t_sampletype on T_OrderSampleT_SampleTypeID = T_SampleTypeID
join t_orderheaderaddon on T_OrderHeaderID = T_OrderHeaderAddOnT_OrderHeaderID
and T_OrderHeaderIsActive = 'Y'
and T_OrderHeaderAddOnIsActive = 'Y'
left join rujukan_bahan on T_OrderHeaderID = rujukanBahanT_OrderHeaderID
where T_OrderHeaderAddOnLabNumberOrigin is not null
and date(T_OrderHeaderDate) = ?
and rujukanBahanID is null
group by T_OrderHeaderID";
$qry = $this->db->query($sql, [$date]);
if (!$qry) {
echo json_encode(
array(
"status" => "ERR",
"message" => "Error Listing Order Rujukan | {$this->db->error()['message']}|{$this->db->last_query()}"
)
);
exit;
}
$rows = $qry->result_array();
echo json_encode(
array(
"status" => "OK",
"count" => count($rows),
"data" => $rows
)
);
exit;
}
public function log($msg)
{
$date = date("Y-m-d H:i:s");
echo "$date $msg\n";
}
public function get_json_pasien($orderID, $patientID)
{
}
public function fix_pasien_address($orderID, $json_patient, $is_api=0)
{
$sql = "select T_OrderHeaderM_PatientID from t_orderheader where T_OrderHeaderID=?";
$qry = $this->db->query($sql, [$orderID]);
if (!$qry) {
if ($is_api == 0) {
$this->db_error();
} else {
return;
}
}
$rows = $qry->result_array();
if (count($rows) == 0) {
if ($is_api == 0) {
echo "T_OrderHeaderID $orderID tidak ketemu";
}
return;
}
$patientID = $rows[0]["T_OrderHeaderM_PatientID"];
$sql = "select count(*) total
from m_patientaddress where M_PatientAddressM_PatientID = ?";
$qry = $this->db->query($sql, [$patientID]);
if (!$qry) {
$this->db_error();
}
$rows = $qry->result_array();
if ($rows[0]["total"] == 0) {
if (isset($json_patient["address"]) && count($json_patient["address"]) > 0) {
$address = $json_patient["address"][0];
unset($address["M_PatientAddressID"]);
$address["M_PatientAddressM_PatientID"] = $patientID;
$qry = $this->db->insert("m_patientaddress", $address);
if (!$qry) {
if ($is_api == 0) {
$this->db_error();
}
}
if ($is_api == 0) {
echo "Data Alamat Pasien berhasil di masukan : " .
print_r($address, true);
}
} else {
if ($is_api == 0) {
echo "Kiriman Alamat Pasien Kosong";
return;
}
}
}
}
public function fix($nolab)
{
$sql = "select
t_orderheader.*,T_OrderHeaderAddOnLabNumberOrigin
from
t_orderheader
join t_orderheaderaddon on T_OrderHeaderID = T_OrderHeaderAddOnID
and
( T_OrderHeaderLabNumber = ? or T_OrderHeaderAddOnLabNumberOrigin = ? )
";
$qry = $this->db->query($sql, [$nolab,$nolab]);
if (!$qry) {
$this->db_error();
}
$rows = $qry->result_array();
if (count($rows) == 0) {
$this->log("Nolab $nolab tidak di ketemukan");
exit;
}
if ($rows[0]["T_OrderHeaderIsActive"] != "Y") {
$this->log("Nolab $nolab void.");
exit;
}
if ($rows[0]["T_OrderHeaderAddOnLabNumberOrigin"] == "") {
$this->log("Nolab $nolab bukan rujukan.");
exit;
}
$orderID = $rows[0]["T_OrderHeaderID"];
echo "<H5> No. Lab: {$rows[0]['T_OrderHeaderLabNumber']} | asal : {$rows[0]['T_OrderHeaderAddOnLabNumberOrigin']} ";
//show detail pasien
$sql ="
select
M_PatientID,M_PatientNoreg,
CONCAT(M_TitleName,' ',ifnull(M_PatientPrefix,''),' ',M_PatientName,' ',ifnull(M_PatientSuffix,''))
AS M_PatientName, M_PatientHP , M_PatientPhone , M_PatientEmail, M_PatientIDNumber,
(SELECT M_PatientAddressDescription
from m_patientaddress AS p
WHERE M_PatientAddressM_PatientID = M_PatientID
ORDER BY M_PatientAddressM_PatientID
LIMIT 1) AS M_PatientAddressDescription,
T_OrderHeaderM_PatientAge
from
t_orderheader
join m_patient on T_OrderHeaderID = ?
join m_title on M_PatientM_TitleID = M_TitleID
and T_OrderHeaderM_PatientID = M_PatientID";
$qry = $this->db->query($sql, [$orderID]);
if (!$qry) {
$this->db_error();
}
$rows = $qry->result_array();
if (count($rows) == 0) {
$this->log("Pasien untuk $nolab tidak di ketemukan");
exit;
}
$patientID = $rows[0]["M_PatientID"];
echo "<H5>Pasien tercatat di Lab Rujukan </H5>";
$this->print_table($rows, array_keys($rows[0]));
echo "<H5>Data pasien di kirim </H5>";
$sql = "select group_concat(distinct(T_SampleTypeT_BahanID) separator '.') bahanID
from
t_ordersample
join t_sampletype on T_OrderSampleT_SampleTypeID = T_SampleTypeID
and T_OrderSampleT_OrderHeaderID = ? and T_OrderSampleIsActive = 'Y'
group by T_OrderSampleT_OrderHeaderID";
$qry = $this->db->query($sql, [$orderID]);
if (!$qry) {
$this->db_error();
}
$rows = $qry->result_array();
$bahanID = $rows[0]["bahanID"];
$this->get_pasien_kiriman($orderID, $bahanID);
}
public function sampling_info()
{
// Bahan | Sampling Date | Sampling Time
$prm = $this->sys_input;
$orderID = $prm["orderID"];
$sql = "select
T_BahanName,T_BahanID,
max(concat(T_OrderSampleReceiveDate,' ',T_OrderSampleReceiveTime))
T_OrderSampleReceiveDateTime
from
t_ordersample
join t_sampletype on T_OrderSampleT_SampleTypeID = T_SampleTypeID
and T_OrderSampleT_OrderHeaderID = ?
and T_OrderSampleIsActive = 'Y'
join t_bahan on T_SampleTypeT_BahanID = T_BahanID
group by T_BahanID
";
$qry = $this->db->query($sql, [$orderID]);
if (!$qry) {
echo json_encode(
array(
"status" => "ERR",
"message" => $this->db->error()['message'] . " | "
. $this->db->last_query()
)
);
exit;
}
$rows = $qry->result_array();
echo json_encode(
array(
"status" => "OK",
"prm" => $prm,
"sql" => $this->db->last_query(),
"data" => $rows
)
);
}
public function get_pasien_kiriman($orderID, $bahanID, $is_api=0)
{
$sql = "select
incomingRefDetailPatient, incomingRefDetailNewM_PatientID,
M_BranchIPAddress, incomingRefDetailT_OrderHeaderID
from
incoming_ref_detail
join incoming_ref on incomingRefID = incomingRefDetailIncomingRefID
join m_branch on incomingRefM_BranchID = M_BranchID
where incomingRefDetailNewT_OrderHeaderID = ?
order by incomingRefDetailID desc
limit 0,1
";
$qry = $this->db->query($sql, [$orderID]);
if (!$qry) {
$this->db_error();
}
$rows = $qry->result_array();
if (count($rows) == 0) {
if ($is_api == 1) {
echo json_encode(
array(
"status" => "ERR",
"message" => "Data tidak di temukan"
)
);
} else {
echo "Data tidak di temukan";
}
exit;
}
$originIpAddress = $rows[0]["M_BranchIPAddress"];
$originOrderID = $rows[0]["incomingRefDetailT_OrderHeaderID"];
$patient = json_decode($rows[0]["incomingRefDetailPatient"], true);
$this->fix_pasien_address($orderID, $patient, $is_api);
if ($is_api == 0) {
echo "<h5>Data Kiriman Pasien<h5>";
}
//TODO : Fix T_OrderHeaderPjM_DoctorID
$sql = "select
M_DoctorID, fn_get_doctor_fullname(M_DoctorID) FullNameDr
from
m_doctor
join m_doctorpj on M_DoctorID = M_DoctorPjM_DoctorID
and M_DoctorIsActive = 'Y'
and M_DoctorPjIsActive = 'Y'
and M_DoctorPjIsDefaultPJ = 'Y'
";
$qry = $this->db->query($sql);
if (!$qry) {
if ($is_api == 1) {
echo json_encode(
array(
"status" => "ERR",
"message" => "Select Dr PJ | " . $this->db->error()['message'] .
"|" . $this->db->last_query()
)
);
exit;
} else {
$this->db_error();
}
}
$rows = $qry->result_array();
if (count($rows) == 0) {
if ($is_api == 1) {
echo json_encode(
array(
"status" => "ERR",
"message" => "Dr Default PJ tidak di temukan"
)
);
exit;
} else {
$this->db_error();
echo "Data Default Dr PJ tidak di temukan";
exit;
}
}
$doctorPjID = $rows[0]["M_DoctorID"];
if ($is_api == 0) {
echo "<H5>Dr PJ </H5>";
$this->print_table($rows, array_keys($rows[0]));
}
//Update Dr PJ
$sql = "update t_orderheader set T_OrderHeaderPjM_DoctorID=?
where T_OrderHeaderID=?";
$this->db->query($sql, [$doctorPjID, $orderID]);
$samplingUrl = "http://$originIpAddress/one-api/fix/fix_rujukan_pasien/sampling_info";
$a_bahan = explode(".", $bahanID);
$param = json_encode(array("orderID" => $originOrderID,"bahanID" => $a_bahan));
if ($is_api == 0) {
echo "<H5> Get Sampling Info from $samplingUrl </h5>";
}
$result = $this->post($samplingUrl, $param);
if ($result["status"] == "ERR") {
if ($is_api == 1) {
echo json_encode(
array(
"status" => "ERR",
"message" => "Error get Sampling" ,
"response" => print_r($result, true)
)
);
exit;
} else {
echo "Error Get Sampling";
print_r($result);
exit;
}
}
if ($is_api == 0) {
$this->print_table($result["data"], array_keys($result["data"][0]));
}
$db_msg = [];
foreach ($result["data"] as $d) {
$sql = "insert into rujukan_bahan(rujukanBahanT_OrderHeaderID,
rujukanBahanT_BahanID,rujukanBahanT_BahanName,
rujukanBahanReceiveDate)
values(?,?,?,?)
on duplicate key update
rujukanBahanT_BahanName = ?,
rujukanBahanReceiveDate = ?
";
$qry = $this->db->query(
$sql,
[ $orderID, $d["T_BahanID"], $d["T_BahanName"], $d["T_OrderSampleReceiveDateTime"],
$d["T_BahanName"], $d["T_OrderSampleReceiveDateTime"]
]
);
if (!$qry) {
if ($is_api == 1) {
$db_msg[] = array(
"orderID" => $orderID,
"originIPAddress" => $originIpAddress,
"error" => $this->db->error()['message'] . "|" . $this->db->last_query()
);
} else {
echo "Error Insert|Update Rujukan Bahan : " .
$this->db->error()['message'] . " |\n" .
$this->db->last_query() . "\n";
exit;
}
}
}
if ($is_api == 1) {
if (count($db_msg) > 0) {
echo json_encode(
array(
"status" => "ERR",
"error" => $db_msg
)
);
}
}
}
public function post($url, $data)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data)
));
$result = curl_exec($ch);
if (curl_errno($ch) > 0) {
return array(
"status" => "ERR",
"message" => curl_error($ch)
);
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode != 200) {
return array(
"status" => "ERR",
"message" => "Http Response : $httpCode | $result"
);
}
$j_result = json_decode($result, true);
if (!$j_result) {
return array(
"status" => "ERR",
"message" => "JSON invalid: $result"
);
}
return $j_result;
}
public 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>
";
}
public 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>";
}
}