<?php
 namespace App\Services\Billing; use App\Models\Setting; final class PaymentTermsResolver { public const DEFAULTS = [ 'monthly' => 7, 'quarterly' => 14, 'semi_annually' => 21, 'annually' => 30, 'biennially' => 30, 'domain' => 45, ]; public static function daysForCycle(?string $cycle): int { $cycle = self::normaliseCycle($cycle); $table = self::table(); return (int) ($table[$cycle] ?? self::DEFAULTS[$cycle] ?? self::DEFAULTS['monthly']); } public static function table(): array { $raw = Setting::getValue('payment_terms_per_cycle', null); $stored = []; if (is_string($raw) && $raw !== '') { $decoded = json_decode($raw, true); if (is_array($decoded)) { $stored = $decoded; } } elseif (is_array($raw)) { $stored = $raw; } $out = []; foreach (self::DEFAULTS as $cycle => $default) { $value = $stored[$cycle] ?? $default; $out[$cycle] = max(0, (int) $value); } return $out; } public static function normaliseCycle(?string $cycle): string { if ($cycle === null || $cycle === '') { return 'monthly'; } $key = strtolower(str_replace(['-', ' '], '_', $cycle)); $map = [ 'monthly' => 'monthly', 'quarterly' => 'quarterly', 'semi_annually' => 'semi_annually', 'semiannually' => 'semi_annually', 'semi_annual' => 'semi_annually', 'annually' => 'annually', 'yearly' => 'annually', 'biennially' => 'biennially', 'biennial' => 'biennially', 'biyearly' => 'biennially', 'domain' => 'domain', ]; return $map[$key] ?? 'monthly'; } } 