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]>
This commit is contained in:
2026-11-24 07:51:19 +02:00
co-authored by Claude Opus 5.5
parent dc2612cf46
commit bdd8d446d8
68 changed files with 2222 additions and 64 deletions
@@ -10,6 +10,7 @@ use App\Modules\Catalog\Domain\Exceptions\InvalidCatalogName;
use App\Modules\Catalog\Domain\Exceptions\InvalidProductDuration;
use App\Modules\Catalog\Domain\Exceptions\ProductNameTaken;
use App\Modules\Catalog\Domain\Exceptions\ProductNeedsWorkType;
use App\Modules\Catalog\Domain\Exceptions\VatRateNotFound;
use App\Modules\Catalog\Domain\ValueObjects\ProductDuration;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
@@ -19,7 +20,7 @@ use Livewire\Form;
/**
* Fields of a product. Price and duration are kept as typed ("12,50") and
* parsed by the use case.
* parsed by the use case. The price includes VAT for a consumer product.
*/
class ProductForm extends Form
{
@@ -29,6 +30,8 @@ class ProductForm extends Form
public string $price = '';
public string $vat_rate_id = '';
public bool $is_consumer_product = false;
public string $duration = '';
@@ -43,6 +46,7 @@ class ProductForm extends Form
$this->name = $product->name;
$this->unit = $product->unit->value;
$this->price = Money::fromCents($product->priceCents)->inputValue();
$this->vat_rate_id = (string) $product->vatRateId;
$this->is_consumer_product = $product->isConsumerProduct;
$this->duration = $product->durationQuarters === 0 ? '' : (new ProductDuration($product->durationQuarters))->format();
$this->work_type_id = $product->workTypeId === null ? '' : (string) $product->workTypeId;
@@ -56,6 +60,7 @@ class ProductForm extends Form
$this->name,
ProductUnit::from($this->unit),
$this->price,
(int) $this->vat_rate_id,
$this->is_consumer_product,
$this->duration,
$this->work_type_id === '' ? null : (int) $this->work_type_id,
@@ -74,6 +79,7 @@ class ProductForm extends Form
InvalidProductDuration::class => ['duration', 'catalog.errors.invalid_duration'],
ProductNeedsWorkType::class => ['work_type_id', 'catalog.errors.work_type_required'],
CatalogItemNotFound::class => ['work_type_id', 'catalog.errors.work_type_unavailable'],
VatRateNotFound::class => ['vat_rate_id', 'catalog.errors.vat_rate_unavailable'],
default => throw $exception,
};
@@ -89,6 +95,7 @@ class ProductForm extends Form
'name' => ['required', 'string', 'max:100'],
'unit' => ['required', Rule::enum(ProductUnit::class)],
'price' => ['required', 'string', 'max:20'],
'vat_rate_id' => ['required', 'integer'],
'is_consumer_product' => ['boolean'],
'duration' => ['nullable', 'string', 'max:10'],
'work_type_id' => ['nullable', 'integer'],
@@ -107,6 +114,8 @@ class ProductForm extends Form
'unit.enum' => __('catalog.errors.invalid_unit'),
'price.required' => __('catalog.errors.price_required'),
'price.max' => __('catalog.errors.invalid_price'),
'vat_rate_id.required' => __('catalog.errors.vat_rate_unavailable'),
'vat_rate_id.integer' => __('catalog.errors.vat_rate_unavailable'),
'duration.max' => __('catalog.errors.invalid_duration'),
'work_type_id.integer' => __('catalog.errors.work_type_unavailable'),
];
@@ -0,0 +1,231 @@
<?php
use App\Modules\Catalog\Application\Contracts\PriceListQueries;
use App\Modules\Catalog\Application\DTOs\HourlyRateGroup;
use App\Modules\Catalog\Application\DTOs\ProductSummary;
use App\Modules\Shared\Domain\ValueObjects\Money;
use App\Modules\Shared\Domain\ValueObjects\VatPercentage;
use Livewire\Attributes\Computed;
use Livewire\Component;
/**
* The price list for reading. Consumer product prices with VAT are shown to
* everyone but subcontractors; hourly rates, other products and the
* mileage rate only to those who may see prices (`money.prices`). Amounts
* stay in computed properties, never in public ones sent to the browser.
*/
new class extends Component
{
public function mount(): void
{
$this->authorize('catalog.view-prices');
}
#[Computed]
public function canSeePrices(): bool
{
return auth()->user()?->can('money.prices') ?? false;
}
/**
* @return list<ProductSummary>
*/
#[Computed]
public function consumerProducts(): array
{
return app(PriceListQueries::class)->consumerProducts();
}
/**
* Active products that are not consumer products; only with the price permission.
*
* @return list<ProductSummary>
*/
#[Computed]
public function otherProducts(): array
{
if (! $this->canSeePrices) {
return [];
}
return array_values(array_filter(
app(PriceListQueries::class)->products(includeInactive: false),
fn (ProductSummary $product): bool => ! $product->isConsumerProduct,
));
}
/**
* @return list<HourlyRateGroup>
*/
#[Computed]
public function hourlyRates(): array
{
if (! $this->canSeePrices) {
return [];
}
return array_values(array_filter(
app(PriceListQueries::class)->hourlyRates(includeInactive: false),
fn (HourlyRateGroup $group): bool => $group->rows !== [],
));
}
#[Computed]
public function mileageRateCents(): ?int
{
return $this->canSeePrices ? app(PriceListQueries::class)->mileageRateCents() : null;
}
/**
* @return list<array{index: string, label: string, sortable: bool, align?: string}>
*/
public function consumerHeaders(): array
{
return [
['index' => 'name', 'label' => __('catalog.price_list.product'), 'sortable' => false],
['index' => 'unit', 'label' => __('catalog.products.unit'), 'sortable' => false],
['index' => 'gross', 'label' => __('catalog.products.gross_column'), 'sortable' => false, 'align' => 'right'],
['index' => 'vat', 'label' => __('catalog.products.vat_column'), 'sortable' => false],
];
}
/**
* @return list<array{index: string, label: string, sortable: bool, align?: string}>
*/
public function otherHeaders(): array
{
return [
['index' => 'name', 'label' => __('catalog.price_list.product'), 'sortable' => false],
['index' => 'unit', 'label' => __('catalog.products.unit'), 'sortable' => false],
['index' => 'net', 'label' => __('catalog.products.net_column'), 'sortable' => false, 'align' => 'right'],
['index' => 'gross', 'label' => __('catalog.products.gross_column'), 'sortable' => false, 'align' => 'right'],
['index' => 'vat', 'label' => __('catalog.products.vat_column'), 'sortable' => false],
];
}
public function money(int $cents): string
{
return Money::fromCents($cents)->format();
}
public function percentage(int $basisPoints): string
{
return (new VatPercentage($basisPoints))->format();
}
public function render(): mixed
{
return $this->view()->title(__('catalog.prices.title'));
}
};
?>
<div class="mx-auto max-w-5xl space-y-8 p-4 sm:p-6">
<h1 class="text-2xl font-semibold">{{ __('catalog.prices.title') }}</h1>
<section aria-labelledby="consumer-products-heading" class="space-y-4">
<div class="space-y-1">
<h2 id="consumer-products-heading" class="text-xl font-semibold">{{ __('catalog.price_list.consumer_products') }}</h2>
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('catalog.price_list.consumer_help') }}</p>
</div>
@if ($this->consumerProducts === [])
<p class="text-sm">{{ __('catalog.products.none') }}</p>
@else
<x-table :headers="$this->consumerHeaders()" :rows="$this->consumerProducts">
@interact('column_name', $product)
<span class="font-medium break-words">{{ $product->name }}</span>
@endinteract
@interact('column_unit', $product)
{{ __('catalog.units.'.$product->unit->value) }}
@endinteract
@interact('column_gross', $product)
<span class="whitespace-nowrap font-medium">{{ $this->money($product->grossCents) }}</span>
@endinteract
@interact('column_vat', $product)
<span class="whitespace-nowrap">{{ $this->percentage($product->vatBasisPoints) }}</span>
@endinteract
</x-table>
@endif
</section>
@if ($this->canSeePrices)
<section aria-labelledby="hourly-rates-heading" class="space-y-4">
<div class="space-y-1">
<h2 id="hourly-rates-heading" class="text-xl font-semibold">{{ __('catalog.prices.hourly_rates') }}</h2>
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('catalog.price_list.hourly_help') }}</p>
</div>
@foreach ($this->hourlyRates as $group)
<table wire:key="rates-{{ $group->serviceUnitId }}" class="w-full border-collapse text-sm">
<caption class="pb-2 text-left text-lg font-medium break-words">{{ $group->serviceUnitName }}</caption>
<thead>
<tr class="border-b border-zinc-300 dark:border-zinc-600">
<th scope="col" class="py-2 pr-3 text-left font-semibold">{{ __('catalog.prices.work_type') }}</th>
<th scope="col" class="w-32 py-2 text-right font-semibold">{{ __('catalog.prices.hourly_rate') }}</th>
</tr>
</thead>
<tbody>
@foreach ($group->rows as $row)
<tr wire:key="rate-{{ $row->workTypeId }}" class="border-b border-zinc-200 dark:border-zinc-700">
<th scope="row" class="py-2 pr-3 text-left font-normal break-words">
@if ($row->serviceName !== null)
<span class="text-zinc-600 dark:text-zinc-300">{{ $row->serviceName }}:</span>
@endif
{{ $row->name }}
</th>
<td class="py-2 text-right whitespace-nowrap">
{{ $row->hourlyRateCents === null ? __('catalog.price_list.no_price') : $this->money($row->hourlyRateCents) }}
</td>
</tr>
@endforeach
</tbody>
</table>
@endforeach
</section>
<section aria-labelledby="other-products-heading" class="space-y-4">
<h2 id="other-products-heading" class="text-xl font-semibold">{{ __('catalog.price_list.other_products') }}</h2>
@if ($this->otherProducts === [])
<p class="text-sm">{{ __('catalog.products.none') }}</p>
@else
<x-table :headers="$this->otherHeaders()" :rows="$this->otherProducts">
@interact('column_name', $product)
<span class="font-medium break-words">{{ $product->name }}</span>
@endinteract
@interact('column_unit', $product)
{{ __('catalog.units.'.$product->unit->value) }}
@endinteract
@interact('column_net', $product)
<span class="whitespace-nowrap">{{ $this->money($product->netCents) }}</span>
@endinteract
@interact('column_gross', $product)
<span class="whitespace-nowrap">{{ $this->money($product->grossCents) }}</span>
@endinteract
@interact('column_vat', $product)
<span class="whitespace-nowrap">{{ $this->percentage($product->vatBasisPoints) }}</span>
@endinteract
</x-table>
@endif
</section>
<section aria-labelledby="mileage-heading" class="space-y-2">
<h2 id="mileage-heading" class="text-xl font-semibold">{{ __('catalog.mileage.title') }}</h2>
<p>
{{ $this->mileageRateCents === null
? __('catalog.price_list.no_price')
: __('catalog.price_list.mileage_value', ['rate' => $this->money($this->mileageRateCents)]) }}
</p>
</section>
@else
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('catalog.price_list.no_permission') }}</p>
@endif
</div>
@@ -4,23 +4,30 @@ use App\Modules\Catalog\Application\Contracts\PriceListQueries;
use App\Modules\Catalog\Application\DTOs\HourlyRateGroup;
use App\Modules\Catalog\Application\DTOs\HourlyRateRow;
use App\Modules\Catalog\Application\DTOs\ProductSummary;
use App\Modules\Catalog\Application\DTOs\VatRateSummary;
use App\Modules\Catalog\Application\UseCases\MoveProduct;
use App\Modules\Catalog\Application\UseCases\MoveVatRate;
use App\Modules\Catalog\Application\UseCases\SetHourlyRate;
use App\Modules\Catalog\Application\UseCases\SetMileageRate;
use App\Modules\Catalog\Application\UseCases\SetProductActive;
use App\Modules\Catalog\Application\UseCases\SetVatRateActive;
use App\Modules\Catalog\Domain\Enums\MoveDirection;
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
use App\Modules\Catalog\Domain\Exceptions\DefaultVatRateMustBeActive;
use App\Modules\Catalog\Domain\Exceptions\ProductNotFound;
use App\Modules\Catalog\Domain\Exceptions\VatRateNotFound;
use App\Modules\Catalog\Domain\ValueObjects\ProductDuration;
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
use App\Modules\Shared\Domain\ValueObjects\Money;
use App\Modules\Shared\Domain\ValueObjects\VatPercentage;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* Price list (hinnasto): hourly rates of work types, products and the
* mileage rate. All prices are without VAT.
* Price list management (hinnasto): hourly rates of work types, products,
* the mileage rate and VAT rates. Prices are without VAT, except consumer
* product prices, which are entered with VAT.
*/
new class extends Component
{
@@ -105,24 +112,34 @@ new class extends Component
}
/**
* Called by the row actions shared with the catalog page, so the level is always "product".
* @return list<VatRateSummary>
*/
public function move(string $level, int $id, string $direction, MoveProduct $moveProduct): void
#[Computed]
public function vatRates(): array
{
return $this->priceList->vatRates($this->showInactive);
}
/**
* Called by the row actions shared with the catalog page; the level is "product" or "vat_rate".
*/
public function move(string $level, int $id, string $direction, MoveProduct $moveProduct, MoveVatRate $moveVatRate): void
{
$this->authorize('catalog.manage');
$moveDirection = MoveDirection::from($direction);
$name = $this->priceList->findProduct($id)?->name ?? '';
$isVatRate = $level === 'vat_rate';
$name = ($isVatRate ? $this->priceList->findVatRate($id)?->name : $this->priceList->findProduct($id)?->name) ?? '';
try {
$moved = $moveProduct->execute($id, $moveDirection);
} catch (ProductNotFound) {
$moved = $isVatRate ? $moveVatRate->execute($id, $moveDirection) : $moveProduct->execute($id, $moveDirection);
} catch (ProductNotFound|VatRateNotFound) {
$this->statusMessage = __('catalog.status.not_found');
return;
}
unset($this->products);
unset($this->products, $this->vatRates);
$this->statusMessage = __(match (true) {
$moved && $moveDirection === MoveDirection::Up => 'catalog.status.moved_up',
@@ -132,13 +149,20 @@ new class extends Component
}, ['name' => $name]);
// The row moved in the DOM: keep focus on the same button.
$this->js("document.getElementById('move-{$moveDirection->value}-product-{$id}')?.focus()");
$level = $isVatRate ? 'vat_rate' : 'product';
$this->js("document.getElementById('move-{$moveDirection->value}-{$level}-{$id}')?.focus()");
}
public function toggleActive(string $level, int $id, SetProductActive $setActive): void
public function toggleActive(string $level, int $id, SetProductActive $setActive, SetVatRateActive $setVatRateActive): void
{
$this->authorize('catalog.manage');
if ($level === 'vat_rate') {
$this->toggleVatRate($id, $setVatRateActive);
return;
}
$product = $this->priceList->findProduct($id);
if ($product === null) {
@@ -181,13 +205,33 @@ new class extends Component
return [
['index' => 'name', 'label' => __('catalog.fields.name'), 'sortable' => false],
['index' => 'unit', 'label' => __('catalog.products.unit'), 'sortable' => false],
['index' => 'price', 'label' => __('catalog.products.price_column'), 'sortable' => false, 'align' => 'right'],
['index' => 'net', 'label' => __('catalog.products.net_column'), 'sortable' => false, 'align' => 'right'],
['index' => 'gross', 'label' => __('catalog.products.gross_column'), 'sortable' => false, 'align' => 'right'],
['index' => 'vat', 'label' => __('catalog.products.vat_column'), 'sortable' => false],
['index' => 'consumer', 'label' => __('catalog.products.consumer_column'), 'sortable' => false],
['index' => 'duration', 'label' => __('catalog.products.duration_column'), 'sortable' => false],
['index' => 'actions', 'label' => __('catalog.products.actions'), 'sortable' => false],
];
}
/**
* @return list<array{index: string, label: string, sortable: bool}>
*/
public function vatRateHeaders(): array
{
return [
['index' => 'name', 'label' => __('catalog.fields.name'), 'sortable' => false],
['index' => 'percentage', 'label' => __('catalog.vat_rates.percentage_column'), 'sortable' => false],
['index' => 'default', 'label' => __('catalog.vat_rates.default_column'), 'sortable' => false],
['index' => 'actions', 'label' => __('catalog.products.actions'), 'sortable' => false],
];
}
public function percentage(int $basisPoints): string
{
return (new VatPercentage($basisPoints))->format();
}
public function money(?int $cents): string
{
return $cents === null ? '' : Money::fromCents($cents)->format();
@@ -208,6 +252,33 @@ new class extends Component
return $this->view()->title(__('catalog.prices.title'));
}
private function toggleVatRate(int $id, SetVatRateActive $setActive): void
{
$vatRate = $this->priceList->findVatRate($id);
$this->errorMessage = '';
if ($vatRate === null) {
$this->statusMessage = __('catalog.status.not_found');
return;
}
try {
$setActive->execute($id, ! $vatRate->isActive);
} catch (DefaultVatRateMustBeActive) {
$this->errorMessage = __('catalog.errors.default_vat_rate_active', ['name' => $vatRate->name]);
// The checkbox was unticked in the browser: render it ticked again.
unset($this->vatRates);
$this->js("document.getElementById('active-vat_rate-{$id}').checked = true");
return;
}
unset($this->vatRates, $this->products);
$this->statusMessage = __($vatRate->isActive ? 'catalog.vat_rates.deactivated' : 'catalog.status.activated', ['name' => $vatRate->name]);
}
private function rateRow(int $workTypeId): ?HourlyRateRow
{
foreach ($this->priceList->hourlyRates() as $group) {
@@ -320,8 +391,22 @@ new class extends Component
{{ __('catalog.units.'.$product->unit->value) }}
@endinteract
@interact('column_price', $product)
<span class="whitespace-nowrap">{{ $this->money($product->priceCents) }}</span>
@interact('column_net', $product)
<span class="whitespace-nowrap">{{ $this->money($product->netCents) }}</span>
@unless ($product->priceIncludesVat)
<span class="block text-xs text-zinc-600 dark:text-zinc-300">{{ __('catalog.products.entered') }}</span>
@endunless
@endinteract
@interact('column_gross', $product)
<span class="whitespace-nowrap">{{ $this->money($product->grossCents) }}</span>
@if ($product->priceIncludesVat)
<span class="block text-xs text-zinc-600 dark:text-zinc-300">{{ __('catalog.products.entered') }}</span>
@endif
@endinteract
@interact('column_vat', $product)
<span class="whitespace-nowrap">{{ $this->percentage($product->vatBasisPoints) }}</span>
@endinteract
@interact('column_consumer', $product)
@@ -352,6 +437,41 @@ new class extends Component
@endif
</section>
<section aria-labelledby="vat-rates-heading" class="space-y-4">
<div class="flex flex-wrap items-center justify-between gap-4">
<h2 id="vat-rates-heading" class="text-xl font-semibold">{{ __('catalog.vat_rates.title') }}</h2>
<x-button :href="route('catalog.vat-rates.create')" wire:navigate icon="plus" :text="__('catalog.vat_rates.add')" />
</div>
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('catalog.vat_rates.help') }}</p>
<x-table :headers="$this->vatRateHeaders()" :rows="$this->vatRates">
@interact('column_name', $vatRate)
<span class="font-medium break-words">{{ $vatRate->name }}</span>
@unless ($vatRate->isActive)
<span class="ml-1 text-sm">({{ __('catalog.inactive') }})</span>
@endunless
@endinteract
@interact('column_percentage', $vatRate)
<span class="whitespace-nowrap">{{ $this->percentage($vatRate->basisPoints) }}</span>
@endinteract
@interact('column_default', $vatRate)
{{ $vatRate->isDefault ? __('catalog.vat_rates.is_default') : '' }}
@endinteract
@interact('column_actions', $vatRate)
<x-catalog.item-actions
level="vat_rate"
:id="$vatRate->id"
:name="$vatRate->name"
:active="$vatRate->isActive"
:edit-url="route('catalog.vat-rates.edit', $vatRate->id)"
/>
@endinteract
</x-table>
</section>
<section aria-labelledby="mileage-heading" class="space-y-4">
<h2 id="mileage-heading" class="text-xl font-semibold">{{ __('catalog.mileage.title') }}</h2>
@@ -2,6 +2,7 @@
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
use App\Modules\Catalog\Application\Contracts\PriceListQueries;
use App\Modules\Catalog\Application\DTOs\VatRateSummary;
use App\Modules\Catalog\Application\DTOs\WorkTypeSummary;
use App\Modules\Catalog\Application\UseCases\AddProduct;
use App\Modules\Catalog\Application\UseCases\UpdateProduct;
@@ -9,6 +10,9 @@ use App\Modules\Catalog\Domain\Enums\ProductUnit;
use App\Modules\Catalog\Domain\Exceptions\ProductNotFound;
use App\Modules\Catalog\Presentation\Livewire\Forms\ProductForm;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
use App\Modules\Shared\Domain\ValueObjects\Money;
use App\Modules\Shared\Domain\ValueObjects\VatPercentage;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Component;
@@ -27,6 +31,12 @@ new class extends Component
#[Locked]
public ?int $storedWorkTypeId = null;
/**
* The stored VAT rate, listed even when it is no longer in use.
*/
#[Locked]
public ?int $storedVatRateId = null;
public ProductForm $form;
public function mount(PriceListQueries $priceList, ?int $product = null): void
@@ -40,8 +50,54 @@ new class extends Component
$this->productId = $summary->id;
$this->storedWorkTypeId = $summary->workTypeId;
$this->storedVatRateId = $summary->vatRateId;
$this->form->fillFrom($summary);
return;
}
$this->form->vat_rate_id = (string) ($priceList->defaultVatRate()?->id ?? '');
}
/**
* Active VAT rates, plus the stored one if it is no longer in use.
*
* @return list<VatRateSummary>
*/
#[Computed]
public function vatRates(): array
{
$priceList = app(PriceListQueries::class);
return array_values(array_filter(
$priceList->vatRates(),
fn (VatRateSummary $vatRate): bool => $vatRate->isActive || $vatRate->id === $this->storedVatRateId,
));
}
/**
* The price on the other side of VAT, e.g. "14,34 € ALV 0" for a consumer
* price of 18,00 €; empty while the price or rate is not valid.
*/
public function otherPrice(): string
{
$vatRate = collect($this->vatRates)->first(fn (VatRateSummary $rate): bool => (string) $rate->id === $this->form->vat_rate_id);
try {
$price = Money::parse($this->form->price);
} catch (InvalidMoneyAmount) {
return '';
}
if ($price === null || $vatRate === null) {
return '';
}
$vat = new VatPercentage($vatRate->basisPoints);
return $this->form->is_consumer_product
? __('catalog.products.net_equivalent', ['price' => $price->removeVat($vat)->format()])
: __('catalog.products.gross_equivalent', ['price' => $price->addVat($vat)->format()]);
}
/**
@@ -135,18 +191,29 @@ new class extends Component
@endforeach
</x-select.native>
<x-input
wire:model="form.price"
:label="__('catalog.products.price')"
:hint="__('catalog.products.price_help')"
inputmode="decimal"
autocomplete="off"
required
/>
<div class="space-y-1">
<x-checkbox id="product-consumer" wire:model.live="form.is_consumer_product" :label="__('catalog.products.consumer')" />
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('catalog.products.consumer_help') }}</p>
</div>
<x-select.native wire:model.live="form.vat_rate_id" :label="__('catalog.products.vat_rate')" :aria-label="__('catalog.products.vat_rate')">
@foreach ($this->vatRates as $vatRate)
<option wire:key="vat-rate-option-{{ $vatRate->id }}" value="{{ $vatRate->id }}">
{{ $vatRate->name }} ({{ (new VatPercentage($vatRate->basisPoints))->format() }})@unless ($vatRate->isActive), {{ __('catalog.inactive') }}@endunless
</option>
@endforeach
</x-select.native>
<div class="space-y-1">
<x-checkbox id="product-consumer" wire:model="form.is_consumer_product" :label="__('catalog.products.consumer')" />
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('catalog.products.consumer_help') }}</p>
<x-input
wire:model.blur="form.price"
:label="__($form->is_consumer_product ? 'catalog.products.price_gross' : 'catalog.products.price_net')"
:hint="__($form->is_consumer_product ? 'catalog.products.price_gross_help' : 'catalog.products.price_help')"
inputmode="decimal"
autocomplete="off"
required
/>
<p aria-live="polite" class="min-h-5 text-sm">{{ $this->otherPrice() }}</p>
</div>
<fieldset class="space-y-4 rounded-lg border border-zinc-200 p-4 dark:border-zinc-700">
@@ -0,0 +1,138 @@
<?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>
@@ -32,4 +32,15 @@ Route::middleware(['web', 'auth', 'can:catalog.manage'])
Route::livewire('/products/{product}/edit', 'catalog::product-form')
->whereNumber('product')
->name('products.edit');
Route::livewire('/vat-rates/create', 'catalog::vat-rate-form')->name('vat-rates.create');
Route::livewire('/vat-rates/{vatRate}/edit', 'catalog::vat-rate-form')
->whereNumber('vatRate')
->name('vat-rates.edit');
});
Route::middleware(['web', 'auth', 'can:catalog.view-prices'])
->prefix('catalog')
->name('catalog.')
->group(function () {
Route::livewire('/price-list', 'catalog::price-list')->name('price-list');
});