From e9c7beeb2b04408202b8cf3857c071ded131ad98 Mon Sep 17 00:00:00 2001 From: mahdahar <89adham@gmail.com> Date: Tue, 3 Mar 2026 06:03:27 +0700 Subject: [PATCH] feat: update test management APIs and reference range models --- app/Controllers/PatVisitController.php | 4 +- app/Controllers/Test/TestMapController.php | 8 +- .../Test/TestMapDetailController.php | 8 +- app/Controllers/Test/TestsController.php | 118 ++++-------------- app/Models/RefRange/RefNumModel.php | 10 ++ app/Models/RefRange/RefTxtModel.php | 10 ++ app/Models/Test/TestDefCalModel.php | 39 ++++-- app/Models/Test/TestDefGrpModel.php | 10 ++ app/Models/Test/TestDefSiteModel.php | 45 +++++++ app/Models/Test/TestMapModel.php | 9 ++ 10 files changed, 144 insertions(+), 117 deletions(-) diff --git a/app/Controllers/PatVisitController.php b/app/Controllers/PatVisitController.php index 180d7d1..35db554 100644 --- a/app/Controllers/PatVisitController.php +++ b/app/Controllers/PatVisitController.php @@ -176,7 +176,7 @@ class PatVisitController extends BaseController { ->join('contact admDoc', 'admDoc.ContactID = patvisitadt.AdmDoc', 'left') ->join('contact cnsDoc', 'cnsDoc.ContactID = patvisitadt.CnsDoc', 'left') ->where('patvisitadt.InternalPVID', $InternalPVID) - ->where('patvisitadt.DelDate IS NULL') + ->where('patvisitadt.DelDate', null) ->orderBy('patvisitadt.CreateDate', 'ASC') ->findAll(); @@ -208,7 +208,7 @@ class PatVisitController extends BaseController { ->join('contact admDoc', 'admDoc.ContactID = patvisitadt.AdmDoc', 'left') ->join('contact cnsDoc', 'cnsDoc.ContactID = patvisitadt.CnsDoc', 'left') ->where('patvisitadt.PVADTID', $PVADTID) - ->where('patvisitadt.DelDate IS NULL') + ->where('patvisitadt.DelDate', null) ->first(); if (empty($row)) { diff --git a/app/Controllers/Test/TestMapController.php b/app/Controllers/Test/TestMapController.php index 18dce7c..23d0701 100644 --- a/app/Controllers/Test/TestMapController.php +++ b/app/Controllers/Test/TestMapController.php @@ -39,7 +39,7 @@ class TestMapController extends BaseController { } public function show($id = null) { - $row = $this->model->where('TestMapID',$id)->where('EndDate IS NULL')->first(); + $row = $this->model->where('TestMapID',$id)->where('EndDate', null)->first(); if (empty($row)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => null ], 200); } $row = ValueSet::transformLabels([$row], [ @@ -83,7 +83,7 @@ class TestMapController extends BaseController { if (!$id) { return $this->failValidationErrors('TestMapID is required.'); } try { - $row = $this->model->where('TestMapID', $id)->where('EndDate IS NULL')->first(); + $row = $this->model->where('TestMapID', $id)->where('EndDate', null)->first(); if (empty($row)) { return $this->respond([ 'status' => 'failed', 'message' => "Data not found or already deleted.", 'data' => null ], 404); } $this->db->transStart(); @@ -93,7 +93,7 @@ class TestMapController extends BaseController { // Soft delete all related details $this->modelDetail->where('TestMapID', $id) - ->where('EndDate IS NULL') + ->where('EndDate', null) ->set('EndDate', date('Y-m-d H:i:s')) ->update(); @@ -189,7 +189,7 @@ class TestMapController extends BaseController { foreach ($ids as $id) { try { - $row = $this->model->where('TestMapID', $id)->where('EndDate IS NULL')->first(); + $row = $this->model->where('TestMapID', $id)->where('EndDate', null)->first(); if (empty($row)) { $results['failed'][] = ['TestMapID' => $id, 'error' => 'Not found or already deleted']; continue; diff --git a/app/Controllers/Test/TestMapDetailController.php b/app/Controllers/Test/TestMapDetailController.php index 4400018..540e961 100644 --- a/app/Controllers/Test/TestMapDetailController.php +++ b/app/Controllers/Test/TestMapDetailController.php @@ -32,7 +32,7 @@ class TestMapDetailController extends BaseController { if ($testMapID) { $rows = $this->model->getDetailsByTestMap($testMapID); } else { - $rows = $this->model->where('EndDate IS NULL')->findAll(); + $rows = $this->model->where('EndDate', null)->findAll(); } if (empty($rows)) { @@ -47,7 +47,7 @@ class TestMapDetailController extends BaseController { return $this->failValidationErrors('TestMapDetailID is required.'); } - $row = $this->model->where('TestMapDetailID', $id)->where('EndDate IS NULL')->first(); + $row = $this->model->where('TestMapDetailID', $id)->where('EndDate', null)->first(); if (empty($row)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => null ], 200); @@ -122,7 +122,7 @@ class TestMapDetailController extends BaseController { } try { - $row = $this->model->where('TestMapDetailID', $id)->where('EndDate IS NULL')->first(); + $row = $this->model->where('TestMapDetailID', $id)->where('EndDate', null)->first(); if (empty($row)) { return $this->respond([ @@ -228,7 +228,7 @@ class TestMapDetailController extends BaseController { foreach ($ids as $id) { try { - $row = $this->model->where('TestMapDetailID', $id)->where('EndDate IS NULL')->first(); + $row = $this->model->where('TestMapDetailID', $id)->where('EndDate', null)->first(); if (empty($row)) { $results['failed'][] = ['TestMapDetailID' => $id, 'error' => 'Not found or already deleted']; diff --git a/app/Controllers/Test/TestsController.php b/app/Controllers/Test/TestsController.php index 8b57e47..b44f457 100644 --- a/app/Controllers/Test/TestsController.php +++ b/app/Controllers/Test/TestsController.php @@ -42,52 +42,16 @@ class TestsController extends BaseController public function index() { - $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'); + $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'), + ]; - $builder = $this->db->table('testdefsite') - ->select( - "testdefsite.TestSiteID, testdefsite.TestSiteCode, testdefsite.TestSiteName, testdefsite.TestType, - testdefsite.SeqScr, testdefsite.SeqRpt, testdefsite.VisibleScr, testdefsite.VisibleRpt, - testdefsite.CountStat, testdefsite.StartDate, testdefsite.EndDate, - COALESCE(testdefsite.DisciplineID, cal.DisciplineID) as DisciplineID, - COALESCE(testdefsite.DepartmentID, cal.DepartmentID) as DepartmentID, - d.DisciplineName, dept.DepartmentName" - ) - ->join('testdefcal cal', 'cal.TestSiteID = testdefsite.TestSiteID AND cal.EndDate IS NULL', 'left') - ->join('discipline d', 'd.DisciplineID = COALESCE(testdefsite.DisciplineID, cal.DisciplineID)', 'left') - ->join('department dept', 'dept.DepartmentID = COALESCE(testdefsite.DepartmentID, cal.DepartmentID)', 'left') - ->where('testdefsite.EndDate IS NULL'); - - if ($siteId) { - $builder->where('testdefsite.SiteID', $siteId); - } - - if ($testType) { - $builder->where('testdefsite.TestType', $testType); - } - - if ($visibleScr !== null) { - $builder->where('testdefsite.VisibleScr', $visibleScr); - } - - if ($visibleRpt !== null) { - $builder->where('testdefsite.VisibleRpt', $visibleRpt); - } - - if ($testSiteName) { - $builder->like('testdefsite.TestSiteName', $testSiteName); - } - - if ($testSiteCode) { - $builder->like('testdefsite.TestSiteCode', $testSiteCode); - } - - $rows = $builder->orderBy('testdefsite.SeqScr', 'ASC')->get()->getResultArray(); + $rows = $this->model->getTestsWithRelations($filters); if (empty($rows)) { return $this->respond([ @@ -133,25 +97,9 @@ class TestsController extends BaseController $typeCode = $row['TestType'] ?? ''; if ($typeCode === 'CALC') { - $row['testdefcal'] = $this->db->table('testdefcal') - ->select('testdefcal.*, d.DisciplineName, dept.DepartmentName') - ->join('discipline d', 'd.DisciplineID=testdefcal.DisciplineID', 'left') - ->join('department dept', 'dept.DepartmentID=testdefcal.DepartmentID', 'left') - ->where('testdefcal.TestSiteID', $id) - ->where('testdefcal.EndDate IS NULL') - ->get()->getResultArray(); + $row['testdefcal'] = $this->modelCal->getByTestSiteID($id); } elseif ($typeCode === 'GROUP') { - $row['testdefgrp'] = $this->db->table('testdefgrp') - ->select('testdefgrp.*, t.TestSiteCode, t.TestSiteName, t.TestType') - ->join('testdefsite t', 't.TestSiteID=testdefgrp.Member', 'left') - ->where('testdefgrp.TestSiteID', $id) - ->where('testdefgrp.EndDate IS NULL') - ->orderBy('testdefgrp.TestGrpID', 'ASC') - ->get()->getResultArray(); - - $row['testdefgrp'] = ValueSet::transformLabels($row['testdefgrp'], [ - 'TestType' => 'test_type', - ]); + $row['testdefgrp'] = $this->modelGrp->getGroupMembers($id); } elseif ($typeCode === 'TITLE') { } else { $row['testdeftech'] = $this->db->table('testdefsite') @@ -168,11 +116,7 @@ class TestsController extends BaseController $resultType = $techData['ResultType'] ?? ''; if (TestValidationService::usesRefNum($resultType, $refType)) { - $refnumData = $this->modelRefNum - ->where('TestSiteID', $id) - ->where('EndDate IS NULL') - ->orderBy('Display', 'ASC') - ->findAll(); + $refnumData = $this->modelRefNum->getActiveByTestSiteID($id); $row['refnum'] = array_map(function ($r) { return [ @@ -198,11 +142,7 @@ class TestsController extends BaseController } if (TestValidationService::usesRefTxt($resultType, $refType)) { - $reftxtData = $this->modelRefTxt - ->where('TestSiteID', $id) - ->where('EndDate IS NULL') - ->orderBy('RefTxtID', 'ASC') - ->findAll(); + $reftxtData = $this->modelRefTxt->getActiveByTestSiteID($id); $row['reftxt'] = array_map(function ($r) { return [ @@ -416,21 +356,15 @@ class TestsController extends BaseController $typeCode = $testType; if (TestValidationService::isCalc($typeCode)) { - $this->db->table('testdefcal') - ->where('TestSiteID', $id) - ->update(['EndDate' => $now]); + $this->modelCal->disableByTestSiteID($id); } elseif (TestValidationService::isGroup($typeCode)) { - $this->db->table('testdefgrp') - ->where('TestSiteID', $id) - ->update(['EndDate' => $now]); + $this->modelGrp->disableByTestSiteID($id); } elseif (TestValidationService::isTechnicalTest($typeCode)) { $this->modelRefNum->where('TestSiteID', $id)->set('EndDate', $now)->update(); $this->modelRefTxt->where('TestSiteID', $id)->set('EndDate', $now)->update(); } - $this->db->table('testmap') - ->where('TestSiteID', $id) - ->update(['EndDate' => $now]); + $this->modelMap->disableByTestSiteID($id); $this->db->transComplete(); @@ -605,10 +539,7 @@ class TestsController extends BaseController ]; if ($action === 'update') { - $exists = $this->db->table('testdefcal') - ->where('TestSiteID', $testSiteID) - ->where('EndDate IS NULL') - ->get()->getRowArray(); + $exists = $this->modelCal->existsByTestSiteID($testSiteID); if ($exists) { $this->modelCal->update($exists['TestCalID'], $calcData); @@ -623,9 +554,7 @@ class TestsController extends BaseController private function saveGroupDetails($testSiteID, $data, $input, $action) { if ($action === 'update') { - $this->db->table('testdefgrp') - ->where('TestSiteID', $testSiteID) - ->update(['EndDate' => date('Y-m-d H:i:s')]); + $this->modelGrp->disableByTestSiteID($testSiteID); } $members = $data['members'] ?? ($input['Members'] ?? []); @@ -647,20 +576,17 @@ class TestsController extends BaseController { if ($action === 'update') { $existingMaps = $this->modelMap->where('TestSiteID', $testSiteID) - ->where('EndDate IS NULL') + ->where('EndDate', null) ->findAll(); foreach ($existingMaps as $existingMap) { $this->modelMapDetail->where('TestMapID', $existingMap['TestMapID']) - ->where('EndDate IS NULL') + ->where('EndDate', null) ->set('EndDate', date('Y-m-d H:i:s')) ->update(); } - $this->db->table('testmap') - ->where('TestSiteID', $testSiteID) - ->where('EndDate IS NULL') - ->update(['EndDate' => date('Y-m-d H:i:s')]); + $this->modelMap->disableByTestSiteID($testSiteID); } if (is_array($mappings)) { @@ -690,4 +616,4 @@ class TestsController extends BaseController } } } -} \ No newline at end of file +} diff --git a/app/Models/RefRange/RefNumModel.php b/app/Models/RefRange/RefNumModel.php index e33db9c..c2bbe61 100644 --- a/app/Models/RefRange/RefNumModel.php +++ b/app/Models/RefRange/RefNumModel.php @@ -36,4 +36,14 @@ class RefNumModel extends BaseModel protected $updatedField = ''; protected $useSoftDeletes = true; protected $deletedField = "EndDate"; + + /** + * Get active numeric reference ranges for a test + */ + public function getActiveByTestSiteID($testSiteID) { + return $this->where('TestSiteID', $testSiteID) + ->where('EndDate', null) + ->orderBy('Display', 'ASC') + ->findAll(); + } } diff --git a/app/Models/RefRange/RefTxtModel.php b/app/Models/RefRange/RefTxtModel.php index 054df49..b61c7cf 100644 --- a/app/Models/RefRange/RefTxtModel.php +++ b/app/Models/RefRange/RefTxtModel.php @@ -30,4 +30,14 @@ class RefTxtModel extends BaseModel protected $updatedField = ''; protected $useSoftDeletes = true; protected $deletedField = "EndDate"; + + /** + * Get active text reference ranges for a test + */ + public function getActiveByTestSiteID($testSiteID) { + return $this->where('TestSiteID', $testSiteID) + ->where('EndDate', null) + ->orderBy('RefTxtID', 'ASC') + ->findAll(); + } } diff --git a/app/Models/Test/TestDefCalModel.php b/app/Models/Test/TestDefCalModel.php index 492ad34..d32b51b 100644 --- a/app/Models/Test/TestDefCalModel.php +++ b/app/Models/Test/TestDefCalModel.php @@ -30,18 +30,35 @@ class TestDefCalModel extends BaseModel { protected $deletedField = "EndDate"; /** - * Get calculation details for a test + * Get calculation details for a test with relations */ - public function getCalcDetails($testSiteID) { - $db = \Config\Database::connect(); - - return $db->table('testdefcal') - ->select('testdefcal.*, d.DisciplineName, dept.DepartmentName') - ->join('discipline d', 'd.DisciplineID=testdefcal.DisciplineID', 'left') - ->join('department dept', 'dept.DepartmentID=testdefcal.DepartmentID', 'left') - ->where('testdefcal.TestSiteID', $testSiteID) - ->where('testdefcal.EndDate IS NULL') - ->get()->getResultArray(); + public function getByTestSiteID($testSiteID) { + return $this->db->table('testdefcal') + ->select('testdefcal.*, d.DisciplineName, dept.DepartmentName') + ->join('discipline d', 'd.DisciplineID=testdefcal.DisciplineID', 'left') + ->join('department dept', 'dept.DepartmentID=testdefcal.DepartmentID', 'left') + ->where('testdefcal.TestSiteID', $testSiteID) + ->where('testdefcal.EndDate IS NULL') + ->get()->getResultArray(); + } + + /** + * Check if calculation exists for a test + */ + public function existsByTestSiteID($testSiteID) { + return $this->db->table('testdefcal') + ->where('TestSiteID', $testSiteID) + ->where('EndDate IS NULL') + ->get()->getRowArray(); + } + + /** + * Disable calculation by TestSiteID + */ + public function disableByTestSiteID($testSiteID) { + $this->db->table('testdefcal') + ->where('TestSiteID', $testSiteID) + ->update(['EndDate' => date('Y-m-d H:i:s')]); } /** diff --git a/app/Models/Test/TestDefGrpModel.php b/app/Models/Test/TestDefGrpModel.php index 2a4f951..77311ce 100644 --- a/app/Models/Test/TestDefGrpModel.php +++ b/app/Models/Test/TestDefGrpModel.php @@ -46,4 +46,14 @@ class TestDefGrpModel extends BaseModel { ->where('testdefgrp.EndDate IS NULL') ->findAll(); } + + + /** + * Disable group members by TestSiteID + */ + public function disableByTestSiteID($testSiteID) { + $this->db->table('testdefgrp') + ->where('TestSiteID', $testSiteID) + ->update(['EndDate' => date('Y-m-d H:i:s')]); + } } diff --git a/app/Models/Test/TestDefSiteModel.php b/app/Models/Test/TestDefSiteModel.php index e6dae91..2f88ee2 100644 --- a/app/Models/Test/TestDefSiteModel.php +++ b/app/Models/Test/TestDefSiteModel.php @@ -78,6 +78,51 @@ class TestDefSiteModel extends BaseModel { ]; } + /** + * Get tests list with discipline and department info + */ + public function getTestsWithRelations($filters = []) { + $builder = $this->db->table('testdefsite') + ->select( + "testdefsite.TestSiteID, testdefsite.TestSiteCode, testdefsite.TestSiteName, testdefsite.TestType, + testdefsite.SeqScr, testdefsite.SeqRpt, testdefsite.VisibleScr, testdefsite.VisibleRpt, + testdefsite.CountStat, testdefsite.StartDate, testdefsite.EndDate, + COALESCE(testdefsite.DisciplineID, cal.DisciplineID) as DisciplineID, + COALESCE(testdefsite.DepartmentID, cal.DepartmentID) as DepartmentID, + d.DisciplineName, dept.DepartmentName" + ) + ->join('testdefcal cal', 'cal.TestSiteID = testdefsite.TestSiteID AND cal.EndDate IS NULL', 'left') + ->join('discipline d', 'd.DisciplineID = COALESCE(testdefsite.DisciplineID, cal.DisciplineID)', 'left') + ->join('department dept', 'dept.DepartmentID = COALESCE(testdefsite.DepartmentID, cal.DepartmentID)', 'left') + ->where('testdefsite.EndDate IS NULL'); + + if (!empty($filters['SiteID'])) { + $builder->where('testdefsite.SiteID', $filters['SiteID']); + } + + if (!empty($filters['TestType'])) { + $builder->where('testdefsite.TestType', $filters['TestType']); + } + + if (isset($filters['VisibleScr'])) { + $builder->where('testdefsite.VisibleScr', $filters['VisibleScr']); + } + + if (isset($filters['VisibleRpt'])) { + $builder->where('testdefsite.VisibleRpt', $filters['VisibleRpt']); + } + + if (!empty($filters['TestSiteName'])) { + $builder->like('testdefsite.TestSiteName', $filters['TestSiteName']); + } + + if (!empty($filters['TestSiteCode'])) { + $builder->like('testdefsite.TestSiteCode', $filters['TestSiteCode']); + } + + return $builder->orderBy('testdefsite.SeqScr', 'ASC')->get()->getResultArray(); + } + public function getTest($TestSiteID) { $db = \Config\Database::connect(); diff --git a/app/Models/Test/TestMapModel.php b/app/Models/Test/TestMapModel.php index 01c613c..186efa9 100644 --- a/app/Models/Test/TestMapModel.php +++ b/app/Models/Test/TestMapModel.php @@ -104,4 +104,13 @@ class TestMapModel extends BaseModel { ->where('testmapdetail.EndDate IS NULL') ->findAll(); } + + /** + * Disable test mappings by TestSiteID + */ + public function disableByTestSiteID($testSiteID) { + $this->db->table('testmap') + ->where('TestSiteID', $testSiteID) + ->update(['EndDate' => date('Y-m-d H:i:s')]); + } }