*/ #[Locked] public array $rejected = []; public string $mileageRate = ''; public string $expenseMarkup = ''; 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()); $markup = $this->priceList->defaultExpenseMarkupBasisPoints(); $this->expenseMarkup = $markup === 0 ? '' : (new VatPercentage($markup))->inputValue(); } /** * @return list */ #[Computed] public function hourlyRates(): array { return $this->priceList->hourlyRates($this->showInactive); } /** * @return list */ #[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 */ #[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()]); } /** * @return list */ 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 */ 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; } }; ?>

{{ __('catalog.prices.title') }}

{{ __('catalog.prices.intro') }}

@if (session('status')) @endif

{{ $statusMessage }}

{{ __('catalog.prices.hourly_rates') }}

{{ __('catalog.prices.hourly_rates_help') }}

@foreach ($this->hourlyRates as $group) @if ($group->rows !== []) {{-- An input grid is a form, so a native table rather than . --}} @foreach ($group->rows as $row) @endforeach
{{ $group->serviceUnitName }} @unless ($group->isActive) ({{ __('catalog.inactive') }}) @endunless
{{ __('catalog.prices.work_type') }} {{ __('catalog.prices.hourly_rate') }}
@if ($row->serviceName !== null) {{ $row->serviceName }}: @endif {{ $row->name }} @unless ($row->isActive) ({{ __('catalog.inactive') }}) @endunless 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-blue-700 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)

{{ $message }}

@enderror
@endif @endforeach

{{ __('catalog.products.title') }}

{{ __('catalog.products.help') }}

@if ($this->products === [])

{{ __('catalog.products.none') }}

@else @interact('column_name', $product) {{ $product->name }} @unless ($product->isActive) ({{ __('catalog.inactive') }}) @endunless @endinteract @interact('column_unit', $product) {{ __('catalog.units.'.$product->unit->value) }} @endinteract @interact('column_net', $product) {{ $this->money($product->netCents) }} @unless ($product->priceIncludesVat) {{ __('catalog.products.entered') }} @endunless @endinteract @interact('column_gross', $product) {{ $this->money($product->grossCents) }} @if ($product->priceIncludesVat) {{ __('catalog.products.entered') }} @endif @endinteract @interact('column_vat', $product) {{ $this->percentage($product->vatBasisPoints) }} @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) {{ $product->workTypeName }} @endif @else {{ __('catalog.products.no_duration') }} @endif @endinteract @interact('column_actions', $product) @endinteract @endif

{{ __('catalog.vat_rates.title') }}

{{ __('catalog.vat_rates.help') }}

@interact('column_name', $vatRate) {{ $vatRate->name }} @unless ($vatRate->isActive) ({{ __('catalog.inactive') }}) @endunless @endinteract @interact('column_percentage', $vatRate) {{ $this->percentage($vatRate->basisPoints) }} @endinteract @interact('column_default', $vatRate) {{ $vatRate->isDefault ? __('catalog.vat_rates.is_default') : '' }} @endinteract @interact('column_actions', $vatRate) @endinteract

{{ __('catalog.expense_markup.section') }}