add refund and event logic

This commit is contained in:
root
2026-03-10 23:12:49 -04:00
parent ba1206e314
commit f6be51576c
67 changed files with 4808 additions and 1000 deletions
@@ -3,13 +3,14 @@
namespace App\Http\Controllers\Api\Finance;
use App\Http\Controllers\Api\BaseApiController;
use App\Http\Requests\Finance\FeeRefundRequest;
use App\Http\Requests\Finance\FeeTuitionTotalRequest;
use App\Http\Resources\Fees\FeeRefundResource;
use App\Http\Resources\Fees\FeeTuitionTotalResource;
use App\Services\Fees\FeeRefundCalculatorService;
use App\Services\Fees\FeeStudentFeeService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Log;
class FeeCalculationController extends BaseApiController
{
@@ -20,52 +21,47 @@ class FeeCalculationController extends BaseApiController
parent::__construct();
}
public function refund(Request $request): JsonResponse
public function refund(FeeRefundRequest $request): JsonResponse
{
$validator = Validator::make($request->all(), [
'parent_id' => ['required', 'integer', 'min:1'],
'students' => ['required', 'array'],
'students.*.student_id' => ['nullable', 'integer'],
'students.*.class_section_id' => ['required', 'integer', 'min:1'],
'students.*.enrollment_status' => ['required', 'string', 'max:50'],
'students.*.admission_status' => ['required', 'string', 'max:50'],
'students.*.withdrawal_date' => ['nullable', 'date'],
]);
$payload = $request->validated();
try {
$result = $this->refunds->calculateRefund($payload['students'], (int) $payload['parent_id']);
} catch (\Throwable $e) {
Log::error('Fee refund calculation failed', [
'parent_id' => $payload['parent_id'] ?? null,
'exception' => $e,
]);
if ($validator->fails()) {
return response()->json([
'message' => 'Validation failed.',
'errors' => $validator->errors(),
], 422);
'ok' => false,
'message' => 'Unable to calculate refund.',
], 500);
}
$payload = $validator->validated();
$result = $this->refunds->calculateRefund($payload['students'], (int) $payload['parent_id']);
return response()->json([
'ok' => true,
'refund' => new FeeRefundResource($result),
]);
}
public function tuitionTotal(Request $request): JsonResponse
public function tuitionTotal(FeeTuitionTotalRequest $request): JsonResponse
{
$validator = Validator::make($request->all(), [
'students' => ['required', 'array'],
'students.*.class_section_id' => ['required', 'integer', 'min:1'],
'students.*.grade' => ['nullable', 'string', 'max:20'],
]);
$payload = $request->validated();
try {
$total = $this->tuition->totalTuitionFee($payload['students']);
} catch (\Throwable $e) {
Log::error('Fee tuition total calculation failed', [
'exception' => $e,
]);
if ($validator->fails()) {
return response()->json([
'message' => 'Validation failed.',
'errors' => $validator->errors(),
], 422);
'ok' => false,
'message' => 'Unable to calculate tuition total.',
], 500);
}
$payload = $validator->validated();
$total = $this->tuition->totalTuitionFee($payload['students']);
return response()->json([
'ok' => true,
'tuition' => new FeeTuitionTotalResource([
@@ -0,0 +1,186 @@
<?php
namespace App\Http\Controllers\Api\Finance;
use App\Http\Controllers\Api\BaseApiController;
use App\Http\Requests\Refunds\RefundDecisionRequest;
use App\Http\Requests\Refunds\RefundIndexRequest;
use App\Http\Requests\Refunds\RefundPaymentRequest;
use App\Http\Requests\Refunds\RefundRecalculateRequest;
use App\Http\Requests\Refunds\RefundRejectRequest;
use App\Http\Requests\Refunds\RefundStoreRequest;
use App\Http\Resources\Refunds\RefundResource;
use App\Models\Configuration;
use App\Services\Refunds\RefundDecisionService;
use App\Services\Refunds\RefundOverpaymentService;
use App\Services\Refunds\RefundPayoutService;
use App\Services\Refunds\RefundQueryService;
use App\Services\Refunds\RefundRequestService;
use App\Services\Refunds\RefundSummaryService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;
class RefundController extends BaseApiController
{
public function __construct(
private RefundQueryService $queryService,
private RefundRequestService $requestService,
private RefundDecisionService $decisionService,
private RefundPayoutService $payoutService,
private RefundOverpaymentService $overpaymentService,
private RefundSummaryService $summaryService
) {
parent::__construct();
}
public function index(RefundIndexRequest $request): JsonResponse
{
$payload = $request->validated();
$result = $this->queryService->list($payload);
return response()->json([
'ok' => true,
'refunds' => RefundResource::collection($result['refunds']),
'pagination' => $result['pagination'],
]);
}
public function show(int $refundId): JsonResponse
{
$refund = $this->queryService->find($refundId);
if (!$refund) {
return response()->json(['ok' => false, 'message' => 'Refund not found.'], 404);
}
return response()->json([
'ok' => true,
'refund' => new RefundResource($refund),
]);
}
public function store(RefundStoreRequest $request): JsonResponse
{
$payload = $request->validated();
$actorId = (int) (auth()->id() ?? 0);
try {
$result = $this->requestService->requestRefund($payload, $actorId);
} catch (\Throwable $e) {
Log::error('Refund request failed.', ['error' => $e->getMessage()]);
return response()->json(['ok' => false, 'message' => 'Failed to submit refund request.'], 500);
}
if (empty($result['ok'])) {
return response()->json([
'ok' => false,
'message' => $result['message'] ?? 'Failed to submit refund request.',
'available' => $result['available'] ?? null,
], 422);
}
return response()->json([
'ok' => true,
'refund_id' => $result['refund_id'] ?? null,
'refund_amount' => $result['refund_amount'] ?? null,
'status' => $result['status'] ?? null,
], 201);
}
public function update(RefundDecisionRequest $request, int $refundId): JsonResponse
{
$payload = $request->validated();
$actorId = (int) (auth()->id() ?? 0);
$result = $this->decisionService->updateDecision(
$refundId,
(string) $payload['status'],
(string) $payload['reason'],
$actorId
);
if (empty($result['ok'])) {
return response()->json(['ok' => false, 'message' => $result['message'] ?? 'Failed to update refund.'], 422);
}
return response()->json(['ok' => true]);
}
public function destroy(int $refundId): JsonResponse
{
$actorId = (int) (auth()->id() ?? 0);
$result = $this->decisionService->cancel($refundId, $actorId);
if (empty($result['ok'])) {
return response()->json(['ok' => false, 'message' => $result['message'] ?? 'Failed to cancel refund.'], 404);
}
return response()->json(['ok' => true]);
}
public function approve(int $refundId): JsonResponse
{
$actorId = (int) (auth()->id() ?? 0);
$result = $this->decisionService->approve($refundId, $actorId);
if (empty($result['ok'])) {
return response()->json(['ok' => false, 'message' => $result['message'] ?? 'Approve failed.'], 422);
}
return response()->json(['ok' => true]);
}
public function reject(RefundRejectRequest $request, int $refundId): JsonResponse
{
$payload = $request->validated();
$actorId = (int) (auth()->id() ?? 0);
$result = $this->decisionService->reject($refundId, (string) $payload['reason'], $actorId);
if (empty($result['ok'])) {
return response()->json(['ok' => false, 'message' => $result['message'] ?? 'Reject failed.'], 422);
}
return response()->json(['ok' => true]);
}
public function recordPayment(RefundPaymentRequest $request, int $refundId): JsonResponse
{
$payload = $request->validated();
$payload['check_file'] = $request->file('check_file');
$actorId = (int) (auth()->id() ?? 0);
$result = $this->payoutService->recordPayment($refundId, $payload, $actorId);
if (empty($result['ok'])) {
return response()->json(['ok' => false, 'message' => $result['message'] ?? 'Failed to update payment.'], 422);
}
return response()->json(['ok' => true]);
}
public function recalculateOverpayments(RefundRecalculateRequest $request): JsonResponse
{
$payload = $request->validated();
$actorId = (int) (auth()->id() ?? 0);
$result = $this->overpaymentService->recalculate(true, $payload['invoice_number'] ?? null, $actorId);
return response()->json([
'ok' => true,
'created_ids' => $result['created_ids'] ?? null,
'updated_ids' => $result['updated_ids'] ?? null,
'message' => $result['message'] ?? null,
]);
}
public function parentBalances(int $parentId): JsonResponse
{
$termSchoolYear = (string) (Configuration::getConfig('school_year') ?? '');
$termSemester = (string) (Configuration::getConfig('semester') ?? '');
$summary = $this->summaryService->getParentFinancialSummary($parentId, $termSchoolYear, $termSemester);
return response()->json([
'ok' => true,
'summary' => $summary,
]);
}
}
@@ -0,0 +1,166 @@
<?php
namespace App\Http\Controllers\Api\Settings;
use App\Http\Controllers\Api\BaseApiController;
use App\Http\Requests\Events\EventChargeIndexRequest;
use App\Http\Requests\Events\EventChargeUpdateRequest;
use App\Http\Requests\Events\EventIndexRequest;
use App\Http\Requests\Events\EventStoreRequest;
use App\Http\Requests\Events\EventStudentsWithChargesRequest;
use App\Http\Requests\Events\EventUpdateRequest;
use App\Http\Resources\Events\EventChargeResource;
use App\Http\Resources\Events\EventResource;
use App\Http\Resources\Events\EventStudentChargeResource;
use App\Services\Events\EventCategoryService;
use App\Services\Events\EventChargeQueryService;
use App\Services\Events\EventChargeService;
use App\Services\Events\EventListService;
use App\Services\Events\EventManagementService;
use App\Services\Events\EventStudentChargeService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;
class EventController extends BaseApiController
{
public function __construct(
private EventListService $listService,
private EventManagementService $managementService,
private EventChargeQueryService $chargeQueryService,
private EventChargeService $chargeService,
private EventStudentChargeService $studentChargeService
) {
parent::__construct();
}
public function index(EventIndexRequest $request): JsonResponse
{
$payload = $request->validated();
$result = $this->listService->list($payload);
return response()->json([
'ok' => true,
'events' => EventResource::collection($result['events']),
'pagination' => $result['pagination'],
'active_count' => $result['active_count'],
'categories' => EventCategoryService::categories(),
]);
}
public function show(int $eventId): JsonResponse
{
$event = $this->listService->find($eventId);
if (!$event) {
return response()->json(['ok' => false, 'message' => 'Event not found.'], 404);
}
return response()->json([
'ok' => true,
'event' => new EventResource($event),
]);
}
public function store(EventStoreRequest $request): JsonResponse
{
$payload = $request->validated();
$actorId = (int) (auth()->id() ?? 0);
try {
$result = $this->managementService->create($payload, $request->file('flyer'), $actorId);
} catch (\Throwable $e) {
Log::error('Event creation failed.', ['error' => $e->getMessage()]);
return response()->json(['ok' => false, 'message' => 'Failed to create event.'], 500);
}
if (empty($result['ok'])) {
return response()->json(['ok' => false, 'message' => 'Failed to create event.'], 422);
}
return response()->json([
'ok' => true,
'event' => new EventResource($result['event']),
'charges_created' => $result['charges_created'] ?? 0,
], 201);
}
public function update(EventUpdateRequest $request, int $eventId): JsonResponse
{
$payload = $request->validated();
$result = $this->managementService->update($eventId, $payload, $request->file('flyer'));
if (empty($result['ok'])) {
return response()->json(['ok' => false, 'message' => $result['message'] ?? 'Failed to update event.'], 404);
}
return response()->json([
'ok' => true,
'event' => new EventResource($result['event']),
]);
}
public function destroy(int $eventId): JsonResponse
{
$actorId = (int) (auth()->id() ?? 0);
$result = $this->managementService->delete($eventId, $actorId);
if (empty($result['ok'])) {
return response()->json(['ok' => false, 'message' => $result['message'] ?? 'Failed to delete event.'], 404);
}
return response()->json(['ok' => true]);
}
public function charges(EventChargeIndexRequest $request): JsonResponse
{
$payload = $request->validated();
$result = $this->chargeQueryService->list($payload);
return response()->json([
'ok' => true,
'charges' => EventChargeResource::collection($result['charges']),
'pagination' => $result['pagination'],
]);
}
public function updateCharges(EventChargeUpdateRequest $request, int $eventId): JsonResponse
{
$payload = $request->validated();
$actorId = (int) (auth()->id() ?? 0);
$result = $this->chargeService->updateParticipation(
(int) $payload['parent_id'],
$eventId,
$payload['participation'],
(string) $payload['school_year'],
(string) $payload['semester'],
$actorId
);
if (empty($result['ok'])) {
return response()->json(['ok' => false, 'message' => $result['message'] ?? 'Failed to update charges.'], 422);
}
return response()->json([
'ok' => true,
'created' => $result['created'],
'updated' => $result['updated'],
'deleted' => $result['deleted'],
]);
}
public function studentsWithCharges(EventStudentsWithChargesRequest $request): JsonResponse
{
$payload = $request->validated();
$students = $this->studentChargeService->listStudentsWithCharges(
(int) $payload['parent_id'],
(string) $payload['school_year'],
(string) $payload['semester']
);
return response()->json([
'ok' => true,
'students' => EventStudentChargeResource::collection($students),
]);
}
}