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

681 lines
21 KiB
PHP

<?php
class Nartestresultmapping extends MY_Controller
{
var $db_onedev;
public function index()
{
echo "SampleStorage API";
}
public function __construct()
{
parent::__construct();
$this->db_onedev = $this->load->database("onedev", true);
}
public function search()
{
$prm = $this->sys_input;
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$join_sql = 'LEFT ';
$status = $prm["status"];
$jpagroup = $prm["jpagroup"];
$search_code = $prm['code'];
$search_name = $prm['name'];
$number_limit = 20;
$number_offset = ($prm['current_page'] - 1) * $number_limit ;
$sql_where = '';
if($search_code != '' || $search_name != ''){
$sql_where .= ' AND (';
if($search_code != ''){
$sql_where .= " Nar_TestResultCode LIKE CONCAT('{$search_code}','%') ";
}
if($search_name != ''){
if($search_code != '')
$sql_where .= ' AND ';
$sql_where .= " Nar_TestResultDisplay LIKE CONCAT('%','{$search_name}','%') ";
}
$sql_where .= ')';
}
$where_status = " AND Nar_TestResultMappingNar_TestID = {$jpagroup} ";
if($status == 'Y'){
$join_sql = '';
}
$sql_param = array($start_date,$end_date);
$sql = " SELECT count(*) as total
FROM nar_test_result
$join_sql JOIN nar_test_result_mapping ON Nar_TestResultMappingNar_TestResultID = Nar_TestResultID $where_status AND Nar_TestResultMappingIsActive = 'Y'
WHERE Nar_TestResultIsActive = 'Y' $sql_where
";
//echo $sql;
$query = $this->db_onedev->query($sql);
$tot_count = 0;
$tot_page = 0;
if ($query) {
$tot_count = $query->result_array()[0]["total"];
$tot_page = ceil($tot_count/$number_limit);
} else {
$this->sys_error_db("t_samplestorage count", $this->db_onedev);
exit;
}
$sql = "SELECT nar_test_result.*, IF(ISNULL(Nar_TestResultMappingID),'N','Y') as status
FROM nar_test_result
$join_sql JOIN nar_test_result_mapping ON Nar_TestResultMappingNar_TestResultID = Nar_TestResultID
$where_status AND Nar_TestResultMappingIsActive = 'Y'
WHERE Nar_TestResultIsActive = 'Y' $sql_where
ORDER BY Nar_TestResultCode ASC
limit $number_limit offset $number_offset";
$query = $this->db_onedev->query($sql);
//echo $this->db_onedev->last_query();
$rows = $query->result_array();
if($rows){
}
//$this->_add_address($rows);
$result = array("total" => $tot_page, "records" => $rows, "sql"=> $this->db_onedev->last_query());
$this->sys_ok($result);
exit;
}
function getinitdatas(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$rows = [];
$query =" SELECT Nat_GroupID as id, Nat_GroupName as name
FROM nat_group
WHERE
Nat_GroupIsActive = 'Y'
";
//echo $query;
$rows['groups'] = $this->db_onedev->query($query)->result_array();
/*$query =" SELECT Nat_SubGroupID as id, CONCAT('[ ',Nat_SubGroupCode,' ] ', Nat_SubGroupName) as name
FROM nat_subgroup
WHERE
Nat_SubGroupIsActive = 'Y'
";
//echo $query;
$rows['subgroups'] = $this->db_onedev->query($query)->result_array();*/
$query =" SELECT Nar_TestResultTypeID as id, Nar_TestResultTypeName as name
FROM t_testtype
WHERE
Nar_TestResultTypeIsActive = 'Y'
";
//echo $query;
$rows['types'] = $this->db_onedev->query($query)->result_array();
$rows['nonlabs'] = array(array("id"=>"","name"=>"LAB"),array("id"=>"XRAY","name"=>"XRAY"), array("id"=>"USG","name"=>"USG"),array("id"=>"ELECTROMEDIS","name"=>"ELECTROMEDIS"));
$result = array(
"total" => count($rows) ,
"records" => $rows,
);
$this->sys_ok($result);
exit;
}
function get_subgroups(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$rows = [];
$query =" SELECT Nat_SubGroupID as id, Nat_SubGroupName as name
FROM nat_subgroup
WHERE
Nat_SubGroupIsActive = 'Y' AND Nat_SubGroupNat_GroupID = {$prm['id']}
";
//echo $query;
$rows = $this->db_onedev->query($query)->result_array();
$result = array(
"total" => count($rows) ,
"records" => $rows,
);
$this->sys_ok($result);
exit;
}
function get_subsubgroups(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$rows = [];
$query =" SELECT Nat_SubSubGroupID as id, Nat_SubSubGroupName as name
FROM nat_subsubgroup
WHERE
Nat_SubSubGroupIsActive = 'Y' AND Nat_SubSubGroupNat_GroupID = {$prm['id']}
";
//echo $query;
$rows = $this->db_onedev->query($query)->result_array();
$result = array(
"total" => count($rows) ,
"records" => $rows,
);
$this->sys_ok($result);
exit;
}
function getracks(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$rows = [];
$query =" SELECT Summary_SampleStorageM_AlmariID as almarid, Summary_SampleStorageM_RackID as rackid , Summary_SampleStorageRowPosition as row, Summary_SampleStorageColPosition as col
FROM summary_samplestorage
WHERE
Summary_SampleStorageStatus = 'FILLED'
";
//echo $query;
$filledrows = $this->db_onedev->query($query)->result_array();
$query =" SELECT {$prm['id']} as almariid,
M_RackID as id,
CONCAT(M_RackCode,' ( ',M_RackRows,' x ',M_RackColumns,' )') as name,
M_RackCode as code,
M_RackRows as row,
M_RackColumns as col,
'' as rackcontens
FROM m_rack
WHERE
M_RackM_AlmariID = {$prm['id']} AND M_RackIsActive = 'Y'
";
//echo $query;
$datarows = $this->db_onedev->query($query)->result_array();
foreach($datarows as $k => $v){
$rows = $v['row'];
$cols = $v['col'];
$rackcontens = array();
for ($x = 1; $x <= $rows; $x++) {
$children = array();
for ($i = 1; $i <= $cols; $i++) {
$content = $x.' x '.$i;
$xrow = $x;
$xcol = $i;
$status = $this->checkexistfilled($filledrows, $v['id'],$xrow,$xcol);
array_push($children,array('content'=>$content,'row'=>$xrow,'col'=>$xcol,'status'=>$status,'selected'=>'N'));
}
array_push($rackcontens,$children);
}
$datarows[$k]['rackcontens'] = $rackcontens;
}
$result = array(
"total" => count($datarows) ,
"records" => $datarows,
);
$this->sys_ok($result);
exit;
}
function checkexistfilled($datas,$rackid,$row,$col){
$rtn = 'N';
foreach($datas as $k => $v){
if($v['rackid'] == $rackid && $v['row'] == $row && $v['col'] == $col){
$rtn = 'Y';
}
}
return $rtn;
}
function searchunit(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$max_rst = 12;
$tot_count =0;
$q = [
'search' => '%'
];
if ($prm['search'] != '')
{
$q['search'] = "%{$prm['search']}%";
}
// QUERY TOTAL
$sql = "SELECT count(*) as total
FROM nat_unit
WHERE
Nat_UnitName like ?
AND Nat_unitIsActive = 'Y'";
$query = $this->db_onedev->query($sql,$q['search']);
//echo $query;
if ($query) {
$tot_count = $query->result_array()[0]["total"];
}
else {
$this->sys_error_db("m_city count",$this->db_onedev);
exit;
}
$sql = "
SELECT Nat_UnitId as id, Nat_UnitName as name
FROM nat_unit
WHERE
Nat_UnitName like ?
AND Nat_unitIsActive = 'Y'
ORDER BY Nat_UnitName ASC
";
$query = $this->db_onedev->query($sql, array($q['search']));
if ($query) {
$rows = $query->result_array();
//echo $this->db_onedev->last_query();
$result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows));
$this->sys_ok($result);
}
else {
$this->sys_error_db("m_city rows",$this->db_onedev);
exit;
}
}
function searchsample(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$max_rst = 12;
$tot_count =0;
$q = [
'search' => '%'
];
if ($prm['search'] != '')
{
$q['search'] = "%{$prm['search']}%";
}
// QUERY TOTAL
$sql = "SELECT count(*) as total
FROM nat_sampletype
WHERE
Nat_SampleTypeName like ?
AND Nat_SampleTypeIsActive = 'Y'";
$query = $this->db_onedev->query($sql,$q['search']);
//echo $query;
if ($query) {
$tot_count = $query->result_array()[0]["total"];
}
else {
$this->sys_error_db("m_city count",$this->db_onedev);
exit;
}
$sql = "
SELECT Nat_SampleTypeID as id, Nat_SampleTypeName as name
FROM nat_sampletype
WHERE
Nat_SampleTypeName like ?
AND Nat_SampleTypeIsActive = 'Y'
ORDER BY Nat_SampleTypeName ASC
";
$query = $this->db_onedev->query($sql, array($q['search']));
if ($query) {
$rows = $query->result_array();
//echo $this->db_onedev->last_query();
$result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows));
$this->sys_ok($result);
}
else {
$this->sys_error_db("m_city rows",$this->db_onedev);
exit;
}
}
function save(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$userid = $this->sys_user["M_UserID"];
$sql = "INSERT INTO nar_test_result_mapping (
Nar_TestResultMappingNar_TestID,
Nar_TestResultMappingNar_TestResultID,
Nar_TestResultMappingUserID,
Nar_TestResultMappingCreated
)
VALUES(
{$prm['jpagroupid']},
{$prm['Nar_TestResultID']},
{$userid},
NOW()
)
on duplicate key update
Nar_TestResultMappingIsActive = '{$prm['status']}',
Nar_TestResultMappingUserID = {$userid}";
$this->db_onedev->query($sql);
$result = array(
"total" => 1 ,
"records" => array('status'=>'OK'),
"numbering" => $data_log_header,
"id" => $last_id
);
$this->sys_ok($result);
exit;
}
function checkcodeexist(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$rtn = 'N';
$query =" SELECT COUNT(*) as countx
FROM t_test
WHERE Nar_TestResultCode = '{$prm['code']}' AND Nar_TestResultIsActive = 'Y'
";
//echo $query;
$rst = $this->db_onedev->query($query)->row()->countx;
if($rst > 0)
$rtn = 'Y';
$result = array(
"total" => 1 ,
"records" => $rtn,
);
$this->sys_ok($result);
exit;
}
function getdataselected(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$id = $prm['Nar_TestResultID'];
$query =" SELECT
Nar_TestResultCode as code,
Nar_TestResultDisplay as name,
Nar_TestResultShortName as shortname,
Nar_TestResultShortNameBarcode as codebarcode,
'' as xgroup,
Nat_GroupID as group_id,
Nat_GroupName as group_name,
'' as subgroup,
Nat_SubGroupID as subgroup_id,
Nat_SubGroupName as subgroup_name,
'' as subsubgroup,
Nat_SubSubGroupID as subsubgroup_id,
Nat_SubSubGroupName as subsubgroup_name,
'' as type,
Nar_TestResultNar_TestResultTypeID as type_id,
Nar_TestResultTypeName as type_name,
'' as unit,
Nar_TestResultNat_UnitID as unit_id,
Nat_UnitName as unit_name,
'' as sample,
Nar_TestResultIsNonLab as nonlab,
Nar_TestResultNat_SampleTypeID as sample_id,
Nat_SampleTypeName as sample_name,
Nar_TestResultFontSize as fontsize,
Nar_TestResultFontColor as fontcolor,
Nar_TestResultIsBold as flagbold,
Nar_TestResultIsItalic as flagitalic,
Nar_TestResultFlagGluc as flaggluc,
Nar_TestResultIsQuantitative as flagquantitative,
Nar_TestResultIsDeltaCheck as deltacheck,
Nar_TestResultIsResult as isresult,
Nar_TestResultIsPrice as isprice,
Nar_TestResultIsPrintResult as printresult,
Nar_TestResultIsPrintNota as printnote,
Nar_TestResultIsTrendAnalysis as trendanalysis,
Nar_TestResultIsWorklist as isworklist,
Nar_TestResultWorklistName as worklistname,
Nar_TestResultFlagLow as flaglow,
Nar_TestResultFlagHigh as flaghigh
FROM t_test
JOIN nat_group ON Nar_TestResultNat_GroupID = Nat_GroupID
JOIN nat_subgroup ON Nar_TestResultNat_SubGroupID = Nat_SubGroupID
LEFT JOIN nat_subsubgroup ON Nar_TestResultNat_SubSubGroupID = Nat_SubSubGroupID
JOIN t_testtype ON Nar_TestResultNar_TestResultTypeID = Nar_TestResultTypeID
LEFT JOIN nat_sampletype ON Nar_TestResultNat_SampleTypeID = Nat_SampleTypeID
LEFT JOIN nat_unit ON Nar_TestResultNat_UnitID = Nat_UnitID
WHERE Nar_TestResultID = '{$id}'
";
//echo $query;
$rows['xform'] = $this->db_onedev->query($query)->row_array();
if($rows['xform']){
$rows['xform']['unit'] = array();
if($rows['xform']['isresult'] == 'Y')
$rows['xform']['unit'] = array('id'=>$rows['xform']['unit_id'],'name'=>$rows['xform']['unit_name']);
$rows['xform']['sample'] = array();
if($rows['xform']['isresult'] == 'Y')
$rows['xform']['sample'] = array('id'=>$rows['xform']['sample_id'],'name'=>$rows['xform']['sample_name']);
$rows['xform']['xgroup'] = array('id'=>$rows['xform']['group_id'],'name'=>$rows['xform']['group_name']);
$rows['xform']['subgroup'] = array('id'=>$rows['xform']['subgroup_id'],'name'=>$rows['xform']['subgroup_name']);
$rows['xform']['subsubgroup'] = array('id'=>$rows['xform']['subsubgroup_id'],'name'=>$rows['xform']['subsubgroup_name']);
$rows['xform']['type'] = array('id'=>$rows['xform']['type_id'],'name'=>$rows['xform']['type_name']);
if($rows['xform']['nonlab'] == '')
$rows['xform']['nonlab'] = array('id'=>'','name'=>'LAB');
else
$rows['xform']['nonlab'] = array('id'=>$rows['xform']['nonlab'],'name'=>$rows['xform']['nonlab']);
unset($rows['xform']['sample_id']);
unset($rows['xform']['unit_id']);
unset($rows['xform']['group_id']);
unset($rows['xform']['subgroup_id']);
unset($rows['xform']['subsubgroup_id']);
unset($rows['xform']['sample_name']);
unset($rows['xform']['unit_name']);
unset($rows['xform']['group_name']);
unset($rows['xform']['subgroup_name']);
unset($rows['xform']['subsubgroup_name']);
}
$sql = "SELECT Nat_SubGroupID as id, Nat_SubGroupName as name FROM nat_subgroup WHERE Nat_SubGroupNat_GroupID = {$prm['Nat_GroupID']} AND Nat_SubGroupIsActive = 'Y'";
//echo $sql;
$rows['subgroups'] = $this->db_onedev->query($sql)->result_array();
$sql = "SELECT Nat_SubSubGroupID as id, Nat_SubSubGroupName as name FROM nat_subsubgroup WHERE Nat_SubSubGroupSubGroupID = {$prm['Nat_SubGroupID']} AND Nat_SubSubGroupIsActive = 'Y'";
//echo $sql;
$rows['subsubgroups'] = $this->db_onedev->query($sql)->result_array();
$result = array(
"total" => 1 ,
"records" => $rows,
);
$this->sys_ok($result);
exit;
}
function doaddtest(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$rtn = true;
$prm = $this->sys_input;
$userid = $this->sys_user["M_UserID"];
$codefrom = $prm['codefrom'];
$codetoward = $prm['codetoward'];
$leng = strlen($codetoward);
$code_parent = substr($codetoward, 0,-2);
$sql = "SELECT count(*) as existtest FROM t_test WHERE Nar_TestResultSasCode LIKE '{$code_parent}%' AND Nar_TestResultCode = '{$codefrom}' AND Nar_TestResultIsActive = 'Y'";
//echo $sql;
$xcount = $this->db_onedev->query($sql)->row()->existtest;
if($xcount == 0){
$sql = "
select Nar_TestResultID ,Nar_TestResultSasCode,
case
when length(Nar_TestResultSasCode) = {$leng} then Nar_TestResultSasCode + 1
else
concat( substr(Nar_TestResultSasCode,1,{$leng}) + 2 , substr(Nar_TestResultSasCode, {$leng} +1) )
end as NewCode,
Nar_TestResultDisplay
from
t_test
where Nar_TestResultSasCode like '{$code_parent}%' AND length(Nar_TestResultSasCode) = {$leng} AND Nar_TestResultSasCode >= '{$codetoward}'
UNION
select Nar_TestResultID , Nar_TestResultSasCode,
case
when length(Nar_TestResultSasCode) = {$leng} then Nar_TestResultSasCode + 1
else
concat( substr(Nar_TestResultSasCode,1,{$leng}) + 1 , substr(Nar_TestResultSasCode, {$leng} +1) )
end as NewCode,
Nar_TestResultDisplay
from
t_test
where ( Nar_TestResultSasCode like '{$code_parent}%' AND Nar_TestResultSasCode > '{$codetoward}' ) OR Nar_TestResultSasCode = '{$codetoward}'
";
//echo $sql;
$toupdate = $this->db_onedev->query($sql)->result();
if($toupdate){
foreach($toupdate as $k => $v){
$sql = "UPDATE t_test SET Nar_TestResultSasCode = '{$v->NewCode}' WHERE Nar_TestResultID = {$v->Nar_TestResultID} ";
$this->db_onedev->query($sql);
}
}
$sql = "SELECT * FROM t_test WHERE Nar_TestResultSasCode = '{$code_parent}' AND Nar_TestResultIsActive = 'Y'";
$parentid = $this->db_onedev->query($sql)->row()->Nar_TestResultID;
$query ="INSERT INTO t_test (
Nar_TestResultParentNar_TestResultID,
Nar_TestResultNar_TestResultID,
Nar_TestResultCode,
Nar_TestResultSasCode,
Nar_TestResultDisplay,
Nar_TestResultShortName,
Nar_TestResultShortNameBarcode,
Nar_TestResultNat_GroupID,
Nar_TestResultNat_SubGroupID,
Nar_TestResultT_SampleTypeID,
Nar_TestResultFontSize,
Nar_TestResultFontColor,
Nar_TestResultIsBold,
Nar_TestResultIsItalic,
Nar_TestResultFlagGluc,
Nar_TestResultIsQuantitative,
Nar_TestResultIsDeltaCheck,
Nar_TestResultIsResult,
Nar_TestResultIsPrice,
Nar_TestResultIsPrintResult,
Nar_TestResultIsPrintNota,
Nar_TestResultIsTrendAnalysis,
Nar_TestResultIsWorklist,
Nar_TestResultIsNonLab,
Nar_TestResultWorklistName,
Nar_TestResultFlagLow,
Nar_TestResultFlagHigh,
Nar_TestResultUserID,
Nar_TestResultCreated
)
SELECT
{$parentid},
Nar_TestResultID,
Nar_TestResultCode,
'{$codetoward}',
Nar_TestResultDisplay,
Nar_TestResultShortName,
Nar_TestResultShortNameBarcode,
Nar_TestResultNat_GroupID,
Nar_TestResultNat_SubGroupID,
Nar_TestResultNat_SampleTypeID,
Nar_TestResultFontSize,
Nar_TestResultFontColor,
Nar_TestResultIsBold,
Nar_TestResultIsItalic,
Nar_TestResultFlagGluc,
Nar_TestResultIsQuantitative,
Nar_TestResultIsDeltaCheck,
Nar_TestResultIsResult,
Nar_TestResultIsPrice,
Nar_TestResultIsPrintResult,
Nar_TestResultIsPrintNota,
Nar_TestResultIsTrendAnalysis,
Nar_TestResultIsWorklist,
Nar_TestResultIsNonLab,
Nar_TestResultWorklistName,
Nar_TestResultFlagLow,
Nar_TestResultFlagHigh,
{$userid},
NOW()
FROM t_test
WHERE
Nar_TestResultCode = '{$codefrom}' AND Nar_TestResultIsActive = 'Y'
";
//echo $query;
$this->db_onedev->query($query);
$sql = "SELECT Nar_TestResultID FROM t_test WHERE Nar_TestResultSasCode = '{$code_parent}' AND Nar_TestResultIsActive = 'Y' LIMIT 1";
$data_parent = $this->db_onedev->query($sql)->row();
if($data_parent){
$testidparent = $data_parent->Nar_TestResultID;
$sql = "UPDATE t_test SET Nar_TestResultIsParent = 'Y' WHERE Nar_TestResultID = {$testidparent}";
$this->db_onedev->query($sql);
}
}
$result = array(
"total" => 1 ,
"records" => $xcount,
"debug"=>$query
);
$this->sys_ok($result);
exit;
}
}