AMS_Backend/app/Controllers/Backend/TopupController.php
2025-11-06 13:41:06 +08:00

174 lines
5.2 KiB
PHP

<?php
namespace App\Controllers\Backend;
use App\Controllers\BaseController;
use CodeIgniter\HTTP\ResponseInterface;
use App\Models\TopupModel;
use CodeIgniter\HTTP\ResponseTrait;
use CodeIgniter\RESTful\ResourceController;
class TopupController extends ResourceController
{
use ResponseTrait;
private $topups;
public function __construct()
{
$this->topups = new TopupModel();
}
public function index()
{
// echo(123);exit;
$data = $this->topups
->select('topup.*, customers.customer_wallet, customers.phone, customers.name')
->join('customers', 'customers.id = topup.customer_id', 'left')
->orderBy('topup.created_at', 'DESC')
->findAll();
return $this->respond([
'status' => 200,
'data' => $data,
]);
}
public function create()
{
// echo('123'); exit;
$payload = $this->request->getJSON(true) ?? $this->request->getPost();
$rules = [
'customer_id' => 'required|integer',
'amount' => 'required|decimal|greater_than[0]',
'payment_method' => 'required|string|max_length[50]',
'topup_setting_id' => 'permit_empty|integer',
'credit' => 'permit_empty|decimal',
'other_amount' => 'permit_empty|decimal',
'status' => 'permit_empty|in_list[pending,completed,failed]'
];
// echo(123);
if (!$this->validate($rules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
$topupNumber = 'Test-123123';
$data = [
'topup_setting_id' => $payload['topup_setting_id'] ?? null,
'customer_id' => $payload['customer_id'],
'topup_number' => $topupNumber,
'amount' => $payload['amount'],
'credit' => $payload['credit'],
'payment_method' => $payload['payment_method'],
'other_amount' => $payload['other_amount'] ?? null,
'status' => $payload['status']
];
$id = $this->topups->insert($data, true);
if (!$id) {
return $this->fail('Topup creation failed', 400);
}
return $this->respond([
'status' => 201,
'message' => 'Topup created successfully',
'data' => $this->topups->find($id)
]);
}
public function update($id = null)
{
$id = (int) $id;
if (!$this->topups->find($id)) {
return $this->fail('Topup not found', 404);
}
$payload = $this->request->getJSON(true) ?? $this->request->getPost();
$rules = [
'status' => 'permit_empty|in_list[pending,completed,failed]',
'amount' => 'permit_empty|decimal|greater_than[0]',
'credit' => 'permit_empty|decimal',
'payment_method' => 'permit_empty|string|max_length[50]'
];
if (!$this->validate($rules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
if (!$this->topups->update($id, $payload)) {
return $this->fail('Update failed', 400);
}
return $this->respond([
'status' => 200,
'message' => 'Topup updated successfully',
'data' => $this->topups->find($id)
]);
}
public function delete($id = null)
{
$id = (int) $id;
if (!$this->topups->find($id)) {
return $this->fail('Topup not found', 404);
}
if (!$this->topups->delete($id)) {
return $this->fail('Delete failed', 400);
}
return $this->respond([
'status' => 200,
'message' => 'Topup deleted successfully'
]);
}
public function show($id = null)
{
$id = (int) $id;
$row = $this->topups
->select('topup.*, topup_setting.id AS setting_id, topup_setting.topup_amount, topup_setting.credit_amount, topup_setting.status AS setting_status')
->join('topup_setting', 'topup_setting.id = topup.topup_setting_id', 'left')
->where('topup.id', $id)
->first();
if (!$row) {
return $this->failNotFound('Topup record not found');
}
$data = [
'id' => $row['id'],
'topup_setting_id' => $row['topup_setting_id'],
'customer_id' => $row['customer_id'],
'topup_number' => $row['topup_number'],
'amount' => $row['amount'],
'credit' => $row['credit'],
'payment_method' => $row['payment_method'],
'other_amount' => $row['other_amount'],
'status' => $row['status'],
'created_at' => $row['created_at'],
'updated_at' => $row['updated_at'],
'topup_setting' => [
'id' => $row['setting_id'],
'topup_amount' => $row['topup_amount'],
'credit_amount' => $row['credit_amount'],
'status' => $row['setting_status']
]
];
return $this->respond([
'status' => 200,
'data' => $data
]);
}
}