clqms-be/app/Controllers/Test/TestsController.php

549 lines
19 KiB
PHP
Raw Normal View History

<?php
namespace App\Controllers\Test;
use App\Controllers\BaseController;
use App\Libraries\TestValidationService;
use App\Libraries\ValueSet;
use App\Traits\ResponseTrait;
class TestsController extends BaseController
{
use ResponseTrait;
protected $model;
protected $modelCal;
protected $modelGrp;
protected $modelMap;
protected $modelMapDetail;
protected $modelRefNum;
protected $modelRefTxt;
protected $rules;
public function __construct()
{
$this->model = new \App\Models\Test\TestDefSiteModel;
$this->modelCal = new \App\Models\Test\TestDefCalModel;
$this->modelGrp = new \App\Models\Test\TestDefGrpModel;
$this->modelMap = new \App\Models\Test\TestMapModel;
$this->modelMapDetail = new \App\Models\Test\TestMapDetailModel;
$this->modelRefNum = new \App\Models\RefRange\RefNumModel;
$this->modelRefTxt = new \App\Models\RefRange\RefTxtModel;
$this->rules = [
'TestSiteCode' => 'required',
'TestSiteName' => 'required',
'TestType' => 'required',
'SiteID' => 'required',
];
}
public function index()
{
$search = $this->request->getGet('search');
$filters = [
'SiteID' => $this->request->getGet('SiteID'),
'TestType' => $this->request->getGet('TestType'),
'VisibleScr' => $this->request->getGet('VisibleScr'),
'VisibleRpt' => $this->request->getGet('VisibleRpt'),
'TestSiteName' => $this->request->getGet('TestSiteName'),
'TestSiteCode' => $this->request->getGet('TestSiteCode'),
'search' => $search,
];
$rows = $this->model->getTestsWithRelations($filters);
if (empty($rows)) {
return $this->respond([
'status' => 'success',
'message' => 'No data.',
'data' => [],
], 200);
}
feat(valueset): refactor from ID-based to name-based lookups Complete overhaul of the valueset system to use human-readable names instead of numeric IDs for improved maintainability and API consistency. - PatientController: Renamed 'Gender' field to 'Sex' in validation rules - ValuesetController: Changed API endpoints from ID-based (/:num) to name-based (/:any) - TestsController: Refactored to use ValueSet library instead of direct valueset queries - Added ValueSet library (app/Libraries/ValueSet.php) with static lookup methods: - getOptions() - returns dropdown format [{value, label}] - getLabel(, ) - returns label for a value - transformLabels(, ) - batch transform records - get() and getRaw() for Lookups compatibility - Added ValueSetApiController for public valueset API endpoints - Added ValueSet refresh endpoint (POST /api/valueset/refresh) - Added DemoOrderController for testing order creation without auth - 2026-01-12-000001: Convert valueset references from VID to VValue - 2026-01-12-000002: Rename patient.Gender column to Sex - OrderTestController: Now uses OrderTestModel with proper model pattern - TestsController: Uses ValueSet library for all lookup operations - ValueSetController: Simplified to use name-based lookups - Updated all organization (account/site/workstation) dialogs and index views - Updated specimen container dialogs and index views - Updated tests_index.php with ValueSet integration - Updated patient dialog form and index views - Removed .factory/config.json and CLAUDE.md (replaced by AGENTS.md) - Consolidated lookups in Lookups.php (removed inline valueset constants) - Updated all test files to match new field names - 32 modified files, 17 new files, 2 deleted files - Net: +661 insertions, -1443 deletions (significant cleanup)
2026-01-12 16:53:41 +07:00
$rows = ValueSet::transformLabels($rows, [
'TestType' => 'test_type',
]);
feat(valueset): refactor from ID-based to name-based lookups Complete overhaul of the valueset system to use human-readable names instead of numeric IDs for improved maintainability and API consistency. - PatientController: Renamed 'Gender' field to 'Sex' in validation rules - ValuesetController: Changed API endpoints from ID-based (/:num) to name-based (/:any) - TestsController: Refactored to use ValueSet library instead of direct valueset queries - Added ValueSet library (app/Libraries/ValueSet.php) with static lookup methods: - getOptions() - returns dropdown format [{value, label}] - getLabel(, ) - returns label for a value - transformLabels(, ) - batch transform records - get() and getRaw() for Lookups compatibility - Added ValueSetApiController for public valueset API endpoints - Added ValueSet refresh endpoint (POST /api/valueset/refresh) - Added DemoOrderController for testing order creation without auth - 2026-01-12-000001: Convert valueset references from VID to VValue - 2026-01-12-000002: Rename patient.Gender column to Sex - OrderTestController: Now uses OrderTestModel with proper model pattern - TestsController: Uses ValueSet library for all lookup operations - ValueSetController: Simplified to use name-based lookups - Updated all organization (account/site/workstation) dialogs and index views - Updated specimen container dialogs and index views - Updated tests_index.php with ValueSet integration - Updated patient dialog form and index views - Removed .factory/config.json and CLAUDE.md (replaced by AGENTS.md) - Consolidated lookups in Lookups.php (removed inline valueset constants) - Updated all test files to match new field names - 32 modified files, 17 new files, 2 deleted files - Net: +661 insertions, -1443 deletions (significant cleanup)
2026-01-12 16:53:41 +07:00
return $this->respond([
'status' => 'success',
'message' => 'Data fetched successfully',
'data' => $rows,
], 200);
}
public function show($id = null)
{
if (!$id) {
return $this->failValidationErrors('TestSiteID is required');
}
$row = $this->model->getTestById($id);
if (!$row) {
return $this->respond([
'status' => 'success',
'message' => 'No data.',
'data' => null,
], 200);
}
feat(valueset): refactor from ID-based to name-based lookups Complete overhaul of the valueset system to use human-readable names instead of numeric IDs for improved maintainability and API consistency. - PatientController: Renamed 'Gender' field to 'Sex' in validation rules - ValuesetController: Changed API endpoints from ID-based (/:num) to name-based (/:any) - TestsController: Refactored to use ValueSet library instead of direct valueset queries - Added ValueSet library (app/Libraries/ValueSet.php) with static lookup methods: - getOptions() - returns dropdown format [{value, label}] - getLabel(, ) - returns label for a value - transformLabels(, ) - batch transform records - get() and getRaw() for Lookups compatibility - Added ValueSetApiController for public valueset API endpoints - Added ValueSet refresh endpoint (POST /api/valueset/refresh) - Added DemoOrderController for testing order creation without auth - 2026-01-12-000001: Convert valueset references from VID to VValue - 2026-01-12-000002: Rename patient.Gender column to Sex - OrderTestController: Now uses OrderTestModel with proper model pattern - TestsController: Uses ValueSet library for all lookup operations - ValueSetController: Simplified to use name-based lookups - Updated all organization (account/site/workstation) dialogs and index views - Updated specimen container dialogs and index views - Updated tests_index.php with ValueSet integration - Updated patient dialog form and index views - Removed .factory/config.json and CLAUDE.md (replaced by AGENTS.md) - Consolidated lookups in Lookups.php (removed inline valueset constants) - Updated all test files to match new field names - 32 modified files, 17 new files, 2 deleted files - Net: +661 insertions, -1443 deletions (significant cleanup)
2026-01-12 16:53:41 +07:00
$typeCode = $row['TestType'] ?? '';
if ($typeCode === 'CALC') {
$row['testdefcal'] = $this->modelCal->getByTestSiteID($id);
} elseif ($typeCode === 'GROUP') {
$row['testdefgrp'] = $this->modelGrp->getGroupMembers($id);
} elseif ($typeCode !== 'TITLE') {
$row['testdeftech'] = $this->model->getTestTechWithRelations($id);
if (!empty($row['testdeftech'])) {
$techData = $row['testdeftech'][0];
feat(valueset): refactor from ID-based to name-based lookups Complete overhaul of the valueset system to use human-readable names instead of numeric IDs for improved maintainability and API consistency. - PatientController: Renamed 'Gender' field to 'Sex' in validation rules - ValuesetController: Changed API endpoints from ID-based (/:num) to name-based (/:any) - TestsController: Refactored to use ValueSet library instead of direct valueset queries - Added ValueSet library (app/Libraries/ValueSet.php) with static lookup methods: - getOptions() - returns dropdown format [{value, label}] - getLabel(, ) - returns label for a value - transformLabels(, ) - batch transform records - get() and getRaw() for Lookups compatibility - Added ValueSetApiController for public valueset API endpoints - Added ValueSet refresh endpoint (POST /api/valueset/refresh) - Added DemoOrderController for testing order creation without auth - 2026-01-12-000001: Convert valueset references from VID to VValue - 2026-01-12-000002: Rename patient.Gender column to Sex - OrderTestController: Now uses OrderTestModel with proper model pattern - TestsController: Uses ValueSet library for all lookup operations - ValueSetController: Simplified to use name-based lookups - Updated all organization (account/site/workstation) dialogs and index views - Updated specimen container dialogs and index views - Updated tests_index.php with ValueSet integration - Updated patient dialog form and index views - Removed .factory/config.json and CLAUDE.md (replaced by AGENTS.md) - Consolidated lookups in Lookups.php (removed inline valueset constants) - Updated all test files to match new field names - 32 modified files, 17 new files, 2 deleted files - Net: +661 insertions, -1443 deletions (significant cleanup)
2026-01-12 16:53:41 +07:00
$refType = $techData['RefType'];
$resultType = $techData['ResultType'] ?? '';
if (TestValidationService::usesRefNum($resultType, $refType)) {
$row['refnum'] = $this->modelRefNum->getFormattedByTestSiteID($id);
}
if (TestValidationService::usesRefTxt($resultType, $refType)) {
$row['reftxt'] = $this->modelRefTxt->getFormattedByTestSiteID($id);
}
}
}
return $this->respond([
'status' => 'success',
'message' => 'Data fetched successfully',
'data' => $row,
], 200);
}
public function create()
{
$input = $this->request->getJSON(true);
if (!$this->validateData($input, $this->rules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
$testType = $input['TestType'] ?? '';
$details = $input['details'] ?? $input;
$resultType = $details['ResultType'] ?? '';
$refType = $details['RefType'] ?? '';
if (TestValidationService::isCalc($testType)) {
$resultType = 'NMRIC';
$refType = $refType ?: 'RANGE';
} elseif (TestValidationService::isGroup($testType) || TestValidationService::isTitle($testType)) {
$resultType = 'NORES';
$refType = 'NOREF';
}
if ($resultType && $refType) {
$validation = TestValidationService::validate($testType, $resultType, $refType);
if (!$validation['valid']) {
return $this->failValidationErrors(['type_validation' => $validation['error']]);
}
}
$db = \Config\Database::connect();
$db->transStart();
try {
$testSiteData = [
'SiteID' => $input['SiteID'],
'TestSiteCode'=> $input['TestSiteCode'],
'TestSiteName'=> $input['TestSiteName'],
'TestType' => $input['TestType'],
'Description' => $input['Description'] ?? null,
'SeqScr' => $input['SeqScr'] ?? 0,
'SeqRpt' => $input['SeqRpt'] ?? 0,
'IndentLeft' => $input['IndentLeft'] ?? 0,
'FontStyle' => $input['FontStyle'] ?? null,
'VisibleScr' => $input['VisibleScr'] ?? 1,
'VisibleRpt' => $input['VisibleRpt'] ?? 1,
'CountStat' => $input['CountStat'] ?? 1,
'StartDate' => $input['StartDate'] ?? date('Y-m-d H:i:s'),
];
$id = $this->model->insert($testSiteData);
if (!$id) {
throw new \Exception('Failed to insert main test definition');
}
$this->handleDetails($id, $input, 'insert');
$db->transComplete();
if ($db->transStatus() === false) {
return $this->failServerError('Transaction failed');
}
return $this->respondCreated([
'status' => 'created',
'message' => 'Test created successfully',
'data' => ['TestSiteId' => $id],
]);
} catch (\Exception $e) {
$db->transRollback();
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
public function update($id = null)
{
$input = $this->request->getJSON(true);
if (!$id && isset($input['TestSiteID'])) {
$id = $input['TestSiteID'];
}
if (!$id) {
return $this->failValidationErrors('TestSiteID is required.');
}
$existing = $this->model->find($id);
if (!$existing) {
return $this->failNotFound('Test not found');
}
$testType = $input['TestType'] ?? $existing['TestType'] ?? '';
$details = $input['details'] ?? $input;
$resultType = $details['ResultType'] ?? $existing['ResultType'] ?? '';
$refType = $details['RefType'] ?? $existing['RefType'] ?? '';
if (TestValidationService::isCalc($testType)) {
$resultType = 'NMRIC';
$refType = $refType ?: 'RANGE';
} elseif (TestValidationService::isGroup($testType) || TestValidationService::isTitle($testType)) {
$resultType = 'NORES';
$refType = 'NOREF';
}
if ($resultType && $refType) {
$validation = TestValidationService::validate($testType, $resultType, $refType);
if (!$validation['valid']) {
return $this->failValidationErrors(['type_validation' => $validation['error']]);
}
}
$db = \Config\Database::connect();
$db->transStart();
try {
$testSiteData = [];
$allowedUpdateFields = [
'TestSiteCode',
'TestSiteName',
'TestType',
'Description',
'SeqScr',
'SeqRpt',
'IndentLeft',
'FontStyle',
'VisibleScr',
'VisibleRpt',
'CountStat',
'StartDate',
];
foreach ($allowedUpdateFields as $field) {
if (isset($input[$field])) {
$testSiteData[$field] = $input[$field];
}
}
if (!empty($testSiteData)) {
$this->model->update($id, $testSiteData);
}
$this->handleDetails($id, $input, 'update');
$db->transComplete();
if ($db->transStatus() === false) {
return $this->failServerError('Transaction failed');
}
return $this->respond([
'status' => 'success',
'message' => 'Test updated successfully',
'data' => ['TestSiteId' => $id],
]);
} catch (\Exception $e) {
$db->transRollback();
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
public function delete($id = null)
{
$input = $this->request->getJSON(true);
if (!$id && isset($input['TestSiteID'])) {
$id = $input['TestSiteID'];
}
if (!$id) {
return $this->failValidationErrors('TestSiteID is required.');
}
$existing = $this->model->find($id);
if (!$existing) {
return $this->failNotFound('Test not found');
}
if (!empty($existing['EndDate'])) {
return $this->failValidationErrors('Test is already disabled');
}
$db = \Config\Database::connect();
$db->transStart();
try {
$now = date('Y-m-d H:i:s');
$this->model->update($id, ['EndDate' => $now]);
$testType = $existing['TestType'];
feat(valueset): refactor from ID-based to name-based lookups Complete overhaul of the valueset system to use human-readable names instead of numeric IDs for improved maintainability and API consistency. - PatientController: Renamed 'Gender' field to 'Sex' in validation rules - ValuesetController: Changed API endpoints from ID-based (/:num) to name-based (/:any) - TestsController: Refactored to use ValueSet library instead of direct valueset queries - Added ValueSet library (app/Libraries/ValueSet.php) with static lookup methods: - getOptions() - returns dropdown format [{value, label}] - getLabel(, ) - returns label for a value - transformLabels(, ) - batch transform records - get() and getRaw() for Lookups compatibility - Added ValueSetApiController for public valueset API endpoints - Added ValueSet refresh endpoint (POST /api/valueset/refresh) - Added DemoOrderController for testing order creation without auth - 2026-01-12-000001: Convert valueset references from VID to VValue - 2026-01-12-000002: Rename patient.Gender column to Sex - OrderTestController: Now uses OrderTestModel with proper model pattern - TestsController: Uses ValueSet library for all lookup operations - ValueSetController: Simplified to use name-based lookups - Updated all organization (account/site/workstation) dialogs and index views - Updated specimen container dialogs and index views - Updated tests_index.php with ValueSet integration - Updated patient dialog form and index views - Removed .factory/config.json and CLAUDE.md (replaced by AGENTS.md) - Consolidated lookups in Lookups.php (removed inline valueset constants) - Updated all test files to match new field names - 32 modified files, 17 new files, 2 deleted files - Net: +661 insertions, -1443 deletions (significant cleanup)
2026-01-12 16:53:41 +07:00
$typeCode = $testType;
if (TestValidationService::isCalc($typeCode)) {
$this->modelCal->disableByTestSiteID($id);
} elseif (TestValidationService::isGroup($typeCode)) {
$this->modelGrp->disableByTestSiteID($id);
} elseif (TestValidationService::isTechnicalTest($typeCode)) {
$this->modelRefNum->disableByTestSiteID($id);
$this->modelRefTxt->disableByTestSiteID($id);
}
// Disable testmap by test code
$testSiteCode = $existing['TestSiteCode'] ?? null;
if ($testSiteCode) {
$existingMaps = $this->modelMap->getMappingsByTestCode($testSiteCode);
foreach ($existingMaps as $existingMap) {
$this->modelMapDetail->disableByTestMapID($existingMap['TestMapID']);
$this->modelMap->update($existingMap['TestMapID'], ['EndDate' => $now]);
}
}
$db->transComplete();
if ($db->transStatus() === false) {
return $this->failServerError('Transaction failed');
}
return $this->respond([
'status' => 'success',
'message' => 'Test disabled successfully',
'data' => ['TestSiteId' => $id, 'EndDate' => $now],
]);
} catch (\Exception $e) {
$db->transRollback();
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
private function handleDetails($testSiteID, $input, $action)
{
$testTypeID = $input['TestType'] ?? null;
$testSiteCode = null;
if (!$testTypeID && $action === 'update') {
$existing = $this->model->find($testSiteID);
$testTypeID = $existing['TestType'] ?? null;
$testSiteCode = $existing['TestSiteCode'] ?? null;
}
if (!$testTypeID) {
return;
}
feat(valueset): refactor from ID-based to name-based lookups Complete overhaul of the valueset system to use human-readable names instead of numeric IDs for improved maintainability and API consistency. - PatientController: Renamed 'Gender' field to 'Sex' in validation rules - ValuesetController: Changed API endpoints from ID-based (/:num) to name-based (/:any) - TestsController: Refactored to use ValueSet library instead of direct valueset queries - Added ValueSet library (app/Libraries/ValueSet.php) with static lookup methods: - getOptions() - returns dropdown format [{value, label}] - getLabel(, ) - returns label for a value - transformLabels(, ) - batch transform records - get() and getRaw() for Lookups compatibility - Added ValueSetApiController for public valueset API endpoints - Added ValueSet refresh endpoint (POST /api/valueset/refresh) - Added DemoOrderController for testing order creation without auth - 2026-01-12-000001: Convert valueset references from VID to VValue - 2026-01-12-000002: Rename patient.Gender column to Sex - OrderTestController: Now uses OrderTestModel with proper model pattern - TestsController: Uses ValueSet library for all lookup operations - ValueSetController: Simplified to use name-based lookups - Updated all organization (account/site/workstation) dialogs and index views - Updated specimen container dialogs and index views - Updated tests_index.php with ValueSet integration - Updated patient dialog form and index views - Removed .factory/config.json and CLAUDE.md (replaced by AGENTS.md) - Consolidated lookups in Lookups.php (removed inline valueset constants) - Updated all test files to match new field names - 32 modified files, 17 new files, 2 deleted files - Net: +661 insertions, -1443 deletions (significant cleanup)
2026-01-12 16:53:41 +07:00
$typeCode = $testTypeID;
$details = $input['details'] ?? $input;
$details['TestSiteID'] = $testSiteID;
$details['SiteID'] = $input['SiteID'] ?? 1;
switch ($typeCode) {
case 'CALC':
$this->saveCalcDetails($testSiteID, $details, $action);
break;
case 'GROUP':
$this->saveGroupDetails($testSiteID, $details, $input, $action);
break;
case 'TITLE':
if (isset($input['testmap']) && is_array($input['testmap'])) {
$this->saveTestMap($testSiteID, $testSiteCode, $input['testmap'], $action);
}
break;
case 'TEST':
case 'PARAM':
default:
$this->saveTechDetails($testSiteID, $details, $action, $typeCode);
if (in_array($typeCode, ['TEST', 'PARAM']) && isset($details['RefType'])) {
feat(api): transition to headless architecture and enhance order management This commit marks a significant architectural shift, transitioning the CLQMS backend to a fully headless REST API. All view-related components have been removed to focus solely on providing a robust, stateless API for clinical laboratory workflows. ### Architectural Changes - **Headless API Transition:** - Removed all view files (`app/Views/v2`), associated page controllers (`PagesController`), and routes (`Routes.php`). The application no longer serves a front-end UI. - The root endpoint (`/`) now returns a simple "Backend Running" status message. - **Developer Tooling & Guidance:** - Replaced `CLAUDE.md` with `GEMINI.md` to provide updated context and instructional guidelines for Gemini agents. - Updated `.serena/project.yml` with project configuration. ### Feature Enhancements - **Advanced Order Management (`OrderTestModel`):** - **Test Expansion:** The `createOrder` process now automatically expands `GROUP` (panel) tests into their individual components and recursively includes all parameter dependencies for `CALC` (calculated) tests. - **Order Comments:** Added support for attaching comments to an order via the `ordercom` table. - **Status Tracking:** Order status updates are now correctly recorded in the `orderstatus` table. - **Schema Alignment:** Switched from `OrderID` to `InternalOID` as the primary key for internal operations. - **Reference Range Refactor (`TestsController`):** - Simplified reference range logic by consolidating `refthold` and `refvset` into the main `refnum` and `reftxt` tables. - Standardized `RefType` handling to support `NMRC`, `TEXT`, `THOLD`, and `VSET` codes from the `reference_type` ValueSet. ### Other Changes - **Documentation:** - `PRD.md`, `README.md`, and `TODO.md` were updated to reflect the headless architecture, refined scope, and current project priorities. - **Database:** - Removed obsolete `RefTHoldID` and `RefVSetID` columns from the `patres` table migration. - **Testing:** - Added new feature tests for `ContactController`, `OrganizationController`, and `TestsController`.
2026-01-31 09:27:32 +07:00
$refType = (string) $details['RefType'];
$resultType = $details['ResultType'] ?? '';
if (TestValidationService::usesRefNum($resultType, $refType) && isset($input['refnum']) && is_array($input['refnum'])) {
$this->saveRefNumRanges($testSiteID, $input['refnum'], $action, $input['SiteID'] ?? 1);
}
if (TestValidationService::usesRefTxt($resultType, $refType) && isset($input['reftxt']) && is_array($input['reftxt'])) {
$this->saveRefTxtRanges($testSiteID, $input['reftxt'], $action, $input['SiteID'] ?? 1);
}
}
break;
}
if ((TestValidationService::isTechnicalTest($typeCode) || TestValidationService::isCalc($typeCode)) && isset($input['testmap']) && is_array($input['testmap'])) {
$this->saveTestMap($testSiteID, $testSiteCode, $input['testmap'], $action);
}
}
private function saveTechDetails($testSiteID, $data, $action, $typeCode)
{
$techData = [
'DisciplineID' => $data['DisciplineID'] ?? null,
'DepartmentID' => $data['DepartmentID'] ?? null,
'ResultType' => $data['ResultType'] ?? null,
'RefType' => $data['RefType'] ?? null,
'VSet' => $data['VSet'] ?? null,
'ReqQty' => $data['ReqQty'] ?? null,
'ReqQtyUnit' => $data['ReqQtyUnit'] ?? null,
'Unit1' => $data['Unit1'] ?? null,
'Factor' => $data['Factor'] ?? null,
'Unit2' => $data['Unit2'] ?? null,
'Decimal' => $data['Decimal'] ?? 2,
'CollReq' => $data['CollReq'] ?? null,
'Method' => $data['Method'] ?? null,
'ExpectedTAT' => $data['ExpectedTAT'] ?? null,
];
$this->model->update($testSiteID, $techData);
}
private function saveRefNumRanges($testSiteID, $ranges, $action, $siteID)
{
if ($action === 'update') {
$this->modelRefNum->disableByTestSiteID($testSiteID);
}
$this->modelRefNum->batchInsert($testSiteID, $siteID, $ranges);
}
private function saveRefTxtRanges($testSiteID, $ranges, $action, $siteID)
{
if ($action === 'update') {
$this->modelRefTxt->disableByTestSiteID($testSiteID);
}
$this->modelRefTxt->batchInsert($testSiteID, $siteID, $ranges);
}
private function saveCalcDetails($testSiteID, $data, $action)
{
$calcData = [
'TestSiteID' => $testSiteID,
'DisciplineID' => $data['DisciplineID'] ?? null,
'DepartmentID' => $data['DepartmentID'] ?? null,
'FormulaInput' => $data['FormulaInput'] ?? null,
'FormulaCode' => $data['FormulaCode'] ?? $data['Formula'] ?? null,
'ResultType' => 'NMRIC',
'RefType' => $data['RefType'] ?? 'RANGE',
'Unit1' => $data['Unit1'] ?? $data['ResultUnit'] ?? null,
'Factor' => $data['Factor'] ?? null,
'Unit2' => $data['Unit2'] ?? null,
'Decimal' => $data['Decimal'] ?? 2,
'Method' => $data['Method'] ?? null,
];
if ($action === 'update') {
$exists = $this->modelCal->existsByTestSiteID($testSiteID);
if ($exists) {
$this->modelCal->update($exists['TestCalID'], $calcData);
} else {
$this->modelCal->insert($calcData);
}
} else {
$this->modelCal->insert($calcData);
}
}
private function saveGroupDetails($testSiteID, $data, $input, $action)
{
if ($action === 'update') {
$this->modelGrp->disableByTestSiteID($testSiteID);
}
$members = $data['members'] ?? ($input['Members'] ?? []);
if (is_array($members)) {
foreach ($members as $m) {
$memberID = is_array($m) ? ($m['Member'] ?? ($m['TestSiteID'] ?? null)) : $m;
if ($memberID) {
$this->modelGrp->insert([
'TestSiteID' => $testSiteID,
'Member' => $memberID,
]);
}
}
}
}
private function saveTestMap($testSiteID, $testSiteCode, $mappings, $action)
{
if ($action === 'update' && $testSiteCode) {
// Find existing mappings by test code through testmapdetail
$existingMaps = $this->modelMap->getMappingsByTestCode($testSiteCode);
foreach ($existingMaps as $existingMap) {
$this->modelMapDetail->disableByTestMapID($existingMap['TestMapID']);
}
// Soft delete the testmap headers
foreach ($existingMaps as $existingMap) {
$this->modelMap->update($existingMap['TestMapID'], ['EndDate' => date('Y-m-d H:i:s')]);
}
}
if (is_array($mappings)) {
foreach ($mappings as $map) {
$mapData = [
'HostType' => $map['HostType'] ?? null,
'HostID' => $map['HostID'] ?? null,
'ClientType' => $map['ClientType'] ?? null,
'ClientID' => $map['ClientID'] ?? null,
];
$testMapID = $this->modelMap->insert($mapData);
if ($testMapID && isset($map['details']) && is_array($map['details'])) {
foreach ($map['details'] as $detail) {
$detailData = [
'TestMapID' => $testMapID,
'HostTestCode' => $detail['HostTestCode'] ?? null,
'HostTestName' => $detail['HostTestName'] ?? null,
'ConDefID' => $detail['ConDefID'] ?? null,
'ClientTestCode' => $detail['ClientTestCode'] ?? null,
'ClientTestName' => $detail['ClientTestName'] ?? null,
];
$this->modelMapDetail->insert($detailData);
}
}
}
}
}
}