283 lines
13 KiB
PHP
283 lines
13 KiB
PHP
<?php
|
|
defined("BASEPATH") or exit("No direct script access allowed");
|
|
class KelainanLab
|
|
{
|
|
function __construct()
|
|
{
|
|
$CI = &get_instance();
|
|
$this->db_onedev = $CI->load->database("default", true);
|
|
$this->db_smartone = $CI->load->database("default", true);
|
|
}
|
|
|
|
function clean_mysqli_connection($dbc)
|
|
{
|
|
while (mysqli_more_results($dbc)) {
|
|
if (mysqli_next_result($dbc)) {
|
|
$result = mysqli_use_result($dbc);
|
|
|
|
if (get_class($result) == 'mysqli_stmt') {
|
|
mysqli_stmt_free_result($result);
|
|
} else {
|
|
unset($result);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function dynamic_comparison($varleft, $op, $varright)
|
|
{
|
|
|
|
switch ($op) {
|
|
case "=":
|
|
return $varleft == $varright;
|
|
case "!=":
|
|
return $varleft != $varright;
|
|
case ">=":
|
|
return $varleft >= $varright;
|
|
case "<=":
|
|
return $varleft <= $varright;
|
|
case ">":
|
|
return $varleft > $varright;
|
|
case "<":
|
|
return $varleft < $varright;
|
|
default:
|
|
return true;
|
|
}
|
|
}
|
|
|
|
function generate_kelainan_lab($orderid=0, $orderdetailID=0)
|
|
{
|
|
$errors = [];
|
|
if ($orderid != 0) {
|
|
$filterOrderID = " AND T_OrderHeaderID = {$orderid}";
|
|
} else {
|
|
$filterOrderID = "";
|
|
}
|
|
if ($orderdetailID != 0) {
|
|
$filterDetailID = " AND T_OrderDetailID = {$orderdetailID}";
|
|
} else {
|
|
$filterDetailID = "";
|
|
}
|
|
$sql = "SELECT
|
|
T_OrderHeaderID,T_OrderHeaderLabNumber,
|
|
T_TestName, T_OrderDetailID, T_OrderDetailResult, T_OrderDetailNat_MethodeID,
|
|
T_OrderDetailNat_MethodeID, T_OrderDetailNat_MethodeName,
|
|
T_OrderDetailMinValue, T_OrderDetailMaxValue, T_OrderDetailMinValueInclusive,
|
|
T_OrderDetailMaxValueInclusive,
|
|
Mcu_SummaryLabID,
|
|
Mcu_SummaryLabValue,
|
|
Mcu_SummaryLabNat_MethodeID,
|
|
Mcu_SummaryLabWithMethode,
|
|
Mcu_SummaryLabType,
|
|
Mcu_SummaryLabIsNormalValue,
|
|
Mcu_SummaryLabIsRange,
|
|
Mcu_SummaryLabMinInclusive,
|
|
Mcu_SummaryLabMinValue,
|
|
Mcu_SummaryLabMaxInclusive,
|
|
Mcu_SummaryLabMaxValue,
|
|
Mcu_KelainanID, Mcu_KelainanName, Mcu_KelainanClasification,
|
|
Nat_TestID,
|
|
Nat_TestCode,
|
|
mcu_kelainangroup.*
|
|
from t_orderdetail
|
|
join t_orderheader on T_OrderHeaderID = T_OrderDetailT_OrderHeaderID {$filterOrderID} {$filterDetailID}
|
|
and T_OrderHeaderID = T_OrderDetailT_OrderHeaderID
|
|
and T_OrderDetailIsActive = 'Y'
|
|
JOIN m_patient
|
|
ON T_OrderHeaderM_PatientID = M_PatientID
|
|
JOIN m_sex ON M_PatientM_SexID = M_SexID
|
|
join t_test
|
|
on T_OrderDetailT_TestID = T_TestID
|
|
join nat_test ON T_TestNat_TestID = Nat_TestID
|
|
join mcu_summarylab on T_TestNat_TestID = Mcu_SummaryLabNat_TestID AND Mcu_SummaryLabIsActive = 'Y'
|
|
AND (Mcu_SummaryLabGender = 'A' OR (Mcu_SummaryLabGender <> 'A' AND Mcu_SummaryLabGender = CASE
|
|
WHEN LOWER(M_SexNameLang) = 'male' THEN 'M'
|
|
WHEN LOWER(M_SexNameLang) = 'female' THEN 'F'
|
|
else ''
|
|
END))
|
|
join mcu_kelainan on Mcu_SummaryLabMcu_KelainanID = Mcu_KelainanID
|
|
join mcu_kelainangroup on Mcu_KelainanMcu_KelainanGroupID = Mcu_KelainanGroupID";
|
|
//echo $sql;
|
|
$qry = $this->db_onedev->query($sql);
|
|
if (!$qry) {
|
|
|
|
$prm_log = ['SELECT_T_ORDERDETAIL_KELAINAN_LAB_1', 'kelainanlab/generate_kelainan_lab'];
|
|
$xsql = $this->db_onedev->last_query();
|
|
$log_error = $this->insert_log_error($xsql, $prm_log,json_encode($errors));
|
|
$errors[] = "SELECT_T_ORDERDETAIL_KELAINAN_LAB_1";
|
|
}
|
|
|
|
$rows = $qry->result_array();
|
|
$results = [];
|
|
|
|
|
|
|
|
foreach ($rows as $r) {
|
|
$result_value = $r['T_OrderDetailResult'];
|
|
|
|
$value_comparison = 0;
|
|
//echo $r['Mcu_SummaryLabIsNormalValue'];
|
|
$kelainan = [];
|
|
$notavailable = [];
|
|
if (trim($result_value) == 'NA') {
|
|
$notavailable = $r;
|
|
} elseif ($r['Mcu_SummaryLabIsNormalValue'] == 'Y') {
|
|
if ($r['Mcu_SummaryLabType'] == '<' || $r['Mcu_SummaryLabType'] == '<=') {
|
|
$value_comparison = $r['T_OrderDetailMinValue'];
|
|
}
|
|
if ($r['Mcu_SummaryLabType'] == '>' || $r['Mcu_SummaryLabType'] == '>=') {
|
|
$value_comparison = $r['T_OrderDetailMaxValue'];
|
|
}
|
|
//echo "Y";
|
|
if ($r['Mcu_SummaryLabWithMethode'] == 'N' && $this->dynamic_comparison($result_value, $r['Mcu_SummaryLabType'], $value_comparison)) {
|
|
//echo $r['T_TestName']." : ".$r['Mcu_KelainanName'].", ";
|
|
$kelainan = $r;
|
|
$results[] = array('test' => $r['T_TestName'], 'nat_test' => $r['Nat_TestCode'], 'kelainan' => $r['Mcu_KelainanName'], 'orderdetailID' => $r['T_OrderDetailID'], 'orderheaderID' => $r['T_OrderHeaderID'], 'nat_testID' => $r['Nat_TestID'], 'mcu_summarylabID' => $r['Mcu_SummaryLabID']);
|
|
}
|
|
} else if ($r['Mcu_SummaryLabIsRange'] == 'Y') {
|
|
// Mcu_SummaryLabIsNormalValue,
|
|
// Mcu_SummaryLabIsRange,
|
|
// Mcu_SummaryLabMinInclusive,
|
|
// Mcu_SummaryLabMinValue,
|
|
// Mcu_SummaryLabMaxInclusive,
|
|
// Mcu_SummaryLabMaxValue,
|
|
$isWithinMin = $r['Mcu_SummaryLabMinInclusive'] === 'Y' ? floatval($result_value) >= floatval($r['Mcu_SummaryLabMinValue']) : floatval($result_value) > floatval($r['Mcu_SummaryLabMinValue']);
|
|
$isWithinMax = $r['Mcu_SummaryLabMaxInclusive'] === 'Y' ? floatval($result_value) <= floatval($r['Mcu_SummaryLabMaxValue']) : floatval($result_value) < floatval($r['Mcu_SummaryLabMaxValue']);
|
|
|
|
if ($isWithinMax && $isWithinMin) {
|
|
$kelainan = $r;
|
|
$results[] = array('test' => $r['T_TestName'], 'nat_test' => $r['Nat_TestCode'], 'kelainan' => $r['Mcu_KelainanName'], 'orderdetailID' => $r['T_OrderDetailID'], 'orderheaderID' => $r['T_OrderHeaderID'], 'nat_testID' => $r['Nat_TestID'], 'mcu_summarylabID' => $r['Mcu_SummaryLabID']);
|
|
}
|
|
} else {
|
|
$value_comparison = $r['Mcu_SummaryLabValue'];
|
|
if ($r['Mcu_SummaryLabType'] == '!=' || $r['Mcu_SummaryLabType'] == '==') {
|
|
if ($this->dynamic_comparison($result_value, $r['Mcu_SummaryLabType'], $value_comparison)) {
|
|
//echo $r['T_TestName']." : ".$r['Mcu_KelainanName'].", ";
|
|
$kelainan = $r;
|
|
$results[] = array('test' => $r['T_TestName'], 'nat_test' => $r['Nat_TestCode'], 'kelainan' => $r['Mcu_KelainanName'], 'orderdetailID' => $r['T_OrderDetailID'], 'orderheaderID' => $r['T_OrderHeaderID'], 'nat_testID' => $r['Nat_TestID'], 'mcu_summarylabID' => $r['Mcu_SummaryLabID']);
|
|
}
|
|
} else {
|
|
if (is_numeric($result_value) && $this->dynamic_comparison($result_value, $r['Mcu_SummaryLabType'], $value_comparison)) {
|
|
//echo $r['T_TestName']." : ".$r['Mcu_KelainanName'].", ";
|
|
$kelainan = $r;
|
|
$results[] = array('test' => $r['T_TestName'], 'nat_test' => $r['Nat_TestCode'], 'kelainan' => $r['Mcu_KelainanName'], 'orderdetailID' => $r['T_OrderDetailID'], 'orderheaderID' => $r['T_OrderHeaderID'], 'nat_testID' => $r['Nat_TestID'], 'mcu_summarylabID' => $r['Mcu_SummaryLabID']);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (count($results) > 0) {
|
|
$sql_update_kel = "";
|
|
if ($orderdetailID != 0) {
|
|
$sql_update_kel = "UPDATE t_kelainan_lab
|
|
SET T_KelainanLabIsActive = 'N'
|
|
WHERE
|
|
T_KelainanLabT_OrderDetailID = {$orderdetailID}";
|
|
} else {
|
|
$sql_update_kel = "UPDATE t_kelainan_lab
|
|
SET T_KelainanLabIsActive = 'N'
|
|
WHERE
|
|
T_KelainanLabT_OrderHeaderID = {$orderid}";
|
|
|
|
}
|
|
$qry_update_kel = $this->db_onedev->query($sql_update_kel);
|
|
if (!$qry_update_kel) {
|
|
$prm_log = ['UPDATE_T_KELAINAN_LAB_N', 'kelainanlab/generate_kelainan_lab'];
|
|
$xsql = $this->db_onedev->last_query();
|
|
$log_error = $this->insert_log_error($xsql, $prm_log,json_encode(['orderid' => $orderid, 'orderdetailID' => $orderdetailID]));
|
|
$errors[] = "UPDATE_T_KELAINAN_LAB_N";
|
|
}
|
|
|
|
foreach ($results as $v_result) {
|
|
$sql_kel_lab = "SELECT *
|
|
FROM t_kelainan_lab
|
|
WHERE T_KelainanLabT_OrderDetailID = ?
|
|
GROUP BY T_KelainanLabT_OrderDetailID";
|
|
$qry_kel_lab = $this->db_onedev->query($sql_kel_lab, array($v_result['orderdetailID']));
|
|
if ($qry_kel_lab) {
|
|
$rows_kel = $qry_kel_lab->result_array();
|
|
} else {
|
|
$prm_log = ['SELECT_T_KELAINAN_LAB', 'kelainanlab/generate_kelainan_lab'];
|
|
$xsql = $this->db_onedev->last_query();
|
|
$log_error = $this->insert_log_error($xsql, $prm_log,json_encode($v_result));
|
|
$errors[] = "SELECT_T_KELAINAN_LAB";
|
|
}
|
|
|
|
if (count($rows_kel) > 0) {
|
|
|
|
// print_r($v_result['orderdetailID']);
|
|
// exit;
|
|
|
|
$sql_update_kel = "UPDATE t_kelainan_lab
|
|
SET T_KelainanLabT_OrderDetailID = {$v_result['orderdetailID']},
|
|
T_KelainanLabT_OrderHeaderID = {$v_result['orderheaderID']},
|
|
T_KelainanLabNat_TestID = {$v_result['nat_testID']},
|
|
T_KelainanLabMcu_SummaryLabID = {$v_result['mcu_summarylabID']},
|
|
T_KelainanLabCreatedUserID = 1,
|
|
T_KelainanLabIsActive = 'Y',
|
|
T_KelainanLabCreated = NOW()
|
|
WHERE T_KelainanLabID = {$rows_kel[0]['T_KelainanLabID']}";
|
|
$qry_update_kel = $this->db_onedev->query($sql_update_kel);
|
|
if (!$qry_update_kel) {
|
|
$prm_log = ['UPDATE_T_KELAINAN_LAB', 'kelainanlab/generate_kelainan_lab'];
|
|
$xsql = $this->db_onedev->last_query();
|
|
$log_error = $this->insert_log_error($xsql, $prm_log,json_encode($v_result));
|
|
$errors[] = "UPDATE_T_KELAINAN_LAB";
|
|
}
|
|
} else {
|
|
// print_r('insert');
|
|
// exit;
|
|
$sql_insert_lab = "INSERT INTO t_kelainan_lab(
|
|
T_KelainanLabT_OrderDetailID,
|
|
T_KelainanLabT_OrderHeaderID,
|
|
T_KelainanLabNat_TestID,
|
|
T_KelainanLabMcu_SummaryLabID,
|
|
T_KelainanLabCreatedUserID,
|
|
T_KelainanLabCreated)
|
|
VALUES(
|
|
'{$v_result['orderdetailID']}',
|
|
'{$v_result['orderheaderID']}',
|
|
'{$v_result['nat_testID']}',
|
|
'{$v_result['mcu_summarylabID']}',
|
|
1,
|
|
NOW())";
|
|
$qry_insert_lab = $this->db_onedev->query($sql_insert_lab);
|
|
|
|
if (!$qry_insert_lab) {
|
|
$prm_log = ['INSERT_T_KELAINAN_LAB', 'kelainanlab/generate_kelainan_lab'];
|
|
$xsql = $this->db_onedev->last_query();
|
|
$log_error = $this->insert_log_error($xsql, $prm_log,json_encode($v_result));
|
|
$errors[] = "INSERT_T_KELAINAN_LAB";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $errors;
|
|
}
|
|
|
|
function insert_log_error($log_sql, $params,$data='')
|
|
{
|
|
// Membersihkan SQL dari \r\n dan whitespace berlebih
|
|
//$clean_log_sql = trim(str_replace(["\r\n", "\r", "\n"], ' ', $log_sql));
|
|
|
|
$sql = "INSERT INTO error_log(
|
|
ErrorLogCode,
|
|
ErrorLogName,
|
|
ErrorLogDescription,
|
|
ErrorLogData,
|
|
ErrorLogOrderCreated
|
|
)
|
|
VALUES(
|
|
?,?,?,?,NOW()
|
|
)";
|
|
|
|
$query = $this->db_log->query($sql, [$params[0],$params[1],$log_sql,json_encode($data)]);
|
|
//echo $this->db_log->last_query();
|
|
if (!$query) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|