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:
2026-11-24 07:30:49 +02:00
co-authored by Claude Opus 5.5
parent 1acdaa5b19
commit dc2612cf46
58 changed files with 2577 additions and 32 deletions
@@ -0,0 +1,99 @@
<?php
namespace App\Modules\Catalog\Infrastructure\Persistence\Repositories;
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
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\ServiceUnitNode;
use App\Modules\Catalog\Application\DTOs\WorkTypeSummary;
use App\Modules\Catalog\Application\UseCases\SetMileageRate;
use App\Modules\Catalog\Domain\Enums\ProductUnit;
use App\Modules\Catalog\Domain\ValueObjects\ProductDuration;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ProductModel;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use App\Modules\Shared\Application\Contracts\Settings;
class EloquentPriceListQueries implements PriceListQueries
{
public function __construct(
private CatalogQueries $catalogQueries,
private Settings $settings,
) {}
public function hourlyRates(bool $includeInactive = true): array
{
/** @var array<int, int|null> $rates */
$rates = WorkTypeModel::query()->pluck('hourly_rate_cents', 'id')->all();
return array_map(function (ServiceUnitNode $node) use ($rates): HourlyRateGroup {
$row = fn (WorkTypeSummary $workType, ?string $serviceName, bool $parentActive): HourlyRateRow => new HourlyRateRow(
$workType->id,
$workType->name,
$serviceName,
$workType->isActive && $parentActive,
$rates[$workType->id] ?? null,
);
$rows = array_map(fn (WorkTypeSummary $workType): HourlyRateRow => $row($workType, null, true), $node->workTypes);
foreach ($node->services as $serviceNode) {
foreach ($serviceNode->workTypes as $workType) {
$rows[] = $row($workType, $serviceNode->service->name, $serviceNode->service->isActive);
}
}
return new HourlyRateGroup($node->serviceUnit->id, $node->serviceUnit->name, $node->serviceUnit->isActive, $rows);
}, $this->catalogQueries->tree($includeInactive));
}
public function hourlyRateCentsOf(int $workTypeId): ?int
{
$cents = WorkTypeModel::query()->whereKey($workTypeId)->value('hourly_rate_cents');
return $cents === null ? null : (int) $cents;
}
public function products(bool $includeInactive = true): array
{
return ProductModel::query()
->with('workType')
->when(! $includeInactive, fn ($query) => $query->where('is_active', true))
->orderBy('sort_order')
->get()
->map(fn (ProductModel $model): ProductSummary => $this->toSummary($model))
->values()
->all();
}
public function findProduct(int $id): ?ProductSummary
{
$model = ProductModel::query()->with('workType')->find($id);
return $model === null ? null : $this->toSummary($model);
}
public function mileageRateCents(): ?int
{
$value = $this->settings->get(SetMileageRate::SETTING_KEY);
return $value === null ? null : (int) $value;
}
private function toSummary(ProductModel $model): ProductSummary
{
return new ProductSummary(
$model->id,
$model->name,
ProductUnit::from($model->unit),
$model->price_cents,
$model->is_consumer_product,
ProductDuration::fromDecimal((float) $model->auto_hours)->quarters,
$model->work_type_id,
$model->workType?->name,
$model->is_active,
);
}
}
@@ -0,0 +1,78 @@
<?php
namespace App\Modules\Catalog\Infrastructure\Persistence\Repositories;
use App\Modules\Catalog\Domain\Contracts\ProductRepository;
use App\Modules\Catalog\Domain\Entities\Product;
use App\Modules\Catalog\Domain\Enums\MoveDirection;
use App\Modules\Catalog\Domain\Enums\ProductUnit;
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
use App\Modules\Catalog\Domain\ValueObjects\ProductDuration;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ProductModel;
use App\Modules\Shared\Domain\ValueObjects\Money;
class EloquentProductRepository implements ProductRepository
{
public function findById(int $id): ?Product
{
$model = ProductModel::query()->find($id);
return $model === null ? null : $this->toEntity($model);
}
public function save(Product $product): Product
{
$model = $product->id() === null ? new ProductModel : ProductModel::query()->findOrFail($product->id());
$model->fill([
'name' => $product->name()->value,
'unit' => $product->unit()->value,
'price_cents' => $product->price()->cents,
'is_consumer_product' => $product->isConsumerProduct(),
'auto_hours' => $product->duration()->decimal(),
'work_type_id' => $product->workTypeId(),
'is_active' => $product->isActive(),
'sort_order' => $product->sortOrder(),
])->save();
return $this->toEntity($model);
}
public function isNameTaken(string $name, ?int $exceptId = null): bool
{
return ProductModel::query()
->whereRaw('LOWER(name) = ?', [mb_strtolower($name)])
->when($exceptId !== null, fn ($query) => $query->whereKeyNot($exceptId))
->exists();
}
public function nextSortOrder(): int
{
return (int) ProductModel::query()->max('sort_order') + 1;
}
public function neighbour(Product $product, MoveDirection $direction): ?Product
{
$model = ProductModel::query()
->where('sort_order', $direction === MoveDirection::Up ? '<' : '>', $product->sortOrder())
->orderBy('sort_order', $direction === MoveDirection::Up ? 'desc' : 'asc')
->first();
return $model === null ? null : $this->toEntity($model);
}
private function toEntity(ProductModel $model): Product
{
return Product::reconstitute(
$model->id,
new CatalogName($model->name),
ProductUnit::from($model->unit),
Money::fromCents($model->price_cents),
$model->is_consumer_product,
ProductDuration::fromDecimal((float) $model->auto_hours),
$model->work_type_id,
$model->is_active,
$model->sort_order,
);
}
}
@@ -7,6 +7,7 @@ use App\Modules\Catalog\Domain\Entities\WorkType;
use App\Modules\Catalog\Domain\Enums\MoveDirection;
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use App\Modules\Shared\Domain\ValueObjects\Money;
use Illuminate\Database\Eloquent\Builder;
class EloquentWorkTypeRepository implements WorkTypeRepository
@@ -26,6 +27,7 @@ class EloquentWorkTypeRepository implements WorkTypeRepository
'service_unit_id' => $workType->serviceUnitId(),
'service_id' => $workType->serviceId(),
'name' => $workType->name()->value,
'hourly_rate_cents' => $workType->hourlyRate()?->cents,
'is_active' => $workType->isActive(),
'sort_order' => $workType->sortOrder(),
])->save();
@@ -79,6 +81,7 @@ class EloquentWorkTypeRepository implements WorkTypeRepository
new CatalogName($model->name),
$model->is_active,
$model->sort_order,
$model->hourly_rate_cents === null ? null : Money::fromCents($model->hourly_rate_cents),
);
}
}