$e->getMessage()]); } return round($eventSubtotal, 2); } public function additionalSubtotal(int $invoiceId, ?string $schoolYear = null): float { $additionalSubtotal = 0.0; try { $builder = AdditionalCharge::query() ->select('charge_type', 'amount') ->where('invoice_id', $invoiceId) ->where('status', 'applied'); if ($schoolYear !== null && $schoolYear !== '') { $builder->where('school_year', $schoolYear); } $rows = $builder->get()->map(fn ($row) => (array) $row)->all(); foreach ($rows as $row) { $amount = (float) ($row['amount'] ?? 0); $type = strtolower((string) ($row['charge_type'] ?? 'add')); $amount = $type === 'deduct' ? -abs($amount) : abs($amount); $additionalSubtotal += $amount; } } catch (\Throwable $e) { Log::warning('Failed to sum additional charges.', ['error' => $e->getMessage()]); } return round($additionalSubtotal, 2); } }