Files
Toiminnanohjaus/app/Modules/Catalog/Presentation/Livewire/⚡prices.blade.php
T
petriandClaude Opus 5.5 f4b58f6b1d Darken the dark canvas, add customer colours, the ongoing status and the board model
- Dark theme page background is #5a5e60; text on it is lifted to zinc-200 /
  primary-100 and TallStackUI's dark labels, hints and checkbox labels are
  lightened so everything stays at 4.5:1.
- Shared HexColor picks white or black text by the higher WCAG contrast; a
  native-dialog colour picker and a large work dialog component.
- Customers get a header colour (customers.header_color), set on the
  customer page.
- New job status Jatkuvat / Kausi (ongoing): open, never late, billed
  monthly like running jobs.
- Kanban model: board columns and views, urgent threshold, per-user board
  preferences (density, column colours and sort), card positions, tasks of
  continuing jobs pinned to the board, MoveBoardCard and board queries with
  logged hours from TimeTracking.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-30 06:11:49 +02:00

572 lines
24 KiB
PHP

<?php
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\SetDefaultExpenseMarkup;
use App\Modules\Catalog\Application\UseCases\SetHourlyRate;
use App\Modules\Catalog\Application\UseCases\SetInternalHourlyCost;
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\Exceptions\InvalidVatPercentage;
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 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
{
public bool $showInactive = true;
public string $statusMessage = '';
public string $errorMessage = '';
/**
* Hourly rates that were not saved, shown back in their cell with the error.
*
* @var array<int, string>
*/
#[Locked]
public array $rejected = [];
public string $mileageRate = '';
public string $expenseMarkup = '';
public string $internalHourlyCost = '';
protected PriceListQueries $priceList;
public function boot(PriceListQueries $priceList): void
{
$this->priceList = $priceList;
}
public function mount(): void
{
$this->authorize('catalog.manage');
$this->mileageRate = $this->inputValue($this->priceList->mileageRateCents());
$this->internalHourlyCost = $this->inputValue($this->priceList->internalHourlyCostCents());
$markup = $this->priceList->defaultExpenseMarkupBasisPoints();
$this->expenseMarkup = $markup === 0 ? '' : (new VatPercentage($markup))->inputValue();
}
/**
* @return list<HourlyRateGroup>
*/
#[Computed]
public function hourlyRates(): array
{
return $this->priceList->hourlyRates($this->showInactive);
}
/**
* @return list<ProductSummary>
*/
#[Computed]
public function products(): array
{
return $this->priceList->products($this->showInactive);
}
public function saveRate(int $workTypeId, string $value, SetHourlyRate $setHourlyRate): void
{
$this->authorize('catalog.manage');
$row = $this->rateRow($workTypeId);
if ($row === null) {
return;
}
$this->resetErrorBag("rates.{$workTypeId}");
unset($this->rejected[$workTypeId]);
$this->errorMessage = '';
try {
$rate = $setHourlyRate->execute($workTypeId, $value);
} catch (InvalidMoneyAmount|CatalogItemNotFound $exception) {
$message = __($exception instanceof InvalidMoneyAmount ? 'catalog.errors.invalid_price' : 'catalog.status.not_found');
$this->addError("rates.{$workTypeId}", $message);
$this->errorMessage = $row->name.': '.$message;
$this->rejected[$workTypeId] = $value;
return;
}
unset($this->hourlyRates);
$this->statusMessage = $rate === null
? __('catalog.prices.rate_removed', ['name' => $row->name])
: __('catalog.prices.rate_saved', ['name' => $row->name, 'rate' => $rate->format()]);
}
/**
* @return list<VatRateSummary>
*/
#[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);
$isVatRate = $level === 'vat_rate';
$name = ($isVatRate ? $this->priceList->findVatRate($id)?->name : $this->priceList->findProduct($id)?->name) ?? '';
try {
$moved = $isVatRate ? $moveVatRate->execute($id, $moveDirection) : $moveProduct->execute($id, $moveDirection);
} catch (ProductNotFound|VatRateNotFound) {
$this->statusMessage = __('catalog.status.not_found');
return;
}
unset($this->products, $this->vatRates);
$this->statusMessage = __(match (true) {
$moved && $moveDirection === MoveDirection::Up => 'catalog.status.moved_up',
$moved => 'catalog.status.moved_down',
$moveDirection === MoveDirection::Up => 'catalog.status.already_first',
default => 'catalog.status.already_last',
}, ['name' => $name]);
// The row moved in the DOM: keep focus on the same button.
$level = $isVatRate ? 'vat_rate' : 'product';
$this->js("document.getElementById('move-{$moveDirection->value}-{$level}-{$id}')?.focus()");
}
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) {
$this->statusMessage = __('catalog.status.not_found');
return;
}
$setActive->execute($id, ! $product->isActive);
unset($this->products);
$this->statusMessage = __($product->isActive ? 'catalog.products.deactivated' : 'catalog.status.activated', ['name' => $product->name]);
}
public function saveExpenseMarkup(SetDefaultExpenseMarkup $setMarkup): void
{
$this->authorize('catalog.manage');
$this->resetErrorBag('expenseMarkup');
try {
$markup = $setMarkup->execute($this->expenseMarkup);
} catch (InvalidVatPercentage) {
$this->addError('expenseMarkup', __('catalog.errors.invalid_markup'));
return;
}
$this->expenseMarkup = $markup === 0 ? '' : (new VatPercentage($markup))->inputValue();
$this->statusMessage = $markup === 0
? __('catalog.expense_markup.removed')
: __('catalog.expense_markup.saved', ['markup' => (new VatPercentage($markup))->format()]);
}
public function saveMileageRate(SetMileageRate $setMileageRate): void
{
$this->authorize('catalog.manage');
$this->resetErrorBag('mileageRate');
try {
$rate = $setMileageRate->execute($this->mileageRate);
} catch (InvalidMoneyAmount) {
$this->addError('mileageRate', __('catalog.errors.invalid_price'));
return;
}
$this->mileageRate = $rate?->inputValue() ?? '';
$this->statusMessage = $rate === null
? __('catalog.mileage.removed')
: __('catalog.mileage.saved', ['rate' => $rate->format()]);
}
public function saveInternalHourlyCost(SetInternalHourlyCost $setInternalHourlyCost): void
{
$this->authorize('catalog.manage');
$this->resetErrorBag('internalHourlyCost');
try {
$cost = $setInternalHourlyCost->execute($this->internalHourlyCost);
} catch (InvalidMoneyAmount) {
$this->addError('internalHourlyCost', __('catalog.errors.invalid_price'));
return;
}
$this->internalHourlyCost = $cost?->inputValue() ?? '';
$this->statusMessage = $cost === null
? __('catalog.internal_cost.removed')
: __('catalog.internal_cost.saved', ['cost' => $cost->format()]);
}
/**
* @return list<array{index: string, label: string, sortable: bool, align?: string}>
*/
public function productHeaders(): array
{
return [
['index' => 'name', 'label' => __('catalog.fields.name'), '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],
['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();
}
public function inputValue(?int $cents): string
{
return $cents === null ? '' : Money::fromCents($cents)->inputValue();
}
public function duration(int $quarters): string
{
return (new ProductDuration($quarters))->format();
}
public function render(): mixed
{
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) {
foreach ($group->rows as $row) {
if ($row->workTypeId === $workTypeId) {
return $row;
}
}
}
return null;
}
};
?>
<div class="mx-auto max-w-5xl space-y-8 p-4 sm:p-6">
<div class="space-y-2">
<x-page-title icon="tag">{{ __('catalog.prices.title') }}</x-page-title>
<p class="text-sm text-zinc-600 dark:text-zinc-200">{{ __('catalog.prices.intro') }}</p>
</div>
@if (session('status'))
<x-alert role="status" color="green" light icon="check-circle" :text="session('status')" />
@endif
<x-checkbox id="show-inactive" wire:model.live="showInactive" :label="__('catalog.show_inactive')" />
<div class="space-y-1">
<p role="status" aria-live="polite" class="min-h-5 text-sm">{{ $statusMessage }}</p>
<p role="alert" class="text-sm font-medium text-red-700 dark:text-red-400">{{ $errorMessage }}</p>
</div>
<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-200">{{ __('catalog.prices.hourly_rates_help') }}</p>
</div>
@foreach ($this->hourlyRates as $group)
@if ($group->rows !== [])
{{-- An input grid is a form, so a native table rather than <x-table>. --}}
<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 }}
@unless ($group->isActive)
<span class="ml-2 text-sm font-normal">({{ __('catalog.inactive') }})</span>
@endunless
</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-left 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 align-top 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-200">{{ $row->serviceName }}:</span>
@endif
{{ $row->name }}
@unless ($row->isActive)
<span class="ml-1">({{ __('catalog.inactive') }})</span>
@endunless
</th>
<td class="py-2">
<input
id="rate-{{ $row->workTypeId }}"
type="text"
inputmode="decimal"
autocomplete="off"
value="{{ $rejected[$row->workTypeId] ?? $this->inputValue($row->hourlyRateCents) }}"
wire:change="saveRate({{ $row->workTypeId }}, $event.target.value)"
aria-label="{{ __('catalog.prices.rate_label', ['name' => ($row->serviceName !== null ? $row->serviceName.': ' : '').$row->name, 'unit' => $group->serviceUnitName]) }}"
@error('rates.'.$row->workTypeId) aria-invalid="true" aria-describedby="rate-{{ $row->workTypeId }}-error" @enderror
class="w-24 rounded-md border border-zinc-400 bg-white px-2 py-1.5 text-right text-zinc-900 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary-600 dark:focus-visible:outline-primary-300 aria-invalid:border-red-700 dark:border-zinc-500 dark:bg-zinc-900 dark:text-zinc-100 dark:aria-invalid:border-red-400"
/>
@error('rates.'.$row->workTypeId)
<p id="rate-{{ $row->workTypeId }}-error" class="mt-1 text-xs text-red-700 dark:text-red-400">{{ $message }}</p>
@enderror
</td>
</tr>
@endforeach
</tbody>
</table>
@endif
@endforeach
</section>
<section aria-labelledby="products-heading" class="space-y-4">
<div class="flex flex-wrap items-center justify-between gap-4">
<h2 id="products-heading" class="text-xl font-semibold">{{ __('catalog.products.title') }}</h2>
<x-button :href="route('catalog.products.create')" wire:navigate icon="plus" :text="__('catalog.products.add')" />
</div>
<p class="text-sm text-zinc-600 dark:text-zinc-200">{{ __('catalog.products.help') }}</p>
@if ($this->products === [])
<x-empty-state icon="tag">{{ __('catalog.products.none') }}</x-empty-state>
@else
<x-table :headers="$this->productHeaders()" :rows="$this->products">
@interact('column_name', $product)
<span class="font-medium break-words">{{ $product->name }}</span>
@unless ($product->isActive)
<span class="ml-1 text-sm">({{ __('catalog.inactive') }})</span>
@endunless
@endinteract
@interact('column_unit', $product)
{{ __('catalog.units.'.$product->unit->value) }}
@endinteract
@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-200">{{ __('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-200">{{ __('catalog.products.entered') }}</span>
@endif
@endinteract
@interact('column_vat', $product)
<span class="whitespace-nowrap">{{ $this->percentage($product->vatBasisPoints) }}</span>
@endinteract
@interact('column_consumer', $product)
{{ $product->isConsumerProduct ? __('catalog.yes') : __('catalog.no') }}
@endinteract
@interact('column_duration', $product)
@if ($product->durationQuarters > 0)
{{ __('catalog.products.duration_value', ['hours' => $this->duration($product->durationQuarters)]) }}
@if ($product->workTypeName !== null)
<span class="block text-xs text-zinc-600 dark:text-zinc-200">{{ $product->workTypeName }}</span>
@endif
@else
{{ __('catalog.products.no_duration') }}
@endif
@endinteract
@interact('column_actions', $product)
<x-catalog.item-actions
level="product"
:id="$product->id"
:name="$product->name"
:active="$product->isActive"
:edit-url="route('catalog.products.edit', $product->id)"
/>
@endinteract
</x-table>
@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-200">{{ __('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="card space-y-4">
<h2 id="mileage-heading" class="text-xl font-semibold">{{ __('catalog.expense_markup.section') }}</h2>
<form wire:submit="saveMileageRate" class="flex max-w-md flex-wrap items-end gap-3" novalidate>
<div class="min-w-40 flex-1">
<x-input
wire:model="mileageRate"
:label="__('catalog.mileage.rate')"
:hint="__('catalog.mileage.help')"
inputmode="decimal"
autocomplete="off"
/>
</div>
<x-button submit :text="__('catalog.save')" />
</form>
<form wire:submit="saveExpenseMarkup" class="flex max-w-md flex-wrap items-end gap-3" novalidate>
<div class="min-w-40 flex-1">
<x-input
wire:model="expenseMarkup"
:label="__('catalog.expense_markup.label')"
:hint="__('catalog.expense_markup.help')"
inputmode="decimal"
autocomplete="off"
/>
</div>
<x-button submit :text="__('catalog.save')" />
</form>
</section>
<section aria-labelledby="internal-cost-heading" class="card space-y-4">
<h2 id="internal-cost-heading" class="text-xl font-semibold">{{ __('catalog.internal_cost.section') }}</h2>
<form wire:submit="saveInternalHourlyCost" class="flex max-w-md flex-wrap items-end gap-3" novalidate>
<div class="min-w-40 flex-1">
<x-input
wire:model="internalHourlyCost"
:label="__('catalog.internal_cost.label')"
:hint="__('catalog.internal_cost.help')"
inputmode="decimal"
autocomplete="off"
/>
</div>
<x-button submit :text="__('catalog.save')" />
</form>
</section>
</div>