clqms-be/app/Models/Location/LocationModel.php
mahdahar bb7df6b70c 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

94 lines
3.2 KiB
PHP

<?php
namespace App\Models\Location;
use App\Models\BaseModel;
use App\Libraries\ValueSet;
class LocationModel extends BaseModel {
protected $table = 'location';
protected $primaryKey = 'LocationID';
protected $allowedFields = ['SiteID', 'LocCode', 'Parent', 'LocFull', 'Description', 'LocType', 'CreateDate', 'EndDate'];
protected $useTimestamps = true;
protected $createdField = 'CreateDate';
protected $updatedField = '';
protected $useSoftDeletes = true;
protected $deletedField = 'EndDate';
public function getLocations($LocCode, $LocName) {
$sql = $this->select("LocationID, LocCode, Parent, LocFull, LocType");
if($LocName != '') { $sql->like('LocFull', $LocName, 'both'); }
if($LocCode != '') { $sql->like('LocCode', $LocCode, 'both'); }
$rows = $sql->findAll();
$rows = ValueSet::transformLabels($rows, [
'LocType' => 'location_type',
]);
return $rows;
}
public function getLocation($LocationID) {
$row = $this->select("location.*, la.Street1, la.Street2, la.PostCode, la.GeoLocationSystem, la.GeoLocationData,
prop.AreaGeoID as ProvinceID, prop.AreaName as Province, city.AreaGeoID as CityID, city.AreaName as City, site.SiteID, site.SiteName")
->join("locationaddress la", "location.LocationID=la.LocationID", "left")
->join("areageo prop", "la.Province=prop.AreaGeoID", "left")
->join("areageo city", "la.City=city.AreaGeoID", "left")
->join("site", "site.SiteID=location.SiteID", "left")
->where('location.LocationID', (int) $LocationID)->first();
if (!$row) return null;
$row = ValueSet::transformLabels([$row], [
'LocType' => 'location_type',
])[0];
return $row;
}
public function saveLocation(array $data): array {
$modelAddress = new \App\Models\Location\LocationAddressModel();
$db = \Config\Database::connect();
$db->transBegin();
try {
if (!empty($data['LocationID'])) {
$LocationID = $data['LocationID'];
$this->update($LocationID, $data);
$modelAddress->update($LocationID, $data);
} else {
$LocationID = $this->insert($data, true);
$data['LocationID'] = $LocationID;
$modelAddress->insert($data);
}
if ($db->transStatus() === false) {
$error = $db->error();
$db->transRollback();
throw new \Exception($error['message'] ?? 'Transaction failed');
}
$db->transCommit();
return [ 'status' => 'success', 'LocationID' => $LocationID ];
} catch (\Throwable $e) {
$db->transRollback();
throw $e;
}
}
public function deleteLocation(array $data): array {
$modelAddress = new \App\Models\Location\LocationAddressModel();
$db = \Config\Database::connect();
$db->transBegin();
try {
$LocationID = $data['LocationID'];
$this->delete($LocationID);
$modelAddress->delete($LocationID);
if ($db->transStatus() === false) {
$error = $db->error();
$db->transRollback();
throw new \Exception($error['message'] ?? 'Transaction failed');
}
$db->transCommit();
return [ 'status' => 'success', 'LocationID' => $LocationID ];
} catch (\Throwable $e) {
$db->transRollback();
throw $e;
}
}
}