2025-10-14 10:48:20 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers\Specimen;
|
|
|
|
|
|
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
|
use App\Controllers\BaseController;
|
2026-01-28 17:31:00 +07:00
|
|
|
use App\Libraries\ValueSet;
|
2025-10-14 10:48:20 +07:00
|
|
|
use App\Models\Specimen\ContainerDefModel;
|
|
|
|
|
|
2026-01-05 16:55:34 +07:00
|
|
|
class ContainerDefController extends BaseController {
|
2025-12-02 07:09:24 +07:00
|
|
|
use ResponseTrait;
|
2025-10-14 10:48:20 +07:00
|
|
|
|
2025-12-02 07:09:24 +07:00
|
|
|
protected $db;
|
|
|
|
|
protected $model;
|
|
|
|
|
protected $rules;
|
2025-10-14 10:48:20 +07:00
|
|
|
|
2025-12-02 07:09:24 +07:00
|
|
|
public function __construct() {
|
|
|
|
|
$this->db = \Config\Database::connect();
|
|
|
|
|
$this->model = new ContainerDefModel();
|
|
|
|
|
$this->rules = [
|
|
|
|
|
'ConCode' => 'required|max_length[50]',
|
|
|
|
|
'ConName' => 'required|max_length[50]'
|
|
|
|
|
];
|
|
|
|
|
}
|
2025-10-14 10:48:20 +07:00
|
|
|
|
2025-12-02 07:09:24 +07:00
|
|
|
public function index() {
|
|
|
|
|
try {
|
|
|
|
|
$filter = [
|
|
|
|
|
'ConCode' => $this->request->getVar('ConCode'),
|
|
|
|
|
'ConName' => $this->request->getVar('ConName')
|
|
|
|
|
];
|
|
|
|
|
$rows = $this->model->getContainers($filter);
|
2026-01-28 17:31:00 +07:00
|
|
|
|
|
|
|
|
// Transform ConCategory, CapColor, ConSize
|
|
|
|
|
foreach ($rows as &$row) {
|
|
|
|
|
if (isset($row['ConCategory'])) {
|
|
|
|
|
$row['conCategory'] = $row['ConCategory'];
|
|
|
|
|
$row['conCategoryLabel'] = ValueSet::getLabel('container_class', $row['ConCategory']) ?? '';
|
|
|
|
|
unset($row['ConCategory']);
|
|
|
|
|
}
|
|
|
|
|
if (isset($row['CapColor'])) {
|
|
|
|
|
$row['capColor'] = $row['CapColor'];
|
|
|
|
|
$row['capColorLabel'] = ValueSet::getLabel('container_cap_color', $row['CapColor']) ?? '';
|
|
|
|
|
unset($row['CapColor']);
|
|
|
|
|
}
|
|
|
|
|
if (isset($row['ConSize'])) {
|
|
|
|
|
$row['conSize'] = $row['ConSize'];
|
|
|
|
|
$row['conSizeLabel'] = ValueSet::getLabel('container_size', $row['ConSize']) ?? '';
|
|
|
|
|
unset($row['ConSize']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-02 07:09:24 +07:00
|
|
|
return $this->respond([ 'status' => 'success', 'message'=> "data fetched successfully", 'data' => $rows ], 200);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->failServerError('Exception : '.$e->getMessage());
|
2025-10-14 10:48:20 +07:00
|
|
|
}
|
2025-12-02 07:09:24 +07:00
|
|
|
}
|
2025-10-14 10:48:20 +07:00
|
|
|
|
2025-12-02 07:09:24 +07:00
|
|
|
public function show($ConDefID) {
|
|
|
|
|
try {
|
2025-12-29 12:55:31 +07:00
|
|
|
$row = $this->model->getContainer($ConDefID);
|
|
|
|
|
if (empty($row)) {
|
|
|
|
|
return $this->respond([ 'status' => 'success', 'message'=> "data not found", 'data' => null ], 200);
|
|
|
|
|
}
|
2026-01-28 17:31:00 +07:00
|
|
|
|
|
|
|
|
// Transform ConCategory, CapColor, ConSize
|
|
|
|
|
if (isset($row['ConCategory'])) {
|
|
|
|
|
$row['conCategory'] = $row['ConCategory'];
|
|
|
|
|
$row['conCategoryLabel'] = ValueSet::getLabel('container_class', $row['ConCategory']) ?? '';
|
|
|
|
|
unset($row['ConCategory']);
|
|
|
|
|
}
|
|
|
|
|
if (isset($row['CapColor'])) {
|
|
|
|
|
$row['capColor'] = $row['CapColor'];
|
|
|
|
|
$row['capColorLabel'] = ValueSet::getLabel('container_cap_color', $row['CapColor']) ?? '';
|
|
|
|
|
unset($row['CapColor']);
|
|
|
|
|
}
|
|
|
|
|
if (isset($row['ConSize'])) {
|
|
|
|
|
$row['conSize'] = $row['ConSize'];
|
|
|
|
|
$row['conSizeLabel'] = ValueSet::getLabel('container_size', $row['ConSize']) ?? '';
|
|
|
|
|
unset($row['ConSize']);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-29 12:55:31 +07:00
|
|
|
return $this->respond([ 'status' => 'success', 'message'=> "data fetched successfully", 'data' => $row ], 200);
|
2025-12-02 07:09:24 +07:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->failServerError('Exception : '.$e->getMessage());
|
2025-10-14 10:48:20 +07:00
|
|
|
}
|
2025-12-02 07:09:24 +07:00
|
|
|
}
|
2025-10-14 10:48:20 +07:00
|
|
|
|
2025-12-02 07:09:24 +07:00
|
|
|
public function create() {
|
|
|
|
|
$input = $this->request->getJSON(true);
|
|
|
|
|
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); }
|
|
|
|
|
try {
|
|
|
|
|
$ConDefID = $this->model->insert($input);
|
|
|
|
|
return $this->respondCreated([ 'status' => 'success', 'message' => "data $ConDefID created successfully" ]);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
2025-10-14 10:48:20 +07:00
|
|
|
}
|
2025-12-02 07:09:24 +07:00
|
|
|
}
|
2025-10-14 10:48:20 +07:00
|
|
|
|
2025-12-02 07:09:24 +07:00
|
|
|
public function update() {
|
|
|
|
|
$input = $this->request->getJSON(true);
|
|
|
|
|
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); }
|
|
|
|
|
try {
|
|
|
|
|
$ConDefID = $this->model->update($input['ConDefID'], $input);
|
|
|
|
|
return $this->respondCreated([ 'status' => 'success', 'message' => "data $ConDefID updated successfully" ]);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
2025-10-14 10:48:20 +07:00
|
|
|
}
|
2025-12-02 07:09:24 +07:00
|
|
|
}
|
2025-10-14 10:48:20 +07:00
|
|
|
|
2026-01-05 16:55:34 +07:00
|
|
|
}
|