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:
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Catalog\Infrastructure\Persistence\Eloquent;
|
||||
|
||||
use Database\Factories\ProductFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||
use Illuminate\Database\Eloquent\Attributes\UseFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* Persistence model, used only inside the Catalog Infrastructure layer.
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property string $unit
|
||||
* @property int $price_cents
|
||||
* @property bool $is_consumer_product
|
||||
* @property string $auto_hours
|
||||
* @property int|null $work_type_id
|
||||
* @property bool $is_active
|
||||
* @property int $sort_order
|
||||
* @property-read WorkTypeModel|null $workType
|
||||
*/
|
||||
#[Table('products')]
|
||||
#[Fillable(['name', 'unit', 'price_cents', 'is_consumer_product', 'auto_hours', 'work_type_id', 'is_active', 'sort_order'])]
|
||||
#[UseFactory(ProductFactory::class)]
|
||||
class ProductModel extends Model
|
||||
{
|
||||
/** @use HasFactory<ProductFactory> */
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @return BelongsTo<WorkTypeModel, $this>
|
||||
*/
|
||||
public function workType(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(WorkTypeModel::class, 'work_type_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'price_cents' => 'integer',
|
||||
'is_consumer_product' => 'boolean',
|
||||
'auto_hours' => 'decimal:2',
|
||||
'work_type_id' => 'integer',
|
||||
'is_active' => 'boolean',
|
||||
'sort_order' => 'integer',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -16,11 +16,12 @@ use Illuminate\Database\Eloquent\Model;
|
||||
* @property int $service_unit_id
|
||||
* @property int|null $service_id
|
||||
* @property string $name
|
||||
* @property int|null $hourly_rate_cents
|
||||
* @property bool $is_active
|
||||
* @property int $sort_order
|
||||
*/
|
||||
#[Table('work_types')]
|
||||
#[Fillable(['service_unit_id', 'service_id', 'name', 'is_active', 'sort_order'])]
|
||||
#[Fillable(['service_unit_id', 'service_id', 'name', 'hourly_rate_cents', 'is_active', 'sort_order'])]
|
||||
#[UseFactory(WorkTypeFactory::class)]
|
||||
class WorkTypeModel extends Model
|
||||
{
|
||||
@@ -35,6 +36,7 @@ class WorkTypeModel extends Model
|
||||
return [
|
||||
'service_unit_id' => 'integer',
|
||||
'service_id' => 'integer',
|
||||
'hourly_rate_cents' => 'integer',
|
||||
'is_active' => 'boolean',
|
||||
'sort_order' => 'integer',
|
||||
];
|
||||
|
||||
+99
@@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
+78
@@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
+3
@@ -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),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user