2025-09-10 16:54:33 +07:00
|
|
|
<?php
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
|
use CodeIgniter\Controller;
|
|
|
|
|
use CodeIgniter\Database\RawSql;
|
|
|
|
|
|
|
|
|
|
class Location extends Controller {
|
|
|
|
|
use ResponseTrait;
|
|
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
$this->db = \Config\Database::connect();
|
|
|
|
|
$this->now = date('Y-m-d H:i:s');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function index() {
|
2025-09-11 11:09:04 +07:00
|
|
|
$rows = $this->db->table('location')
|
|
|
|
|
->select("location.LocationID, LocCode, Parent, LocFull, Description, Street1, Street2, City, Province, PostCode, GeoLocationSystem, GeoLocationData")
|
|
|
|
|
->join("locationaddress", "location.LocationID=locationaddress.LocationID")
|
|
|
|
|
->get()->getRowArray();
|
|
|
|
|
|
|
|
|
|
if (empty($rows)) {
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => "Location no Data.",
|
|
|
|
|
'data' => [],
|
|
|
|
|
], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message'=> "Locations fetched successfully",
|
|
|
|
|
'data' => $rows,
|
|
|
|
|
], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function show($LocationID = null) {
|
|
|
|
|
$rows = $this->db->table('location')
|
|
|
|
|
->select("location.LocationID, LocCode, Parent, LocFull, Description, Street1, Street2, City, Province, PostCode, GeoLocationSystem, GeoLocationData")
|
|
|
|
|
->join("locationaddress", "location.LocationID=locationaddress.LocationID")
|
|
|
|
|
->where('location.LocationID', (int) $LocationID)
|
|
|
|
|
->get()->getResultArray();
|
|
|
|
|
|
|
|
|
|
if (empty($rows)) {
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => "Patient with ID $LocationID not found.",
|
|
|
|
|
'data' => [],
|
|
|
|
|
], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message'=> "Locations fetched successfully",
|
|
|
|
|
'data' => $rows,
|
|
|
|
|
], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function create() {
|
|
|
|
|
try {
|
|
|
|
|
$input = $this->request->getJSON(true);
|
|
|
|
|
$now = date('Y-m-d H:i:s');
|
|
|
|
|
|
|
|
|
|
// Prepare data
|
|
|
|
|
$dataLocation = $this->prepareLocationData($input, $now, 'create');
|
|
|
|
|
$dataLocationAddress = $this->prepareLocationAddressData($input, $now, 'create');
|
|
|
|
|
|
|
|
|
|
// Validation rules
|
|
|
|
|
$rulesLocation = [
|
|
|
|
|
'LocCode' => 'required|is_unique[location.LocCode]|max_length[6]',
|
|
|
|
|
'LocFull' => 'required',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (!$this->validateData($dataLocation, $rulesLocation)) {
|
|
|
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start transaction
|
|
|
|
|
$this->db->transStart();
|
|
|
|
|
|
|
|
|
|
// Insert location
|
|
|
|
|
$this->db->table('location')->insert($dataLocation);
|
|
|
|
|
$newLocationID = $this->db->insertID();
|
|
|
|
|
|
|
|
|
|
// Insert address if available
|
|
|
|
|
if (!empty($dataLocationAddress)) {
|
|
|
|
|
$dataLocationAddress['LocationID'] = $newLocationID;
|
|
|
|
|
$this->db->table('locationaddress')->insert($dataLocationAddress);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Complete transaction
|
|
|
|
|
$this->db->transComplete();
|
|
|
|
|
|
|
|
|
|
if ($this->db->transStatus() === false) {
|
|
|
|
|
$dbError = $this->db->error();
|
|
|
|
|
return $this->failServerError(
|
|
|
|
|
'Failed to create location data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown database error')
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->respondCreated([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => 'Location created successfully',
|
|
|
|
|
'data' => $newLocationID,
|
|
|
|
|
], 201);
|
|
|
|
|
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
// Ensure rollback if something goes wrong
|
|
|
|
|
if ($this->db->transStatus() !== false) {
|
|
|
|
|
$this->db->transRollback();
|
|
|
|
|
}
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update($LocationID = null) {
|
|
|
|
|
try {
|
|
|
|
|
$input = $this->request->getJSON(true);
|
|
|
|
|
$now = $this->now;
|
|
|
|
|
|
|
|
|
|
// Prepare data
|
|
|
|
|
$dataLocation = $this->prepareLocationData($input, $now, 'update');
|
|
|
|
|
$dataLocationAddress = $this->prepareLocationAddressData($input, $now, 'update');
|
|
|
|
|
|
|
|
|
|
// Validation rules
|
|
|
|
|
$rulesLocation = [
|
|
|
|
|
'LocCode' => 'required|is_unique[location.LocCode]|max_length[6]',
|
|
|
|
|
'LocFull' => 'required',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (!$this->validateData($dataLocation, $rulesLocation)) {
|
|
|
|
|
return $this->failValidationErrors( $this->validator->getErrors());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start transaction
|
|
|
|
|
$this->db->transStart();
|
|
|
|
|
|
|
|
|
|
// Insert location
|
|
|
|
|
$this->db->table('location')->where('LocationID', $LocationID)->update($dataLocation);
|
|
|
|
|
|
|
|
|
|
// Insert address if available
|
|
|
|
|
if (!empty($dataLocationAddress)) {
|
|
|
|
|
$dataLocationAddress['LocationID'] = $LocationID;
|
|
|
|
|
$this->db->table('locationaddress')->upsert($dataLocationAddress);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Complete transaction
|
|
|
|
|
$this->db->transComplete();
|
|
|
|
|
|
|
|
|
|
if ($this->db->transStatus() === false) {
|
|
|
|
|
$dbError = $this->db->error();
|
|
|
|
|
return $this->failServerError(
|
|
|
|
|
'Failed to update location data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown database error')
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->respondCreated([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => 'Location updated successfully',
|
|
|
|
|
'data' => $LocationID,
|
|
|
|
|
], 201);
|
|
|
|
|
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
// Ensure rollback if something goes wrong
|
|
|
|
|
if ($this->db->transStatus() !== false) {
|
|
|
|
|
$this->db->transRollback();
|
|
|
|
|
}
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function delete($LocationID = null) {
|
|
|
|
|
try {
|
|
|
|
|
if (!$LocationID) {
|
|
|
|
|
return $this->failValidationError('LocationID is required.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$location = $this->db->table('location')->where('LocationID', $LocationID)->get()->getRow();
|
|
|
|
|
if (!$location) {
|
|
|
|
|
return $this->failNotFound("LocationID with {$LocationID} not found.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->db->table('location')->where('LocationID', $LocationID)->update(['DelDate' => $this->now()]);
|
|
|
|
|
|
|
|
|
|
return $this->respondDeleted([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => "Location with {$LocationID} deleted successfully."
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
// Ensure rollback if something goes wrong
|
|
|
|
|
if ($this->db->transStatus() !== false) {
|
|
|
|
|
$this->db->transRollback();
|
|
|
|
|
}
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function prepareLocationData(array $input, string $now, string $mode = 'create'): array {
|
|
|
|
|
$LinkTo = null;
|
|
|
|
|
if (!empty($input['LinkTo'])) {
|
|
|
|
|
$ids = array_column($input['LinkTo'], 'InternalPID');
|
|
|
|
|
$LinkTo = implode(',', $ids);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
|
"LocCode" => $input['LocCode'] ?? null,
|
|
|
|
|
"Parent" => $input['Parent'] ?? null,
|
|
|
|
|
"LocFull" => $input['LocFull'] ?? null,
|
|
|
|
|
"Description" => $input['Description'] ?? null,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if ($mode === 'create') {
|
|
|
|
|
$data["CreateDate"] = $now;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function prepareLocationAddressData(array $input, string $now, string $mode = 'create'): array {
|
|
|
|
|
$data = [
|
|
|
|
|
"LocationID" => $input['LocationID'] ?? null,
|
|
|
|
|
"Street1" => $input['Street1'] ?? null,
|
|
|
|
|
"Street2" => $input['Street2'] ?? null,
|
|
|
|
|
"City" => $input['City'] ?? null,
|
|
|
|
|
"Province" => $input['Province'] ?? null,
|
|
|
|
|
"PostCode" => $input['PostCode'] ?? null,
|
|
|
|
|
"GeoLocationSystem" => $input['GeoLocationSystem'] ?? null,
|
|
|
|
|
"GeoLocationData" => $input['GeoLocationData'] ?? null,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if ($mode === 'create') {
|
|
|
|
|
$data["CreateDate"] = $now;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $data;
|
2025-09-10 16:54:33 +07:00
|
|
|
}
|
|
|
|
|
}
|