2025-09-19 15:22:25 +07:00
|
|
|
<?php
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
|
use CodeIgniter\Controller;
|
2026-01-28 17:31:00 +07:00
|
|
|
use App\Libraries\ValueSet;
|
|
|
|
|
use App\Models\OrderTestModel;
|
2026-01-12 16:53:41 +07:00
|
|
|
use App\Models\Patient\PatientModel;
|
2026-01-28 17:31:00 +07:00
|
|
|
use App\Models\Patient\PatVisitModel;
|
2025-09-19 15:22:25 +07:00
|
|
|
|
2026-01-05 16:55:34 +07:00
|
|
|
class OrderTestController extends Controller {
|
2025-09-19 15:22:25 +07:00
|
|
|
use ResponseTrait;
|
|
|
|
|
|
2026-01-12 16:53:41 +07:00
|
|
|
protected $db;
|
|
|
|
|
protected $model;
|
|
|
|
|
protected $patientModel;
|
|
|
|
|
protected $visitModel;
|
|
|
|
|
protected $rules;
|
|
|
|
|
|
2025-09-19 15:22:25 +07:00
|
|
|
public function __construct() {
|
|
|
|
|
$this->db = \Config\Database::connect();
|
2026-01-12 16:53:41 +07:00
|
|
|
$this->model = new OrderTestModel();
|
|
|
|
|
$this->patientModel = new PatientModel();
|
|
|
|
|
$this->visitModel = new PatVisitModel();
|
|
|
|
|
$this->rules = [
|
|
|
|
|
'InternalPID' => 'required|is_natural'
|
2025-09-19 15:22:25 +07:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function index() {
|
2026-01-12 16:53:41 +07:00
|
|
|
$internalPID = $this->request->getVar('InternalPID');
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if ($internalPID) {
|
|
|
|
|
$rows = $this->model->getOrdersByPatient($internalPID);
|
|
|
|
|
} else {
|
|
|
|
|
$rows = $this->db->table('ordertest')
|
|
|
|
|
->where('DelDate', null)
|
|
|
|
|
->orderBy('OrderDateTime', 'DESC')
|
|
|
|
|
->get()
|
|
|
|
|
->getResultArray();
|
|
|
|
|
}
|
2025-09-19 15:22:25 +07:00
|
|
|
|
2026-01-28 17:31:00 +07:00
|
|
|
// Transform Priority and OrderStatus
|
|
|
|
|
foreach ($rows as &$row) {
|
|
|
|
|
if (isset($row['Priority'])) {
|
|
|
|
|
$row['priority'] = $row['Priority'];
|
|
|
|
|
$row['priorityLabel'] = ValueSet::getLabel('priority', $row['Priority']) ?? '';
|
|
|
|
|
unset($row['Priority']);
|
|
|
|
|
}
|
|
|
|
|
if (isset($row['OrderStatus'])) {
|
|
|
|
|
$row['orderStatus'] = $row['OrderStatus'];
|
|
|
|
|
$row['orderStatusLabel'] = ValueSet::getLabel('order_status', $row['OrderStatus']) ?? '';
|
|
|
|
|
unset($row['OrderStatus']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 15:22:25 +07:00
|
|
|
return $this->respond([
|
2026-01-12 16:53:41 +07:00
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => 'Data fetched successfully',
|
|
|
|
|
'data' => $rows
|
2025-09-19 15:22:25 +07:00
|
|
|
], 200);
|
2026-01-12 16:53:41 +07:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
2025-09-19 15:22:25 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-12 16:53:41 +07:00
|
|
|
public function show($orderID = null) {
|
|
|
|
|
try {
|
|
|
|
|
$row = $this->model->getOrder($orderID);
|
|
|
|
|
if (empty($row)) {
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => 'Data not found.',
|
|
|
|
|
'data' => null
|
|
|
|
|
], 200);
|
|
|
|
|
}
|
2026-01-28 17:31:00 +07:00
|
|
|
|
|
|
|
|
// Transform Priority and OrderStatus
|
|
|
|
|
if (isset($row['Priority'])) {
|
|
|
|
|
$row['priority'] = $row['Priority'];
|
|
|
|
|
$row['priorityLabel'] = ValueSet::getLabel('priority', $row['Priority']) ?? '';
|
|
|
|
|
unset($row['Priority']);
|
|
|
|
|
}
|
|
|
|
|
if (isset($row['OrderStatus'])) {
|
|
|
|
|
$row['orderStatus'] = $row['OrderStatus'];
|
|
|
|
|
$row['orderStatusLabel'] = ValueSet::getLabel('order_status', $row['OrderStatus']) ?? '';
|
|
|
|
|
unset($row['OrderStatus']);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 15:22:25 +07:00
|
|
|
return $this->respond([
|
2026-01-12 16:53:41 +07:00
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => 'Data fetched successfully',
|
|
|
|
|
'data' => $row
|
2025-09-19 15:22:25 +07:00
|
|
|
], 200);
|
2026-01-12 16:53:41 +07:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
2025-09-19 15:22:25 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function create() {
|
2026-01-12 16:53:41 +07:00
|
|
|
$input = $this->request->getJSON(true);
|
2025-09-19 15:22:25 +07:00
|
|
|
|
2026-01-12 16:53:41 +07:00
|
|
|
if (!$this->validateData($input, $this->rules)) {
|
|
|
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
|
|
|
}
|
2025-09-19 15:22:25 +07:00
|
|
|
|
2026-01-12 16:53:41 +07:00
|
|
|
try {
|
|
|
|
|
if (!$this->patientModel->find($input['InternalPID'])) {
|
|
|
|
|
return $this->failValidationErrors(['InternalPID' => 'Patient not found']);
|
2025-09-19 15:22:25 +07:00
|
|
|
}
|
|
|
|
|
|
2026-01-12 16:53:41 +07:00
|
|
|
if (!empty($input['PatVisitID'])) {
|
|
|
|
|
$visit = $this->visitModel->find($input['PatVisitID']);
|
|
|
|
|
if (!$visit) {
|
|
|
|
|
return $this->failValidationErrors(['PatVisitID' => 'Visit not found']);
|
|
|
|
|
}
|
2025-09-19 15:22:25 +07:00
|
|
|
}
|
|
|
|
|
|
2026-01-12 16:53:41 +07:00
|
|
|
$orderID = $this->model->createOrder($input);
|
2025-09-19 15:22:25 +07:00
|
|
|
|
|
|
|
|
return $this->respondCreated([
|
2026-01-12 16:53:41 +07:00
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => 'Order created successfully',
|
|
|
|
|
'data' => ['OrderID' => $orderID]
|
2025-09-19 15:22:25 +07:00
|
|
|
], 201);
|
2026-01-12 16:53:41 +07:00
|
|
|
} catch (\Exception $e) {
|
2025-09-19 15:22:25 +07:00
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update() {
|
2026-01-12 16:53:41 +07:00
|
|
|
$input = $this->request->getJSON(true);
|
2025-09-19 15:22:25 +07:00
|
|
|
|
2026-01-12 16:53:41 +07:00
|
|
|
if (empty($input['OrderID'])) {
|
|
|
|
|
return $this->failValidationErrors(['OrderID' => 'OrderID is required']);
|
|
|
|
|
}
|
2025-09-19 15:22:25 +07:00
|
|
|
|
2026-01-12 16:53:41 +07:00
|
|
|
try {
|
|
|
|
|
$order = $this->model->getOrder($input['OrderID']);
|
|
|
|
|
if (!$order) {
|
|
|
|
|
return $this->failNotFound('Order not found');
|
2025-09-19 15:22:25 +07:00
|
|
|
}
|
|
|
|
|
|
2026-01-12 16:53:41 +07:00
|
|
|
$updateData = [];
|
|
|
|
|
if (isset($input['Priority'])) $updateData['Priority'] = $input['Priority'];
|
|
|
|
|
if (isset($input['OrderStatus'])) $updateData['OrderStatus'] = $input['OrderStatus'];
|
|
|
|
|
if (isset($input['OrderingProvider'])) $updateData['OrderingProvider'] = $input['OrderingProvider'];
|
|
|
|
|
if (isset($input['DepartmentID'])) $updateData['DepartmentID'] = $input['DepartmentID'];
|
|
|
|
|
if (isset($input['WorkstationID'])) $updateData['WorkstationID'] = $input['WorkstationID'];
|
2025-09-19 15:22:25 +07:00
|
|
|
|
2026-01-12 16:53:41 +07:00
|
|
|
if (!empty($updateData)) {
|
|
|
|
|
$this->model->update($input['OrderID'], $updateData);
|
2025-09-19 15:22:25 +07:00
|
|
|
}
|
|
|
|
|
|
2026-01-12 16:53:41 +07:00
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => 'Order updated successfully',
|
|
|
|
|
'data' => $this->model->getOrder($input['OrderID'])
|
|
|
|
|
], 200);
|
|
|
|
|
} catch (\Exception $e) {
|
2025-09-19 15:22:25 +07:00
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function delete() {
|
2026-01-12 16:53:41 +07:00
|
|
|
$input = $this->request->getJSON(true);
|
|
|
|
|
$orderID = $input['OrderID'] ?? null;
|
2025-09-19 15:22:25 +07:00
|
|
|
|
2026-01-12 16:53:41 +07:00
|
|
|
if (empty($orderID)) {
|
|
|
|
|
return $this->failValidationErrors(['OrderID' => 'OrderID is required']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$order = $this->model->getOrder($orderID);
|
|
|
|
|
if (!$order) {
|
|
|
|
|
return $this->failNotFound('Order not found');
|
2025-09-19 15:22:25 +07:00
|
|
|
}
|
|
|
|
|
|
2026-01-12 16:53:41 +07:00
|
|
|
$this->model->softDelete($orderID);
|
2025-09-19 15:22:25 +07:00
|
|
|
|
|
|
|
|
return $this->respondDeleted([
|
|
|
|
|
'status' => 'success',
|
2026-01-12 16:53:41 +07:00
|
|
|
'message' => 'Order deleted successfully'
|
2025-09-19 15:22:25 +07:00
|
|
|
]);
|
2026-01-12 16:53:41 +07:00
|
|
|
} catch (\Exception $e) {
|
2025-09-19 15:22:25 +07:00
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-12 16:53:41 +07:00
|
|
|
public function updateStatus() {
|
|
|
|
|
$input = $this->request->getJSON(true);
|
|
|
|
|
|
|
|
|
|
if (empty($input['OrderID']) || empty($input['OrderStatus'])) {
|
|
|
|
|
return $this->failValidationErrors(['error' => 'OrderID and OrderStatus are required']);
|
2025-09-19 15:22:25 +07:00
|
|
|
}
|
|
|
|
|
|
2026-01-12 16:53:41 +07:00
|
|
|
$validStatuses = ['ORD', 'SCH', 'ANA', 'VER', 'REV', 'REP'];
|
|
|
|
|
if (!in_array($input['OrderStatus'], $validStatuses)) {
|
|
|
|
|
return $this->failValidationErrors(['OrderStatus' => 'Invalid status. Valid: ' . implode(', ', $validStatuses)]);
|
|
|
|
|
}
|
2025-09-19 15:22:25 +07:00
|
|
|
|
2026-01-12 16:53:41 +07:00
|
|
|
try {
|
|
|
|
|
$order = $this->model->getOrder($input['OrderID']);
|
|
|
|
|
if (!$order) {
|
|
|
|
|
return $this->failNotFound('Order not found');
|
|
|
|
|
}
|
2025-09-19 15:22:25 +07:00
|
|
|
|
2026-01-12 16:53:41 +07:00
|
|
|
$this->model->updateStatus($input['OrderID'], $input['OrderStatus']);
|
2025-09-19 15:22:25 +07:00
|
|
|
|
2026-01-12 16:53:41 +07:00
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => 'Order status updated successfully',
|
|
|
|
|
'data' => $this->model->getOrder($input['OrderID'])
|
|
|
|
|
], 200);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
2025-09-19 15:22:25 +07:00
|
|
|
}
|
2026-01-05 16:55:34 +07:00
|
|
|
}
|