add projet
This commit is contained in:
+149
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Models\Configuration;
|
||||
use App\Models\Reimbursement;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class ReimbursementController extends BaseApiController
|
||||
{
|
||||
protected Reimbursement $reimbursement;
|
||||
protected Configuration $config;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->reimbursement = model(Reimbursement::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)));
|
||||
$status = $this->request->getGet('status');
|
||||
$reimbursedTo = $this->request->getGet('reimbursed_to');
|
||||
$schoolYear = $this->config->getConfig('school_year');
|
||||
$semester = $this->config->getConfig('semester');
|
||||
|
||||
$query = $this->reimbursement->newQuery()
|
||||
->where('school_year', $schoolYear)
|
||||
->where('semester', $semester);
|
||||
|
||||
if (!empty($status)) {
|
||||
$query->where('status', $status);
|
||||
}
|
||||
|
||||
if (!empty($reimbursedTo)) {
|
||||
$query->where('reimbursed_to', $reimbursedTo);
|
||||
}
|
||||
|
||||
$result = $this->paginate($query, $page, $perPage);
|
||||
|
||||
return $this->success($result, 'Reimbursements retrieved successfully');
|
||||
}
|
||||
|
||||
public function show($id = null)
|
||||
{
|
||||
$reimbursement = $this->reimbursement->find($id);
|
||||
if (!$reimbursement) {
|
||||
return $this->error('Reimbursement not found', Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
return $this->success($reimbursement, 'Reimbursement 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 = [
|
||||
'amount' => 'required|numeric',
|
||||
'reimbursed_to' => 'required|integer',
|
||||
'description' => 'required',
|
||||
'expense_id' => 'required|integer',
|
||||
];
|
||||
|
||||
$errors = $this->validateRequest($data, $rules);
|
||||
if (!empty($errors)) {
|
||||
return $this->respondValidationError($errors);
|
||||
}
|
||||
|
||||
$schoolYear = $this->config->getConfig('school_year');
|
||||
$semester = $this->config->getConfig('semester');
|
||||
|
||||
$reimbursementData = [
|
||||
'amount' => $data['amount'],
|
||||
'reimbursed_to' => $data['reimbursed_to'],
|
||||
'description' => $data['description'],
|
||||
'expense_id' => $data['expense_id'],
|
||||
'added_by' => $user->id,
|
||||
'school_year' => $schoolYear,
|
||||
'semester' => $semester,
|
||||
'status' => $data['status'] ?? 'pending',
|
||||
'reimbursement_method' => $data['reimbursement_method'] ?? null,
|
||||
'check_number' => $data['check_number'] ?? null,
|
||||
];
|
||||
|
||||
try {
|
||||
$reimbursement = $this->reimbursement->create($reimbursementData);
|
||||
return $this->success($reimbursement->toArray(), 'Reimbursement created successfully', Response::HTTP_CREATED);
|
||||
} catch (\Throwable $e) {
|
||||
log_message('error', 'Reimbursement creation error: ' . $e->getMessage());
|
||||
return $this->respondError('Failed to create reimbursement', Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
public function update($id = null)
|
||||
{
|
||||
$reimbursement = $this->reimbursement->find($id);
|
||||
if (!$reimbursement) {
|
||||
return $this->error('Reimbursement 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', 'reimbursement_method', 'check_number'];
|
||||
$updateData = [];
|
||||
|
||||
foreach ($allowedFields as $field) {
|
||||
if (array_key_exists($field, $data)) {
|
||||
$updateData[$field] = $data[$field];
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($updateData['status']) && $updateData['status'] === 'approved') {
|
||||
$updateData['approved_by'] = $user->id;
|
||||
}
|
||||
|
||||
if (empty($updateData)) {
|
||||
return $this->error('No valid fields to update', Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
try {
|
||||
$this->reimbursement->update($id, $updateData);
|
||||
$updatedReimbursement = $this->reimbursement->find($id);
|
||||
return $this->success($updatedReimbursement, 'Reimbursement updated successfully');
|
||||
} catch (\Throwable $e) {
|
||||
log_message('error', 'Reimbursement update error: ' . $e->getMessage());
|
||||
return $this->respondError('Failed to update reimbursement', Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user