Files
Toiminnanohjaus/app/Modules/Catalog/Presentation/Livewire/⚡vat-rate-form.blade.php
petriandClaude Opus 5.5 bdd8d446d8 Add money permissions, a read-only price list and VAT rates
- Admins give employees and interns personal money permissions (prices,
  expenses, quotes, billing) on the user form. Other modules check them
  through the money.* abilities; admins and management see all amounts,
  subcontractors none.
- Employees and interns get a read-only price list: consumer products
  with VAT always, hourly rates, other products and the mileage rate only
  with the price permission. Subcontractors cannot open it.
- VAT rates are maintained on the price list page, with one default rate
  that always stays in use. Products get a VAT rate; changing a rate's
  percentage applies to every product using it.
- Consumer product prices are entered and stored with VAT, so cash prices
  stay round; the price without VAT is calculated. The product form shows
  the other price as you type.
- Shared VatPercentage value object and Money::addVat()/removeVat(),
  rounding half up to the cent in integer arithmetic.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 07:51:19 +02:00

139 lines
4.8 KiB
PHP

<?php
use App\Modules\Catalog\Application\Contracts\PriceListQueries;
use App\Modules\Catalog\Application\UseCases\AddVatRate;
use App\Modules\Catalog\Application\UseCases\UpdateVatRate;
use App\Modules\Catalog\Domain\Exceptions\InvalidCatalogName;
use App\Modules\Catalog\Domain\Exceptions\VatRateNameTaken;
use App\Modules\Catalog\Domain\Exceptions\VatRateNotFound;
use App\Modules\Shared\Domain\Exceptions\InvalidVatPercentage;
use App\Modules\Shared\Domain\ValueObjects\VatPercentage;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* Add page (no {vatRate}) and edit page (with {vatRate}) of a VAT rate.
*/
new class extends Component
{
#[Locked]
public ?int $vatRateId = null;
/**
* Whether the edited rate already is the default, which can only be moved to another rate.
*/
#[Locked]
public bool $isDefault = false;
public string $name = '';
public string $percentage = '';
public bool $makeDefault = false;
public function mount(PriceListQueries $priceList, ?int $vatRate = null): void
{
$this->authorize('catalog.manage');
if ($vatRate === null) {
return;
}
$summary = $priceList->findVatRate($vatRate);
abort_if($summary === null, 404);
$this->vatRateId = $summary->id;
$this->isDefault = $summary->isDefault;
$this->name = $summary->name;
$this->percentage = (new VatPercentage($summary->basisPoints))->inputValue();
}
public function save(AddVatRate $addVatRate, UpdateVatRate $updateVatRate): void
{
$this->authorize('catalog.manage');
$this->validate(
['name' => ['required', 'string', 'max:100'], 'percentage' => ['required', 'string', 'max:10'], 'makeDefault' => ['boolean']],
[
'name.required' => __('catalog.errors.name_required'),
'name.max' => __('catalog.errors.invalid_name'),
'percentage.required' => __('catalog.errors.vat_percentage_required'),
'percentage.max' => __('catalog.errors.invalid_vat_percentage'),
],
);
try {
if ($this->vatRateId === null) {
$addVatRate->execute($this->name, $this->percentage, $this->makeDefault);
session()->flash('status', __('catalog.status.created', ['name' => trim($this->name)]));
} else {
$updateVatRate->execute($this->vatRateId, $this->name, $this->percentage, $this->makeDefault);
session()->flash('status', __('catalog.status.updated', ['name' => trim($this->name)]));
}
} catch (VatRateNotFound) {
abort(404);
} catch (InvalidCatalogName) {
$this->addError('name', __('catalog.errors.invalid_name'));
return;
} catch (VatRateNameTaken) {
$this->addError('name', __('catalog.errors.vat_rate_name_taken'));
return;
} catch (InvalidVatPercentage) {
$this->addError('percentage', __('catalog.errors.invalid_vat_percentage'));
return;
}
$this->redirectRoute('catalog.prices', navigate: true);
}
public function heading(): string
{
return __($this->vatRateId === null ? 'catalog.vat_rates.create_title' : 'catalog.vat_rates.edit_title');
}
public function render(): mixed
{
return $this->view()->title($this->heading());
}
};
?>
<div class="mx-auto max-w-2xl space-y-6 p-4 sm:p-6">
<h1 class="text-2xl font-semibold">{{ $this->heading() }}</h1>
@if ($vatRateId !== null)
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('catalog.vat_rates.change_help') }}</p>
@endif
<form wire:submit="save" class="space-y-6" novalidate>
<x-input wire:model="name" :label="__('catalog.fields.name')" :hint="__('catalog.vat_rates.name_help')" autocomplete="off" required />
<x-input
wire:model="percentage"
:label="__('catalog.vat_rates.percentage')"
:hint="__('catalog.vat_rates.percentage_help')"
inputmode="decimal"
autocomplete="off"
required
/>
@if ($isDefault)
<p class="text-sm">{{ __('catalog.vat_rates.is_default_help') }}</p>
@else
<div class="space-y-1">
<x-checkbox id="vat-rate-default" wire:model="makeDefault" :label="__('catalog.vat_rates.make_default')" />
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('catalog.vat_rates.make_default_help') }}</p>
</div>
@endif
<div class="flex flex-wrap gap-3">
<x-button submit :text="__('catalog.save')" />
<x-button :href="route('catalog.prices')" wire:navigate subtle color="black" :text="__('catalog.cancel')" />
</div>
</form>
</div>