152 lines
5.0 KiB
PHP
Executable File
152 lines
5.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Models\Configuration;
|
|
use App\Models\Refund;
|
|
use Carbon\Carbon;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class RefundController extends BaseApiController
|
|
{
|
|
protected Refund $refund;
|
|
protected Configuration $config;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->refund = model(Refund::class);
|
|
$this->config = model(Configuration::class);
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$page = max(1, (int) ($this->request->getGet('page') ?? 1));
|
|
$perPage = min(100, max(1, (int) ($this->request->getGet('per_page') ?? 20)));
|
|
$parentId = $this->request->getGet('parent_id');
|
|
$status = $this->request->getGet('status');
|
|
|
|
$query = $this->refund->newQuery();
|
|
|
|
if ($parentId) {
|
|
$query->where('parent_id', $parentId);
|
|
}
|
|
|
|
if ($status) {
|
|
$query->where('status', $status);
|
|
}
|
|
|
|
$result = $this->paginate($query, $page, $perPage);
|
|
|
|
return $this->success($result, 'Refunds retrieved successfully');
|
|
}
|
|
|
|
public function show($id = null)
|
|
{
|
|
$refund = $this->refund->find($id);
|
|
if (!$refund) {
|
|
return $this->error('Refund not found', Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
return $this->success($refund, 'Refund retrieved successfully');
|
|
}
|
|
|
|
public function store()
|
|
{
|
|
$user = $this->getCurrentUser();
|
|
if (!$user) {
|
|
return $this->respondError('Authentication required', Response::HTTP_UNAUTHORIZED);
|
|
}
|
|
|
|
$data = $this->payloadData();
|
|
if (empty($data)) {
|
|
return $this->error('Invalid request data', Response::HTTP_BAD_REQUEST);
|
|
}
|
|
|
|
$rules = [
|
|
'parent_id' => 'required|integer',
|
|
'invoice_id' => 'required|integer',
|
|
'refund_amount' => 'required|numeric',
|
|
'reason' => 'required',
|
|
];
|
|
|
|
$errors = $this->validateRequest($data, $rules);
|
|
if (!empty($errors)) {
|
|
return $this->respondValidationError($errors);
|
|
}
|
|
|
|
$schoolYear = $this->config->getConfig('school_year');
|
|
$semester = $this->config->getConfig('semester');
|
|
|
|
$refundData = [
|
|
'parent_id' => $data['parent_id'],
|
|
'invoice_id' => $data['invoice_id'],
|
|
'refund_amount' => $data['refund_amount'],
|
|
'reason' => $data['reason'],
|
|
'request' => $data['request'] ?? null,
|
|
'status' => 'pending',
|
|
'requested_at' => Carbon::now('UTC')->toDateTimeString(),
|
|
'school_year' => $schoolYear,
|
|
'semester' => $semester,
|
|
];
|
|
|
|
try {
|
|
$refund = $this->refund->create($refundData);
|
|
return $this->success($refund->toArray(), 'Refund request created successfully', Response::HTTP_CREATED);
|
|
} catch (\Throwable $e) {
|
|
log_message('error', 'Refund creation error: ' . $e->getMessage());
|
|
return $this->respondError('Failed to create refund request', Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
}
|
|
|
|
public function update($id = null)
|
|
{
|
|
$refund = $this->refund->find($id);
|
|
if (!$refund) {
|
|
return $this->error('Refund not found', Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
$user = $this->getCurrentUser();
|
|
if (!$user) {
|
|
return $this->respondError('Authentication required', Response::HTTP_UNAUTHORIZED);
|
|
}
|
|
|
|
$data = $this->payloadData();
|
|
if (empty($data)) {
|
|
return $this->error('Invalid request data', Response::HTTP_BAD_REQUEST);
|
|
}
|
|
|
|
$allowedFields = ['status', 'refund_paid_amount', 'refund_method', 'check_nbr', 'note'];
|
|
$updateData = [];
|
|
foreach ($allowedFields as $field) {
|
|
if (array_key_exists($field, $data)) {
|
|
$updateData[$field] = $data[$field];
|
|
}
|
|
}
|
|
|
|
if (isset($updateData['status']) && $updateData['status'] === 'approved') {
|
|
$updateData['approved_at'] = Carbon::now('UTC')->toDateTimeString();
|
|
$updateData['approved_by'] = $user->id;
|
|
}
|
|
|
|
if (isset($updateData['status']) && $updateData['status'] === 'refunded') {
|
|
$updateData['refunded_at'] = Carbon::now('UTC')->toDateTimeString();
|
|
}
|
|
|
|
if (empty($updateData)) {
|
|
return $this->error('No valid fields to update', Response::HTTP_BAD_REQUEST);
|
|
}
|
|
|
|
$updateData['updated_by'] = $user->id;
|
|
|
|
try {
|
|
$this->refund->update($id, $updateData);
|
|
$updatedRefund = $this->refund->find($id);
|
|
return $this->success($updatedRefund, 'Refund updated successfully');
|
|
} catch (\Throwable $e) {
|
|
log_message('error', 'Refund update error: ' . $e->getMessage());
|
|
return $this->respondError('Failed to update refund', Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
}
|
|
}
|