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, ]); return response()->json([ 'ok' => false, 'message' => 'Unable to calculate refund.', ], 500); } return response()->json([ 'ok' => true, 'refund' => new FeeRefundResource($result), ]); } public function tuitionTotal(FeeTuitionTotalRequest $request): JsonResponse { $payload = $request->validated(); try { $total = $this->tuition->totalTuitionFee($payload['students']); } catch (\Throwable $e) { Log::error('Fee tuition total calculation failed', [ 'exception' => $e, ]); return response()->json([ 'ok' => false, 'message' => 'Unable to calculate tuition total.', ], 500); } return response()->json([ 'ok' => true, 'tuition' => new FeeTuitionTotalResource([ 'total_tuition' => $total, ]), ]); } public function tuitionBreakdown(FeeTuitionBreakdownRequest $request): JsonResponse { $payload = $request->validated(); try { $result = $this->tuitionCalculator->calculate($payload['students']); } catch (\Throwable $e) { Log::error('Tuition breakdown calculation failed', [ 'exception' => $e, ]); return response()->json([ 'ok' => false, 'message' => 'Unable to calculate tuition breakdown.', ], 500); } return response()->json([ 'ok' => true, 'tuition' => new FeeTuitionBreakdownResource($result), ]); } public function familyBalance(FeeFamilyBalanceRequest $request): JsonResponse { $payload = $request->validated(); try { $result = $this->balance->calculateFamilyAccount( $payload['students'], (int) $payload['parent_id'], $payload['school_year'] ?? null ); } catch (\Throwable $e) { Log::error('Family balance calculation failed', [ 'parent_id' => $payload['parent_id'] ?? null, 'exception' => $e, ]); return response()->json([ 'ok' => false, 'message' => 'Unable to calculate family balance.', ], 500); } return response()->json([ 'ok' => true, 'account' => new FeeFamilyBalanceResource($result), ]); } }