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

2687 lines
91 KiB
PHP

<?php
class Auto_valid extends MY_Controller
{
/*
drop table if exists auto_valid;
create table auto_valid (
AutoValidID int not null auto_increment primary key,
AutoValidDate datetime default current_timestamp(),
AutoValidT_OrderDetailID int,
AutoValidT_OrderDetailResult varchar(300),
AutoValidHaveReq varchar(1) default 'N',
AutoValidNat_NormalValueID int,
AutoValidNat_MethodeID int,
AutoValidNat_ConditionID int,
AutoValidVerifDate datetime,
AutoValidVerifUserID int,
AutoValidVerifUserName varchar(100),
AutoValidNote text,
AutoValidIsOk varchar(1) default 'N',
AutoValidIsActive varchar(1) default 'Y',
key (AutoValidDate),
key (AutoValidT_OrderDetailID),
key (AutoValidIsActive),
key (AutoValidIsOk)
);
alter table auto_valid add AutoValidIsReviewRange
varchar(1) default 'N',
add key(AutoValidIsReviewRange);
drop table if exists auto_valid_v2;
create table auto_valid_v2 (
AutoValidID int not null auto_increment primary key,
AutoValidDate datetime default current_timestamp(),
AutoValidT_OrderDetailID int,
AutoValidAutoVerifIsOK varchar(1) default 'N',
AutoValidAutoVerifNote varchar(100),
AutoValidIsQuantitative varchar(1) default 'Y',
AutoValidIsInReviewRange varchar(1) default 'N',
AutoValidInReviewRangeNote varchar(100) default '',
AutoValidIsInconsistencyOK varchar(1) default 'N',
AutoValidInconsistencyNote varchar(1) default 'N',
AutoValidIsDeltacheckOK varchar(1) default 'N',
AutoValidDeltaCheckNote varchar(100) default '',
AutoValidReflexTestIsOK varchar(1) default 'N',
AutoValidReflexTestNote varchar(300) default '',
AutoValidIsActive varchar(1) default 'Y',
key (AutoValidDate),
key (AutoValidT_OrderDetailID),
key (AutoValidIsActive)
);
*/
var $isUpdate = true;
function check($nolab)
{
$sql = "select
T_OrderHeaderDate, T_OrderHeaderLabNumber,
T_TestName, T_OrderDetailID, T_OrderDetailVerDate, T_OrderDetailVerUserID,
T_OrderDetailResult
from t_orderheader
join t_orderdetail on T_OrderHeaderLabNumber = ?
and T_OrderHeaderID = T_OrderDetailT_OrderHeaderID
and T_OrderHeaderIsActive = 'Y'
and T_OrderDetailIsActive = 'Y'
and T_OrderDetailVerification = 'Y'
and T_OrderDetailID not in (
select AutoValidT_OrderDetailID
from auto_valid
where AutoValidIsActive = 'Y'
)
join t_test on T_OrderDetailT_TestID = T_TestID and T_TestIsResult = 'Y'
order by T_OrderHeaderID,T_TestSasCode
";
$qry = $this->db->query($sql, [$nolab]);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] . "\n|" . $this->db->last_query()
);
exit();
}
$rows = $qry->result_array();
foreach ($rows as $idx => $r) {
$rows[$idx]["Process"] =
"<a href='/one-api/process/auto_valid/debug/" .
$r["T_OrderDetailID"] .
"'> Debug </a>";
}
$this->print_table_style();
$this->print_table($rows, array_keys($rows[0]));
}
function list($date = "", $format = "html")
{
if ($date == "") {
$date = date("Y-m-d");
}
$start = $date . " 00:00:01";
$end = $date . " 23:59:59";
$sql = "select
T_OrderHeaderDate, T_OrderHeaderLabNumber,
AutoValidDate,
AutoValidHaveReq HaveRequirement,
AutoValidIsOk,
T_OrderDetailT_TestName T_TestName,
-- Nat_MethodeName,
Nat_ConditionName,
replace(AutoValidNote,'\n','<br/>') Note
,replace(AutoVerifTrendAnalysisNote,'\n','<br/>') VerifNote
,T_OrderDetailID
from auto_valid
join auto_verif on AutoValidT_OrderDetailID = AutoVerifT_OrderDetailID
join t_orderdetail on AutoValidT_OrderDetailID = T_OrderDetailID
and T_OrderDetailIsActive = 'Y'
join t_orderheader on T_OrderDetailT_OrderHeaderID = T_OrderHeaderID
join t_test on T_OrderDetailT_TestID = T_TestID
and T_TestNat_GroupID = 1
left join nat_methode on AutoValidNat_MethodeID = Nat_MethodeID
left join nat_condition on AutoValidNat_ConditionID = Nat_ConditionID
where T_OrderHeaderDate >= ? and T_OrderHeaderDate <= ?
order by T_OrderHeaderID, T_TestSasCode
";
$qry = $this->db->query($sql, [$start, $end]);
if (!$qry) {
echo "Error : " .
$this->db->error()["message"] .
" | " .
$this->db->last_query();
exit();
}
$rows = $qry->result_array();
$header = [];
$header[] = [
"TotalPx" => 0,
"TotalAutoValid" => 0,
];
$prevLabNo = "";
foreach ($rows as $idx => $r) {
if ($r["HaveRequirement"] == "N" && $r["AutoValidIsOk"] == "Y") {
$header[0]["TotalAutoValid"]++;
}
$header[0]["TotalPx"]++;
if ($r["T_OrderHeaderLabNumber"] == $prevLabNo) {
$rows[$idx]["T_OrderHeaderLabNumber"] = "";
$rows[$idx]["T_OrderHeaderDate"] = "";
}
$prevLabNo = $r["T_OrderHeaderLabNumber"];
$rows[$idx]["Telusur"] =
"<a href='/one-api/process/auto_valid/debug/" .
$r["T_OrderDetailID"] .
"'> Check </a>";
if ($r["AutoValidIsReviewRange"] != "Y") {
$rows[$idx]["AutoValidNote"] .=
($r["AutoValidNote"] != "" ? "|" : "") . $r["VerifNote"];
}
unset($rows[$idx]["T_OrderDetailID"]);
unset($rows[$idx]["VerifNote"]);
}
if ($format == "html") {
$this->print_table_style();
$this->print_table($header, array_keys($header[0]));
$this->print_table($rows, array_keys($rows[0]));
}
}
function index()
{
$this->list();
}
function get_param()
{
$jparam = file_get_contents("php://input");
$param = json_decode($jparam, true);
return $param;
}
function calculate($id, $debug = false)
{
$sql = "select T_OrderDetailID, T_OrderDetailT_OrderHeaderID,
ifnull(T_OrderReqStatus,'Y') T_OrderReqStatus,
T_TestName, T_OrderDetailID, T_OrderDetailVerDate, T_OrderDetailVerUserID,
T_OrderDetailResult, T_OrderDetailNat_NormalValueID, T_OrderDetailNat_MethodeID,
M_UserUserName, T_OrderDetailMinValue,T_OrderDetailMinValueInclusive,
T_OrderDetailMaxValue, T_OrderDetailMaxValueInclusive, T_OrderdetailNat_MethodeName,
T_TestNat_TestID, ifnull(AutoVerifReviewRangeIsOK,'N') ReviewRangeIsOK,
AutoVerifTrendAnalysisImage
from t_orderdetail
join t_test on T_OrderDetailIsActive = 'Y'
and T_OrderDetailT_TestID = T_TestID
and T_OrderDetailVerification = 'Y'
and T_TestIsResult = 'Y'
join auto_verif on AutoVerifT_OrderDetailID = T_OrderDetailID
join m_user on T_OrderDetailVerUserID = M_UserID
left join t_orderreq on T_OrderDetailT_OrderHeaderID = T_OrderReqT_OrderHeaderID
and T_OrderReqIsActive = 'Y'
where T_OrderDetailID = ?
and T_OrderDetailID not in (select AutoValidT_OrderDetailID from auto_valid where AutoValidIsActive = 'Y' )";
if ($debug) {
$sql = "select T_OrderDetailID, T_OrderDetailT_OrderHeaderID,
ifnull(T_OrderReqStatus,'Y') T_OrderReqStatus,
T_TestName, T_OrderDetailID, T_OrderDetailVerDate, T_OrderDetailVerUserID,
T_OrderDetailResult, T_OrderDetailNat_NormalValueID, T_OrderDetailNat_MethodeID,
M_UserUserName, T_OrderDetailMinValue,T_OrderDetailMinValueInclusive,
T_OrderDetailMaxValue, T_OrderDetailMaxValueInclusive, T_OrderdetailNat_MethodeName,
T_TestNat_TestID, ifnull(AutoVerifReviewRangeIsOK,'N') ReviewRangeIsOK,
AutoVerifTrendAnalysisImage
from t_orderdetail
join t_test on T_OrderDetailIsActive = 'Y'
and T_OrderDetailT_TestID = T_TestID
and T_OrderDetailVerification = 'Y'
and T_TestIsResult = 'Y'
join auto_verif on AutoVerifT_OrderDetailID = T_OrderDetailID
join m_user on T_OrderDetailVerUserID = M_UserID
left join t_orderreq on T_OrderDetailT_OrderHeaderID = T_OrderReqT_OrderHeaderID
and T_OrderReqIsActive = 'Y'
where T_OrderDetailID = ?";
}
$qry = $this->db->query($sql, [$id]);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] . "\n|" . $this->db->last_query()
);
exit();
}
$rows = $qry->result_array();
if (count($rows) == 0) {
if ($debug) {
echo "OrderDetail Not Found";
}
return false;
}
$r = $rows[0];
$haveReq = "N";
if ($r["T_OrderReqStatus"] != "Y") {
$haveReq = "Y";
return [
"AutoValidT_OrderDetailID" => $r["T_OrderDetailID"],
"AutoValidHaveReq" => $haveReq,
"AutoValidIsReviewRange" => $r["ReviewRangeIsOK"],
"AutoValidNote" => "Tidak berlaku, karena ada catatan",
"AutoValidIsOk" => "N",
"AutoValidVerifDate" => $r["T_OrderDetailVerDate"],
"AutoValidVerifUserID" => $r["T_OrderDetailVerUserID"],
"AutoValidNat_NormalValueID" =>
$r["T_OrderDetailNat_NormalValueID"],
"AutoValidNat_MethodeID" => $r["T_OrderDetailNat_MethodeID"],
"AutoValidVerifUserName" => $r["M_UserUserName"],
"AutoValidT_OrderDetailResult" => $r["T_OrderDetailResult"],
"image" => $r["AutoVerifTrendAnalysisImage"],
];
}
if ($r["ReviewRangeIsOK"] != "Y") {
return [
"AutoValidT_OrderDetailID" => $r["T_OrderDetailID"],
"AutoValidHaveReq" => $haveReq,
"AutoValidIsReviewRange" => $r["ReviewRangeIsOK"],
"AutoValidNote" =>
"Tidak berlaku, Review Range tidak terpenuhi",
"AutoValidIsOk" => "N",
"AutoValidVerifDate" => $r["T_OrderDetailVerDate"],
"AutoValidVerifUserID" => $r["T_OrderDetailVerUserID"],
"AutoValidNat_NormalValueID" =>
$r["T_OrderDetailNat_NormalValueID"],
"AutoValidNat_MethodeID" => $r["T_OrderDetailNat_MethodeID"],
"AutoValidVerifUserName" => $r["M_UserUserName"],
"AutoValidT_OrderDetailResult" => $r["T_OrderDetailResult"],
"image" => $r["AutoVerifTrendAnalysisImage"],
];
}
if (!is_numeric($r["T_OrderDetailResult"])) {
return [
"AutoValidT_OrderDetailID" => $r["T_OrderDetailID"],
"AutoValidHaveReq" => $haveReq,
"AutoValidIsReviewRange" => $r["ReviewRangeIsOK"],
"AutoValidNote" => "Tidak berlaku, karena hasil bukan numerik",
"AutoValidIsOk" => "N",
"AutoValidVerifDate" => $r["T_OrderDetailVerDate"],
"AutoValidVerifUserID" => $r["T_OrderDetailVerUserID"],
"AutoValidNat_NormalValueID" =>
$r["T_OrderDetailNat_NormalValueID"],
"AutoValidNat_MethodeID" => $r["T_OrderDetailNat_MethodeID"],
"AutoValidVerifUserName" => $r["M_UserUserName"],
"AutoValidT_OrderDetailResult" => $r["T_OrderDetailResult"],
"image" => $r["AutoVerifTrendAnalysisImage"],
];
}
if (
!($r["T_OrderDetailNat_NormalValueID"] > 0 &&
$r["T_OrderDetailNat_MethodeID"] > 0
)
) {
return [
"AutoValidT_OrderDetailID" => $r["T_OrderDetailID"],
"AutoValidHaveReq" => $haveReq,
"AutoValidIsReviewRange" => $r["ReviewRangeIsOK"],
"AutoValidNote" =>
"Tidak berlaku, karena tidak ada Normal Value atau Methode",
"AutoValidIsOk" => "N",
"AutoValidVerifDate" => $r["T_OrderDetailVerDate"],
"AutoValidVerifUserID" => $r["T_OrderDetailVerUserID"],
"AutoValidVerifUserName" => $r["M_UserUserName"],
"AutoValidNat_NormalValueID" =>
$r["T_OrderDetailNat_NormalValueID"],
"AutoValidNat_MethodeID" => $r["T_OrderDetailNat_MethodeID"],
"AutoValidT_OrderDetailResult" => $r["T_OrderDetailResult"],
"image" => $r["AutoVerifTrendAnalysisImage"],
];
}
$minValue = $r["T_OrderDetailMinValue"];
$maxValue = $r["T_OrderDetailMaxValue"];
$minInclusive = $r["T_OrderDetailMinValueInclusive"];
$maxInclusive = $r["T_OrderDetailMaxValueInclusive"];
$methodeID = $r["T_OrderDetailNat_MethodeID"];
$methode = $r["T_OrderdetailNat_MethodeName"];
$value = $r["T_OrderDetailResult"];
$theTestName = $r["T_TestName"];
$nat_test_id = $r["T_TestNat_TestID"];
$headerID = $r["T_OrderDetailT_OrderHeaderID"];
try {
$n_value = floatval($value);
$n_minValue = floatval($minValue);
$n_maxValue = floatval($maxValue);
} catch (Exception $e) {
return [
"AutoValidT_OrderDetailID" => $r["T_OrderDetailID"],
"AutoValidHaveReq" => $haveReq,
"AutoValidIsReviewRange" => $r["ReviewRangeIsOK"],
"AutoValidNote" =>
"Multirule tidak berlaku karena Normal Value Min / Max tidak numerik",
"AutoValidIsOk" => "N",
"AutoValidVerifDate" => $r["T_OrderDetailVerDate"],
"AutoValidVerifUserID" => $r["T_OrderDetailVerUserID"],
"AutoValidVerifUserName" => $r["M_UserUserName"],
"AutoValidNat_NormalValueID" =>
$r["T_OrderDetailNat_NormalValueID"],
"AutoValidNat_MethodeID" => $r["T_OrderDetailNat_MethodeID"],
"AutoValidT_OrderDetailResult" => $r["T_OrderDetailResult"],
"image" => $r["AutoVerifTrendAnalysisImage"],
];
}
// Klasifikasi hasil
$conditionID = 0;
$condition = "Unknown";
if (
$n_value < $n_minValue ||
($n_value <= $n_minValue && $minInclusive == "N")
) {
$conditionID = 1;
$condition = "Low";
} elseif (
$n_value > $n_maxValue ||
($n_value >= $n_maxValue && $maxInclusive == "N")
) {
$conditionID = 3;
$condition = "High";
} else {
$conditionID = 2;
$condition = "Normal";
}
if ($conditionID == 0) {
return [
"AutoValidT_OrderDetailID" => $r["T_OrderDetailID"],
"AutoValidHaveReq" => $haveReq,
"AutoValidIsReviewRange" => $r["ReviewRangeIsOK"],
"AutoValidNote" => "Multirule tidak berlaku karena kondisi Low/Normal/High tidak bisa ditentukan [ $n_value | min : $n_minValue | max : $n_maxValue ]",
"AutoValidIsOk" => "N",
"AutoValidVerifDate" => $r["T_OrderDetailVerDate"],
"AutoValidVerifUserID" => $r["T_OrderDetailVerUserID"],
"AutoValidVerifUserName" => $r["M_UserUserName"],
"AutoValidNat_NormalValueID" =>
$r["T_OrderDetailNat_NormalValueID"],
"AutoValidNat_MethodeID" => $r["T_OrderDetailNat_MethodeID"],
"AutoValidT_OrderDetailResult" => $r["T_OrderDetailResult"],
"image" => $r["AutoVerifTrendAnalysisImage"],
];
}
$sql = "select * from nat_multirule
where NatMultiruleIsActive = 'Y' and NatMultiruleNat_TestID = ?
and ( NatMultiruleNat_MethodeID is null or NatMultiruleNat_MethodeID = 0 or NatMultiruleNat_MethodeID = ?
or NatMultiruleIsAllMethode='Y' )
and NatMultiruleNat_ConditionID = ?
order by NatMultiruleNat_MethodeID desc
limit 0,1";
$qry = $this->db->query($sql, [$nat_test_id, $methodeID, $conditionID]);
if ($debug) {
$arr = [];
$arr[] = ["Query" => $this->db->last_query()];
$this->print_table_style();
$this->print_table($arr, array_keys($arr[0]));
}
if (!$qry) {
return [
"AutoValidT_OrderDetailID" => $r["T_OrderDetailID"],
"AutoValidHaveReq" => $haveReq,
"AutoValidIsReviewRange" => $r["ReviewRangeIsOK"],
"AutoValidNote" =>
"Multirule tidak berlaku karena multirule belum di setup [ $theTestName | kondisi : $condition | methode : $methode ]" .
$this->db->error()["message"],
"AutoValidIsOk" => "N",
"AutoValidVerifDate" => $r["T_OrderDetailVerDate"],
"AutoValidVerifUserID" => $r["T_OrderDetailVerUserID"],
"AutoValidVerifUserName" => $r["M_UserUserName"],
"AutoValidNat_ConditionID" => $conditionID,
"AutoValidNat_NormalValueID" =>
$r["T_OrderDetailNat_NormalValueID"],
"AutoValidNat_MethodeID" => $r["T_OrderDetailNat_MethodeID"],
"AutoValidT_OrderDetailResult" => $r["T_OrderDetailResult"],
"image" => $r["AutoVerifTrendAnalysisImage"],
];
}
$rows_rule = $qry->result_array();
if (count($rows_rule) == 0) {
if ($conditionID == 2) {
return [
"AutoValidT_OrderDetailID" => $r["T_OrderDetailID"],
"AutoValidHaveReq" => $haveReq,
"AutoValidIsReviewRange" => $r["ReviewRangeIsOK"],
"AutoValidNote" => "Terpenuhi, $theTestName untuk kondisi $condition dengan methode $methode tidak memiliki reflex test.",
"AutoValidIsOk" => "Y",
"AutoValidVerifDate" => $r["T_OrderDetailVerDate"],
"AutoValidVerifUserID" => $r["T_OrderDetailVerUserID"],
"AutoValidVerifUserName" => $r["M_UserUserName"],
"AutoValidNat_ConditionID" => $conditionID,
"AutoValidNat_NormalValueID" =>
$r["T_OrderDetailNat_NormalValueID"],
"AutoValidNat_MethodeID" =>
$r["T_OrderDetailNat_MethodeID"],
"AutoValidT_OrderDetailResult" => $r["T_OrderDetailResult"],
"image" => $r["AutoVerifTrendAnalysisImage"],
];
}
return [
"AutoValidT_OrderDetailID" => $r["T_OrderDetailID"],
"AutoValidHaveReq" => $haveReq,
"AutoValidIsReviewRange" => $r["ReviewRangeIsOK"],
"AutoValidNote" => "Tidak terpenuhi, $theTestName untuk kondisi $condition dengan methode $methode tidak memiliki reflex test.",
"AutoValidIsOk" => "N",
"AutoValidVerifDate" => $r["T_OrderDetailVerDate"],
"AutoValidVerifUserID" => $r["T_OrderDetailVerUserID"],
"AutoValidVerifUserName" => $r["M_UserUserName"],
"AutoValidNat_ConditionID" => $conditionID,
"AutoValidNat_NormalValueID" =>
$r["T_OrderDetailNat_NormalValueID"],
"AutoValidNat_MethodeID" => $r["T_OrderDetailNat_MethodeID"],
"AutoValidT_OrderDetailResult" => $r["T_OrderDetailResult"],
"image" => $r["AutoVerifTrendAnalysisImage"],
];
}
$r_rule = $rows_rule[0];
$mrID = $r_rule["NatMultiruleID"];
if (count($rows_rule) > 0) {
// print_r($rows_rule);
}
//Extra Low
if ($conditionID == 1) {
try {
$n_x_low = floatval($r_rule["NatMultiruleExtraLow"]);
} catch (Exception $e) {
return [
"AutoValidT_OrderDetailID" => $r["T_OrderDetailID"],
"AutoValidHaveReq" => $haveReq,
"AutoValidIsReviewRange" => $r["ReviewRangeIsOK"],
"AutoValidNote" =>
"Tidak berlaku, karena masterdata extra low bukan numerik",
"AutoValidIsOk" => "N",
"AutoValidVerifDate" => $r["T_OrderDetailVerDate"],
"AutoValidVerifUserID" => $r["T_OrderDetailVerUserID"],
"AutoValidVerifUserName" => $r["M_UserUserName"],
"AutoValidNat_NormalValueID" =>
$r["T_OrderDetailNat_NormalValueID"],
"AutoValidNat_ConditionID" => $conditionID,
"AutoValidNat_MethodeID" =>
$r["T_OrderDetailNat_MethodeID"],
"AutoValidT_OrderDetailResult" => $r["T_OrderDetailResult"],
"image" => $r["AutoVerifTrendAnalysisImage"],
];
}
if ($n_x_low > $n_value) {
return [
"AutoValidT_OrderDetailID" => $r["T_OrderDetailID"],
"AutoValidHaveReq" => $haveReq,
"AutoValidIsReviewRange" => $r["ReviewRangeIsOK"],
"AutoValidNote" => "Tidak berlaku, karena hasil lebih rendah dari extra low [ $n_value < $n_x_low ]",
"AutoValidIsOk" => "N",
"AutoValidVerifDate" => $r["T_OrderDetailVerDate"],
"AutoValidVerifUserID" => $r["T_OrderDetailVerUserID"],
"AutoValidVerifUserName" => $r["M_UserUserName"],
"AutoValidNat_NormalValueID" =>
$r["T_OrderDetailNat_NormalValueID"],
"AutoValidNat_ConditionID" => $conditionID,
"AutoValidNat_MethodeID" =>
$r["T_OrderDetailNat_MethodeID"],
"AutoValidT_OrderDetailResult" => $r["T_OrderDetailResult"],
"image" => $r["AutoVerifTrendAnalysisImage"],
];
}
}
//Extra Hight
if ($conditionID == 3) {
try {
$n_x_high = floatval($r_rule["NatMultiruleExtraHigh"]);
} catch (Exception $e) {
return [
"AutoValidT_OrderDetailID" => $r["T_OrderDetailID"],
"AutoValidHaveReq" => $haveReq,
"AutoValidIsReviewRange" => $r["ReviewRangeIsOK"],
"AutoValidNote" =>
"Tidak berlaku, karena masterdata extra high bukan numerik",
"AutoValidIsOk" => "N",
"AutoValidVerifDate" => $r["T_OrderDetailVerDate"],
"AutoValidVerifUserID" => $r["T_OrderDetailVerUserID"],
"AutoValidVerifUserName" => $r["M_UserUserName"],
"AutoValidNat_ConditionID" => $conditionID,
"AutoValidNat_NormalValueID" =>
$r["T_OrderDetailNat_NormalValueID"],
"AutoValidNat_MethodeID" =>
$r["T_OrderDetailNat_MethodeID"],
"AutoValidT_OrderDetailResult" => $r["T_OrderDetailResult"],
"image" => $r["AutoVerifTrendAnalysisImage"],
];
}
if ($n_x_high < $n_value) {
return [
"AutoValidT_OrderDetailID" => $r["T_OrderDetailID"],
"AutoValidHaveReq" => $haveReq,
"AutoValidIsReviewRange" => $r["ReviewRangeIsOK"],
"AutoValidNote" => "Tidak berlaku, karena hasil lebih tinggi dari extra hight [ $n_value > $n_x_high]",
"AutoValidIsOk" => "N",
"AutoValidVerifDate" => $r["T_OrderDetailVerDate"],
"AutoValidVerifUserID" => $r["T_OrderDetailVerUserID"],
"AutoValidVerifUserName" => $r["M_UserUserName"],
"AutoValidNat_ConditionID" => $conditionID,
"AutoValidNat_NormalValueID" =>
$r["T_OrderDetailNat_NormalValueID"],
"AutoValidNat_MethodeID" =>
$r["T_OrderDetailNat_MethodeID"],
"AutoValidT_OrderDetailResult" => $r["T_OrderDetailResult"],
"image" => $r["AutoVerifTrendAnalysisImage"],
];
}
}
//Argument 1
$sql = "select distinct Nat_ReflexTestID,
Nat_TestName, Nat_ConditionName, Nat_TestID,
Nat_ReflexTestNat_ConditionID,
T_OrderDetailNat_NormalValueID, T_OrderDetailResult, T_OrderDetailNat_MethodeName,
T_OrderDetailMaxValueInclusive, T_OrderDetailMinValueInclusive,
T_OrderDetailMinValue, T_OrderDetailMaxValue, T_OrderDetailVerification
from
nat_reflextest
join nat_condition on Nat_ReflexTestNat_ConditionID = Nat_ConditionID and Nat_ReflexTestIsActive = 'Y'
join nat_test on Nat_ReflexTestNat_TestID = Nat_TestID and Nat_TestIsActive = 'Y'
and Nat_ReflexTestIsActive = 'Y' and Nat_ReflexTestNatMultiRuleID = ?
join t_test on Nat_TestID = T_TestNat_TestID and T_TestIsActive = 'Y'
join t_orderdetail on T_TestID = T_OrderDetailT_TestID
and T_OrderDetailIsActive = 'Y' and T_OrderDetailT_OrderHeaderID = ?
order by Nat_ReflexTestID, Nat_ReflexTestNat_RelationID desc";
$qry = $this->db->query($sql, [$mrID, $headerID]);
if (!$qry) {
return [
"AutoValidT_OrderDetailID" => $r["T_OrderDetailID"],
"AutoValidHaveReq" => $haveReq,
"AutoValidIsReviewRange" => $r["ReviewRangeIsOK"],
"AutoValidNote" =>
"Tidak berlaku, karena $theTestName dengan kondisi $condition, tidak memiliki masterdata Argument 1" .
"|" .
$this->db->error()["message"],
"AutoValidIsOk" => "N",
"AutoValidVerifDate" => $r["T_OrderDetailVerDate"],
"AutoValidVerifUserID" => $r["T_OrderDetailVerUserID"],
"AutoValidNat_ConditionID" => $conditionID,
"AutoValidVerifUserName" => $r["M_UserUserName"],
"AutoValidNat_NormalValueID" =>
$r["T_OrderDetailNat_NormalValueID"],
"AutoValidNat_MethodeID" => $r["T_OrderDetailNat_MethodeID"],
"AutoValidT_OrderDetailResult" => $r["T_OrderDetailResult"],
"image" => $r["AutoVerifTrendAnalysisImage"],
];
}
$rows_reflex = $qry->result_array();
if (count($rows_reflex) == 0) {
return [
"AutoValidT_OrderDetailID" => $r["T_OrderDetailID"],
"AutoValidHaveReq" => $haveReq,
"AutoValidIsReviewRange" => $r["ReviewRangeIsOK"],
"AutoValidNote" => "Tidak berlaku, karena $theTestName dengan kondisi $condition, tidak memiliki masterdata Argument 1",
"AutoValidIsOk" => "N",
"AutoValidVerifDate" => $r["T_OrderDetailVerDate"],
"AutoValidVerifUserID" => $r["T_OrderDetailVerUserID"],
"AutoValidVerifUserName" => $r["M_UserUserName"],
"AutoValidNat_ConditionID" => $conditionID,
"AutoValidNat_NormalValueID" =>
$r["T_OrderDetailNat_NormalValueID"],
"AutoValidNat_MethodeID" => $r["T_OrderDetailNat_MethodeID"],
"AutoValidT_OrderDetailResult" => $r["T_OrderDetailResult"],
"image" => $r["AutoVerifTrendAnalysisImage"],
];
}
$argument_1_list = [];
$arr_px_1 = [];
foreach ($rows_reflex as $rfx) {
$rfx_conditionID = $rfx["Nat_ReflexTestNat_ConditionID"];
$rfx_conditionName = $rfx["Nat_ConditionName"];
$rfx_nat_TestID = $rfx["Nat_TestID"];
if (isset($arr_px_1[$rfx_nat_TestID])) {
$rfx_idx = $arr_px_1[$rfx_nat_TestID];
} else {
$argument_1_list[] = [
"px" => $rfx["Nat_TestName"],
"pxID" => $rfx["Nat_TestID"],
"reflexID" => $rfx["Nat_ReflexTestID"],
"is_ok" => false,
"is_show_mandatory" => false,
"condition" => $rfx_conditionName,
"conditionID" => $rfx_conditionID,
"note" => "",
];
$rfx_idx = count($argument_1_list) - 1;
$arr_px_1[$rfx_nat_TestID] = $rfx_idx;
}
// nat test ini sdh terpenuhi
if ($argument_1_list[$rfx_idx]["is_ok"] === true) {
continue;
}
if ($rfx["T_OrderDetailVerification"] != "Y") {
$argument_1_list[$rfx_idx]["note"] =
"Reflex Test [" .
$rfx["Nat_TestName"] .
"] belum di verifikasi";
continue;
}
if (!($rfx["T_OrderDetailNat_NormalValueID"] > 0)) {
$argument_1_list[$rfx_idx]["note"] =
"Reflex Test [" .
$rfx["Nat_TestName"] .
"] belum memiliki nilai normal";
continue;
}
try {
$d_value = floatval($rfx["T_OrderDetailResult"]);
$d_min_value = floatval($rfx["T_OrderDetailMinValue"]);
$d_max_value = floatval($rfx["T_OrderDetailMaxValue"]);
$d_min_inclusive = $rfx["T_OrderDetailMinValueInclusive"];
$d_max_inclusive = $rfx["T_OrderDetailMaxValueInclusive"];
$rfx_condition = "Unknown";
$argument_1_list[$rfx_idx]["result"] = $rfx_condition;
if (
$d_value < $d_min_value ||
($d_value <= $d_min_value && $d_min_inclusive == "N")
) {
$rfx_conditionID = 1;
$rfx_condition = "Low";
} elseif (
$d_value > $d_max_value ||
($d_value >= $d_max_value && $d_max_inclusive == "N")
) {
$rfx_conditionID = 3;
$rfx_condition = "High";
} else {
$rfx_conditionID = 2;
$rfx_condition = "Normal";
}
if ($rfx_conditionID == 0) {
$argument_1_list[$rfx_idx]["note"] =
"Reflex Test [" .
$rfx["Nat_TestName"] .
"] kondisi [ Low/Normal/Hight ] tidak bisa ditentukan ";
continue;
}
$argument_1_list[$rfx_idx]["result"] = $rfx_condition;
if ($rfx_conditionID == $rfx["Nat_ReflexTestNat_ConditionID"]) {
$argument_1_list[$rfx_idx]["note"] =
"Reflex Test [" .
$rfx["Nat_TestName"] .
"] hasil $rfx_condition, Argument 1 " .
$rfx["Nat_ConditionName"];
$argument_1_list[$rfx_idx]["is_ok"] = true;
$argument_1_list[$rfx_idx]["is_show_mandatory"] = true;
$argument_1_list[$rfx_idx]["reflexID"] =
$rfx["Nat_ReflexTestID"];
$argument_1_list[$rfx_idx]["condition"] = $rfx_conditionName;
$argument_1_list[$rfx_idx]["conditionID"] = $rfx_conditionID;
} else {
$argument_1_list[$rfx_idx]["note"] =
"Reflex Test [" .
$rfx["Nat_TestName"] .
"] hasil $rfx_condition, Argument 1 " .
$rfx["Nat_ConditionName"];
$argument_1_list[$rfx_idx]["reflexID"] =
$rfx["Nat_ReflexTestID"];
$argument_1_list[$rfx_idx]["is_ok"] = false;
}
} catch (Exception $e) {
$argument_1_list[$rfx_idx]["note"] =
"Reflex Test [" .
$rfx["Nat_TestName"] .
"] kondisi [ Low/Normal/Hight ] tidak bisa ditentukan ( konversi numerik )";
continue;
}
if ($debug) {
//echo "<pre>";
//print_r($argument_1_list);
//echo "</pre>";
}
}
$flag_error = false;
$s_ids = "0";
//cari reflex yg memenuhi
foreach ($argument_1_list as $list) {
if ($list["is_ok"] == false) {
$flag_error = true;
continue;
}
$s_ids .= "," . $list["reflexID"];
}
// Mandatory
$sql = "select Nat_ReflexTestID,
Nat_TestName, Nat_ConditionName, Nat_TestID,
Nat_ReflexMandatoryNat_ConditionID, Nat_ConditionName,
T_OrderDetailNat_NormalValueID, T_OrderDetailResult, T_OrderDetailNat_MethodeName,
T_OrderDetailMaxValueInclusive, T_OrderDetailMinValueInclusive,
T_OrderDetailMinValue, T_OrderDetailMaxValue, T_OrderDetailVerification
from
nat_reflextest
join nat_reflexmandatory on Nat_ReflexMandatoryNat_ReflexTestID in ( $s_ids )
and Nat_ReflexTestID = Nat_ReflexMandatoryNat_ReflexTestID and Nat_ReflexTestIsActive = 'Y'
join nat_condition on Nat_ReflexMandatoryNat_ConditionID = Nat_ConditionID and Nat_ReflexMandatoryIsActive = 'Y'
join nat_test on Nat_ReflexMandatoryNat_TestID = Nat_TestID and Nat_TestIsActive = 'Y'
and Nat_ReflexMandatoryIsActive = 'Y'
join t_test on Nat_TestID = T_TestNat_TestID and T_TestIsActive = 'Y'
join t_orderdetail on T_TestID = T_OrderDetailT_TestID
and T_OrderDetailIsActive = 'Y' and T_OrderDetailT_OrderHeaderID = ?
order by Nat_ReflexTestID, Nat_ReflexTestNat_RelationID desc";
$qry = $this->db->query($sql, [$headerID]);
$reflex_id_not_ok = [];
if ($qry) {
$mdt_rows = $qry->result_array();
foreach ($mdt_rows as $r_mdt) {
$is_ok = false;
$is_display = true;
$result = "";
if ($r_mdt["T_OrderDetailVerification"] != "Y") {
$result = "Belum di verifikasi";
continue;
}
if (!($r_mdt["T_OrderDetailNat_NormalValueID"] > 0)) {
$result = "Belum memiliki nilai normal";
continue;
}
if ($result == "") {
try {
$d_value = floatval($r_mdt["T_OrderDetailResult"]);
$d_min_value = floatval(
$r_mdt["T_OrderDetailMinValue"]
);
$d_max_value = floatval(
$r_mdt["T_OrderDetailMaxValue"]
);
$d_min_inclusive =
$r_mdt["T_OrderDetailMinValueInclusive"];
$d_max_inclusive =
$r_mdt["T_OrderDetailMaxValueInclusive"];
$mdt_condition = "Unknown";
if (
$d_value < $d_min_value ||
($d_value <= $d_min_value &&
$d_min_inclusive == "N")
) {
$mdt_conditionID = 1;
$mdt_condition = "Low";
} elseif (
$d_value > $d_max_value ||
($d_value >= $d_max_value &&
$d_max_inclusive == "N")
) {
$mdt_conditionID = 3;
$mdt_condition = "High";
} else {
$mdt_conditionID = 2;
$mdt_condition = "Normal";
}
if ($mdt_conditionID == 0) {
$result =
"Mandatory Reflex Test [" .
$rfx["Nat_TestName"] .
"] kondisi [ Low/Normal/Hight ] tidak bisa ditentukan ";
}
} catch (Exception $e) {
$result =
"Mandatory Reflex Test [" .
$rfx["Nat_TestName"] .
"] kondisi [ Low/Normal/Hight ] tidak bisa ditentukan ( konversi numerik )";
}
if (
$mdt_conditionID ==
$r_mdt["Nat_ReflexMandatoryNat_ConditionID"]
) {
$result =
$r_mdt["Nat_TestName"] . " kondisi : " . $condition;
$is_ok = true;
} else {
$result =
$r_mdt["Nat_TestName"] . " kondisi : " . $condition;
$is_ok = false;
}
}
$mandatory[] = [
"reflexID" => $r_mdt["Nat_ReflexTestID"],
"px" => $r_mdt["Nat_TestName"],
"condition" => $r_mdt["Nat_ConditionName"],
"is_display" => $is_display,
"result" => $result,
"is_ok" => $is_ok,
];
if (!$is_ok) {
$reflex_id_not_ok[] = $r_mdt["Nat_ReflexTestID"];
}
}
}
$arr_notes = [];
if ($debug) {
//echo "<pre> Reflex ID Not OK :\n";
//print_r($reflex_id_not_ok);
//echo "</pre>";
}
foreach ($argument_1_list as $x_idx => $l) {
if (in_array($l["reflexID"], $reflex_id_not_ok)) {
$argument_1_list[$x_idx]["is_ok"] = false;
$x_mandatory = array_filter($mandatory, function ($m) use (
$reflex_id_not_ok
) {
if (in_array($m["reflexID"], $reflex_id_not_ok)) {
return true;
}
});
foreach ($x_mandatory as $m) {
$argument_1_list[$x_idx]["note"] = $m["result"];
}
continue;
}
$argument_1_list[$x_idx]["mandatory"] = [];
foreach ($mandatory as $m) {
if ($m["reflexID"] == $l["reflexID"]) {
$argument_1_list[$x_idx]["mandatory"][] = $m;
}
}
}
$flag_error = false;
$notes = "";
//print_r($argument_1_list);
foreach ($argument_1_list as $list) {
if ($list["is_ok"] == false) {
$flag_error = true;
}
if ($notes != "") {
$notes .= "\n";
}
$notes .= $list["note"];
}
if ($flag_error) {
return [
"AutoValidT_OrderDetailID" => $r["T_OrderDetailID"],
"AutoValidHaveReq" => $haveReq,
"AutoValidIsReviewRange" => $r["ReviewRangeIsOK"],
"AutoValidNote" => $notes,
"AutoValidIsOk" => "N",
"AutoValidVerifDate" => $r["T_OrderDetailVerDate"],
"AutoValidVerifUserID" => $r["T_OrderDetailVerUserID"],
"AutoValidVerifUserName" => $r["M_UserUserName"],
"AutoValidNat_ConditionID" => $conditionID,
"AutoValidNat_NormalValueID" =>
$r["T_OrderDetailNat_NormalValueID"],
"AutoValidNat_MethodeID" => $r["T_OrderDetailNat_MethodeID"],
"AutoValidT_OrderDetailResult" => $r["T_OrderDetailResult"],
"image" => $r["AutoVerifTrendAnalysisImage"],
];
}
return [
"AutoValidT_OrderDetailID" => $r["T_OrderDetailID"],
"AutoValidHaveReq" => $haveReq,
"AutoValidIsReviewRange" => $r["ReviewRangeIsOK"],
"AutoValidNote" => $notes,
"AutoValidIsOk" => "Y",
"AutoValidVerifDate" => $r["T_OrderDetailVerDate"],
"AutoValidVerifUserID" => $r["T_OrderDetailVerUserID"],
"AutoValidVerifUserName" => $r["M_UserUserName"],
"AutoValidNat_ConditionID" => $conditionID,
"AutoValidNat_NormalValueID" =>
$r["T_OrderDetailNat_NormalValueID"],
"AutoValidNat_MethodeID" => $r["T_OrderDetailNat_MethodeID"],
"AutoValidT_OrderDetailResult" => $r["T_OrderDetailResult"],
"image" => $r["AutoVerifTrendAnalysisImage"],
];
}
function debug($id)
{
$result = $this->calculate($id, true);
if ($result === false) {
echo "No Auto Valid Response";
exit();
}
$this->print_table_style();
$img =
"<iframe style='width:800px; height:400px; border: 0;' src='" .
$result["image"] .
"' />";
unset($result["image"]);
$this->print_table([$result], array_keys($result));
echo "<br/>$img";
}
function order_debug($date = "")
{
if ($date == "") {
$date = date("Y-m-d");
}
$start_date = $date . " 00:00:01";
$end_date = $date . " 23:59:59";
$sql = "select
T_OrderHeaderDate, T_OrderHeaderLabNumber,
T_OrderHeaderAddonVerificationDone,
group_concat(T_OrderDetailT_TestName) TestName
from t_orderheader
join t_orderheaderaddon on T_OrderHeaderDate >= ? and T_OrderHeaderDate < ?
and T_OrderHeaderID = T_OrderHeaderAddonT_OrderHeaderID
and T_OrderHeaderIsActive = 'Y'
join t_orderdetail on T_OrderHeaderID = T_OrderDetailT_OrderHeaderID
and T_OrderDetailIsActive = 'Y'
and T_OrderDetailVerification = 'Y'
join t_test on T_OrderDetailT_TestID = T_TestID and T_TestIsResult = 'Y'
group by T_OrderHeaderID
limit 0,100
";
$qry = $this->db->query($sql, [$start_date, $end_date]);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] . "\n|" . $this->db->last_query()
);
exit();
}
$rows = $qry->result_array();
foreach ($rows as $idx => $r) {
$rows[$idx]["Check"] =
"<a href='/one-api/process/auto_valid/check/" .
$r["T_OrderHeaderLabNumber"] .
"' > Check </a>";
}
$this->print_table_style();
$this->print_table($rows, array_keys($rows[0]));
}
function get_verif_status($detailID)
{
$sql =
"select * from auto_verif_v2 where AutoVerifT_OrderDetailID = ? and autoVerifIsActive = 'Y'";
$qry = $this->db->query($sql, [$detailID]);
if (!$qry) {
return ["N", "Error : " . $this->db->error()["message"]];
}
$rows = $qry->result_array();
if (count($rows) == 0) {
return ["X", "Auto Verif N/A"];
}
$r = $rows[0];
if (
$r["autoVerifIsPanicValue"] == "N" &&
$r["autoVerifIsPreAnalityc"] == "N" &&
$r["autoVerifIsInAmr"] == "Y" &&
$r["autoVerifIsUjiTrendOK"] == "Y"
) {
return ["Y", "Auto Verif terpenuhi."];
} else {
return ["N", "Auto Verif tidak terpenuhi."];
}
}
function get_is_quantitative($detailID)
{
$sql = "select T_TestIsQuantitative,T_OrderDetailResult
from t_orderdetail
join t_test on T_OrderDetailID = ? and T_OrderDetailT_TestID = T_TestID";
$qry = $this->db->query($sql, [$detailID]);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] . "\n|" . $this->db->last_query()
);
exit();
}
$rows = $qry->result_array();
if (count($rows) == 0) {
$this->reply_error("Record not found");
exit();
}
if ($rows[0]["T_TestIsQuantitative"] == "N") {
return "N";
}
if (
!is_numeric($rows[0]["T_OrderDetailResult"]) &&
$rows[0]["T_OrderDetailResult"] != ""
) {
return "N";
}
return "Y";
}
function get_condition_sedimen($result)
{
$negatif = ["Negatif", "Non Reaktif"];
$borderline = ["BorderLine", "GrayZone"];
$positif = ["Positif", "Reaktif"];
foreach ($negatif as $x) {
if (strpos($result, $x) !== false) {
return -1;
}
}
foreach ($positif as $x) {
if (strpos($result, $x) !== false) {
return 1;
}
}
foreach ($borderline as $x) {
if (strpos($result, $x) !== false) {
return 0;
}
}
return 99;
}
// -1 : negatif , 0: borderline , 1 : positive , 99 : n/a
function get_condition_qualitative($result, $debug = "")
{
$negatif = [
"Negatif",
"Normal",
"Non Reaktif",
"Kuning",
"Kuning Muda",
"Jernih",
];
$borderline = ["BorderLine", "GrayZone"];
$positif = ["Positif", "Reaktif"];
foreach ($negatif as $x) {
if (strpos($result, $x) !== false) {
return -1;
}
}
foreach ($positif as $x) {
if (strpos($result, $x) !== false) {
return 1;
}
}
foreach ($borderline as $x) {
if (strpos($result, $x) !== false) {
return 0;
}
}
return 99;
}
function get_condition($detailID)
{
$sql = "select T_OrderDetailT_OrderHeaderID,
T_TestName, T_OrderDetailID, date(T_OrderHeaderDate) orderDate,
T_OrderHeaderDate,
T_OrderDetailResult, T_OrderDetailNat_NormalValueID,
T_OrderDetailNat_MethodeID,
T_OrderDetailMinValue, T_OrderDetailMinValueInclusive,
T_OrderDetailMaxValue, T_OrderDetailMaxValueInclusive, T_OrderdetailNat_MethodeName,
T_TestNat_TestID, T_OrderHeaderM_PatientID, T_TestIsDeltaCheck
from t_orderdetail
join t_orderheader on T_OrderDetailID = ?
and T_OrderDetailT_OrderHeaderID = T_OrderHeaderID
join t_test on T_OrderDetailIsActive = 'Y'
and T_OrderDetailT_TestID = T_TestID
and T_OrderDetailVerification = 'Y'
and T_TestIsResult = 'Y' ";
$qry = $this->db->query($sql, [$detailID]);
if (!$qry) {
$this->reply_error(
"Get Condition" .
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
exit();
}
$rows = $qry->result_array();
if (count($rows) == 0) {
$this->reply_error("Record not found");
exit();
}
$r = $rows[0];
$minValue = $r["T_OrderDetailMinValue"];
$maxValue = $r["T_OrderDetailMaxValue"];
$minInclusive = $r["T_OrderDetailMinValueInclusive"];
$maxInclusive = $r["T_OrderDetailMaxValueInclusive"];
$methodeID = $r["T_OrderDetailNat_MethodeID"];
$methode = $r["T_OrderdetailNat_MethodeName"];
$value = $r["T_OrderDetailResult"];
$theTestName = $r["T_TestName"];
$nat_test_id = $r["T_TestNat_TestID"];
$headerID = $r["T_OrderDetailT_OrderHeaderID"];
$conditionID = 0;
$condition = "Unknown";
try {
$n_value = floatval($value);
$n_minValue = floatval($minValue);
$n_maxValue = floatval($maxValue);
} catch (Exception $e) {
return [
"conditionID" => 0,
"conditionName" => "N/A Hasil: $value, Min: $minValue, Max: $maxValue",
];
}
if (
$n_value < $n_minValue ||
($n_value <= $n_minValue && $minInclusive == "N")
) {
$conditionID = 1;
$condition = "Low";
} elseif (
$n_value > $n_maxValue ||
($n_value >= $n_maxValue && $maxInclusive == "N")
) {
$conditionID = 3;
$condition = "High";
} else {
$conditionID = 2;
$condition = "Normal";
}
return [
"conditionID" => $conditionID,
"conditionName" => $condition,
"headerID" => $headerID,
"testName" => $theTestName,
"methodeID" => $methodeID,
"methode" => $methode,
"natTestID" => $nat_test_id,
"patientID" => $r["T_OrderHeaderM_PatientID"],
"orderDate" => $r["orderDate"],
"T_OrderHeaderDate" => $r["T_OrderHeaderDate"],
"orderResult" => $r["T_OrderDetailResult"],
];
}
function inReviewRange($natTestID, $result, $decimalDigit)
{
//rata-rata harian
$sql = "select Nat_TrendAnalysisLow, Nat_TrendAnalysisHigh, Nat_TrendAnalysisMinCount
from nat_trend_analysis
where Nat_TrendAnalysisNat_TestID = ?";
$qry = $this->db->query($sql, [$natTestID]);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] . "\n|" . $this->db->last_query()
);
exit();
}
$rows = $qry->result_array();
if (count($rows) == 0) {
return ["N", "Review Range belum di setting."];
}
$low = round($rows[0]["Nat_TrendAnalysisLow"], $decimalDigit);
$high = round($rows[0]["Nat_TrendAnalysisHigh"], $decimalDigit);
//yyyy-hh-mm
if ($result >= $low && $result <= $high) {
return [
"N",
"Tidak masuk Review Range. Hasil:$result, -2SD: $low, +2SD:$high",
];
}
return ["Y", "Masuk Review Range"];
}
function getInconsistensi($natTestID, $orderHeaderID, $debug = "")
{
//Glukosa Darah Puasa diabaikan
if ($natTestID == 4716) {
return ["Y", "N/A"];
}
$sql = "select distinct Nat_InkonsistensiID, Nat_InkonsistensiName
from nat_inkonsistensi_detail
join nat_inkonsistensi on Nat_InkonsistensiDetailNat_TestID = ?
and Nat_InkonsistensiDetailIsActive = 'Y'
and Nat_InkonsistensiDetailNat_InkonsistensiID = Nat_InkonsistensiID ";
$qry = $this->db->query($sql, [$natTestID]);
if (!$qry) {
$this->reply_error(
"Get Inkonsistensi Detail" .
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
exit();
}
$rows = $qry->result_array();
if (count($rows) == 0) {
return ["Y", "N/A"];
}
$aInkonsistency = [];
foreach ($rows as $r) {
$aInkonsistency[$r["Nat_InkonsistensiID"]] =
$r["Nat_InkonsistensiName"];
}
$s_inkonsistensiID = implode(",", array_keys($aInkonsistency));
if ($s_inkonsistensiID == "") {
$s_inkonsistensiID = "0";
}
$sql = "select T_TestNat_TestID,T_OrderDetailResult, T_OrderDetailT_TestName
from t_orderdetail
join t_test on T_OrderDetailT_OrderHeaderID = ?
and T_OrderDetailIsActive = 'Y'
and T_OrderDetailT_TestID = T_TestID";
$qry = $this->db->query($sql, [$orderHeaderID]);
if (!$qry) {
$this->reply_error(
"Get T_OrderDetail Nat_Test" .
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
exit();
}
$rows_nat = $qry->result_array();
$orderNatTestID = [];
foreach ($rows_nat as $r) {
$orderNatTestID[$r["T_TestNat_TestID"]] = $r["T_OrderDetailResult"];
}
$sql = "select Nat_InkonsistensiID,
group_concat(Nat_InkonsistensiDetailNat_TestID) aNatTest,
group_concat(Nat_InkonsistensiDetailCode) aNatCode,
group_concat(Nat_TestName) aNatTestName,
Nat_InkonsistensiFormula
from nat_inkonsistensi_detail
join nat_inkonsistensi on Nat_InkonsistensiID = Nat_InkonsistensiDetailNat_InkonsistensiID
and Nat_InkonsistensiID in($s_inkonsistensiID)
join nat_test on Nat_InkonsistensiDetailNat_TestID = Nat_TestID
group by Nat_InkonsistensiID";
$qry = $this->db->query($sql);
if (!$qry) {
$this->reply_error(
"Get Inkonsistensi Detail" .
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
exit();
}
$rows_d = $qry->result_array();
$havingInconsistency = false;
$inconsistencyNote = "N/A";
$haveError = false;
foreach ($rows_d as $r) {
$natInkonsistensiID = $r["Nat_InkonsistensiID"];
$formula = $r["Nat_InkonsistensiFormula"];
$aNatTest = explode(",", $r["aNatTest"]);
$aNatCode = explode(",", $r["aNatCode"]);
$aNatTestName = explode(",", $r["aNatTestName"]);
$isAllPxExists = true;
$aResult = [];
$note = "";
foreach ($aNatTest as $idx => $nt) {
if (!in_array($nt, array_keys($orderNatTestID))) {
$isAllPxExists = false;
$note .=
$aInkonsistency[$nt] .
" N/A (" .
$aNatTestName[$idx] .
")";
break;
}
$aResult[$aNatCode[$idx]] = $orderNatTestID[$nt];
}
if ($isAllPxExists) {
foreach ($aResult as $ks => $rs) {
$formula = str_ireplace("{" . $ks . "}", $rs, $formula);
}
try {
$rstFormula = false;
try {
eval("\$rstFormula = {$formula} ;");
} catch (ParseError $e) {
return [
"N",
$aInkonsistency[$natInkonsistensiID] .
" | $formula Error ",
];
}
if ($debug != "") {
echo "Formula : $formula | " .
($rstFormula ? "Y" : "N");
}
if ($rstFormula == true) {
return [
"Y",
$aInkonsistency[$natInkonsistensiID] .
" | $formula | $rstFormula",
];
} else {
$haveError = true;
if ($inconsistencyNote == "N/A") {
$inconsistencyNote = "";
}
if ($inconsistencyNote != "") {
$inconsistencyNote .= ",";
}
$inconsistencyNote .=
$aInkonsistency[$natInkonsistensiID] .
"| $formula |" .
($rstFormula ? "TRUE" : "FALSE");
}
} catch (Exception $e) {
return [
"N",
$aInkonsistency[$natInkonsistensiID] .
" | $formula Error ",
];
}
}
}
if ($debug != "") {
echo "E : $inconsistencyNote \n";
}
return [$haveError ? "N" : "Y", $inconsistencyNote];
}
function get_delta_check_is_ok_sedimen(
$orderResult,
$orderHeaderID,
$patientID,
$natTestID,
$conditionID,
$low,
$high
) {
//last Result
if ($conditionID == 1) {
$conditionName = "Low";
}
if ($conditionID == 2) {
$conditionName = "Normal";
}
if ($conditionID == 3) {
$conditionName = "High";
}
$sql = "select T_OrderDetailResult
from t_orderheader
join t_orderdetail on T_OrderHeaderM_PatientID = ?
and T_OrderHeaderID < ? and T_OrderHeaderIsActive = 'Y'
and T_OrderDetailVerification = 'Y'
and T_OrderDetailIsActive = 'Y'
and T_OrderDetailT_OrderHeaderID = T_OrderHeaderID
join t_test on T_OrderDetailT_TestID = T_TestID
and T_TestNat_TestID = ?
order by T_OrderHeaderDate desc
limit 0,1";
$qry = $this->db->query($sql, [$patientID, $orderHeaderID, $natTestID]);
if (!$qry) {
return [
"N",
"Error Get Last Result | " . $this->db->error()["message"],
];
}
$rows = $qry->result_array();
if (count($rows) == 0) {
return [
"N/A",
"N/A No Historical Result. Hasil : {$orderResult}, Low: {$low}, High : {$high}",
];
}
$lastResult = $rows[0]["T_OrderDetailResult"];
$tmpResult = explode("-", $lastResult);
$orgResult = $lastResult;
if (count($tmpResult) > 1) {
$lastResult = trim($tmpResult[1]);
}
if ($lastResult < $low) {
$x_conditionID = 1;
} elseif ($lastResult <= $high) {
$x_conditionID = 2;
} elseif ($lastResult > $high) {
$x_conditionID = 3;
} else {
$x_conditionID = 99;
}
if ($conditionID != $x_conditionID) {
return [
"N",
"Sedimen Delta Check tidak terpenuhi. Hasil {$orderResult}, hasil sebelumnya: {$lastResult} | {$orgResult}",
];
} else {
return [
"Y",
"Sedimen Delta Check terpenuhi. Hasil {$orderResult}, hasil sebelumnya: {$lastResult} | {$lastResult}",
];
}
}
function get_delta_check_is_ok_qualitative(
$orderResult,
$orderHeaderID,
$patientID,
$natTestID,
$debug = ""
) {
//last Result
$sql = "select T_OrderHeaderID,T_OrderDetailID,T_OrderHeaderM_PatientID,
T_OrderHeaderDate,T_OrderHeaderLabNumber, T_OrderDetailResult, T_OrderDetailT_TestName
from t_orderheader
join t_orderdetail on T_OrderHeaderM_PatientID = ?
and T_OrderHeaderID < ? and T_OrderHeaderIsActive = 'Y'
and T_OrderDetailVerification = 'Y'
and T_OrderDetailIsActive = 'Y'
and T_OrderDetailT_OrderHeaderID = T_OrderHeaderID
join t_test on T_OrderDetailT_TestID = T_TestID
and T_TestNat_TestID = ?
order by T_OrderHeaderDate desc
limit 0,3";
$qry = $this->db->query($sql, [$patientID, $orderHeaderID, $natTestID]);
if (!$qry) {
return [
"N",
"Error Get Last Result | " . $this->db->error()["message"],
];
}
$rows = $qry->result_array();
if ($debug != "") {
print_r($rows);
}
if (count($rows) == 0) {
return ["Y", "N/A No Historical Result."];
}
$lastResult = $rows[0]["T_OrderDetailResult"];
if (
$this->get_condition_qualitative($lastResult) !=
$this->get_condition_qualitative($orderResult)
) {
return [
"N",
"Qualitative Delta Check tidak terpenuhi. Hasil {$orderResult}, hasil sebelumnya: {$lastResult}",
];
} else {
return [
"Y",
"Qualitative Delta Check terpenuhi. Hasil {$orderResult}, hasil sebelumnya: {$lastResult}",
];
}
}
function get_delta_check_is_ok(
$orderHeaderDate,
$orderResult,
$orderHeaderID,
$patientID,
$natTestID,
$testIsDeltaCheck,
$decimalDigit,
$conditionID = 0,
$debug = ""
) {
$sql = " select
Nat_DeltaCheckNat_DeltaTypeID, Nat_DeltaCheckInterval,
Nat_DeltaCheckM_TimeID,
Nat_DeltaCheckMinValue, Nat_DeltaCheckMaxValue
from nat_delta_check
where Nat_DeltaCheckNat_TestID = ?
and Nat_DeltaCheckIsActive = 'Y'";
$qry = $this->db->query($sql, [$natTestID]);
if (!$qry) {
return [
"N",
"Error Get Nat Delta Check | " . $this->db->error()["message"],
];
}
$rows = $qry->result_array();
if (count($rows) == 0) {
if ($testIsDeltaCheck == "Y") {
return ["N", "Delta Check belum di setting."];
}
return ["Y", "Delta Check N/A"];
}
$r = $rows[0];
$minValue = $r["Nat_DeltaCheckMinValue"];
$maxValue = $r["Nat_DeltaCheckMaxValue"];
$type = $r["Nat_DeltaCheckNat_DeltaTypeID"];
$unitTime = $r["Nat_DeltaCheckM_TimeID"];
//last Result
$sql = "select T_OrderDetailResult, T_OrderHeaderDate,
T_OrderDetailMinValue, T_OrderDetailMinValueInclusive, T_OrderDetailMaxValue, T_OrderDetailMaxValueInclusive,
case
when '$unitTime' = '1' then
TIMESTAMPDIFF(MINUTE,T_OrderHeaderDate,'$orderHeaderDate')
when '$unitTime' = '2' then
TIMESTAMPDIFF(HOUR,T_OrderHeaderDate,'$orderHeaderDate')
when '$unitTime' = '3' then
TIMESTAMPDIFF(DAY,T_OrderHeaderDate,'$orderHeaderDate')
else
-1
end itv
from t_orderheader
join t_orderdetail on T_OrderHeaderM_PatientID = ?
and T_OrderHeaderID = T_OrderDetailT_OrderHeaderID
and T_OrderHeaderID < ? and T_OrderHeaderIsActive = 'Y'
and T_OrderDetailVerification = 'Y'
and T_OrderDetailIsActive = 'Y'
join t_test on T_OrderDetailT_TestID = T_TestID
and T_TestNat_TestID = ?
order by T_OrderHeaderDate desc
limit 0,1";
$qry = $this->db->query($sql, [$patientID, $orderHeaderID, $natTestID]);
if (!$qry) {
return [
"N",
"Error Get Last Result | " . $this->db->error()["message"],
];
}
$rows = $qry->result_array();
if (count($rows) == 0) {
return ["Y", "N/A No Historical Result."];
}
$r = $rows[0];
if ($debug != "") {
print_r($r);
}
$last_minValue = $r["T_OrderDetailMinValue"];
$last_maxValue = $r["T_OrderDetailMaxValue"];
$last_minInclusive = $r["T_OrderDetailMinValueInclusive"];
$last_maxInclusive = $r["T_OrderDetailMaxValueInclusive"];
$last_value = $r["T_OrderDetailResult"];
try {
$last_n_value = floatval($last_value);
$last_n_minValue = floatval($last_minValue);
$last_n_maxValue = floatval($last_maxValue);
if (
$last_n_value < $last_n_minValue ||
($last_n_value <= $last_n_minValue && $last_minInclusive == "N")
) {
$last_conditionID = 1;
} elseif (
$last_n_value > $last_n_maxValue ||
($last_n_value >= $last_n_maxValue && $last_maxInclusive == "N")
) {
$last_conditionID = 3;
} else {
$last_conditionID = 2;
}
if ($last_conditionID == 2 && $conditionID == 2) {
return [
"Y",
"Delta Check terpenuhi. Hasil Sebelumnya $last_value [Normal], hasil saat ini $orderResult [ Normal ].",
];
}
} catch (Exception $e) {
}
$lastResult = $rows[0]["T_OrderDetailResult"];
$interval = $rows[0]["itv"];
if ($type == 1) {
$delta = round($orderResult - $lastResult, $decimalDigit);
if ($delta < $minValue || $delta > $maxValue) {
return [
"N",
"Delta Difference tidak terpenuhi. Delta {$delta}, min : {$minValue} , max : ${maxValue}, hasil: ${orderResult}, hasil sebelumnya : ${lastResult} ",
];
} else {
return [
"Y",
"Delta Difference terpenuhi. Delta {$delta}, min : {$minValue} , max : ${maxValue}, hasil: ${orderResult}, hasil sebelumnya : ${lastResult} ",
];
}
} elseif ($type == 2) {
$deltaPctChange = round(
(($orderResult - $lastResult) / $lastResult) * 100,
0
);
if ($deltaPctChange < $minValue || $deltaPctChange > $maxValue) {
return [
"N",
"Delta Percent Change tidak terpenuhi. Delta Percent Change {$deltaPctChange} , min : {$minValue} , max : ${maxValue}, hasil: ${orderResult}, hasil sebelumnya : ${lastResult} ",
];
} else {
return [
"Y",
"Delta Percent Change terpenuhi. Delta Percent Change {$deltaPctChange} , min : {$minValue} , max : ${maxValue}, hasil: ${orderResult}, hasil sebelumnya : ${lastResult} ",
];
}
} elseif ($type == 3) {
if ($interval == -1) {
return ["N", "Rate Difference, Interval Setting Error."];
}
$rateDiff = round(
($orderResult - $lastResult) / $interval,
$decimalDigit
);
if ($rateDiff < $minValue || $rateDiff > $maxValue) {
return [
"N",
"Rate difference tidak terpenuhi. Rate difference {$rateDiff} , min : {$minValue} , max : ${maxValue}, hasil: ${orderResult}, hasil sebelumnya : ${lastResult} ",
];
} else {
return [
"Y",
"Rate difference terpenuhi. Rate difference {$rateDiff} , min : {$minValue} , max : ${maxValue}, hasil: ${orderResult}, hasil sebelumnya : ${lastResult} ",
];
}
} elseif ($type == 4) {
if ($interval == -1) {
return ["N", "Rate Percent Change, Interval Setting Error."];
}
$ratePctChange = round(
(($orderResult - $lastResult) / $interval) * $lastResult,
$decimalDigit
);
if ($ratePctChange < $minValue || $ratePctChange > $maxValue) {
return [
"N",
"Rate Percent Change tidak terpenuhi. Rate Percent Change {$ratePctChange} , min : {$minValue} , max : ${maxValue}, hasil: ${orderResult}, hasil sebelumnya : ${lastResult} ",
];
} else {
return [
"Y",
"Rate Percent Change terpenuhi. Rate Percent Change {$ratePctChange} , min : {$minValue} , max : ${maxValue}, hasil: ${orderResult}, hasil sebelumnya : ${lastResult}",
];
}
} else {
return ["N", "Delta Check Interval Unit not Checked."];
}
}
function process_v2()
{
$param = $this->get_param();
$ids = $param["ids"];
$is_update = "N";
if (isset($param["is_update"])) {
$is_update = $param["is_update"];
}
if ($ids == "") {
$this->reply([
"total" => 0,
"total_ok" => 0,
"total_not_ok" => 0,
]);
exit();
}
$total = 0;
$total_ok = 0;
$total_not_ok = 0;
$a_ids = explode(",", $ids);
foreach ($a_ids as $id) {
$isOk = $this->re_process($id);
$total++;
if ($isOk) {
$total_ok++;
} else {
$total_not_ok++;
}
}
$this->reply([
"total" => $total,
"total_ok" => $total_ok,
"total_not_ok" => $total_not_ok,
]);
}
function get_status_sedimen()
{
}
function re_process($detailID, $debug = "")
{
$this->db->trans_begin();
$sql = "delete from auto_valid_v2 where AutoValidT_OrderDetailID=?";
$qry = $this->db->query($sql, [$detailID]);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] . "\n|" . $this->db->last_query()
);
$this->db->trans_rollback();
exit();
}
$isQuantitative = $this->get_is_quantitative($detailID);
//A. check AutoVerif Is OK
//B. get condition L |N | H
// LOW
//1. IsReviewRange -> Y -> Not OK
//2. InkonsistensiRule -> tidak terpenuhi -> NOK OK
// ( N/A or OK lanjut 3)
//3. Delta Check -> tidak terpenuhi -> NOT OK
// ( N/A or OK lanjut 4 )
//4. Reflex Test -> N/A atau tidak terpenuhi -> NOT OK
// NORMAL
// (* Review Range pasti OK )
//1. InkonsistensiRule -> tidak terpenuhi -> NOK OK
// ( N/A or OK lanjut 2)
//2. Delta Check -> tidak terpenuhi -> NOT OK
// ( N/A or OK lanjut 3 )
//3. Reflex Test -> N/A atau tidak terpenuhi -> NOT OK
//
//1. IsReviewRange -> Y -> Not OK
//2. InkonsistensiRule -> tidak terpenuhi -> NOK OK
// ( N/A or OK lanjut 3)
//3. Delta Check -> tidak terpenuhi -> NOT OK
// ( N/A or OK lanjut 4 )
//4. Reflex Test -> N/A atau tidak terpenuhi -> NOT OK
list($autoVerifIsOK, $autoVerifNote) = $this->get_verif_status(
$detailID
);
$arr = [];
$arr["AutoValidT_OrderDetailID"] = $detailID;
$arr["AutoValidAutoVerifIsOK"] = $autoVerifIsOK;
$arr["AutoValidAutoVerifNote"] = $autoVerifNote;
if ($autoVerifIsOK == "X") {
//belum di kerjakan
$this->db->trans_commit();
return false;
}
if ($autoVerifIsOK != "Y" && $debug == "") {
$qry = $this->db->insert("auto_valid_v2", $arr);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
$this->db->trans_rollback();
exit();
}
$this->db->trans_commit();
return false;
}
$conditions = $this->get_condition($detailID);
// if ($debug != "") {
// echo $this->db->last_query();
// print_r($conditions);
// }
$orderResult = $conditions["orderResult"];
$orderHeaderDate = $conditions["T_OrderHeaderDate"];
$patientID = $conditions["patientID"];
$orderHeaderID = $conditions["headerID"];
$natTestID = $conditions["natTestID"];
$testIsDeltaCheck = $conditions["T_TestIsDeltaCheck"];
$isSedimen = false;
$arrNatTestSedimen = [4348, 4349, 4350, 4351, 4352, 4353];
if (in_array($natTestID, $arrNatTestSedimen)) {
$isSedimen = true;
$tmpResult = explode("-", $orderResult);
$orgResult = $orderResult;
if (count($tmpResult) > 1) {
$orderResult = trim($tmpResult[1]);
}
$isQuantitative = "N";
if (strpos("Negatif", $orderResult) !== false) {
$isSedimen = false;
$isQuantitative = "N";
}
if (strpos("Positif", $orderResult) !== false) {
$isSedimen = false;
$isQuantitative = "N";
}
}
if ($debug != "") {
echo "IsQuantitative : $isQuantitative \n";
print_r($conditions);
}
if ($isQuantitative == "Y") {
$decimalDigit = 0;
if (strpos($orderResult, ".") !== false) {
$decimalDigit =
strlen($orderResult) - strpos($orderResult, ".") - 1;
if ($decimalDigit < 0) {
$decimalDigit = 0;
}
}
//history interval di abaikan
$arr["AutoValidIsQuantitative"] = "Y";
if ($conditions["conditionID"] == 1) {
//LOW
list($isInReviewRange, $reviewRangeNote) = $this->inReviewRange(
$conditions["natTestID"],
$orderResult,
$decimalDigit
);
$arr["AutoValidIsInReviewRange"] = $isInReviewRange;
$arr["AutoValidInReviewRangeNote"] = $reviewRangeNote;
if ($isInReviewRange == "Y") {
$qry = $this->db->insert("auto_valid_v2", $arr);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
$this->db->trans_rollback();
exit();
}
$this->db->trans_commit();
return false;
}
list(
$isInconsistencyOK,
$inconsistencyNote,
) = $this->getInconsistensi(
$conditions["natTestID"],
$conditions["headerID"],
$debug
);
$arr["AutoValidIsInconsistencyOK"] = $isInconsistencyOK;
$arr["AutoValidInconsistencyNote"] = $inconsistencyNote;
if ($isInconsistencyOK == "N") {
$qry = $this->db->insert("auto_valid_v2", $arr);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
$this->db->trans_rollback();
exit();
}
$this->db->trans_commit();
return false;
}
list(
$isDeltaCheckOk,
$deltaCheckNote,
) = $this->get_delta_check_is_ok(
$orderHeaderDate,
$orderResult,
$orderHeaderID,
$patientID,
$natTestID,
$testIsDeltaCheck,
$decimalDigit,
$conditions["conditionID"],
$debug
);
$arr["AutoValidIsDeltacheckOK"] = $isDeltaCheckOk;
$arr["AutoValidDeltaCheckNote"] = $deltaCheckNote;
if ($isDeltaCheckOk != "Y") {
$qry = $this->db->insert("auto_valid_v2", $arr);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
$this->db->trans_rollback();
exit();
}
$this->db->trans_commit();
return false;
}
list(
$reflexTestIsOK,
$reflexTextNote,
) = $this->get_reflex_test_is_ok(
$natTestID,
$conditions["conditionID"],
$detailID,
$orderHeaderID
);
//Low N/A => manual validasi
if ($reflexTestIsOK == "X") {
$reflexTestIsOK = "N";
}
$arr["AutoValidReflexTestIsOK"] = $reflexTestIsOK;
$arr["AutoValidReflexTestNote"] =
$conditions["testName"] .
" " .
$conditions["conditionName"] .
"|" .
$reflexTextNote;
$qry = $this->db->insert("auto_valid_v2", $arr);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
$this->db->trans_rollback();
exit();
}
$this->db->trans_commit();
} elseif ($conditions["conditionID"] == 2) {
$arr["AutoValidIsInReviewRange"] = "N";
$arr["AutoValidInReviewRangeNote"] = "Normal Condition";
//NORMAL
list(
$isInconsistencyOK,
$inconsistencyNote,
) = $this->getInconsistensi(
$conditions["natTestID"],
$conditions["headerID"],
$debug
);
$arr["AutoValidIsInconsistencyOK"] = $isInconsistencyOK;
$arr["AutoValidInconsistencyNote"] = $inconsistencyNote;
if ($isInconsistencyOK == "N") {
$qry = $this->db->insert("auto_valid_v2", $arr);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
$this->db->trans_rollback();
exit();
}
$this->db->trans_commit();
return false;
}
list(
$isDeltaCheckOk,
$deltaCheckNote,
) = $this->get_delta_check_is_ok(
$orderHeaderDate,
$orderResult,
$orderHeaderID,
$patientID,
$natTestID,
$testIsDeltaCheck,
$decimalDigit,
$conditions["conditionID"],
$debug
);
$arr["AutoValidIsDeltacheckOK"] = $isDeltaCheckOk;
$arr["AutoValidDeltaCheckNote"] = $deltaCheckNote;
if ($isDeltaCheckOk != "Y") {
$qry = $this->db->insert("auto_valid_v2", $arr);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
$this->db->trans_rollback();
exit();
}
$this->db->trans_commit();
return false;
}
list(
$reflexTestIsOK,
$reflexTextNote,
) = $this->get_reflex_test_is_ok(
$natTestID,
$conditions["conditionID"],
$detailID,
$orderHeaderID
);
//Normal N/A => auto validasi
if ($reflexTestIsOK == "X") {
$reflexTestIsOK = "Y";
}
$arr["AutoValidReflexTestIsOK"] = $reflexTestIsOK;
$arr["AutoValidReflexTestNote"] =
$conditions["testName"] .
" " .
$conditions["conditionName"] .
"|" .
$reflexTextNote;
$qry = $this->db->insert("auto_valid_v2", $arr);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
$this->db->trans_rollback();
exit();
}
$this->db->trans_commit();
} elseif ($conditions["conditionID"] == 3) {
//HIGH
list($isInReviewRange, $reviewRangeNote) = $this->inReviewRange(
$conditions["natTestID"],
$orderResult,
$decimalDigit
);
$arr["AutoValidIsInReviewRange"] = $isInReviewRange;
$arr["AutoValidInReviewRangeNote"] = $reviewRangeNote;
if ($isInReviewRange == "Y") {
$qry = $this->db->insert("auto_valid_v2", $arr);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
$this->db->trans_rollback();
exit();
}
$this->db->trans_commit();
return false;
}
list(
$isInconsistencyOK,
$inconsistencyNote,
) = $this->getInconsistensi(
$conditions["natTestID"],
$conditions["headerID"],
$debug
);
$arr["AutoValidIsInconsistencyOK"] = $isInconsistencyOK;
$arr["AutoValidInconsistencyNote"] = $inconsistencyNote;
if ($isInconsistencyOK == "N") {
$qry = $this->db->insert("auto_valid_v2", $arr);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
$this->db->trans_rollback();
exit();
}
$this->db->trans_commit();
return false;
}
list(
$isDeltaCheckOk,
$deltaCheckNote,
) = $this->get_delta_check_is_ok(
$orderHeaderDate,
$orderResult,
$orderHeaderID,
$patientID,
$natTestID,
$testIsDeltaCheck,
$decimalDigit,
$conditions["conditionID"],
$debug
);
$arr["AutoValidIsDeltacheckOK"] = $isDeltaCheckOk;
$arr["AutoValidDeltaCheckNote"] = $deltaCheckNote;
if ($isDeltaCheckOk != "Y") {
$qry = $this->db->insert("auto_valid_v2", $arr);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
$this->db->trans_rollback();
exit();
}
$this->db->trans_commit();
return false;
}
list(
$reflexTestIsOK,
$reflexTextNote,
) = $this->get_reflex_test_is_ok(
$natTestID,
$conditions["conditionID"],
$detailID,
$orderHeaderID
);
//High N/A => auto validasi
if ($reflexTestIsOK == "X") {
$reflexTestIsOK = "N";
}
$arr["AutoValidReflexTestIsOK"] = $reflexTestIsOK;
$arr["AutoValidReflexTestNote"] =
$conditions["testName"] .
" " .
$conditions["conditionName"] .
"|" .
$reflexTextNote;
$qry = $this->db->insert("auto_valid_v2", $arr);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
$this->db->trans_rollback();
exit();
}
$this->db->trans_commit();
} else {
//Unknown Condition
$arr["AutoValidIsInReviewRange"] = "Y";
$arr["AutoValidInReviewRangeNote"] = "N/A : Invalid Condition : {$conditions["conditionID"]}";
$qry = $this->db->insert("auto_valid_v2", $arr);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
$this->db->trans_rollback();
exit();
}
$this->db->trans_commit();
}
} else {
// Kualitatif
if ($isSedimen) {
$sql = "select
T_OrderDetailID,
max(Nat_TrendAnalysisLow) Low,
min(Nat_TrendAnalysisHigh) High,
T_OrderDetailResult
from t_orderdetail
join t_test on T_OrderDetailID = ?
and T_OrderDetailIsActive = 'Y'
and T_OrderDetailT_TestID = T_TestID
and T_TestIsResult = 'Y'
and T_TestIsQuantitative='Y'
and T_OrderDetailResult <> ''
join nat_trend_analysis on T_TestNat_TestID = Nat_TrendAnalysisNat_TestID
and Nat_TrendAnalysisIsActive = 'Y'
group by T_OrderDetailID";
$qry = $this->db->query($sql, [$detailID]);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
exit();
}
$rows = $qry->result_array();
if (count($rows) == 0) {
$arr["AutoValidIsQuantitative"] = "N";
$arr["AutoValidIsInReviewRange"] = "Y";
$arr["AutoValidInReviewRangeNote"] = "N/A : Sedimen";
$qry = $this->db->insert("auto_valid_v2", $arr);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
$this->db->trans_rollback();
exit();
}
$this->db->trans_commit();
return false;
}
$arr["AutoValidIsInconsistencyOK"] = "Y";
$arr["AutoValidInconsistencyNote"] = "N/A: Sedimen";
$r = $rows[0];
if ($r["Low"] <= $orderResult && $orderResult <= $r["High"]) {
$conditionID = 2;
} elseif ($orderResult > $r["High"]) {
$conditionID = 3;
} else {
$arr["AutoValidIsInReviewRange"] = "Y";
$arr["AutoValidInReviewRangeNote"] = "N/A : Sedimen Invalid Condition : {$orgResult} => {$orderResult}";
$qry = $this->db->insert("auto_valid_v2", $arr);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
$this->db->trans_rollback();
exit();
}
$this->db->trans_commit();
return false;
}
$arr["AutoValidIsInconsistencyOK"] = "Y";
$arr["AutoValidInconsistencyNote"] = "N/A: Sedimen";
//Delta Check
list(
$isDeltaCheckOk,
$deltaCheckNote,
) = $this->get_delta_check_is_ok_sedimen(
$orderResult,
$orderHeaderID,
$patientID,
$natTestID,
$conditionID,
$r["Low"],
$r["High"]
);
if ($isDeltaCheckOk == "N/A") {
if ($conditionID == 2) {
$isDeltaCheckOk = "Y";
} else {
$isDeltaCheckOk = "N";
}
}
$arr["AutoValidIsDeltacheckOK"] = $isDeltaCheckOk;
$arr["AutoValidDeltaCheckNote"] = $deltaCheckNote;
$arr["AutoValidReflexTestIsOK"] = "Y";
$arr["AutoValidReflexTestNote"] = "N/A : Qualitative";
$qry = $this->db->insert("auto_valid_v2", $arr);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
$this->db->trans_rollback();
exit();
}
} else {
$arr["AutoValidIsQuantitative"] = "N";
$arr["AutoValidIsInReviewRange"] = "N";
$arr["AutoValidInReviewRangeNote"] = "N/A : Qualitative";
$conditionID = $this->get_condition_qualitative(
$orderResult,
$debug
);
if ($conditionID == 99) {
// N/A
$arr["AutoValidIsInReviewRange"] = "Y";
$arr["AutoValidInReviewRangeNote"] = "N/A : Qualitative Invalid Condition : {$orderResult}";
$qry = $this->db->insert("auto_valid_v2", $arr);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
$this->db->trans_rollback();
exit();
}
$this->db->trans_commit();
return false;
}
$arr["AutoValidIsInconsistencyOK"] = "Y";
$arr["AutoValidInconsistencyNote"] = "N/A: Qualitative hasil : $orderResult";
//Delta Check
list(
$isDeltaCheckOk,
$deltaCheckNote,
) = $this->get_delta_check_is_ok_qualitative(
$orderResult,
$orderHeaderID,
$patientID,
$natTestID,
$debug
);
$arr["AutoValidIsDeltacheckOK"] = $isDeltaCheckOk;
$arr["AutoValidDeltaCheckNote"] = $deltaCheckNote;
$arr["AutoValidReflexTestIsOK"] = "Y";
$arr["AutoValidReflexTestNote"] = "N/A : Qualitative";
$qry = $this->db->insert("auto_valid_v2", $arr);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
$this->db->trans_rollback();
exit();
}
}
$this->db->trans_commit();
}
if ($debug != "") {
print_r($arr);
exit();
}
//$this->db->trans_rollback();
if (
$this->isUpdate &&
($autoVerifIsOK == "N" ||
$isInconsistencyOK == "N" ||
$isDeltaCheckOk == "N" ||
$reflexTestIsOK == "N" ||
$isInReviewRange == "Y")
) {
return false;
} else {
$this->db->trans_begin();
$sql = "update t_orderdetail set T_OrderDetailValidation='Y'
,T_OrderDetailValDate = now(), T_OrderDetailValUserID = 1500
where T_OrderDetailID = ? and T_OrderDetailValidation='X'";
$qry = $this->db->query($sql, [$detailID]);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] .
"\n|" .
$this->db->last_query()
);
exit();
}
if ($debug != "") {
$this->db->trans_rollback();
} else {
$this->db->trans_commit();
}
$this->load->library("Txbranchstatus");
$this->txbranchstatus->update(
"VALIDATION",
$detailID,
"Auto Validation"
);
return true;
}
}
function get_reflex_test_is_ok(
$natTestID,
$conditionID,
$detailID,
$headerID
) {
$sql = "select
nat_multirule.*
from nat_multirule
join t_orderdetail on NatMultiruleNat_TestID = ?
and NatMultiruleIsActive = 'Y'
and NatMultiruleNat_ConditionID = ?
and T_OrderDetailID = ?
and ( T_OrderDetailNat_MethodeID = NatMultiruleNat_MethodeID
or NatMultiruleIsAllMethode = 'Y')";
$qry = $this->db->query($sql, [$natTestID, $conditionID, $detailID]);
if (!$qry) {
return ["N", "Error : " . $this->db->error()["message"]];
}
$rows = $qry->result_array();
if (count($rows) == 0) {
return ["X", "Reflex Test N/A"];
}
$s_ids = "0";
foreach ($rows as $r) {
$s_ids .= "," . $r["NatMultiruleID"];
}
$sql = "select distinct Nat_ReflexTestID,
Nat_ConditionName,
Nat_ReflexTestNat_ConditionID,
T_OrderDetailNat_NormalValueID, T_OrderDetailResult, T_OrderDetailNat_MethodeName,
T_OrderDetailMaxValueInclusive, T_OrderDetailMinValueInclusive,
T_OrderDetailMinValue, T_OrderDetailMaxValue, T_OrderDetailVerification,
Nat_TestName
from nat_reflextest
join nat_condition on Nat_ReflexTestNat_ConditionID = Nat_ConditionID
and Nat_ReflexTestIsActive = 'Y'
join nat_test on Nat_ReflexTestNat_TestID = Nat_TestID and Nat_TestIsActive = 'Y'
and Nat_ReflexTestIsActive = 'Y' and Nat_ReflexTestNatMultiRuleID in ($s_ids)
join t_test on Nat_TestID = T_TestNat_TestID and T_TestIsActive = 'Y'
join t_orderdetail on T_TestID = T_OrderDetailT_TestID
and T_OrderDetailIsActive = 'Y' and T_OrderDetailT_OrderHeaderID = ?
order by Nat_ReflexTestID, Nat_ReflexTestNat_RelationID desc";
$qry = $this->db->query($sql, [$headerID]);
if (!$qry) {
return [
"N",
"Error Get Reflex Test " . $this->db->error()["message"],
];
}
$rows = $qry->result_array();
$xnote = [];
$xnatcond = [];
foreach ($rows as $r) {
if (trim($r["T_OrderDetailResult"]) == "") {
continue;
}
$xConditionID = $r["Nat_ReflexTestNat_ConditionID"];
$xCondition = $r["Nat_ConditionName"];
if ($xConditionID == 1) {
//LOW
if ($r["T_OrderDetailMinValueInclusive"] == "Y") {
if (
$r["T_OrderDetailResult"] >= $r["T_OrderDetailMinValue"]
) {
return [
"N",
"{$r["Nat_TestName"]} {$xCondition}, hasil: {$r["T_OrderDetailResult"]}, min: {$r["T_OrderDetailMinValue"]}, max: {$r["T_OrderDetailMaxValue"]}",
];
}
} else {
if (
$r["T_OrderDetailResult"] > $r["T_OrderDetailMinValue"]
) {
return [
"N",
"{$r["Nat_TestName"]} {$xCondition}, hasil: {$r["T_OrderDetailResult"]}, min: {$r["T_OrderDetailMinValue"]}, max: {$r["T_OrderDetailMaxValue"]}",
];
}
}
$xnatcond_idx = $r["Nat_TestName"] . "-" . $xCondition;
if (!isset($xnatcond[$xnatcond_idx])) {
$xnote[] = "{$r["Nat_TestName"]} {$xCondition}, hasil: {$r["T_OrderDetailResult"]}, min: {$r["T_OrderDetailMinValue"]}, max: {$r["T_OrderDetailMaxValue"]}";
$xnatcond[$xnatcond_idx] = true;
}
} elseif ($xConditionID == 2) {
//NORMAL
if (
$r["T_OrderDetailMaxValueInclusive"] == "Y" &&
$r["T_OrderDetailMinValueInclusive"] == "Y"
) {
if (
$r["T_OrderDetailResult"] >
$r["T_OrderDetailMaxValue"] &&
$r["T_OrderDetailResult"] < $r["T_OrderDetailMinValue"]
) {
return [
"N",
"{$r["Nat_TestName"]} {$xCondition}, hasil: {$r["T_OrderDetailResult"]}, min: {$r["T_OrderDetailMinValue"]}, max: {$r["T_OrderDetailMaxValue"]}",
];
}
} elseif (
$r["T_OrderDetailMaxValueInclusive"] == "Y" &&
$r["T_OrderDetailMinValueInclusive"] == "N"
) {
if (
$r["T_OrderDetailResult"] >
$r["T_OrderDetailMaxValue"] &&
$r["T_OrderDetailResult"] <= $r["T_OrderDetailMinValue"]
) {
return [
"N",
"{$r["Nat_TestName"]} {$xCondition}, hasil: {$r["T_OrderDetailResult"]}, min: {$r["T_OrderDetailMinValue"]}, max: {$r["T_OrderDetailMaxValue"]}",
];
}
} elseif (
$r["T_OrderDetailMaxValueInclusive"] == "N" &&
$r["T_OrderDetailMinValueInclusive"] == "N"
) {
if (
$r["T_OrderDetailResult"] >=
$r["T_OrderDetailMaxValue"] &&
$r["T_OrderDetailResult"] <= $r["T_OrderDetailMinValue"]
) {
return [
"N",
"{$r["Nat_TestName"]} {$xCondition}, hasil: {$r["T_OrderDetailResult"]}, min: {$r["T_OrderDetailMinValue"]}, max: {$r["T_OrderDetailMaxValue"]}",
];
}
}
$xnatcond_idx = $r["Nat_TestName"] . "-" . $xCondition;
if (!isset($xnatcond[$xnatcond_idx])) {
$xnote[] = "{$r["Nat_TestName"]} {$xCondition}, hasil: {$r["T_OrderDetailResult"]}, min: {$r["T_OrderDetailMinValue"]}, max: {$r["T_OrderDetailMaxValue"]}";
$xnatcond[$xnatcond_idx] = true;
}
} elseif ($xConditionID == 3) {
//HIGHT
if ($r["T_OrderDetailMaxValueInclusive"] == "Y") {
if (
$r["T_OrderDetailResult"] < $r["T_OrderDetailMaxValue"]
) {
return [
"N",
"{$r["Nat_TestName"]} {$xCondition}, hasil: {$r["T_OrderDetailResult"]}, min: {$r["T_OrderDetailMinValue"]}, max: {$r["T_OrderDetailMaxValue"]}",
];
}
} else {
if (
$r["T_OrderDetailResult"] <= $r["T_OrderDetailMaxValue"]
) {
return [
"N",
"{$r["Nat_TestName"]} {$xCondition}, hasil: {$r["T_OrderDetailResult"]}, min: {$r["T_OrderDetailMinValue"]}, max: {$r["T_OrderDetailMaxValue"]}",
];
}
}
$xnatcond_idx = $r["Nat_TestName"] . "-" . $xCondition;
if (!isset($xnatcond[$xnatcond_idx])) {
$xnote[] = "{$r["Nat_TestName"]} {$xCondition}, hasil: {$r["T_OrderDetailResult"]}, min: {$r["T_OrderDetailMinValue"]}, max: {$r["T_OrderDetailMaxValue"]}";
$xnatcond[$xnatcond_idx] = true;
}
} else {
return [
"N",
"Reflex Test $xCondition tidak terpenuhi. invalid conditionID $xConditionID",
];
}
}
return ["Y", implode("|", $xnote)];
}
function process()
{
$param = $this->get_param();
$ids = $param["ids"];
$this->db->trans_begin();
$is_update = "N";
if (isset($param["is_update"])) {
$is_update = $param["is_update"];
}
if ($ids == "") {
$this->reply(["Total Order" => 0]);
exit();
}
$arr_id = explode(",", $ids);
$tot_ok = 0;
$tot_not_ok = 0;
$tot_req = 0;
$tot_review_range = 0;
foreach ($arr_id as $id) {
$result = $this->calculate($id);
if ($result === false) {
$tot_not_ok++;
continue;
}
unset($result["image"]);
$qry = $this->db->insert("auto_valid", $result);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] .
"|" .
$this->db->last_query()
);
$this->db->trans_rollback();
exit();
}
if ($result["AutoValidHaveReq"] == "Y") {
$tot_req++;
continue;
}
if ($result["AutoValidIsReviewRange"] != "Y") {
$tot_review_range++;
continue;
}
if ($result["AutoValidIsOk"] == "Y") {
$tot_ok++;
continue;
}
$tot_not_ok++;
}
$this->db->trans_commit();
$this->reply([
"total" => count($arr_id),
"requirement" => $tot_req,
"ok" => $tot_ok,
"not_ok" => $tot_not_ok,
"review_range_not_ok" => $tot_review_range,
]);
}
function order_v2($date = "")
{
if ($date == "") {
$date = "2021-07-21";
}
$start_date = $date . " 00:00:01";
$end_date = $date . " 23:59:59";
$sql = "select
T_OrderHeaderID, T_OrderHeaderDate, T_OrderHeaderLabNumber,
group_concat(T_TestName) TestName,
group_concat(T_OrderDetailID) OrderID
from t_orderheader
join t_orderdetail on T_OrderHeaderDate >= ? and T_OrderHeaderDate < ?
and T_OrderHeaderID = T_OrderDetailT_OrderHeaderID
and T_OrderHeaderIsActive = 'Y'
and T_OrderDetailIsActive = 'Y'
and T_OrderDetailVerification = 'Y'
and T_OrderDetailID not in (
select AutoValidT_OrderDetailID
from auto_valid_v2
where AutoValidIsActive = 'Y'
)
join t_test on T_OrderDetailT_TestID = T_TestID
and T_TestIsResult = 'Y'
and T_TestNat_GroupID = 1
group by T_OrderHeaderID, T_OrderHeaderDate, T_OrderHeaderLabNumber
order by T_OrderHeaderID
limit 0,20
";
$qry = $this->db->query($sql, [$start_date, $end_date]);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] . "\n|" . $this->db->last_query()
);
exit();
}
$rows = $qry->result_array();
$this->reply($rows);
}
function order($date = "")
{
if ($date == "") {
$date = "2021-07-21";
}
$start_date = $date . " 00:00:01";
$end_date = $date . " 23:59:59";
$sql = "select
T_OrderHeaderID,T_OrderHeaderDate, T_OrderHeaderLabNumber,
T_TestName, T_OrderDetailID, T_OrderDetailVerDate, T_OrderDetailVerUserID,
T_OrderDetailResult, T_OrderDetailNat_NormalValueID, T_OrderDetailNat_MethodeID
from t_orderheader
join t_orderdetail on T_OrderHeaderDate >= ? and T_OrderHeaderDate < ?
and T_OrderHeaderID = T_OrderDetailT_OrderHeaderID
and T_OrderHeaderIsActive = 'Y'
and T_OrderDetailIsActive = 'Y'
and T_OrderDetailVerification = 'Y'
and T_OrderDetailID not in (
select AutoValidT_OrderDetailID
from auto_valid
where AutoValidIsActive = 'Y'
)
join t_test on T_OrderDetailT_TestID = T_TestID and T_TestIsResult = 'Y'
order by T_OrderHeaderID,T_TestSasCode
limit 0,100
";
$qry = $this->db->query($sql, [$start_date, $end_date]);
if (!$qry) {
$this->reply_error(
$this->db->error()["message"] . "\n|" . $this->db->last_query()
);
exit();
}
$rows = $qry->result_array();
$this->reply($rows);
}
function reply_error($message)
{
echo json_encode(["status" => "ERR", "message" => $message]);
}
function reply($data)
{
echo json_encode(["status" => "OK", "data" => $data]);
}
public function print_table_style()
{
echo "
<style>
th, td {
padding: 15px;
text-align: left;
}
tr:nth-child(even) {background-color: #f2f2f2;}
tr.green {background-color: #00ee00;}
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) {
if ($r["AutoValidIsOk"] == "Y") {
echo "<tr class='green'>";
} else {
echo "<tr>";
}
foreach ($keys as $k) {
echo "<td>" . $r[$k] . "</td>";
}
echo "</tr>";
}
echo "</table>";
}
}