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,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Domain\Contracts;
|
||||
|
||||
use App\Modules\Catalog\Domain\Entities\Product;
|
||||
use App\Modules\Catalog\Domain\Enums\MoveDirection;
|
||||
|
||||
interface ProductRepository
|
||||
{
|
||||
public function findById(int $id): ?Product;
|
||||
|
||||
/**
|
||||
* Inserts a new product or updates a stored one, and returns the stored product.
|
||||
*/
|
||||
public function save(Product $product): Product;
|
||||
|
||||
/**
|
||||
* Whether a product other than $exceptId has this name, ignoring case.
|
||||
*/
|
||||
public function isNameTaken(string $name, ?int $exceptId = null): bool;
|
||||
|
||||
/**
|
||||
* The sort order that puts a new product last.
|
||||
*/
|
||||
public function nextSortOrder(): int;
|
||||
|
||||
/**
|
||||
* The product just before or after this one, or null at either end.
|
||||
*/
|
||||
public function neighbour(Product $product, MoveDirection $direction): ?Product;
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Domain\Entities;
|
||||
|
||||
use App\Modules\Catalog\Domain\Enums\ProductUnit;
|
||||
use App\Modules\Catalog\Domain\Exceptions\ProductNeedsWorkType;
|
||||
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
|
||||
use App\Modules\Catalog\Domain\ValueObjects\ProductDuration;
|
||||
use App\Modules\Shared\Domain\ValueObjects\Money;
|
||||
|
||||
/**
|
||||
* A fixed-price product (passikuvat, tulosteet). Consumer products' prices
|
||||
* are visible to every user. Selling a product with a duration books those
|
||||
* hours for the seller on the product's work type.
|
||||
*/
|
||||
final class Product extends CatalogItem
|
||||
{
|
||||
private function __construct(
|
||||
?int $id,
|
||||
CatalogName $name,
|
||||
private ProductUnit $unit,
|
||||
private Money $price,
|
||||
private bool $consumerProduct,
|
||||
private ProductDuration $duration,
|
||||
private ?int $workTypeId,
|
||||
bool $active,
|
||||
int $sortOrder,
|
||||
) {
|
||||
parent::__construct($id, $name, $active, $sortOrder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ProductNeedsWorkType
|
||||
*/
|
||||
public static function create(CatalogName $name, ProductUnit $unit, Money $price, bool $consumerProduct, ProductDuration $duration, ?int $workTypeId, int $sortOrder): self
|
||||
{
|
||||
self::ensureWorkTypeForDuration($duration, $workTypeId);
|
||||
|
||||
return new self(null, $name, $unit, $price, $consumerProduct, $duration, $workTypeId, true, $sortOrder);
|
||||
}
|
||||
|
||||
public static function reconstitute(int $id, CatalogName $name, ProductUnit $unit, Money $price, bool $consumerProduct, ProductDuration $duration, ?int $workTypeId, bool $active, int $sortOrder): self
|
||||
{
|
||||
return new self($id, $name, $unit, $price, $consumerProduct, $duration, $workTypeId, $active, $sortOrder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ProductNeedsWorkType
|
||||
*/
|
||||
public function update(CatalogName $name, ProductUnit $unit, Money $price, bool $consumerProduct, ProductDuration $duration, ?int $workTypeId): void
|
||||
{
|
||||
self::ensureWorkTypeForDuration($duration, $workTypeId);
|
||||
|
||||
$this->name = $name;
|
||||
$this->unit = $unit;
|
||||
$this->price = $price;
|
||||
$this->consumerProduct = $consumerProduct;
|
||||
$this->duration = $duration;
|
||||
$this->workTypeId = $workTypeId;
|
||||
}
|
||||
|
||||
public function unit(): ProductUnit
|
||||
{
|
||||
return $this->unit;
|
||||
}
|
||||
|
||||
public function price(): Money
|
||||
{
|
||||
return $this->price;
|
||||
}
|
||||
|
||||
public function isConsumerProduct(): bool
|
||||
{
|
||||
return $this->consumerProduct;
|
||||
}
|
||||
|
||||
public function duration(): ProductDuration
|
||||
{
|
||||
return $this->duration;
|
||||
}
|
||||
|
||||
public function workTypeId(): ?int
|
||||
{
|
||||
return $this->workTypeId;
|
||||
}
|
||||
|
||||
private static function ensureWorkTypeForDuration(ProductDuration $duration, ?int $workTypeId): void
|
||||
{
|
||||
if (! $duration->isNone() && $workTypeId === null) {
|
||||
throw new ProductNeedsWorkType;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,10 +6,11 @@ namespace App\Modules\Catalog\Domain\Entities;
|
||||
|
||||
use App\Modules\Catalog\Domain\Exceptions\ServiceNotInUnit;
|
||||
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
|
||||
use App\Modules\Shared\Domain\ValueObjects\Money;
|
||||
|
||||
/**
|
||||
* A work type (työtehtävä), e.g. editointi. Tasks and time entries are
|
||||
* recorded against work types, and hourly rates (phase 2) belong to them.
|
||||
* recorded against work types, and the hourly rate (VAT 0) belongs to them.
|
||||
*/
|
||||
final class WorkType extends CatalogItem
|
||||
{
|
||||
@@ -20,6 +21,7 @@ final class WorkType extends CatalogItem
|
||||
CatalogName $name,
|
||||
bool $active,
|
||||
int $sortOrder,
|
||||
private ?Money $hourlyRate = null,
|
||||
) {
|
||||
parent::__construct($id, $name, $active, $sortOrder);
|
||||
}
|
||||
@@ -27,16 +29,29 @@ final class WorkType extends CatalogItem
|
||||
/**
|
||||
* @throws ServiceNotInUnit
|
||||
*/
|
||||
public static function create(ServiceUnit $serviceUnit, ?Service $service, CatalogName $name, int $sortOrder): self
|
||||
public static function create(ServiceUnit $serviceUnit, ?Service $service, CatalogName $name, int $sortOrder, ?Money $hourlyRate = null): self
|
||||
{
|
||||
self::ensureServiceInUnit($serviceUnit, $service);
|
||||
|
||||
return new self(null, (int) $serviceUnit->id(), $service?->id(), $name, true, $sortOrder);
|
||||
return new self(null, (int) $serviceUnit->id(), $service?->id(), $name, true, $sortOrder, $hourlyRate);
|
||||
}
|
||||
|
||||
public static function reconstitute(int $id, int $serviceUnitId, ?int $serviceId, CatalogName $name, bool $active, int $sortOrder): self
|
||||
public static function reconstitute(int $id, int $serviceUnitId, ?int $serviceId, CatalogName $name, bool $active, int $sortOrder, ?Money $hourlyRate = null): self
|
||||
{
|
||||
return new self($id, $serviceUnitId, $serviceId, $name, $active, $sortOrder);
|
||||
return new self($id, $serviceUnitId, $serviceId, $name, $active, $sortOrder, $hourlyRate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Null means the work type has no price yet.
|
||||
*/
|
||||
public function hourlyRate(): ?Money
|
||||
{
|
||||
return $this->hourlyRate;
|
||||
}
|
||||
|
||||
public function changeHourlyRate(?Money $hourlyRate): void
|
||||
{
|
||||
$this->hourlyRate = $hourlyRate;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Domain\Enums;
|
||||
|
||||
/**
|
||||
* Unit in which a product is sold. Finnish labels are in `catalog.units`.
|
||||
*/
|
||||
enum ProductUnit: string
|
||||
{
|
||||
case Piece = 'piece';
|
||||
case Page = 'page';
|
||||
case SquareMetre = 'square_metre';
|
||||
case Metre = 'metre';
|
||||
case Hour = 'hour';
|
||||
case Package = 'package';
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class InvalidProductDuration extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('A product duration must be 0–24 hours to the nearest quarter of an hour.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class ProductNameTaken extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('Another product already has this name.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class ProductNeedsWorkType extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('A product with automatic hours needs a work type.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class ProductNotFound extends DomainException
|
||||
{
|
||||
public function __construct(public readonly int $productId)
|
||||
{
|
||||
parent::__construct("Product {$productId} was not found.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Domain\ValueObjects;
|
||||
|
||||
use App\Modules\Catalog\Domain\Exceptions\InvalidProductDuration;
|
||||
|
||||
/**
|
||||
* Hours booked for the seller automatically when a product is sold,
|
||||
* 0–24 to the nearest quarter of an hour. Zero means no booking.
|
||||
*/
|
||||
final readonly class ProductDuration
|
||||
{
|
||||
public const int MAX_QUARTERS = 96;
|
||||
|
||||
public int $quarters;
|
||||
|
||||
public function __construct(int $quarters)
|
||||
{
|
||||
if ($quarters < 0 || $quarters > self::MAX_QUARTERS) {
|
||||
throw new InvalidProductDuration;
|
||||
}
|
||||
|
||||
$this->quarters = $quarters;
|
||||
}
|
||||
|
||||
public static function none(): self
|
||||
{
|
||||
return new self(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses "0,5", "0.25" or "1"; an empty value is zero.
|
||||
*
|
||||
* @throws InvalidProductDuration
|
||||
*/
|
||||
public static function parse(string $value): self
|
||||
{
|
||||
$value = str_replace(',', '.', trim($value));
|
||||
|
||||
if ($value === '') {
|
||||
return self::none();
|
||||
}
|
||||
|
||||
if (preg_match('/^\d{1,2}(?:\.\d{1,2})?$/', $value) !== 1) {
|
||||
throw new InvalidProductDuration;
|
||||
}
|
||||
|
||||
$quarters = (float) $value * 4;
|
||||
|
||||
if (abs($quarters - round($quarters)) > 1e-9) {
|
||||
throw new InvalidProductDuration;
|
||||
}
|
||||
|
||||
return new self((int) round($quarters));
|
||||
}
|
||||
|
||||
public static function fromDecimal(float $hours): self
|
||||
{
|
||||
return new self((int) round($hours * 4));
|
||||
}
|
||||
|
||||
public function isNone(): bool
|
||||
{
|
||||
return $this->quarters === 0;
|
||||
}
|
||||
|
||||
public function decimal(): float
|
||||
{
|
||||
return $this->quarters / 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finnish notation without trailing zeros, e.g. "0,25" or "1".
|
||||
*/
|
||||
public function format(): string
|
||||
{
|
||||
return str_replace('.', ',', rtrim(rtrim(number_format($this->quarters / 4, 2, '.', ''), '0'), '.'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user