feat(billing): renewals, coupons, VAT numbering, billing cycles and invoice payment tracking
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
ea9a53f742
commit
25f23d541e
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use App\Models\Setting;
|
||||
|
||||
class VatSettings
|
||||
{
|
||||
public const RATES_KEY = 'vat_settings';
|
||||
public const NUMBERING_KEY = 'vat_numbering';
|
||||
|
||||
private static ?array $rates = null;
|
||||
|
||||
public static function rateFor(?string $productType): float
|
||||
{
|
||||
if (!$productType) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
return (float) (self::rates()[$productType] ?? 0);
|
||||
}
|
||||
|
||||
public static function numbering(): array
|
||||
{
|
||||
$saved = self::read(self::NUMBERING_KEY);
|
||||
|
||||
return [
|
||||
'serial' => (string) ($saved['serial'] ?? '1C26TCP'),
|
||||
'next_number' => max(1, (int) ($saved['next_number'] ?? 1)),
|
||||
'padding' => min(12, max(1, (int) ($saved['padding'] ?? 8))),
|
||||
'auto_issue' => (bool) ($saved['auto_issue'] ?? false),
|
||||
];
|
||||
}
|
||||
|
||||
public static function saveNumbering(array $config): void
|
||||
{
|
||||
Setting::updateOrCreate(
|
||||
['key' => self::NUMBERING_KEY],
|
||||
['value' => json_encode($config, JSON_UNESCAPED_UNICODE)]
|
||||
);
|
||||
}
|
||||
|
||||
public static function forget(): void
|
||||
{
|
||||
self::$rates = null;
|
||||
}
|
||||
|
||||
private static function rates(): array
|
||||
{
|
||||
return self::$rates ??= collect(self::read(self::RATES_KEY))
|
||||
->filter(fn ($row) => is_array($row) && isset($row['type']))
|
||||
->mapWithKeys(fn ($row) => [$row['type'] => (float) ($row['vat_rate'] ?? 0)])
|
||||
->all();
|
||||
}
|
||||
|
||||
private static function read(string $key): array
|
||||
{
|
||||
$value = Setting::where('key', $key)->value('value');
|
||||
$decoded = is_string($value) ? json_decode($value, true) : null;
|
||||
|
||||
return is_array($decoded) ? $decoded : [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user