59 lines
1.9 KiB
PHP
59 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Finance;
|
|
|
|
use App\Http\Controllers\Api\BaseApiController;
|
|
use App\Http\Requests\Payments\PaymentByParentRequest;
|
|
use App\Http\Requests\Payments\PaymentCreateRequest;
|
|
use App\Http\Requests\Payments\PaymentUpdateBalanceRequest;
|
|
use App\Http\Resources\Payments\PaymentResource;
|
|
use App\Services\Payments\PaymentBalanceService;
|
|
use App\Services\Payments\PaymentLookupService;
|
|
use App\Services\Payments\PaymentPlanService;
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
class PaymentController extends BaseApiController
|
|
{
|
|
public function __construct(
|
|
private PaymentPlanService $planService,
|
|
private PaymentLookupService $lookupService,
|
|
private PaymentBalanceService $balanceService
|
|
) {
|
|
parent::__construct();
|
|
}
|
|
|
|
public function store(PaymentCreateRequest $request): JsonResponse
|
|
{
|
|
$payload = $request->validated();
|
|
$payment = $this->planService->createPlan($payload);
|
|
|
|
return response()->json([
|
|
'ok' => true,
|
|
'payment' => new PaymentResource($payment),
|
|
], 201);
|
|
}
|
|
|
|
public function byParent(PaymentByParentRequest $request, int $parentId): JsonResponse
|
|
{
|
|
$payload = $request->validated();
|
|
$payments = $this->lookupService->getByParent($parentId, $payload['school_year'] ?? null);
|
|
|
|
return response()->json([
|
|
'ok' => true,
|
|
'payments' => PaymentResource::collection($payments),
|
|
]);
|
|
}
|
|
|
|
public function updateBalance(PaymentUpdateBalanceRequest $request, int $paymentId): JsonResponse
|
|
{
|
|
$payload = $request->validated();
|
|
$updated = $this->balanceService->updateBalance($paymentId, (float) $payload['paid_amount']);
|
|
|
|
if (!$updated) {
|
|
return response()->json(['ok' => false, 'message' => 'Payment not found.'], 404);
|
|
}
|
|
|
|
return response()->json(['ok' => true]);
|
|
}
|
|
}
|