2026-03-16 07:24:50 +07:00
|
|
|
<?php
|
|
|
|
|
namespace App\Controllers\Organization;
|
|
|
|
|
|
2026-04-08 08:37:41 +07:00
|
|
|
use App\Traits\PatchValidationTrait;
|
|
|
|
|
use App\Traits\ResponseTrait;
|
|
|
|
|
use App\Controllers\BaseController;
|
|
|
|
|
|
|
|
|
|
use App\Models\Organization\AccountModel;
|
2026-03-16 07:24:50 +07:00
|
|
|
|
2026-04-08 08:37:41 +07:00
|
|
|
class AccountController extends BaseController {
|
|
|
|
|
use ResponseTrait;
|
|
|
|
|
use PatchValidationTrait;
|
2026-03-16 07:24:50 +07:00
|
|
|
|
|
|
|
|
protected $db;
|
|
|
|
|
protected $model;
|
|
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
$this->db = \Config\Database::connect();
|
|
|
|
|
$this->model = new AccountModel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function index() {
|
|
|
|
|
$filter = [
|
|
|
|
|
'Parent' => $this->request->getVar('Parent'),
|
|
|
|
|
'AccountName' => $this->request->getVar('AccountName'),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$rows = $this->model->getAccounts($filter);
|
|
|
|
|
|
|
|
|
|
if (empty($rows)) {
|
|
|
|
|
return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $rows ], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function show($AccountID = null) {
|
|
|
|
|
//$rows = $this->model->where('AccountID', $AccountID)->findAll();
|
|
|
|
|
$row = $this->model->getAccount($AccountID);
|
|
|
|
|
|
|
|
|
|
if (empty($row)) {
|
|
|
|
|
return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => null ], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $row ], 200);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-08 16:07:19 +07:00
|
|
|
public function delete() {
|
|
|
|
|
try {
|
|
|
|
|
$input = $this->request->getJSON(true);
|
|
|
|
|
$id = $this->requirePatchId($input['AccountID'] ?? null, 'AccountID');
|
|
|
|
|
if ($id === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$this->model->delete($id);
|
|
|
|
|
return $this->respondDeleted([ 'status' => 'success', 'message' => "{$id} deleted successfully."]);
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-16 07:24:50 +07:00
|
|
|
|
2026-04-08 16:07:19 +07:00
|
|
|
public function create() {
|
|
|
|
|
$input = $this->request->getJSON(true);
|
|
|
|
|
|
|
|
|
|
$validation = service('validation');
|
|
|
|
|
$validation->setRules([
|
|
|
|
|
'AccountName' => 'required|string|max_length[255]',
|
|
|
|
|
'Parent' => 'permit_empty|integer',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (!$validation->run($input)) {
|
|
|
|
|
return $this->failValidationErrors($validation->getErrors());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$id = $this->model->insert($input,true);
|
|
|
|
|
return $this->respondCreated([ 'status' => 'success', 'message' => 'data created successfully', 'data' => $id ], 201);
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-16 07:24:50 +07:00
|
|
|
|
2026-03-16 15:58:56 +07:00
|
|
|
public function update($AccountID = null) {
|
2026-04-08 08:37:41 +07:00
|
|
|
$input = $this->requirePatchPayload($this->request->getJSON(true));
|
|
|
|
|
if ($input === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$id = $this->requirePatchId($AccountID, 'AccountID');
|
|
|
|
|
if ($id === null) {
|
|
|
|
|
return;
|
2026-03-16 15:58:56 +07:00
|
|
|
}
|
2026-04-08 08:37:41 +07:00
|
|
|
|
|
|
|
|
$existing = $this->model->find($id);
|
|
|
|
|
if (!$existing) {
|
|
|
|
|
return $this->respond([ 'status' => 'failed', 'message' => 'Account not found', 'data' => [] ], 404);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-08 16:07:19 +07:00
|
|
|
$validation = service('validation');
|
|
|
|
|
$validation->setRules([
|
|
|
|
|
'AccountName' => 'permit_empty|string|max_length[255]',
|
|
|
|
|
'Parent' => 'permit_empty|integer',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (!$validation->run($input)) {
|
|
|
|
|
return $this->failValidationErrors($validation->getErrors());
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-08 08:37:41 +07:00
|
|
|
$input['AccountID'] = $id;
|
2026-03-16 15:58:56 +07:00
|
|
|
try {
|
2026-04-08 08:37:41 +07:00
|
|
|
$this->model->update($id, $input);
|
|
|
|
|
return $this->respond([ 'status' => 'success', 'message' => 'data updated successfully', 'data' => $id ], 200);
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|