Add the price list: hourly rates, products and mileage rate
- Work types get an hourly rate (VAT 0), set on the work type form or in the hourly rate grid of the new Hinnasto page (/catalog/prices), which saves a cell when focus leaves it. - Fixed-price products with a unit from a list, consumer product flag and automatic hours, which need a work type. Products are moved and deactivated, never deleted. - The mileage rate is stored in a new settings table through the shared Settings contract. - Shared Money value object keeps amounts in cents, parses "85,50" and formats "85,50 €". - Prices are read only through PriceListQueries, not CatalogQueries, so personal money permissions can be added in one place next. - Only admins and management can open the pages for now. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -7,10 +7,11 @@ use App\Modules\Catalog\Domain\Exceptions\InvalidCatalogName;
|
||||
use App\Modules\Catalog\Domain\Exceptions\ServiceNotInUnit;
|
||||
use App\Modules\Catalog\Domain\Exceptions\SharedUnitHasNoServices;
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
|
||||
use Livewire\Form;
|
||||
|
||||
/**
|
||||
* Name (and for work types, service) of a service unit, service or work type.
|
||||
* Name (and for work types, service and hourly rate) of a service unit, service or work type.
|
||||
*/
|
||||
class CatalogItemForm extends Form
|
||||
{
|
||||
@@ -21,6 +22,11 @@ class CatalogItemForm extends Form
|
||||
*/
|
||||
public string $service_id = '';
|
||||
|
||||
/**
|
||||
* Only for work types, as typed ("85,50"); '' means no rate.
|
||||
*/
|
||||
public string $hourly_rate = '';
|
||||
|
||||
public function validatedName(): string
|
||||
{
|
||||
$this->validate();
|
||||
@@ -43,6 +49,7 @@ class CatalogItemForm extends Form
|
||||
CatalogNameTaken::class => ['name', 'catalog.errors.name_taken'],
|
||||
ServiceNotInUnit::class => ['service_id', 'catalog.errors.service_not_in_unit'],
|
||||
SharedUnitHasNoServices::class => ['name', 'catalog.errors.shared_unit_has_no_services'],
|
||||
InvalidMoneyAmount::class => ['hourly_rate', 'catalog.errors.invalid_price'],
|
||||
default => throw $exception,
|
||||
};
|
||||
|
||||
@@ -57,6 +64,7 @@ class CatalogItemForm extends Form
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:100'],
|
||||
'service_id' => ['nullable', 'integer'],
|
||||
'hourly_rate' => ['nullable', 'string', 'max:20'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -69,6 +77,7 @@ class CatalogItemForm extends Form
|
||||
'name.required' => __('catalog.errors.name_required'),
|
||||
'name.max' => __('catalog.errors.invalid_name'),
|
||||
'service_id.integer' => __('catalog.errors.service_not_in_unit'),
|
||||
'hourly_rate.max' => __('catalog.errors.invalid_price'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Catalog\Presentation\Livewire\Forms;
|
||||
|
||||
use App\Modules\Catalog\Application\DTOs\ProductInput;
|
||||
use App\Modules\Catalog\Application\DTOs\ProductSummary;
|
||||
use App\Modules\Catalog\Domain\Enums\ProductUnit;
|
||||
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
|
||||
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\ValueObjects\ProductDuration;
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
|
||||
use App\Modules\Shared\Domain\ValueObjects\Money;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Livewire\Form;
|
||||
|
||||
/**
|
||||
* Fields of a product. Price and duration are kept as typed ("12,50") and
|
||||
* parsed by the use case.
|
||||
*/
|
||||
class ProductForm extends Form
|
||||
{
|
||||
public string $name = '';
|
||||
|
||||
public string $unit = 'piece';
|
||||
|
||||
public string $price = '';
|
||||
|
||||
public bool $is_consumer_product = false;
|
||||
|
||||
public string $duration = '';
|
||||
|
||||
/**
|
||||
* '' means no work type.
|
||||
*/
|
||||
public string $work_type_id = '';
|
||||
|
||||
public function fillFrom(ProductSummary $product): void
|
||||
{
|
||||
$this->name = $product->name;
|
||||
$this->unit = $product->unit->value;
|
||||
$this->price = Money::fromCents($product->priceCents)->inputValue();
|
||||
$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;
|
||||
}
|
||||
|
||||
public function toInput(): ProductInput
|
||||
{
|
||||
$this->validate();
|
||||
|
||||
return new ProductInput(
|
||||
$this->name,
|
||||
ProductUnit::from($this->unit),
|
||||
$this->price,
|
||||
$this->is_consumer_product,
|
||||
$this->duration,
|
||||
$this->work_type_id === '' ? null : (int) $this->work_type_id,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows a broken business rule as an error on the field it concerns.
|
||||
*/
|
||||
public function showDomainError(DomainException $exception): void
|
||||
{
|
||||
[$field, $key] = match ($exception::class) {
|
||||
InvalidCatalogName::class => ['name', 'catalog.errors.invalid_name'],
|
||||
ProductNameTaken::class => ['name', 'catalog.errors.product_name_taken'],
|
||||
InvalidMoneyAmount::class => ['price', 'catalog.errors.invalid_price'],
|
||||
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'],
|
||||
default => throw $exception,
|
||||
};
|
||||
|
||||
$this->addError($field, __($key));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array<int, mixed>>
|
||||
*/
|
||||
protected function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:100'],
|
||||
'unit' => ['required', Rule::enum(ProductUnit::class)],
|
||||
'price' => ['required', 'string', 'max:20'],
|
||||
'is_consumer_product' => ['boolean'],
|
||||
'duration' => ['nullable', 'string', 'max:10'],
|
||||
'work_type_id' => ['nullable', 'integer'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function messages(): array
|
||||
{
|
||||
return [
|
||||
'name.required' => __('catalog.errors.name_required'),
|
||||
'name.max' => __('catalog.errors.invalid_name'),
|
||||
'unit.required' => __('catalog.errors.invalid_unit'),
|
||||
'unit.enum' => __('catalog.errors.invalid_unit'),
|
||||
'price.required' => __('catalog.errors.price_required'),
|
||||
'price.max' => __('catalog.errors.invalid_price'),
|
||||
'duration.max' => __('catalog.errors.invalid_duration'),
|
||||
'work_type_id.integer' => __('catalog.errors.work_type_unavailable'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -119,7 +119,10 @@ new class extends Component
|
||||
<x-button :href="route('catalog.service-units.create')" wire:navigate icon="plus" :text="__('catalog.actions.add_service_unit')" />
|
||||
</div>
|
||||
|
||||
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('catalog.intro') }}</p>
|
||||
<div class="space-y-1 text-sm">
|
||||
<p class="text-zinc-600 dark:text-zinc-300">{{ __('catalog.intro') }}</p>
|
||||
<p><x-link :href="route('catalog.prices')" navigate>{{ __('catalog.prices_link') }}</x-link></p>
|
||||
</div>
|
||||
|
||||
@if (session('status'))
|
||||
<x-alert role="status" color="green" light icon="check-circle" :text="session('status')" />
|
||||
|
||||
@@ -0,0 +1,371 @@
|
||||
<?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\UseCases\MoveProduct;
|
||||
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\Domain\Enums\MoveDirection;
|
||||
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
|
||||
use App\Modules\Catalog\Domain\Exceptions\ProductNotFound;
|
||||
use App\Modules\Catalog\Domain\ValueObjects\ProductDuration;
|
||||
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
|
||||
use App\Modules\Shared\Domain\ValueObjects\Money;
|
||||
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.
|
||||
*/
|
||||
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 = '';
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
/**
|
||||
* @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()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by the row actions shared with the catalog page, so the level is always "product".
|
||||
*/
|
||||
public function move(string $level, int $id, string $direction, MoveProduct $moveProduct): void
|
||||
{
|
||||
$this->authorize('catalog.manage');
|
||||
|
||||
$moveDirection = MoveDirection::from($direction);
|
||||
$name = $this->priceList->findProduct($id)?->name ?? '';
|
||||
|
||||
try {
|
||||
$moved = $moveProduct->execute($id, $moveDirection);
|
||||
} catch (ProductNotFound) {
|
||||
$this->statusMessage = __('catalog.status.not_found');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
unset($this->products);
|
||||
|
||||
$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.
|
||||
$this->js("document.getElementById('move-{$moveDirection->value}-product-{$id}')?.focus()");
|
||||
}
|
||||
|
||||
public function toggleActive(string $level, int $id, SetProductActive $setActive): void
|
||||
{
|
||||
$this->authorize('catalog.manage');
|
||||
|
||||
$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 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<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' => 'price', 'label' => __('catalog.products.price_column'), 'sortable' => false, 'align' => 'right'],
|
||||
['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],
|
||||
];
|
||||
}
|
||||
|
||||
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 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">
|
||||
<h1 class="text-2xl font-semibold">{{ __('catalog.prices.title') }}</h1>
|
||||
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('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-300">{{ __('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-300">{{ $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-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)
|
||||
<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-300">{{ __('catalog.products.help') }}</p>
|
||||
|
||||
@if ($this->products === [])
|
||||
<p class="text-sm">{{ __('catalog.products.none') }}</p>
|
||||
@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_price', $product)
|
||||
<span class="whitespace-nowrap">{{ $this->money($product->priceCents) }}</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-300">{{ $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="mileage-heading" class="space-y-4">
|
||||
<h2 id="mileage-heading" class="text-xl font-semibold">{{ __('catalog.mileage.title') }}</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>
|
||||
</section>
|
||||
</div>
|
||||
@@ -0,0 +1,185 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||||
use App\Modules\Catalog\Application\Contracts\PriceListQueries;
|
||||
use App\Modules\Catalog\Application\DTOs\WorkTypeSummary;
|
||||
use App\Modules\Catalog\Application\UseCases\AddProduct;
|
||||
use App\Modules\Catalog\Application\UseCases\UpdateProduct;
|
||||
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 Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Component;
|
||||
|
||||
/**
|
||||
* Add page (no {product}) and edit page (with {product}) of a product.
|
||||
*/
|
||||
new class extends Component
|
||||
{
|
||||
#[Locked]
|
||||
public ?int $productId = null;
|
||||
|
||||
/**
|
||||
* The stored work type, listed even when it is no longer in use.
|
||||
*/
|
||||
#[Locked]
|
||||
public ?int $storedWorkTypeId = null;
|
||||
|
||||
public ProductForm $form;
|
||||
|
||||
public function mount(PriceListQueries $priceList, ?int $product = null): void
|
||||
{
|
||||
$this->authorize('catalog.manage');
|
||||
|
||||
if ($product !== null) {
|
||||
$summary = $priceList->findProduct($product);
|
||||
|
||||
abort_if($summary === null, 404);
|
||||
|
||||
$this->productId = $summary->id;
|
||||
$this->storedWorkTypeId = $summary->workTypeId;
|
||||
$this->form->fillFrom($summary);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Work types to choose from, grouped by service unit (and service).
|
||||
*
|
||||
* @return array<string, list<WorkTypeSummary>>
|
||||
*/
|
||||
#[Computed]
|
||||
public function workTypeGroups(): array
|
||||
{
|
||||
$catalogQueries = app(CatalogQueries::class);
|
||||
$groups = [];
|
||||
$listed = [];
|
||||
|
||||
foreach ($catalogQueries->tree(includeInactive: false) as $node) {
|
||||
if ($node->workTypes !== []) {
|
||||
$groups[$node->serviceUnit->name] = $node->workTypes;
|
||||
}
|
||||
|
||||
foreach ($node->services as $serviceNode) {
|
||||
if ($serviceNode->workTypes !== []) {
|
||||
$groups[$node->serviceUnit->name.' – '.$serviceNode->service->name] = $serviceNode->workTypes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($groups as $workTypes) {
|
||||
foreach ($workTypes as $workType) {
|
||||
$listed[] = $workType->id;
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->storedWorkTypeId !== null && ! in_array($this->storedWorkTypeId, $listed, true)) {
|
||||
$stored = $catalogQueries->findWorkType($this->storedWorkTypeId);
|
||||
|
||||
if ($stored !== null) {
|
||||
$groups[__('catalog.inactive')] = [$stored];
|
||||
}
|
||||
}
|
||||
|
||||
return $groups;
|
||||
}
|
||||
|
||||
public function save(AddProduct $addProduct, UpdateProduct $updateProduct): void
|
||||
{
|
||||
$this->authorize('catalog.manage');
|
||||
|
||||
$input = $this->form->toInput();
|
||||
|
||||
try {
|
||||
if ($this->productId === null) {
|
||||
$addProduct->execute($input);
|
||||
session()->flash('status', __('catalog.status.created', ['name' => trim($input->name)]));
|
||||
} else {
|
||||
$updateProduct->execute($this->productId, $input);
|
||||
session()->flash('status', __('catalog.status.updated', ['name' => trim($input->name)]));
|
||||
}
|
||||
} catch (ProductNotFound) {
|
||||
abort(404);
|
||||
} catch (DomainException $exception) {
|
||||
$this->form->showDomainError($exception);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->redirectRoute('catalog.prices', navigate: true);
|
||||
}
|
||||
|
||||
public function heading(): string
|
||||
{
|
||||
return __($this->productId === null ? 'catalog.products.create_title' : 'catalog.products.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>
|
||||
|
||||
<form wire:submit="save" class="space-y-6" novalidate>
|
||||
<x-input wire:model="form.name" :label="__('catalog.fields.name')" autocomplete="off" required />
|
||||
|
||||
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
|
||||
<x-select.native wire:model="form.unit" :label="__('catalog.products.unit')" :aria-label="__('catalog.products.unit')">
|
||||
@foreach (ProductUnit::cases() as $unit)
|
||||
<option wire:key="unit-{{ $unit->value }}" value="{{ $unit->value }}">{{ __('catalog.units.'.$unit->value) }}</option>
|
||||
@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="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>
|
||||
|
||||
<fieldset class="space-y-4 rounded-lg border border-zinc-200 p-4 dark:border-zinc-700">
|
||||
<legend class="px-1 font-medium">{{ __('catalog.products.auto_booking') }}</legend>
|
||||
|
||||
<x-input
|
||||
wire:model="form.duration"
|
||||
:label="__('catalog.products.duration')"
|
||||
:hint="__('catalog.products.duration_help')"
|
||||
inputmode="decimal"
|
||||
autocomplete="off"
|
||||
/>
|
||||
|
||||
<x-select.native
|
||||
wire:model="form.work_type_id"
|
||||
:label="__('catalog.products.work_type')"
|
||||
:aria-label="__('catalog.products.work_type')"
|
||||
:hint="__('catalog.products.work_type_help')"
|
||||
>
|
||||
<option value="">{{ __('catalog.products.no_work_type') }}</option>
|
||||
@foreach ($this->workTypeGroups as $groupName => $workTypes)
|
||||
<optgroup wire:key="work-type-group-{{ $loop->index }}" label="{{ $groupName }}">
|
||||
@foreach ($workTypes as $workType)
|
||||
<option wire:key="work-type-option-{{ $workType->id }}" value="{{ $workType->id }}">{{ $workType->name }}</option>
|
||||
@endforeach
|
||||
</optgroup>
|
||||
@endforeach
|
||||
</x-select.native>
|
||||
</fieldset>
|
||||
|
||||
<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>
|
||||
@@ -1,12 +1,14 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||||
use App\Modules\Catalog\Application\Contracts\PriceListQueries;
|
||||
use App\Modules\Catalog\Application\DTOs\ServiceSummary;
|
||||
use App\Modules\Catalog\Application\UseCases\AddWorkType;
|
||||
use App\Modules\Catalog\Application\UseCases\UpdateWorkType;
|
||||
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
|
||||
use App\Modules\Catalog\Presentation\Livewire\Forms\CatalogItemForm;
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
use App\Modules\Shared\Domain\ValueObjects\Money;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Component;
|
||||
@@ -27,7 +29,7 @@ new class extends Component
|
||||
|
||||
public CatalogItemForm $form;
|
||||
|
||||
public function mount(CatalogQueries $catalogQueries, ?int $serviceUnit = null, ?int $workType = null): void
|
||||
public function mount(CatalogQueries $catalogQueries, PriceListQueries $priceList, ?int $serviceUnit = null, ?int $workType = null): void
|
||||
{
|
||||
$this->authorize('catalog.manage');
|
||||
|
||||
@@ -40,6 +42,9 @@ new class extends Component
|
||||
$this->form->name = $summary->name;
|
||||
$this->form->service_id = $summary->serviceId === null ? '' : (string) $summary->serviceId;
|
||||
$serviceUnit = $summary->serviceUnitId;
|
||||
|
||||
$rateCents = $priceList->hourlyRateCentsOf($summary->id);
|
||||
$this->form->hourly_rate = $rateCents === null ? '' : Money::fromCents($rateCents)->inputValue();
|
||||
}
|
||||
|
||||
$unit = $serviceUnit === null ? null : $catalogQueries->findServiceUnit($serviceUnit);
|
||||
@@ -68,10 +73,10 @@ new class extends Component
|
||||
|
||||
try {
|
||||
if ($this->workTypeId === null) {
|
||||
$addWorkType->execute((int) $this->serviceUnitId, $serviceId, $name);
|
||||
$addWorkType->execute((int) $this->serviceUnitId, $serviceId, $name, $this->form->hourly_rate);
|
||||
session()->flash('status', __('catalog.status.created', ['name' => trim($name)]));
|
||||
} else {
|
||||
$updateWorkType->execute($this->workTypeId, $serviceId, $name);
|
||||
$updateWorkType->execute($this->workTypeId, $serviceId, $name, $this->form->hourly_rate);
|
||||
session()->flash('status', __('catalog.status.updated', ['name' => trim($name)]));
|
||||
}
|
||||
} catch (CatalogItemNotFound) {
|
||||
@@ -127,6 +132,14 @@ new class extends Component
|
||||
</x-select.native>
|
||||
@endif
|
||||
|
||||
<x-input
|
||||
wire:model="form.hourly_rate"
|
||||
:label="__('catalog.work_types.hourly_rate')"
|
||||
:hint="__('catalog.work_types.hourly_rate_help')"
|
||||
inputmode="decimal"
|
||||
autocomplete="off"
|
||||
/>
|
||||
|
||||
<div class="flex flex-wrap gap-3">
|
||||
<x-button submit :text="__('catalog.save')" />
|
||||
<x-button :href="route('catalog.index')" wire:navigate subtle color="black" :text="__('catalog.cancel')" />
|
||||
|
||||
@@ -26,4 +26,10 @@ Route::middleware(['web', 'auth', 'can:catalog.manage'])
|
||||
Route::livewire('/work-types/{workType}/edit', 'catalog::work-type-form')
|
||||
->whereNumber('workType')
|
||||
->name('work-types.edit');
|
||||
|
||||
Route::livewire('/prices', 'catalog::prices')->name('prices');
|
||||
Route::livewire('/products/create', 'catalog::product-form')->name('products.create');
|
||||
Route::livewire('/products/{product}/edit', 'catalog::product-form')
|
||||
->whereNumber('product')
|
||||
->name('products.edit');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user