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:
@@ -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>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user