64 lines
1.7 KiB
PHP
64 lines
1.7 KiB
PHP
<?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 : [];
|
|
}
|
|
}
|