180 lines
6.6 KiB
PHP
180 lines
6.6 KiB
PHP
<?php
|
|
defined('BASEPATH') or exit('No direct script access allowed');
|
|
class ResultCalc
|
|
{
|
|
public function auto($id, $debug = "", $skip_orderdetail_ids = array())
|
|
{
|
|
$CI = &get_instance();
|
|
$this->db = $CI->load->database("onedev", true);
|
|
$skip_lookup = array_fill_keys(array_map('strval', $skip_orderdetail_ids), true);
|
|
$sql = "select
|
|
T_TestCalculationID,T_TestCalculationFormula,
|
|
T_OrderDetailID, T_OrderDetailT_TestID, T_TestNat_TestID Nat_TestID,
|
|
T_TestCalculationID,T_OrderDetailResult,
|
|
fn_global_age_count_day(M_PatientDOB, date(T_OrderHeaderDate)) / 365 AgeInYear,
|
|
T_TestCalculationNat_SexID,
|
|
T_OrderDetailT_TestName
|
|
from t_orderdetail
|
|
join t_orderheader on T_OrderDetailT_OrderHeaderID = T_OrderHeaderID
|
|
and T_OrderHeaderID = ?
|
|
join m_patient on T_OrderHeaderM_PatientID = M_PatientID
|
|
join t_test on T_OrderDetailT_TestID = T_TestID
|
|
and T_OrderDetailIsActive = 'Y' and T_TestIsActive = 'Y'
|
|
join t_testcalculation on T_TestNat_TestID = T_TestCalculationNat_TestID
|
|
and T_TestCalculationIsActive = 'Y'
|
|
and (
|
|
T_TestCalculationNat_SexID = M_PatientM_SexID
|
|
or
|
|
T_TestCalculationNat_SexID = 0
|
|
)
|
|
";
|
|
|
|
$sql_det = "select T_OrderDetailT_TestName, T_TestCalculationDetailCode, T_OrderDetailResult
|
|
from
|
|
t_testcalculation_detail td
|
|
join t_testcalculation on T_TestCalculationID = T_TestCalculationDetailT_TestCalculationID
|
|
and T_TestCalculationID = ? and T_TestCalculationDetailIsActive = 'Y'
|
|
join t_test t on td.T_TestCalculationDetailNat_TestID = T_TestNat_TestID
|
|
left join t_orderdetail on T_TestID = T_OrderDetailT_TestID and T_OrderDetailIsActive = 'Y'
|
|
and T_OrderDetailT_OrderHeaderID = ?
|
|
Order by T_TestCalculationDetailCode,T_OrderDetailResult desc";
|
|
//where T_OrderDetailID is not null";
|
|
|
|
|
|
$qry = $this->db->query($sql, array($id));
|
|
$date = date("Y-m-d H:i:s");
|
|
//file_put_contents("/xtmp/debug-calc.log", "$date : {$this->db->last_query()} \n");
|
|
$rows = $qry->result_array();
|
|
if ($debug != "") {
|
|
print_r($rows);
|
|
}
|
|
|
|
if (count($rows) > 0) {
|
|
foreach ($rows as $r) {
|
|
$tc_id = $r["T_TestCalculationID"];
|
|
$od_id = (string) $r["T_OrderDetailID"];
|
|
if (isset($skip_lookup[$od_id])) {
|
|
continue;
|
|
}
|
|
$qry_det = $this->db->query($sql_det, array($tc_id, $id));
|
|
$drows = $qry_det->result_array();
|
|
$formula = $r["T_TestCalculationFormula"];
|
|
$have_all = true;
|
|
$have_one = false;
|
|
$formula = str_replace("AGE", $r["AgeInYear"], $formula);
|
|
$flag_recursive = false;
|
|
if (strpos($formula, "[REC]") > -1) {
|
|
$flag_recursive = true;
|
|
}
|
|
if (!$flag_recursive) {
|
|
if (trim($r["T_OrderDetailResult"]) != "") continue;
|
|
}
|
|
|
|
$formula = str_replace("[REC]", "", $formula);
|
|
$org_formula = $formula;
|
|
$s_code_value = "";
|
|
$tc_name = $r["T_OrderDetailT_TestName"];
|
|
|
|
$arr_code = array();
|
|
|
|
$arr_param = [];
|
|
foreach ($drows as $dr) {
|
|
$code = $dr["T_TestCalculationDetailCode"];
|
|
$value = $dr["T_OrderDetailResult"];
|
|
$s_code_value .= "^$code=$value";
|
|
$arr_param[] = $code;
|
|
if (isset($arr_code[$code])) continue;
|
|
$arr_code[$code] = "exist";
|
|
if ($dr["T_OrderDetailResult"] == "" || $dr["T_OrderDetailResult"] == "-") {
|
|
$have_all = false;
|
|
break;
|
|
}
|
|
$arr_code[$code] = $value;
|
|
$have_one = true;
|
|
$formula = str_replace($code, $value, $formula);
|
|
}
|
|
$have_all = true;
|
|
if (count($arr_param) == 0) $have_all = false;
|
|
foreach($arr_param as $p) {
|
|
if ($debug != "") {
|
|
echo "code $p : ";
|
|
}
|
|
if (!isset($arr_code[$p])) {
|
|
if ($debug != "") {
|
|
echo "not exists \n";
|
|
}
|
|
$have_all = false;
|
|
break;
|
|
} else {
|
|
if ($arr_code[$p] == "exist") {
|
|
$have_all = false;
|
|
}
|
|
if ($debug != "") {
|
|
echo "exists : {$arr_code[$p]} \n";
|
|
}
|
|
}
|
|
}
|
|
if ($debug != "") {
|
|
echo "$formula " . $r["T_OrderDetailResult"] . "\n";
|
|
echo "-- have one : " . $have_one ? "Y" : "N";
|
|
echo "\n";
|
|
echo "-- have all : " . $have_all ? "Y" : "N";
|
|
echo "\n";
|
|
}
|
|
if ($have_all && $have_one) {
|
|
|
|
$date = date("Y-m-d H:i:s");
|
|
try {
|
|
// file_put_contents("/xtmp/debug-calc.log", "$date : $tc_name => \$f_value = $formula; \n");
|
|
// file_put_contents("/xtmp/debug-calc.log", "$date : Formula : $org_formula\n",FILE_APPEND);
|
|
// file_put_contents("/xtmp/debug-calc.log", print_r($drows,true),FILE_APPEND);
|
|
|
|
eval("\$f_value = $formula;");
|
|
//formating
|
|
$f_value = $this->format_value($f_value, $r["Nat_TestID"]);
|
|
if (is_numeric($f_value)) {
|
|
$f_value = $this->format_value($f_value, $r["Nat_TestID"]);
|
|
}
|
|
$sql = "update t_orderdetail set T_OrderDetailResult = ?
|
|
where T_OrderDetailID = ?";
|
|
$this->db->query($sql, array($f_value, $od_id));
|
|
|
|
if ($tc_id == 2) {
|
|
$sql = "insert into t_order_calc(T_OrderCalcT_OrderDetailID,
|
|
T_OrderCalcFormula,T_OrderCalcResult) values(?,?,?)";
|
|
$this->db->query($sql, array($od_id, $formula . " | " . $s_code_value, $f_value));
|
|
}
|
|
} catch (Exception $e) {
|
|
//print_r($e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
function format_value($f_value, $nat_test_id)
|
|
{
|
|
$CI = &get_instance();
|
|
$this->db = $CI->load->database("onedev", true);
|
|
$sql = "select * from m_instrumentmethode
|
|
where M_InstrumentMethodeIsActive = 'Y'
|
|
and M_InstrumentMethodeNat_TestID = ?
|
|
limit 0,1";
|
|
$qry = $this->db->query($sql, array($nat_test_id));
|
|
if ($qry) {
|
|
$rows = $qry->result_array();
|
|
if (count($rows) > 0) {
|
|
if ($f_value > 0) {
|
|
$fmt = $rows[0]["M_InstrumentMethodeResultFormatAboveSF"];
|
|
} else {
|
|
$fmt = $rows[0]["M_InstrumentMethodeResultFormatSF"];
|
|
}
|
|
$idx = strpos($fmt, ".");
|
|
$dec = strlen($fmt) - $idx;
|
|
if ($dec > 0) $dec = $dec - 1;
|
|
return number_format($f_value, $dec);
|
|
}
|
|
}
|
|
return number_format($f_value, 0, ".", ",");
|
|
}
|
|
}
|