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,64 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Application\UseCases;
|
||||
|
||||
use App\Modules\Catalog\Application\DTOs\ProductInput;
|
||||
use App\Modules\Catalog\Domain\Contracts\ProductRepository;
|
||||
use App\Modules\Catalog\Domain\Contracts\WorkTypeRepository;
|
||||
use App\Modules\Catalog\Domain\Entities\Product;
|
||||
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\Shared\Application\Contracts\TransactionManager;
|
||||
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
|
||||
|
||||
/**
|
||||
* Adds a product last in the product list.
|
||||
*/
|
||||
final class AddProduct
|
||||
{
|
||||
public function __construct(
|
||||
private ProductRepository $products,
|
||||
private WorkTypeRepository $workTypes,
|
||||
private TransactionManager $transactions,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return int The new product's id.
|
||||
*
|
||||
* @throws CatalogItemNotFound
|
||||
* @throws InvalidCatalogName
|
||||
* @throws InvalidMoneyAmount
|
||||
* @throws InvalidProductDuration
|
||||
* @throws ProductNameTaken
|
||||
* @throws ProductNeedsWorkType
|
||||
*/
|
||||
public function execute(ProductInput $input): int
|
||||
{
|
||||
$fields = ProductFields::from($input);
|
||||
|
||||
return $this->transactions->run(function () use ($fields): int {
|
||||
$fields->ensureWorkTypeIsUsable($this->workTypes);
|
||||
|
||||
if ($this->products->isNameTaken($fields->name->value)) {
|
||||
throw new ProductNameTaken;
|
||||
}
|
||||
|
||||
$product = Product::create(
|
||||
$fields->name,
|
||||
$fields->unit,
|
||||
$fields->price,
|
||||
$fields->isConsumerProduct,
|
||||
$fields->duration,
|
||||
$fields->workTypeId,
|
||||
$this->products->nextSortOrder(),
|
||||
);
|
||||
|
||||
return (int) $this->products->save($product)->id();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,8 @@ use App\Modules\Catalog\Domain\Exceptions\CatalogNameTaken;
|
||||
use App\Modules\Catalog\Domain\Exceptions\ServiceNotInUnit;
|
||||
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
|
||||
use App\Modules\Shared\Domain\ValueObjects\Money;
|
||||
|
||||
final class AddWorkType
|
||||
{
|
||||
@@ -29,13 +31,15 @@ final class AddWorkType
|
||||
*
|
||||
* @throws CatalogItemNotFound
|
||||
* @throws CatalogNameTaken
|
||||
* @throws InvalidMoneyAmount
|
||||
* @throws ServiceNotInUnit
|
||||
*/
|
||||
public function execute(int $serviceUnitId, ?int $serviceId, string $name): int
|
||||
public function execute(int $serviceUnitId, ?int $serviceId, string $name, string $hourlyRate = ''): int
|
||||
{
|
||||
$name = new CatalogName($name);
|
||||
$rate = Money::parse($hourlyRate);
|
||||
|
||||
return $this->transactions->run(function () use ($serviceUnitId, $serviceId, $name): int {
|
||||
return $this->transactions->run(function () use ($serviceUnitId, $serviceId, $name, $rate): int {
|
||||
$serviceUnit = $this->serviceUnits->findById($serviceUnitId)
|
||||
?? throw new CatalogItemNotFound(CatalogLevel::ServiceUnit, $serviceUnitId);
|
||||
$service = $serviceId === null ? null : ($this->services->findById($serviceId)
|
||||
@@ -45,7 +49,7 @@ final class AddWorkType
|
||||
throw new CatalogNameTaken;
|
||||
}
|
||||
|
||||
$workType = WorkType::create($serviceUnit, $service, $name, $this->workTypes->nextSortOrder($serviceUnitId, $serviceId));
|
||||
$workType = WorkType::create($serviceUnit, $service, $name, $this->workTypes->nextSortOrder($serviceUnitId, $serviceId), $rate);
|
||||
|
||||
return (int) $this->workTypes->save($workType)->id();
|
||||
});
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Application\UseCases;
|
||||
|
||||
use App\Modules\Catalog\Domain\Contracts\ProductRepository;
|
||||
use App\Modules\Catalog\Domain\Enums\MoveDirection;
|
||||
use App\Modules\Catalog\Domain\Exceptions\ProductNotFound;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
|
||||
/**
|
||||
* Moves a product one place up or down in the product list.
|
||||
*/
|
||||
final class MoveProduct
|
||||
{
|
||||
public function __construct(
|
||||
private ProductRepository $products,
|
||||
private TransactionManager $transactions,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return bool False when the product already was first (up) or last (down).
|
||||
*
|
||||
* @throws ProductNotFound
|
||||
*/
|
||||
public function execute(int $productId, MoveDirection $direction): bool
|
||||
{
|
||||
return $this->transactions->run(function () use ($productId, $direction): bool {
|
||||
$product = $this->products->findById($productId) ?? throw new ProductNotFound($productId);
|
||||
$neighbour = $this->products->neighbour($product, $direction);
|
||||
|
||||
if ($neighbour === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$product->swapOrderWith($neighbour);
|
||||
$this->products->save($product);
|
||||
$this->products->save($neighbour);
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Application\UseCases;
|
||||
|
||||
use App\Modules\Catalog\Application\DTOs\ProductInput;
|
||||
use App\Modules\Catalog\Domain\Contracts\WorkTypeRepository;
|
||||
use App\Modules\Catalog\Domain\Enums\CatalogLevel;
|
||||
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\ValueObjects\CatalogName;
|
||||
use App\Modules\Catalog\Domain\ValueObjects\ProductDuration;
|
||||
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
|
||||
use App\Modules\Shared\Domain\ValueObjects\Money;
|
||||
|
||||
/**
|
||||
* A product's input parsed into value objects, shared by AddProduct and UpdateProduct.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final readonly class ProductFields
|
||||
{
|
||||
private function __construct(
|
||||
public CatalogName $name,
|
||||
public ProductUnit $unit,
|
||||
public Money $price,
|
||||
public bool $isConsumerProduct,
|
||||
public ProductDuration $duration,
|
||||
public ?int $workTypeId,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws InvalidCatalogName
|
||||
* @throws InvalidMoneyAmount
|
||||
* @throws InvalidProductDuration
|
||||
*/
|
||||
public static function from(ProductInput $input): self
|
||||
{
|
||||
return new self(
|
||||
new CatalogName($input->name),
|
||||
$input->unit,
|
||||
Money::parse($input->price) ?? throw new InvalidMoneyAmount,
|
||||
$input->isConsumerProduct,
|
||||
ProductDuration::parse($input->duration),
|
||||
$input->workTypeId,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A newly chosen work type must exist and be in use.
|
||||
*
|
||||
* @throws CatalogItemNotFound
|
||||
*/
|
||||
public function ensureWorkTypeIsUsable(WorkTypeRepository $workTypes): void
|
||||
{
|
||||
if ($this->workTypeId === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$workType = $workTypes->findById($this->workTypeId);
|
||||
|
||||
if ($workType === null || ! $workType->isActive()) {
|
||||
throw new CatalogItemNotFound(CatalogLevel::WorkType, $this->workTypeId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Application\UseCases;
|
||||
|
||||
use App\Modules\Catalog\Domain\Contracts\WorkTypeRepository;
|
||||
use App\Modules\Catalog\Domain\Enums\CatalogLevel;
|
||||
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
|
||||
use App\Modules\Shared\Domain\ValueObjects\Money;
|
||||
|
||||
/**
|
||||
* Sets a work type's hourly rate (VAT 0); an empty value removes it. The
|
||||
* rate applies to billing exports made after the change.
|
||||
*/
|
||||
final class SetHourlyRate
|
||||
{
|
||||
public function __construct(
|
||||
private WorkTypeRepository $workTypes,
|
||||
private TransactionManager $transactions,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return Money|null The stored rate.
|
||||
*
|
||||
* @throws CatalogItemNotFound
|
||||
* @throws InvalidMoneyAmount
|
||||
*/
|
||||
public function execute(int $workTypeId, string $hourlyRate): ?Money
|
||||
{
|
||||
$rate = Money::parse($hourlyRate);
|
||||
|
||||
$this->transactions->run(function () use ($workTypeId, $rate): void {
|
||||
$workType = $this->workTypes->findById($workTypeId)
|
||||
?? throw new CatalogItemNotFound(CatalogLevel::WorkType, $workTypeId);
|
||||
|
||||
$workType->changeHourlyRate($rate);
|
||||
$this->workTypes->save($workType);
|
||||
});
|
||||
|
||||
return $rate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Application\UseCases;
|
||||
|
||||
use App\Modules\Shared\Application\Contracts\Settings;
|
||||
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
|
||||
use App\Modules\Shared\Domain\ValueObjects\Money;
|
||||
|
||||
/**
|
||||
* Sets the price of a driven kilometre (VAT 0); an empty value removes it.
|
||||
*/
|
||||
final class SetMileageRate
|
||||
{
|
||||
public const string SETTING_KEY = 'catalog.mileage_rate_cents';
|
||||
|
||||
public function __construct(private Settings $settings) {}
|
||||
|
||||
/**
|
||||
* @return Money|null The stored rate.
|
||||
*
|
||||
* @throws InvalidMoneyAmount
|
||||
*/
|
||||
public function execute(string $mileageRate): ?Money
|
||||
{
|
||||
$rate = Money::parse($mileageRate);
|
||||
|
||||
$this->settings->put(self::SETTING_KEY, $rate === null ? null : (string) $rate->cents);
|
||||
|
||||
return $rate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Application\UseCases;
|
||||
|
||||
use App\Modules\Catalog\Domain\Contracts\ProductRepository;
|
||||
use App\Modules\Catalog\Domain\Exceptions\ProductNotFound;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
|
||||
/**
|
||||
* Takes a product out of sale or back into sale. Products are never
|
||||
* deleted, so old sales that refer to them keep working.
|
||||
*/
|
||||
final class SetProductActive
|
||||
{
|
||||
public function __construct(
|
||||
private ProductRepository $products,
|
||||
private TransactionManager $transactions,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws ProductNotFound
|
||||
*/
|
||||
public function execute(int $productId, bool $active): void
|
||||
{
|
||||
$this->transactions->run(function () use ($productId, $active): void {
|
||||
$product = $this->products->findById($productId) ?? throw new ProductNotFound($productId);
|
||||
|
||||
$active ? $product->activate() : $product->deactivate();
|
||||
$this->products->save($product);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Application\UseCases;
|
||||
|
||||
use App\Modules\Catalog\Application\DTOs\ProductInput;
|
||||
use App\Modules\Catalog\Domain\Contracts\ProductRepository;
|
||||
use App\Modules\Catalog\Domain\Contracts\WorkTypeRepository;
|
||||
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\Exceptions\ProductNotFound;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
|
||||
|
||||
/**
|
||||
* Changes a product's fields. A price change applies to sales and billing
|
||||
* exports made after it.
|
||||
*/
|
||||
final class UpdateProduct
|
||||
{
|
||||
public function __construct(
|
||||
private ProductRepository $products,
|
||||
private WorkTypeRepository $workTypes,
|
||||
private TransactionManager $transactions,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws CatalogItemNotFound
|
||||
* @throws InvalidCatalogName
|
||||
* @throws InvalidMoneyAmount
|
||||
* @throws InvalidProductDuration
|
||||
* @throws ProductNameTaken
|
||||
* @throws ProductNeedsWorkType
|
||||
* @throws ProductNotFound
|
||||
*/
|
||||
public function execute(int $productId, ProductInput $input): void
|
||||
{
|
||||
$fields = ProductFields::from($input);
|
||||
|
||||
$this->transactions->run(function () use ($productId, $fields): void {
|
||||
$product = $this->products->findById($productId) ?? throw new ProductNotFound($productId);
|
||||
|
||||
// An inactive work type may stay, but it cannot be newly chosen.
|
||||
if ($fields->workTypeId !== $product->workTypeId()) {
|
||||
$fields->ensureWorkTypeIsUsable($this->workTypes);
|
||||
}
|
||||
|
||||
if ($this->products->isNameTaken($fields->name->value, $productId)) {
|
||||
throw new ProductNameTaken;
|
||||
}
|
||||
|
||||
$product->update($fields->name, $fields->unit, $fields->price, $fields->isConsumerProduct, $fields->duration, $fields->workTypeId);
|
||||
$this->products->save($product);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -13,10 +13,12 @@ use App\Modules\Catalog\Domain\Exceptions\CatalogNameTaken;
|
||||
use App\Modules\Catalog\Domain\Exceptions\ServiceNotInUnit;
|
||||
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
|
||||
use App\Modules\Shared\Domain\ValueObjects\Money;
|
||||
|
||||
/**
|
||||
* Renames a work type and moves it under another service of its unit (or
|
||||
* under no service).
|
||||
* Renames a work type, sets its hourly rate (empty removes it) and moves it
|
||||
* under another service of its unit (or under no service).
|
||||
*/
|
||||
final class UpdateWorkType
|
||||
{
|
||||
@@ -30,13 +32,15 @@ final class UpdateWorkType
|
||||
/**
|
||||
* @throws CatalogItemNotFound
|
||||
* @throws CatalogNameTaken
|
||||
* @throws InvalidMoneyAmount
|
||||
* @throws ServiceNotInUnit
|
||||
*/
|
||||
public function execute(int $workTypeId, ?int $serviceId, string $name): void
|
||||
public function execute(int $workTypeId, ?int $serviceId, string $name, string $hourlyRate): void
|
||||
{
|
||||
$name = new CatalogName($name);
|
||||
$rate = Money::parse($hourlyRate);
|
||||
|
||||
$this->transactions->run(function () use ($workTypeId, $serviceId, $name): void {
|
||||
$this->transactions->run(function () use ($workTypeId, $serviceId, $name, $rate): void {
|
||||
$workType = $this->workTypes->findById($workTypeId)
|
||||
?? throw new CatalogItemNotFound(CatalogLevel::WorkType, $workTypeId);
|
||||
|
||||
@@ -54,6 +58,7 @@ final class UpdateWorkType
|
||||
}
|
||||
|
||||
$workType->rename($name);
|
||||
$workType->changeHourlyRate($rate);
|
||||
$this->workTypes->save($workType);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user