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'),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user