Files
BE_IBL/application/controllers/meshbio/Uploader.php
2026-04-15 15:23:57 +07:00

1890 lines
58 KiB
PHP

<?php
class Uploader extends MY_Controller
{
var $startVt;
var $stopFs;
var $cr;
function __construct()
{
parent::__construct();
$this->startVt = chr(0x0b);
$this->stopFs = chr(0x1c);
$this->cr = chr(0x0d);
}
function update_email()
{
$param = $this->sys_input;
$ids = implode(",", $param["ids"]);
if ($ids == "") $ids = 0;
$sql = "update meshbio_email set meshbio_emailIsSent='Y'
where meshbio_emailID in ($ids)";
$qry = $this->db->query($sql, [$ids]);
if (!$qry) {
$this->reply_err($this->db->error()["message"]);
}
$this->reply([]);
}
function get_email()
{
list($branchID, $branchCode) = $this->get_branch();
$sql = "select
? M_BranchID, ? M_BranchCode,
T_OrderHeaderID,
T_OrderHeaderDate,
T_OrderHeaderLabNumber,
T_OrderHeaderLabNumberExt,
M_TitleName,
M_PatientPrefix,
M_PatientName,
M_PatientSuffix,
meshbio_emailDestination,
meshbio_emailID
from
meshbio_email
join t_orderheader on meshbio_emailT_OrderHeaderID = T_OrderHeaderID
and ( meshbio_emailIsSent = 'N' or meshbio_emailIsSent = 'R')
join m_patient on T_OrderHeaderM_PatientID = M_PatientID
join m_title on M_PatientM_TitleID = M_TitleID
limit 0,50
";
$qry = $this->db->query($sql, [$branchID, $branchCode]);
if (!$qry) {
$this->reply_err($this->db->error()["message"]);
}
$rows = $qry->result_array();
foreach ($rows as $idx => $r) {
$title = $r["M_TitleName"];
$prefix = trim($r["M_PatientPrefix"]);
$sufix = trim($r["M_PatientSuffix"]);
$name = $title;
if ($prefix != "") $name = $name . " " . $prefix;
$name = $name . " " . $r["M_PatientName"];
if ($sufix != "") $name = $name . " " . $sufix;
$rows[$idx]["FullName"] = $name;
}
$this->reply($rows);
}
function meshbio_nonlab_code($parent, $name)
{
$map = [
"TANDA_VITAL^DENYUT NADI" => "0100",
"TANDA_VITAL^RITME DENYUT NADI" => "0101",
"TANDA_VITAL^LAJU PERNAFASAN" => "0102",
"TANDA_VITAL^POLA NAFAS" => "0103",
"TANDA_VITAL^TEKANAN DARAH" => "0104",
"TANDA_VITAL^SUHU" => "0105",
"STATUS_GIZI^BERAT BADAN" => "0200",
"STATUS_GIZI^TINGGI BADAN" => "0201",
"STATUS_GIZI^LINGKAR PERUT" => "0202",
"STATUS_GIZI^BMI" => "0203",
"STATUS_GIZI^LINGKAR PINGGANG / PANGGUL" => "0204",
"STATUS_GIZI^KLASIFIKASI" => "0205",
"KEADAAN_UMUM^KESADARAN" => "0300",
"KEADAAN_UMUM^SIKAP & TINGKAH LAKU" => "0301",
"KEADAAN_UMUM^KONTAK PSIKIS / PERHATIAN" => "0302",
"MULUT^MUKOSA RONGGA MULUT" => "0400",
"MULUT^TENGGOROKAN" => "0401",
"VISUS^TANPA KACAMATA" => "0600",
"VISUS^DENGAN KACAMATA" => "0601",
"KEPALA_WAJAH^KEPALA_WAJAH" => "0700",
"MATA^MATA" => "0800",
"TELINGA^TELINGA" => "0900",
"THORAX^THORAX" => "1000",
"PARU^PARU" => "1100",
"JANTUNG^JVP" => "1200",
"JANTUNG^APEX" => "1201",
"JANTUNG^SUARA JANTUNG" => "1202",
"JANTUNG^BISING JANTUNG / MURMUR" => "1203",
"INTEGUMEN^KULIT" => "1300",
"INTEGUMEN^KUKU" => "1301",
"INTEGUMEN^RAMBUT" => "1302",
"SISTEM^REFLEKS FISIOLOGIS" => "1400",
"SISTEM^REFLEKS PATHOLOGIS" => "1401",
"SISTEM^KESEIMBANGAN & KOORDINASI" => "1402",
"SISTEM^REFLEKS NEUROLOGIS LAINNYA" => "1403",
"ANGGOTA^EKSTRIMITAS ATAS" => "1500",
"ANGGOTA^EKSTRIMITAS BAWAH" => "1501",
"ANGGOTA^TONUS / OTOT" => "1502",
"GENITOURINARIA^GENITOURINARIA" => "1600",
"PERUT^ABDOMEN" => "1700",
"PERUT^BISING USUS" => "1701",
"PERUT^HATI" => "1702",
"PERUT^LIMPA" => "1703",
"PERUT^HERNIA" => "1704",
"PERUT^HEMORROID" => "1705",
"LEHER^LEHER" => "1800",
"HIDUNG^HIDUNG" => "1900",
"PERSEPSI_WARNA^PERSEPSI_WARNA" => "2000",
"GIGI^TAMBALAN" => "3000",
"GIGI^BERLUBANG" => "3001",
"GIGI^TANGGAL" => "3002",
"GIGI^SISA AKAR" => "3003",
"GIGI^GIGI PALSU" => "3004",
"GIGI^KARANG GIGI" => "3005",
"GIGI^BERLUBANG" => "3006",
"GIGI^NORMAL" => "3007",
];
$idx = strtoupper($parent . "^" . $name);
if (in_array($idx, array_keys($map))) {
return $map[$idx];
}
return "XXXX";
}
function create_log_table()
{
$sql = "create table meshbio_log(
meshbioLogID int not null auto_increment primary key,
meshbioLogDate datetime default current_timestamp(),
meshbioLogType enum('LAB','NON-LAB'),
meshbioLogT_OrderHeaderID int,
meshbioLogT_OrderHeaderLabNumber varchar(20),
meshbioLogStatus enum('OK','ERR'),
meshbioLogUrl varchar(200),
meshbioLogNote varchar(200),
meshbioLogIsActive enum('Y','N') default 'Y',
key (meshbioLogT_OrderHeaderID),
key (meshbioLogIsActive),
key (meshbioLogStatus),
key(meshbioLogType)
)";
$qry = $this->db->query($sql);
if (!$qry) {
$this->reply_err($this->db->error()["message"]);
}
}
function is_uploaded($type, $orderHeaderID)
{
$sql = "select meshbioLogDate,meshbioLogStatus
from meshbio_log
where meshbioLogT_OrderHeaderID=? and meshbioLogIsActive='Y' and meshbioLogType=?
and meshbioLogStatus ='OK'
order by meshbioLogID desc limit 0,1";
$qry = $this->db->query($sql, [$orderHeaderID, $type]);
if (!$qry) {
$this->reply_err(
$this->db->error()["message"] . "|" . $this->db->last_query()
);
}
$rows = $qry->result_array();
//skip already sent check control manually
// if (count($rows) > 0) {
// $this->reply_err("Already sent at {$rows[0]["meshbioLogDate"]}");
// }
}
function check_log()
{
$sql = "select * from meshbio_log";
$qry = $this->db->query($sql);
if (!$qry) {
$err = $this->db->error();
if (
$err["code"] == "1146" &&
stripos($err["message"], "meshbio_log") !== false &&
stripos($err["message"], "exist") !== false
) {
$this->create_log_table();
} else {
$this->reply_err($this->db->error()["message"]);
}
}
}
function update_log()
{
$this->check_log();
$prm = $this->sys_input;
$orderID = $prm["T_OrderHeaderID"];
$nolab = $prm["T_OrderHeaderLabNumber"];
$type = $prm["type"];
$status = $prm["status"];
$url = $prm["url"];
$note = $prm["note"];
$sql = "insert into meshbio_log(meshbioLogT_OrderHeaderID, meshbioLogT_OrderHeaderLabNumber,
meshbioLogType,meshbioLogStatus,meshbioLogUrl, meshbioLogNote)
values(?,?, ?,?,?,?)";
$qry = $this->db->query($sql, [
$orderID,
$nolab,
$type,
$status,
$url,
$note,
]);
if (!$qry) {
$this->reply_err(
$this->db->error()["message"] . "|" . $this->db->last_query()
);
}
$this->reply("");
}
function update_order($nolab, $status)
{
$sql = "update meshbio_session
join t_orderheader on T_OrderheaderLabNumber=?
and T_OrderHeaderID = meshbioSessionT_OrderHeaderID
set meshbioSessionhl7status = ?";
$qry = $this->db->query($sql, [$nolab, $status]);
if (!$qry) {
echo json_encode(["status" => "ERR", "message" => $this->db->error()["message"]]);
exit;
}
echo json_encode(["status" => "OK"]);
}
function order($last_day = 3)
{
if ($last_day == 3) {
$last_day = 30;
}
$arr_nat_test_meshbio = ["'40122100'", "'40122200'", "'40122300'"];
$nat_test_meshbio = implode(",", $arr_nat_test_meshbio);
// need to add specific rule for result upload to HL7
//
$sql = "select T_OrderHeaderID,T_OrderHeaderDate,T_OrderHeaderLabNumberExt, T_OrderHeaderLabNumber,
year(T_OrderHeaderDate) orderYear,
right(T_OrderHeaderLabNumber,2) branchCode,
M_PatientNoreg , M_PatientIDNumber
from
t_orderheader
join t_orderheaderaddon on T_OrderHeaderID = T_OrderHeaderAddonT_OrderHeaderID
join t_orderdetail on
( T_OrderHeaderDate + interval ? day > now()
or
( T_OrderHeaderAddonIsComingDate is not null and T_OrderHeaderAddonIsComingDate + interval ? day > now() )
)
and T_OrderHeaderIsActive = 'Y'
and T_OrderHeaderID = T_OrderDetailT_OrderHeaderID
and T_OrderDetailIsActive = 'Y'
join t_test on T_OrderDetailT_TestID = T_TestID
join nat_test on T_TestNat_TestID = Nat_TestID
and Nat_TestCode in ($nat_test_meshbio)
join m_patient on T_OrderHeaderM_PatientID = M_PatientID
join meshbio_patient on M_PatientNoreg = meshbioPatientBizonePID
join meshbio_session on T_OrderHeaderID = meshbioSessionT_OrderHeaderID
and (meshbioSessionhl7status = 'C' or meshbioSessionhl7status = 'R')
group by T_OrderHeaderId";
$qry = $this->db->query($sql, [$last_day, $last_day]);
if (!$qry) {
echo json_encode(["status" => "ERR", "message" => $this->db->error()["message"]]);
exit;
}
$rows = $qry->result_array();
if (count($rows) == 0) {
echo json_encode(["status" => "OK", "data" => [], "message" => "No Order Process", "query" => $this->db->last_query()]);
exit;
}
$result = [];
foreach ($rows as $r) {
$result[] = $r;
}
echo json_encode(["status" => "OK", "data" => $result, "query" => $this->db->last_query()]);
}
function order_old($last_day = 3)
{
$arr_nat_test_meshbio = ["'40122100'", "'40122200'", "'40122300'"];
$nat_test_meshbio = implode(",", $arr_nat_test_meshbio);
// need to add specific rule for result upload to HL7
//
$sql = "select T_OrderHeaderID,T_OrderHeaderDate,T_OrderHeaderLabNumberExt, T_OrderHeaderLabNumber,
year(T_OrderHeaderDate) orderYear,
right(T_OrderHeaderLabNumber,2) branchCode,
M_PatientNoreg , M_PatientIDNumber
from
t_orderheader
join t_orderheaderaddon on T_OrderHeaderID = T_OrderHeaderAddonT_OrderHeaderID
join t_orderdetail on T_OrderHeaderDate + interval ? day > now()
and T_OrderHeaderIsActive = 'Y'
and T_OrderHeaderID = T_OrderDetailT_OrderHeaderID
and T_OrderDetailIsActive = 'Y'
join t_test on T_OrderDetailT_TestID = T_TestID
join nat_test on T_TestNat_TestID = Nat_TestID
and Nat_TestCode in ($nat_test_meshbio)
join m_patient on T_OrderHeaderM_PatientID = M_PatientID
join meshbio_patient on M_PatientNoreg = meshbioPatientBizonePID
join meshbio_session on T_OrderHeaderID = meshbioSessionT_OrderHeaderID
and (meshbioSessionhl7status = 'C' or meshbioSessionhl7status = 'R')
group by T_OrderHeaderId";
$qry = $this->db->query($sql, [$last_day]);
if (!$qry) {
echo json_encode(["status" => "ERR", "message" => $this->db->error()["message"]]);
exit;
}
$rows = $qry->result_array();
if (count($rows) == 0) {
echo json_encode(["status" => "OK", "data" => [], "message" => "No Order Process", "query" => $this->db->last_query()]);
exit;
}
$result = [];
foreach ($rows as $r) {
$result[] = $r;
}
echo json_encode(["status" => "OK", "data" => $result, "query" => $this->db->last_query()]);
}
function old_order()
{
$hardcode = [
"03503848DA",
// "03101484LB",
// "03101485LB",
// "03101487LB",
// "03101489LB",
];
$nolabs = implode(
",",
array_map(function ($nl) {
return "'{$nl}'";
}, $hardcode)
);
$sql = "select T_OrderHeaderID,T_OrderHeaderDate,T_OrderHeaderLabNumberExt, T_OrderHeaderLabNumber,
year(T_OrderHeaderDate) orderYear,
right(T_OrderHeaderLabNumber,2) branchCode,
M_PatientNoreg , M_PatientIDNumber
from t_orderheader
join m_patient on T_OrderHeaderLabNumber in ($nolabs)
and T_OrderHeaderM_PatientID = M_PatientID";
$qry = $this->db->query($sql);
if (!$qry) {
$this->reply_err(
$this->db->error()["message"] . "|" . $this->db->last_query()
);
}
$rows = $qry->result_array();
$this->reply($rows);
}
function get_branch()
{
$sql =
"select M_BranchID,M_BranchCode from m_branch where M_BranchIsDefault='Y' and M_BranchIsActive='Y'";
$qry = $this->db->query($sql);
if (!$qry) {
$this->reply_err(
$this->db->error()["message"] . "|" . $this->db->last_query()
);
}
$rows = $qry->result_array();
if (count($rows) == 0) {
$this->reply_err("No Default Branch.");
}
return [$rows[0]["M_BranchCode"], $rows[0]["M_BranchCode"]];
}
function get_doctor_name($doctorID)
{
$sql = "select M_DoctorCode,
concat(M_DoctorPrefix, if(M_DoctorPrefix <> '' , ' ','') ,
M_DoctorPrefix2,if(M_DoctorPrefix2 <> '' , ' ',''),
M_DoctorName,
M_DoctorSufix) M_DoctorName
from m_doctor where M_DoctorID=?";
$qry = $this->db->query($sql, [$doctorID]);
if (!$qry) {
$this->reply_err(
$this->db->error()["message"] . "|" . $this->db->last_query()
);
}
$rows = $qry->result_array();
if (count($rows) == 0) {
$this->reply_err("No Doctor $doctorID.");
}
return [$rows[0]["M_DoctorCode"], $rows[0]["M_DoctorName"]];
}
function get_patient($patientID)
{
$sql = "select M_PatientNoreg,M_PatientDob, M_SexCode,
fn_get_patient_atribute(M_PatientID) patient
from m_patient
join m_sex on M_PatientM_SexID = M_SexID
where M_PatientID = ?
";
$qry = $this->db->query($sql, [$patientID]);
if (!$qry) {
$this->reply_err(
$this->db->error()["message"] . "|" . $this->db->last_query()
);
}
$rows = $qry->result_array();
if (count($rows) == 0) {
$this->reply_err("No Patient $patientID.");
}
$pat = json_decode($rows[0]["patient"], true);
$noreg = $rows[0]["M_PatientNoreg"];
$name = $pat["patient_fullname"];
$dob = date("Ymd", strtotime($rows[0]["M_PatientDOB"]));
$sex = $rows[0]["M_SexCode"] == "L" ? "M" : "F";
return [$noreg, $name, $dob, $sex];
}
function lab($param)
{
list($branchID, $branchCode) = $this->get_branch();
$msh = $this->msh($branchCode, $param["orderHeaderID"]);
list($senderCode, $senderName) = $this->get_doctor_name(
$param["senderID"]
);
list($pjCode, $pjName) = $this->get_doctor_name($param["pjID"]);
list($noreg, $name, $dob, $sex) = $this->get_patient(
$param["patientID"]
);
//patientID patientName dob sex
$pid = $this->pid(
$param["nik"],
$name,
$dob,
$sex,
$param["companyNatCode"]
);
//assignPatientLocation = "LAB" drPj drSender
$drPj = substr("$pjCode^$pjName", 0, 60);
$drSender = substr("$senderCode^$senderName", 0, 60);
$pv1 = $this->pv1($branchID, $drPj, $drSender, $param["labNoExt"]);
$orderDateTime = $param["orderDate"];
$resultDatetime = $param["readyPrintDate"];
$observationDateTime = $param["observationDate"];
//noLab orderdatetime resultDatetime drSender
$orc = $this->orc(
$param["labNoExt"],
$orderDateTime,
$resultDatetime,
$drSender
);
//nolab observationDateTime resultDateTime
$obr = $this->obr(
$param["labNoExt"],
$observationDateTime,
$resultDatetime
);
// arry of code name isQuantitative result unit range flag
$obx_start_counter = 1;
$obx = $this->obx($param["result"], $obx_start_counter);
$message =
$this->startVt .
$msh .
$this->cr .
$pid .
$this->cr .
$pv1 .
$this->cr .
$orc .
$this->cr .
$obr .
$this->cr .
$obx .
$this->cr .
$this->stopFs;
return $message;
}
function nonlab($param, $lang = "id")
{
list($branchID, $branchCode) = $this->get_branch();
$msh = $this->msh($branchCode, $param["orderHeaderID"]);
list($senderCode, $senderName) = $this->get_doctor_name(
$param["senderID"]
);
list($pjCode, $pjName) = $this->get_doctor_name($param["pjID"]);
list($noreg, $name, $dob, $sex) = $this->get_patient(
$param["patientID"]
);
//patientID patientName dob sex
$pid = $this->pid(
$param["nik"],
$name,
$dob,
$sex,
$param["companyNatCode"]
);
//assignPatientLocation = "LAB" drPj drSender
$drPj = substr("$pjCode^$pjName", 0, 60);
$drSender = substr("$senderCode^$senderName", 0, 60);
$pv1 = $this->pv1($branchID, $drPj, $drSender, $param["labNoExt"]);
$orderDateTime = $param["orderDate"];
$resultDatetime = $param["readyPrintDate"];
$observationDateTime = $param["observationDate"];
//noLab orderdatetime resultDatetime drSender
$orc = $this->orc(
$param["labNoExt"],
$orderDateTime,
$resultDatetime,
$drSender
);
//nolab observationDateTime resultDateTime
$obr = $this->obr(
$param["labNoExt"],
$observationDateTime,
$resultDatetime
);
// arry of code name isQuantitative result unit range flag
$obx = "";
$obx_start_counter = 1;
foreach ($param["result"] as $r) {
if ($obx != "") {
$obx .= $this->cr;
}
foreach ($r as $idx => $rr) {
$r[$idx]["result"] = $lang . "^" . $rr["result"];
}
$obx .= $this->obx($r, $obx_start_counter);
}
$message =
$this->startVt .
$msh .
$this->cr .
$pid .
$this->cr .
$pv1 .
$this->cr .
$orc .
$this->cr .
$obr .
$this->cr .
$obx .
$this->cr .
$this->stopFs;
return $message;
}
function get_nik($patientID)
{
$sql = "select M_PatientIDNumber
from m_patient
where M_PatientM_IdTypeID = 1 and M_PatientID = ? ";
$qry = $this->db->query($sql, [$patientID]);
if (!$qry) {
return [false, "Error :" . $this->db->error()["message"]];
}
$rows = $qry->result_array();
if (count($rows) == 0) {
return [false, "Error : No NIK | " . $this->db->last_query()];
}
if (strlen(trim($rows[0]["M_PatientIDNumber"])) != 16) {
return [
false,
"Error: Invalid NIK " . $rows[0]["M_PatientIDNumber"],
];
}
return [true, $rows[0]["M_PatientIDNumber"]];
}
function nonlab_by_nolab($labNo, $debug = "")
{
if ($debug != "") {
$this->startVt = "<0x0b>";
$this->stopFs = "<0x1c>";
$this->cr = "<cr>\n";
}
$sql = "select T_OrderHeaderID,T_OrderHeaderM_PatientID,
T_OrderHeaderPjM_DoctorID, T_OrderHeaderSenderM_DoctorID,
T_OrderHeaderDate, T_OrderHeaderLabNumberExt,
min(concat(T_OrderSampleHandlingDate,' ', T_OrderSampleHandlingTime)) xtime ,
T_OrderHeaderAddOnReadyPrintDate,
max(T_OrderDetailValDate) valDate,
group_concat(distinct T_TestNat_GroupID) Nat_GroupID
, '' M_CompanyNatCode
from t_orderheader
join t_orderheaderaddon on T_OrderHeaderID = T_OrderHeaderAddOnT_OrderHeaderID
and T_OrderheaderLabNumber = ?
join m_company on T_OrderHeaderM_CompanyID = M_CompanyID
join t_ordersample on T_OrderHeaderID = T_OrderSampleT_OrderheaderID and T_OrderSampleIsActive = 'Y'
join t_orderdetail on T_OrderDetailT_OrderHeaderID = T_OrderHeaderID
and T_OrderDetailIsActive = 'Y'
join t_test on T_OrderDetailT_TestID = T_TestID
and T_TestNat_GroupID > 1
and T_OrderSampleHandlingDate is not null
group by T_OrderHeaderID";
if ($debug != "") {
$sql = "select T_OrderHeaderID,T_OrderHeaderM_PatientID,
T_OrderHeaderPjM_DoctorID, T_OrderHeaderSenderM_DoctorID,
T_OrderHeaderDate, T_OrderHeaderLabNumberExt,
min(concat(T_OrderSampleHandlingDate,' ', T_OrderSampleHandlingTime)) xtime ,
T_OrderHeaderAddOnReadyPrintDate,
max(T_OrderDetailValDate) valDate,
group_concat(distinct T_TestNat_GroupID) Nat_GroupID
, '' M_CompanyNatCode
from t_orderheader
join t_orderheaderaddon on T_OrderHeaderID = T_OrderHeaderAddOnT_OrderHeaderID
and T_OrderheaderLabNumber = ?
join m_company on T_OrderHeaderM_CompanyID = M_CompanyID
join t_ordersample on T_OrderHeaderID = T_OrderSampleT_OrderheaderID and T_OrderSampleIsActive = 'Y'
join t_orderdetail on T_OrderDetailT_OrderHeaderID = T_OrderHeaderID
and T_OrderDetailIsActive = 'Y'
join t_test on T_OrderDetailT_TestID = T_TestID
and T_TestNat_GroupID > 1
group by T_OrderHeaderID";
}
$qry = $this->db->query($sql, [$labNo]);
if (!$qry) {
$this->reply_err(
$this->db->error()["message"] . "|" . $this->db->last_query()
);
}
$rows = $qry->result_array();
if (count($rows) == 0) {
$this->reply_err("Nolab $labNo not found.");
}
$headerID = $rows[0]["T_OrderHeaderID"];
$labNoExt = $rows[0]["T_OrderHeaderLabNumberExt"];
$patientID = $rows[0]["T_OrderHeaderM_PatientID"];
//$companyNatCode = "--"; //$rows[0]["M_CompanyNatCode"];
$companyNatCode = $rows[0]["M_CompanyNatCode"];
$senderID = $rows[0]["T_OrderHeaderSenderM_DoctorID"];
$pjID = $rows[0]["T_OrderHeaderPjM_DoctorID"];
$readyPrintDate = $rows[0]["T_OrderHeaderAddOnReadyPrintDate"];
if ($readyPrintDate == "") {
$readyPrintDate = $rows[0]["valDate"];
}
//$this->is_uploaded("NON-LAB", $headerID);
list($xstat, $nik) = $this->get_nik($patientID);
if (!$xstat) {
$this->reply_err("Nolab $labNo | " . $nik);
}
$arrGroup = explode(",", $rows[0]["Nat_GroupID"]);
$param = [
"headerID" => $headerID,
"labNo" => $labNo,
"companyNatCode" => $companyNatCode,
"labNoExt" => $labNoExt,
"patientID" => $patientID,
"senderID" => $senderID,
"nik" => $nik,
"pjID" => $pjID,
"observationDate" => date("YmdHis", strtotime($rows[0]["xtime"])),
"readyPrintDate" => date("YmdHis", strtotime($readyPrintDate)),
"orderDate" => date(
"YmdHis",
strtotime($rows[0]["T_OrderHeaderDate"])
),
];
$param["result"] = [];
foreach ($arrGroup as $groupID) {
$param["result"][] = $this->get_nonlab_result($param, $groupID, $debug);
}
$msg = $this->nonlab($param);
if ($debug != "") {
print_r($msg);
}
$this->reply($msg);
}
function gigi_code($code)
{
if ($code == 'C') {
return "Tambalan";
}
if ($code == 'X') {
return "Berlubang";
}
if ($code == 'O') {
return "Tanggal";
}
if ($code == 'R') {
return "Sisa akar";
}
if ($code == 'A') {
return "Gigi palsu";
}
if ($code == 'K') {
return "Karang gigi";
}
return $code;
}
function get_fisik_result($resultEntryID, $debug = "")
{
//Ambil kesimpulan saja
$arr_sub_url = [
// "pribadi",
// "keluhan_saat_ini_new",
// "riwayat_phobia",
// "riwayat_obat",
// "riwayat_penyakit",
// "riwayat_hidup",
// "riwayat_keluarga",
"tanda_vital",
"status_gizi",
"keadaan_umum",
"mulut",
"gigi",
"visus",
"kepala_wajah",
"mata",
"telinga",
"thorax",
"paru",
"jantung",
"integumen",
"sistem",
"anggota",
"genitourinaria",
"perut",
"leher",
"hidung",
"persepsi_warna",
];
$code_result = [];
foreach ($arr_sub_url as $sub_url) {
$result = $this->get_fisik($sub_url, $resultEntryID, "fisik");
switch ($sub_url) {
case "tanda_vital":
foreach ($result as $rr) {
$nonlab_code = $this->meshbio_nonlab_code(
$sub_url,
$rr["aa1"]
);
$code_result[] = [
"parent" => $sub_url,
"code" => $nonlab_code,
"name" => $rr["aa1"],
"result" => $rr["aa2"] . " " . $rr["aa3"],
];
}
break;
case "status_gizi":
foreach ($result as $rr) {
$nonlab_code = $this->meshbio_nonlab_code(
$sub_url,
$rr["qq1"]
);
$code_result[] = [
"parent" => $sub_url,
"code" => $nonlab_code,
"name" => $rr["qq1"],
"result" => $rr["qq2"] . " " . $rr["qq3"],
];
}
break;
case "keadaan_umum":
$sub_code = 0;
foreach ($result as $rr) {
$nonlab_code = $this->meshbio_nonlab_code(
$sub_url,
$rr["rr1"]
);
$code_result[] = [
"parent" => $sub_url,
"code" => $nonlab_code,
"name" => $rr["rr1"],
"result" => $rr["rr2"] . " " . $rr["rr3"],
];
$sub_code++;
}
break;
case "mulut":
$sub_code = 0;
foreach ($result as $rr) {
$nonlab_code = $this->meshbio_nonlab_code(
$sub_url,
$rr["hh1"]
);
$code_result[] = [
"parent" => $sub_url,
"code" => $nonlab_code,
"name" => $rr["hh1"],
"result" => $rr["hh2"] . " " . $rr["hh3"],
];
$sub_code++;
}
break;
case "gigi": // tidak ada
if ($debug != "") {
echo "GIGI: $sub_url | $resultEntryID \n";
// print_r($result);
}
if (true || $debug != "") {
$is_normal = false;
foreach ($result as $rr) {
if (isset($rr["header"]) && $rr["header"] == "N") {
$is_normal = true;
}
if (isset($rr["gg3"]) && $rr["gg3"] != "") {
$is_normal = false;
$nonlab_code = $this->meshbio_nonlab_code(
$sub_url,
$this->gigi_code($rr["gg3"])
);
$code_result[] = [
"parent" => $sub_url,
"code" => $nonlab_code,
"name" => "Gigi",
"result" => $this->gigi_code($rr["gg3"]) . ": " . $rr["gg1"],
];
}
// print_r($rr);
// echo "$nonlab_code \n";
// print_r($code_result);
}
if ($is_normal) {
$nonlab_code = $this->meshbio_nonlab_code(
$sub_url,
"Normal"
);
$code_result[] = [
"parent" => $sub_url,
"code" => $nonlab_code,
"name" => "Gigi",
"result" => "Normal",
];
}
//print_r($result);
}
break;
case "visus":
$sub_code = 0;
foreach ($result as $rr) {
$nonlab_code = $this->meshbio_nonlab_code(
$sub_url,
$rr["xx1"]
);
$code_result[] = [
"parent" => $sub_url,
"code" => $nonlab_code,
"name" => $rr["xx1"],
"result" => $rr["xx2"] . " " . $rr["xx3"],
];
$sub_code++;
}
break;
case "kepala_wajah":
$sub_code = 0;
foreach ($result as $rr) {
$nonlab_code = $this->meshbio_nonlab_code(
$sub_url,
"kepala_wajah"
);
$code_result[] = [
"parent" => $sub_url,
"code" => $nonlab_code,
"name" => "kepala_wajah",
"result" => $rr["bb1"] . " " . $rr["bb2"],
];
$sub_code++;
}
break;
case "mata":
$sub_code = 0;
foreach ($result as $rr) {
$nonlab_code = $this->meshbio_nonlab_code(
$sub_url,
"mata"
);
$code_result[] = [
"parent" => $sub_url,
"code" => $nonlab_code,
"name" => "mata",
"result" => $rr["cc1"] . " " . $rr["cc2"],
];
$sub_code++;
}
break;
case "telinga":
$sub_code = 0;
foreach ($result as $rr) {
$nonlab_code = $this->meshbio_nonlab_code(
$sub_url,
"telinga"
);
$code_result[] = [
"parent" => $sub_url,
"code" => $nonlab_code,
"name" => "telinga",
"result" => $rr["ee1"] . " " . $rr["ee2"],
];
$sub_code++;
}
break;
case "thorax":
$sub_code = 0;
foreach ($result as $rr) {
$nonlab_code = $this->meshbio_nonlab_code(
$sub_url,
"thorax"
);
$code_result[] = [
"parent" => $sub_url,
"code" => $nonlab_code,
"name" => "thorax",
"result" => $rr["jj1"] . " " . $rr["jj2"],
];
$sub_code++;
}
break;
case "paru":
$sub_code = 0;
foreach ($result as $rr) {
$nonlab_code = $this->meshbio_nonlab_code(
$sub_url,
"paru"
);
$code_result[] = [
"parent" => $sub_url,
"code" => $nonlab_code,
"name" => "paru",
"result" => $rr["kk1"] . " " . $rr["kk2"],
];
$sub_code++;
}
break;
case "jantung":
$sub_code = 0;
foreach ($result as $rr) {
$nonlab_code = $this->meshbio_nonlab_code(
$sub_url,
$rr["ll1"]
);
$code_result[] = [
"parent" => $sub_url,
"code" => $nonlab_code,
"name" => $rr["ll1"],
"result" => $rr["ll2"] . " " . $rr["ll3"],
];
$sub_code++;
}
break;
case "integumen":
$sub_code = 0;
foreach ($result as $rr) {
$nonlab_code = $this->meshbio_nonlab_code(
$sub_url,
$rr["pp1"]
);
$code_result[] = [
"parent" => $sub_url,
"code" => $nonlab_code,
"name" => $rr["pp1"],
"result" => $rr["pp2"] . " " . $rr["pp3"],
];
$sub_code++;
}
break;
case "sistem":
$sub_code = 0;
foreach ($result as $rr) {
$nonlab_code = $this->meshbio_nonlab_code(
$sub_url,
$rr["pp1"]
);
$code_result[] = [
"parent" => $sub_url,
"code" => $nonlab_code,
"name" => $rr["pp1"],
"result" => $rr["pp2"] . " " . $rr["pp3"],
];
$sub_code++;
}
break;
case "anggota":
$sub_code = 0;
foreach ($result as $rr) {
$nonlab_code = $this->meshbio_nonlab_code(
$sub_url,
$rr["oo1"]
);
$code_result[] = [
"parent" => $sub_url,
"code" => $nonlab_code,
"name" => $rr["oo1"],
"result" => $rr["oo2"] . " " . $rr["oo3"],
];
$sub_code++;
}
break;
case "genitourinaria":
$sub_code = 0;
foreach ($result as $rr) {
$nonlab_code = $this->meshbio_nonlab_code(
$sub_url,
"genitourinaria"
);
$code_result[] = [
"parent" => $sub_url,
"code" => $nonlab_code,
"name" => "genitourinaria",
"result" => $rr["nn1"] . " " . $rr["nn2"],
];
$sub_code++;
}
break;
case "perut":
$sub_code = 0;
foreach ($result as $rr) {
$nonlab_code = $this->meshbio_nonlab_code(
$sub_url,
$rr["mm1"]
);
$code_result[] = [
"parent" => $sub_url,
"code" => $nonlab_code,
"name" => $rr["mm1"],
"result" => $rr["mm2"] . " " . $rr["mm3"],
];
$sub_code++;
}
break;
case "leher":
$sub_code = 0;
foreach ($result as $rr) {
$nonlab_code = $this->meshbio_nonlab_code(
$sub_url,
"leher"
);
$code_result[] = [
"parent" => $sub_url,
"code" => $nonlab_code,
"name" => "leher",
"result" => $rr["ii1"] . " " . $rr["ii2"],
];
$sub_code++;
}
break;
case "hidung":
$sub_code = 0;
foreach ($result as $rr) {
$nonlab_code = $this->meshbio_nonlab_code(
$sub_url,
"hidung"
);
$code_result[] = [
"parent" => $sub_url,
"code" => $nonlab_code,
"name" => "hidung",
"result" => $rr["ff1"] . " " . $rr["ff2"],
];
$sub_code++;
}
break;
case "persepsi_warna":
$sub_code = 0;
foreach ($result as $rr) {
$nonlab_code = $this->meshbio_nonlab_code(
$sub_url,
"persepsi_warna"
);
$code_result[] = [
"parent" => $sub_url,
"code" => $nonlab_code,
"name" => "persepsi_warna",
"result" => $rr["dd1"] . " " . $rr["dd2"],
];
$sub_code++;
}
break;
}
}
return $code_result;
}
function get_nonlab_result($param, $groupID, $debug = "")
{
$sql = "select Nat_TestCode,Nat_TestName, So_ResultEntrySo_TemplateName,
So_ResultEntryID
from t_orderdetail
join t_test on T_OrderDetailT_OrderHeaderID=?
and T_OrderDetailT_TestID = T_TestID
and T_OrderDetailIsActive = 'Y'
join so_resultentry on T_OrderDetailID = So_ResultEntryT_OrderDetailID
and So_ResultEntryIsActive = 'Y'
join nat_test on Nat_TestID = T_TestNat_TestID
and Nat_TestIsResult = 'Y'
and Nat_TestNat_GroupID=?";
$qry = $this->db->query($sql, [$param["headerID"], $groupID]);
if (!$qry) {
$this->reply_err(
$this->db->error()["message"] . "|" . $this->db->last_query()
);
}
$rows = $qry->result_array();
if (count($rows) == 0) {
return [];
}
$result = [];
if ($debug != "") {
echo "groupID : $groupID\n";
//print_r($rows);
}
foreach ($rows as $r) {
if ($groupID != 4) {
$result[] = [
"code" => $r["Nat_TestCode"],
"name" => $r["Nat_TestName"],
"groupID" => $groupID,
"result" => "",
"unit" => "",
"range" => "",
"flag" => "",
];
$idx = count($result) - 1;
}
if ($groupID == 2) {
if ($debug != "") {
echo $r["Nat_TestCode"] . "\n";
}
if (
substr($r["Nat_TestCode"], 0, 4) == "2012" ||
substr($r["Nat_TestCode"], 0, 4) == "2013" ||
substr($r["Nat_TestCode"], 0, 4) == "2014"
) {
$sql = "call sp_rpt_t_hasil_so_elmd(?,1,'admin')";
$resp = $this->get_sp($sql, [$r["So_ResultEntryID"]]);
if ($resp["status"] == -1) {
$this->reply_err($resp["message"]);
}
if ($debug != "" && substr($r["Nat_TestCode"], 0, 4) == "2012") {
echo "ResultEntryID : " . $r["So_ResultEntryID"] . "\n";
print_r($resp);
}
if (
count($resp["data"]) > 0
) {
$result_as = "";
$result_ad = "";
if($debug != "") {
print_r($resp["data"]);
}
foreach ($resp["data"] as $ecg_d) {
if (
strtolower(trim($ecg_d["So_TemplateDetailName"])) == "kesan"
|| strtolower(trim($ecg_d["So_TemplateDetailName"])) == "interpretasi"
) {
if (
substr($r["Nat_TestCode"], 0, 4) == "2012"
|| substr($r["Nat_TestCode"], 0, 4) == "2013"
|| substr($r["Nat_TestCode"], 0, 4) == "2014"
) {
$result[$idx]["result"] = $ecg_d["So_ResultEntryDetailResult"];
}
}
if (trim($ecg_d["So_TemplateDetailName"]) == "AS") {
$result_as = $ecg_d["So_ResultEntryDetailResult"];
}
if (trim($ecg_d["So_TemplateDetailName"]) == "AD") {
$result_ad = $ecg_d["So_ResultEntryDetailResult"];
}
}
if ($debug != "") {
echo "AS : $result_as | AD : $result_ad \n";
}
if ($result_as != "") {
$result[] = [
"code" => $r["Nat_TestCode"] . "-AS",
"name" => $r["Nat_TestName"],
"groupID" => $groupID,
"result" => $result_as,
"unit" => "",
"range" => "",
"flag" => "",
];
$idx = count($result) - 1;
}
if ($result_ad != "") {
$result[] = [
"code" => $r["Nat_TestCode"] . "-AD",
"name" => $r["Nat_TestName"],
"groupID" => $groupID,
"result" => $result_ad,
"unit" => "",
"range" => "",
"flag" => "",
];
$idx = count($result) - 1;
}
}
}
}
if ($groupID == 3) {
//thorax
if (
substr($r["Nat_TestCode"], 0, 4) == "3015" ||
substr($r["Nat_TestCode"], 0, 4) == "3021"
) {
//$sql = "call sp_rpt_t_hasil_so_xray(?,1,'admin')";
$sql = "call sp_rpt_t_hasil_so_xray_meshbio(?,?,1,'admin')";
$resp = $this->get_sp($sql, [$param["headerID"], $r["So_ResultEntryID"]]);
if ($resp["status"] == -1) {
$this->reply_err($resp["message"]);
}
$xray_result = "";
foreach ($resp["data"] as $d) {
if ($d["So_TemplateDetailName"] != "Kesan") continue;
$xray_result .=
$d["So_TemplateDetailName"] .
" : " .
str_ireplace(
"\n",
"~",
trim($d["So_ResultEntryDetailResult"])
) .
" ";
}
$result[$idx]["result"] = $xray_result;
} elseif (substr($r["Nat_TestCode"], 0, 3) == "306") {
$sql = "call sp_rpt_t_hasil_so_usg(?,1,'admin')";
$resp = $this->get_sp($sql, [$r["So_ResultEntryID"]]);
if ($resp["status"] == -1) {
$this->reply_err($resp["message"]);
}
$xray_result = "";
foreach ($resp["data"] as $d) {
if (strtolower($d["So_TemplateDetailName"]) != "kesan") continue;
$xray_result .=
$d["So_TemplateDetailName"] .
" : " .
str_ireplace(
"\n",
"~",
trim($d["So_ResultEntryDetailResult"])
) .
" ";
}
$result[$idx]["result"] = $xray_result;
} else {
}
}
if ($groupID == 4 && $r["Nat_TestCode"] == "40210100") {
//Harvard
if (true || $debug != "") {
$sql = "call sp_rpt_t_hasil_so_layanan(?, '1', 'admin')";
$resp = $this->get_sp($sql, [$r["So_ResultEntryID"]]);
if ($resp["status"] == -1) {
$this->reply_err($resp["message"]);
}
$hardvard_result = "";
foreach ($resp["data"] as $d) {
if (trim($d["So_TemplateDetailName"]) == "VO2 Max") {
$hardvard_code = "-0A";
} else {
$hardvard_code = "-0B";
}
$hardvard_result =
$d["So_TemplateDetailName"] .
" : " .
str_ireplace(
"\n",
"~",
trim($d["So_ResultEntryDetailResult"])
);
$result[] = [
"code" => $r["Nat_TestCode"] . $hardvard_code,
"name" => $r["Nat_TestName"],
"groupID" => $groupID,
"result" => $hardvard_result,
"unit" => "",
"range" => "",
"flag" => "",
];
}
} else {
$result[] = [
"code" => $r["Nat_TestCode"],
"name" => $r["Nat_TestName"],
"groupID" => $groupID,
"result" => "",
"unit" => "",
"range" => "",
"flag" => "",
];
$idx = count($result) - 1;
$sql = "call sp_rpt_t_hasil_so_layanan(?, '1', 'admin')";
$resp = $this->get_sp($sql, [$r["So_ResultEntryID"]]);
if ($resp["status"] == -1) {
$this->reply_err($resp["message"]);
}
$hardvard_result = "";
foreach ($resp["data"] as $d) {
$hardvard_result .=
$d["So_TemplateDetailName"] .
" : " .
str_ireplace(
"\n",
"~",
trim($d["So_ResultEntryDetailResult"])
) .
" ";
}
$result[$idx]["result"] = $hardvard_result;
}
} else if ($groupID == 4) {
$arr_fisik_result = $this->get_fisik_result(
$rows[0]["So_ResultEntryID"],
$debug
);
if ($debug != "") {
//print_r($arr_fisik_result);
}
foreach ($arr_fisik_result as $fr) {
$fr_code = $fr["code"];
if (in_array($fr_code, ["0601", "0600"])) {
if (strpos($fr["result"], "-OD") !== false) {
$fr_code = $fr_code . "-OD";
}
if (strpos($fr["result"], "-OS") !== false) {
$fr_code = $fr_code . "-OS";
}
}
$fr_parent = $fr["parent"];
if ($fr_parent == "visus" && strtolower($fr["name"]) == "tidak diperiksa") {
$fr_code = "0602";
$result[] = [
"code" => $r["Nat_TestCode"] . "-" . $fr_code . "-OD",
"name" => $r["Nat_TestName"],
"sub_name" => "Visus",
"groupID" => $groupID,
"result" => "Tidak diperiksa",
"unit" => "",
"range" => "",
"flag" => "",
];
$result[] = [
"code" => $r["Nat_TestCode"] . "-" . $fr_code . "-OS",
"name" => $r["Nat_TestName"],
"sub_name" => "Visus",
"groupID" => $groupID,
"result" => "Tidak diperiksa",
"unit" => "",
"range" => "",
"flag" => "",
];
} else if ($fr_code == "0203") { // BMI
$result[] = [
"code" => $r["Nat_TestCode"] . "-" . $fr_code,
"name" => $r["Nat_TestName"],
"sub_name" => $fr["name"],
"groupID" => $groupID,
"result" => trim($fr["result"]),
"unit" => "kg/m2",
"range" => "",
"flag" => "",
];
} else {
$result[] = [
"code" => $r["Nat_TestCode"] . "-" . $fr_code,
"name" => $r["Nat_TestName"],
"sub_name" => $fr["name"],
"groupID" => $groupID,
"result" => $fr["result"],
"unit" => "",
"range" => "",
"flag" => "",
];
}
}
}
$sql = "call sp_rpt_result_category_value(?,'admin')";
$resp = $this->get_sp($sql, [$r["So_ResultEntryID"]]);
if ($resp["status"] == -1) {
$this->reply_err($resp["message"]);
}
if (count($resp["data"]) > 0) {
$xcat = "";
if ($debug != "") {
// print_r($resp);
}
foreach ($resp["data"] as $xc) {
if ($xcat != "") $xcat .= ",";
if (strpos($xc["Mcu_StatusResultValueName"], "BI-RAD ") !== false) {
if ($debug != "") {
// echo "\n\n" . $xc["Mcu_StatusResultValueName"] . "\n\n";
$xc["Mcu_StatusResultValueName"] = str_replace("BI-RAD ", "BI-RADS ", $xc["Mcu_StatusResultValueName"]);
}
}
$xcat .= $xc["Mcu_StatusResultValueName"];
}
$r_kes = [
"code" => $r["Nat_TestCode"] . "-cat",
"name" => $r["Nat_TestName"],
"groupID" => $groupID,
"result" => $xcat,
"unit" => "",
"range" => "",
"flag" => "",
];
$result[] = $r_kes;
}
}
$split_result_code = [
"0100", "0101", "0102", "0103", "0104", "0105",
"0200", "0201", "0202", "0203", "0204", "0205"
];
foreach ($result as $xidx => $xr) {
$tmp_code = trim($xr["code"]);
$arr_tmp = explode("-", $tmp_code);
$fr_code = "x123x123";
if (count($arr_tmp) > 1) {
$fr_code = $arr_tmp[1];
}
if (in_array($fr_code, $split_result_code)) {
$tmp_result = trim($xr["result"]);
$tmp_arr = preg_split('/\s+/', $tmp_result);
if ($debug != "") {
echo "$fr_code : $tmp_result => $tmp_arr[0] | $tmp_arr[1] \n\n";
}
if (count($tmp_arr) > 1) {
$result[$xidx]["result"] = trim($tmp_arr[0]);
$result[$xidx]["unit"] = trim($tmp_arr[1]);
}
}
$result_org = $result[$xidx]["result"];
if (strpos($result_org, "\n") > 0) {
$result[$xidx]["result"] = str_ireplace("\n", "~", $result_org);
}
}
return $result;
}
function lab_by_nolab($labNo, $debug = "")
{
$sql = "select T_OrderHeaderID,T_OrderHeaderM_PatientID,
T_OrderHeaderPjM_DoctorID, T_OrderHeaderSenderM_DoctorID,
T_OrderHeaderDate, T_OrderHeaderLabNumberExt,
min(concat(T_OrderSampleHandlingDate,' ', T_OrderSampleHandlingTime)) xtime ,
T_OrderHeaderAddOnReadyPrintDate,
max(T_OrderDetailValDate) valDate,
'' M_CompanyNatCode
from t_orderheader
join m_company on T_OrderHeaderM_CompanyID = M_CompanyID
join t_orderheaderaddon on T_OrderHeaderID = T_OrderHeaderAddOnT_OrderHeaderID
join t_ordersample on T_OrderHeaderID = T_OrderSampleT_OrderheaderID and T_OrderSampleIsActive = 'Y'
join t_orderdetail on T_OrderDetailT_OrderHeaderID = T_OrderHeaderID
and T_OrderDetailIsActive = 'Y'
where T_OrderheaderLabNumber = ?
and T_OrderSampleHandlingDate is not null
group by T_OrderHeaderID";
$qry = $this->db->query($sql, [$labNo]);
if (!$qry) {
$this->reply_err(
$this->db->error()["message"] . "|" . $this->db->last_query()
);
}
$rows = $qry->result_array();
if (count($rows) == 0) {
$this->reply_err("Nolab $labNo not found.");
}
$headerID = $rows[0]["T_OrderHeaderID"];
$companyNatCode = $rows[0]["M_CompanyNatCode"];
$labNoExt = $rows[0]["T_OrderHeaderLabNumberExt"];
$patientID = $rows[0]["T_OrderHeaderM_PatientID"];
$senderID = $rows[0]["T_OrderHeaderSenderM_DoctorID"];
$pjID = $rows[0]["T_OrderHeaderPjM_DoctorID"];
$readyPrintDate = $rows[0]["T_OrderHeaderAddOnReadyPrintDate"];
if ($readyPrintDate == "") {
$readyPrintDate = $rows[0]["valDate"];
}
if ($debug == "") {
$this->is_uploaded("LAB", $headerID);
}
list($xstat, $nik) = $this->get_nik($patientID);
if (!$xstat) {
$this->reply_err("Nolab $labNo | " . $nik);
}
$param = [
"headerID" => $headerID,
"companyNatCode" => $companyNatCode,
"labNo" => $labNo,
"labNoExt" => $labNoExt,
"patientID" => $patientID,
"senderID" => $senderID,
"nik" => $nik,
"pjID" => $pjID,
"observationDate" => date("YmdHis", strtotime($rows[0]["xtime"])),
"readyPrintDate" => date("YmdHis", strtotime($readyPrintDate)),
"orderDate" => date(
"YmdHis",
strtotime($rows[0]["T_OrderHeaderDate"])
),
];
//unit using nat_unit
$sql = "select
ifnull(Nat_TestLoincCode,'ZZZZ') code,Nat_TestName name,
T_OrderDetailResult result,
concat(T_OrderDetailMinValue,'-', T_OrderDetailMaxValue) `range`,
T_OrderDetailMinValue,
T_OrderDetailMaxValue,
T_OrderDetailNormalValueDescription, T_OrderDetailNormalValueNote,
Nat_UnitName unit, T_OrderDetailResultFlag flag,
T_TestIsQuantitative isQuantitative
from t_orderdetail
join t_test on T_OrderDetailT_OrderHeaderID = ?
and T_OrderDetailT_TestID = T_TestID
and T_OrderDetailIsActive = 'Y'
and T_OrderdetailValidation = 'Y'
and T_OrderDetailResult <> ''
and T_TestNat_GroupID =1
join nat_test on Nat_TestID = T_TestNat_TestID
left join nat_unit on T_OrderDetailNat_UnitID = Nat_UnitID
left join nat_test_loinc on Nat_TestID = Nat_TestLoincNat_TestID";
if ($debug != "") {
$sql = "select
ifnull(Nat_TestLoincCode,'ZZZZ') code,Nat_TestName name,
T_OrderDetailResult result,
concat(T_OrderDetailMinValue,'-', T_OrderDetailMaxValue) `range`,
T_OrderDetailMinValue,
T_OrderDetailMaxValue,
T_OrderDetailMinValueInclusive,
T_OrderDetailMaxValueInclusive,
T_OrderDetailNormalValueDescription, T_OrderDetailNormalValueNote,
Nat_UnitName unit, T_OrderDetailResultFlag flag,
T_TestIsQuantitative isQuantitative
from t_orderdetail
join t_test on T_OrderDetailT_OrderHeaderID = ?
and T_OrderDetailT_TestID = T_TestID
and T_OrderDetailIsActive = 'Y'
and T_OrderdetailValidation = 'Y'
and T_OrderDetailResult <> ''
and T_TestNat_GroupID =1
join nat_test on Nat_TestID = T_TestNat_TestID
left join nat_unit on T_OrderDetailNat_UnitID = Nat_UnitID
left join nat_test_loinc on Nat_TestID = Nat_TestLoincNat_TestID";
}
$qry = $this->db->query($sql, [$headerID]);
$last_qry = $this->db->last_query();
if (!$qry) {
$this->reply_err(
$this->db->error()["message"] . "|" . $this->db->last_query()
);
}
$rows = $qry->result_array();
if (count($rows) == 0) {
$this->reply_err("No Result $headerID.");
}
if ($debug != "") {
// print_r($rows);
}
foreach ($rows as $idx => $r) {
if (trim($r["range"]) == "-") {
$rows[$idx]["range"] = "";
}
if (trim($r["unit"]) == "-") {
$rows[$idx]["unit"] = "";
}
if ($debug != "") {
$xrange = "";
$minValue = trim($r["T_OrderDetailMinValue"]);
$maxValue = trim($r["T_OrderDetailMaxValue"]);
if ($r["T_OrderDetailMinValueInclusive"] == "Y") {
//if ($minValue != "") $minValue = "$minValue >=";
} else if ($r["T_OrderDetailMinValueInclusive"] == "N") {
if ($minValue != "") $minValue = "$minValue >";
}
if ($r["T_OrderDetailMaxValueInclusive"] == "N") {
if ($maxValue != "") $maxValue = " < $maxValue";
} else if ($r["T_OrderDetailMaxValueInclusive"] == "Y") {
//if ($maxValue != "") $maxValue = " <= $maxValue >";
}
if ($minValue != "") $xrange = "$minValue";
if ($maxValue != "") {
if ($xrange != "") $xrange .= " - ";
$xrange = $xrange . $maxValue;
}
$rows[$idx]["range"] = $xrange;
}
}
$param["result"] = $rows;
$msg = $this->lab($param);
if ($debug != "") {
echo $last_qry;
}
$this->reply($msg);
}
function msh($branchCode, $headerID)
{
//controlID = 10 digit T_OrderHeaderID
//MSH|^~\&|ULTRA|LAB|MESHBIO|MESHBIO|20220713040814||ORU^R01|ORU_R010001798440|P|2.3.1|||AL|ER|SG||en
$sendingApp = "BizOne"; // max 15
$sendingFacility = $branchCode; // 20
$curDate = date("YmdHis");
$controlID = sprintf("%010d", $headerID);
$version = "2.3.1";
$arr = [
"MSH",
"^~\&",
$sendingApp,
$sendingFacility,
"MESHBIO",
"MESHBIO",
$curDate,
"",
"ORU^R01",
"ORU_R01${controlID}",
"P",
$version,
// "",
// "",
// "AL",
// "ER",
// "SG",
// "",
// "en",
];
return implode("|", $arr);
}
function pid($patientID, $patientName, $dob, $sex, $companyNatCode)
{
//PID|1|S0000000G||S0000000G|PATIENT TEST||19881004|M||||||||||||||||||||NATIONALITY
// $internalPatientID = $patientID;
$alternatePatientID = $patientID; // max 16 , 20 , 12
if ($companyNatCode != "") {
$alternatePatientID = $companyNatCode;
}
$patientName = substr($patientName, 0, 40);
$arr = [
"PID",
"1",
$patientID, // 16
"",
$alternatePatientID, // 12
$patientName, // 48
"",
$dob,
$sex,
// "",
// "",
// "",
// "",
// "",
// "",
// "",
// "",
// "",
// "",
// "",
// "",
// "",
// "",
// "",
// "",
// "",
// "",
// "",
// $nationality, //
];
return implode("|", $arr);
}
function pv1($assignPatientLocation = "LAB", $drPj, $drSender, $noLab)
{
// PV1|1|O|CLINIC CODE||||WH096^DOCTOR NAME||||||||||||QW096-22-3805581
$visitNumber = substr($assignPatientLocation, 0, 3) . $noLab;
$arr = [
"PV1",
"1",
"O", // Out Patient
$assignPatientLocation, // <nurse unit> ^ <room> ^ <bed> ^ < facility ID> ^ <bed status>
"",
"",
"",
$drPj, // attending Dr 60 $code^$name
$drSender, // refferingDr 60 $code^$name
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
$visitNumber, //visit Number 15
];
return implode("|", $arr);
}
function orc($noLab, $orderDateTime, $resultDateTime, $drSender)
{
//ORC|RE|22-3805581^LAB|22-3805581^ULTRA||CM||^^^20220712193600^^RT|X111084^WACS2&HEALTH SCREEN (WACS2)|20220712193600|||WH096^DOCTOR NAME
$arr = [
"ORC",
"RE",
$noLab . "^LAB", // 15 nolab^LAB -- placer
$noLab . "^BIZ", // -- filler
"",
"CM",
"",
"^^^$resultDateTime^^RT",
"", //parent placer
$orderDateTime,
"",
"",
$drSender,
];
return implode("|", $arr);
}
function obr($noLab, $observationDateTime, $resultDateTime)
{
// OBR|1|22-3805581^LAB|22-3805581^ULTRA|X111084^HEALTH SCREEN (WACS2)^QUE||20220712193600|20220712193600|||||||||||||||20220713040500|||F
$arr = [
"OBR",
"1",
$noLab . "^LAB", // 15 nolab^LAB -- placer
$noLab . "^BIZ", // -- filler
"",
"",
$observationDateTime,
$observationDateTime,
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
$resultDateTime,
"",
"",
"F",
];
return implode("|", $arr);
}
// code name isQuantitative result unit range flag
function obx($results, &$obx_start_counter)
{
$ret = "";
foreach ($results as $idx => $r) {
// if ($r["code"] == "") {
// print_r($r);
// }
if ($ret != "") {
$ret .= $this->cr;
}
$counter = $obx_start_counter++;
$valueType = "NM";
if ($r["IsQuantitative"] == "N" || !is_numeric($r["result"])) {
$valueType = "FT";
}
$systemCode = "QUE";
if ($r["code"] == "ZZZZ") {
$systemCode = "PRM";
}
$r["result"] = str_replace("\n", "~", $r["result"]);
if (in_array("sub_name", array_keys($r))) {
$arr = [
"OBX",
$counter,
$valueType, //Kualitative FT | Quantitative NM
"${r["code"]}^^^${r["sub_name"]}^^^${r["name"]}^${systemCode}",
"",
$r["result"],
$r["unit"], //units
$r["range"], //ref range
$r["flag"], //flag default N
"",
"",
"F",
];
} else {
$arr = [
"OBX",
$counter,
$valueType, //Kualitative FT | Quantitative NM
"${r["code"]}^${r["name"]}^${systemCode}",
"",
$r["result"],
$r["unit"], //units
$r["range"], //ref range
$r["flag"], //flag default N
"",
"",
"F",
];
}
$ret .= implode("|", $arr);
}
return $ret;
}
function reply($data)
{
echo json_encode(["status" => "OK", "data" => $data]);
exit();
}
function reply_err($message)
{
echo json_encode(["status" => "ERR", "data" => $message]);
exit();
}
function get_fisik_pajanan($param, $result_entry_id, $type, $lang_id = 1)
{
$url = "http://localhost/one-api/v1/report/fisik/$param/$result_entry_id/$lang_id/$type/k3";
$resp = $this->get($url);
// if ($debug != "") {
// echo "$url \n";
// print_r($resp);
// echo "\n";
// }
return json_decode($resp, true);
}
function get_fisik($param, $result_entry_id, $type, $lang_id = 1)
{
$url = "http://localhost/one-api/v1/report/fisik/$param/$result_entry_id/$lang_id/$type";
$resp = $this->get($url);
// if ($debug != "") {
// echo "url : $url\n";
// print_r($resp);
// echo "\n";
// }
return json_decode($resp, true);
}
function get($url, $timeout = 60, $c_timeout = 5)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $c_timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$err_msg = curl_error($ch);
if ($err_msg != "") {
return json_encode(["status" => "ERR", "message" => $err_msg]);
}
return $result;
}
function get_sp($sql, $param = false)
{
$resp = $this->get_rows($sql, $param);
$this->clean_mysqli_connection($this->db->conn_id);
return $resp;
}
function get_rows($sql, $param = false)
{
if ($param) {
$qry = $this->db->query($sql, $param);
} else {
$qry = $this->db->query($sql);
}
if (!$qry) {
return [
"status" => -1,
"message" =>
$this->db->last_query() .
"|" .
$this->db->error()["message"],
];
}
return ["status" => 0, "data" => $qry->result_array()];
}
function get_row($sql, $param = false)
{
$resp = $this->get_rows($sql, $param);
if ($resp["status"] == -1) {
return $resp;
}
if (count($resp["data"]) == 0) {
return ["status" => 0, "message" => "Not found."];
}
return ["status" => 1, "data" => $resp["data"][0]];
}
}