Add money permissions, a read-only price list and VAT rates

- Admins give employees and interns personal money permissions (prices,
  expenses, quotes, billing) on the user form. Other modules check them
  through the money.* abilities; admins and management see all amounts,
  subcontractors none.
- Employees and interns get a read-only price list: consumer products
  with VAT always, hourly rates, other products and the mileage rate only
  with the price permission. Subcontractors cannot open it.
- VAT rates are maintained on the price list page, with one default rate
  that always stays in use. Products get a VAT rate; changing a rate's
  percentage applies to every product using it.
- Consumer product prices are entered and stored with VAT, so cash prices
  stay round; the price without VAT is calculated. The product form shows
  the other price as you type.
- Shared VatPercentage value object and Money::addVat()/removeVat(),
  rounding half up to the cent in integer arithmetic.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-24 07:51:19 +02:00
co-authored by Claude Opus 5.5
parent dc2612cf46
commit bdd8d446d8
68 changed files with 2222 additions and 64 deletions
@@ -6,6 +6,7 @@ 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\VatRateRepository;
use App\Modules\Catalog\Domain\Contracts\WorkTypeRepository;
use App\Modules\Catalog\Domain\Entities\Product;
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
@@ -13,6 +14,7 @@ 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\VatRateNotFound;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
@@ -24,6 +26,7 @@ final class AddProduct
public function __construct(
private ProductRepository $products,
private WorkTypeRepository $workTypes,
private VatRateRepository $vatRates,
private TransactionManager $transactions,
) {}
@@ -36,6 +39,7 @@ final class AddProduct
* @throws InvalidProductDuration
* @throws ProductNameTaken
* @throws ProductNeedsWorkType
* @throws VatRateNotFound
*/
public function execute(ProductInput $input): int
{
@@ -43,6 +47,7 @@ final class AddProduct
return $this->transactions->run(function () use ($fields): int {
$fields->ensureWorkTypeIsUsable($this->workTypes);
$fields->ensureVatRateIsUsable($this->vatRates);
if ($this->products->isNameTaken($fields->name->value)) {
throw new ProductNameTaken;
@@ -52,6 +57,7 @@ final class AddProduct
$fields->name,
$fields->unit,
$fields->price,
$fields->vatRateId,
$fields->isConsumerProduct,
$fields->duration,
$fields->workTypeId,
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Application\UseCases;
use App\Modules\Catalog\Domain\Contracts\VatRateRepository;
use App\Modules\Catalog\Domain\Entities\VatRate;
use App\Modules\Catalog\Domain\Exceptions\InvalidCatalogName;
use App\Modules\Catalog\Domain\Exceptions\VatRateNameTaken;
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Shared\Domain\Exceptions\InvalidVatPercentage;
use App\Modules\Shared\Domain\ValueObjects\VatPercentage;
/**
* Adds a VAT rate last in the list, optionally as the new default.
*/
final class AddVatRate
{
public function __construct(
private VatRateRepository $vatRates,
private TransactionManager $transactions,
) {}
/**
* @return int The new rate's id.
*
* @throws InvalidCatalogName
* @throws InvalidVatPercentage
* @throws VatRateNameTaken
*/
public function execute(string $name, string $percentage, bool $makeDefault = false): int
{
$name = new CatalogName($name);
$percentage = VatPercentage::parse($percentage);
return $this->transactions->run(function () use ($name, $percentage, $makeDefault): int {
if ($this->vatRates->isNameTaken($name->value)) {
throw new VatRateNameTaken;
}
$vatRate = VatRate::create($name, $percentage, $this->vatRates->nextSortOrder());
if ($makeDefault) {
DefaultVatRate::moveTo($vatRate, $this->vatRates);
}
return (int) $this->vatRates->save($vatRate)->id();
});
}
}
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Application\UseCases;
use App\Modules\Catalog\Domain\Contracts\VatRateRepository;
use App\Modules\Catalog\Domain\Entities\VatRate;
/**
* Keeps exactly one default VAT rate. Called inside the caller's transaction.
*
* @internal
*/
final class DefaultVatRate
{
public static function moveTo(VatRate $vatRate, VatRateRepository $vatRates): void
{
$current = $vatRates->findDefault();
if ($current !== null && $current->id() !== $vatRate->id()) {
$current->clearDefault();
$vatRates->save($current);
}
$vatRate->makeDefault();
}
}
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Application\UseCases;
use App\Modules\Catalog\Domain\Contracts\VatRateRepository;
use App\Modules\Catalog\Domain\Enums\MoveDirection;
use App\Modules\Catalog\Domain\Exceptions\VatRateNotFound;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Moves a VAT rate one place up or down in the list.
*/
final class MoveVatRate
{
public function __construct(
private VatRateRepository $vatRates,
private TransactionManager $transactions,
) {}
/**
* @return bool False when the rate already was first (up) or last (down).
*
* @throws VatRateNotFound
*/
public function execute(int $vatRateId, MoveDirection $direction): bool
{
return $this->transactions->run(function () use ($vatRateId, $direction): bool {
$vatRate = $this->vatRates->findById($vatRateId) ?? throw new VatRateNotFound($vatRateId);
$neighbour = $this->vatRates->neighbour($vatRate, $direction);
if ($neighbour === null) {
return false;
}
$vatRate->swapOrderWith($neighbour);
$this->vatRates->save($vatRate);
$this->vatRates->save($neighbour);
return true;
});
}
}
@@ -5,12 +5,14 @@ declare(strict_types=1);
namespace App\Modules\Catalog\Application\UseCases;
use App\Modules\Catalog\Application\DTOs\ProductInput;
use App\Modules\Catalog\Domain\Contracts\VatRateRepository;
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\Exceptions\VatRateNotFound;
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
use App\Modules\Catalog\Domain\ValueObjects\ProductDuration;
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
@@ -27,6 +29,7 @@ final readonly class ProductFields
public CatalogName $name,
public ProductUnit $unit,
public Money $price,
public int $vatRateId,
public bool $isConsumerProduct,
public ProductDuration $duration,
public ?int $workTypeId,
@@ -43,12 +46,27 @@ final readonly class ProductFields
new CatalogName($input->name),
$input->unit,
Money::parse($input->price) ?? throw new InvalidMoneyAmount,
$input->vatRateId,
$input->isConsumerProduct,
ProductDuration::parse($input->duration),
$input->workTypeId,
);
}
/**
* A newly chosen VAT rate must exist and be in use.
*
* @throws VatRateNotFound
*/
public function ensureVatRateIsUsable(VatRateRepository $vatRates): void
{
$vatRate = $vatRates->findById($this->vatRateId);
if ($vatRate === null || ! $vatRate->isActive()) {
throw new VatRateNotFound($this->vatRateId);
}
}
/**
* A newly chosen work type must exist and be in use.
*
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Application\UseCases;
use App\Modules\Catalog\Domain\Contracts\VatRateRepository;
use App\Modules\Catalog\Domain\Exceptions\DefaultVatRateMustBeActive;
use App\Modules\Catalog\Domain\Exceptions\VatRateNotFound;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Takes a VAT rate out of use or back into use. A rate out of use cannot be
* chosen for products anew; products that have it keep it.
*/
final class SetVatRateActive
{
public function __construct(
private VatRateRepository $vatRates,
private TransactionManager $transactions,
) {}
/**
* @throws DefaultVatRateMustBeActive
* @throws VatRateNotFound
*/
public function execute(int $vatRateId, bool $active): void
{
$this->transactions->run(function () use ($vatRateId, $active): void {
$vatRate = $this->vatRates->findById($vatRateId) ?? throw new VatRateNotFound($vatRateId);
$active ? $vatRate->activate() : $vatRate->deactivate();
$this->vatRates->save($vatRate);
});
}
}
@@ -6,6 +6,7 @@ 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\VatRateRepository;
use App\Modules\Catalog\Domain\Contracts\WorkTypeRepository;
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
use App\Modules\Catalog\Domain\Exceptions\InvalidCatalogName;
@@ -13,6 +14,7 @@ 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\Catalog\Domain\Exceptions\VatRateNotFound;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
@@ -25,6 +27,7 @@ final class UpdateProduct
public function __construct(
private ProductRepository $products,
private WorkTypeRepository $workTypes,
private VatRateRepository $vatRates,
private TransactionManager $transactions,
) {}
@@ -35,6 +38,7 @@ final class UpdateProduct
* @throws InvalidProductDuration
* @throws ProductNameTaken
* @throws ProductNeedsWorkType
* @throws VatRateNotFound
* @throws ProductNotFound
*/
public function execute(int $productId, ProductInput $input): void
@@ -44,16 +48,20 @@ final class UpdateProduct
$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.
// An inactive work type or VAT rate may stay, but it cannot be newly chosen.
if ($fields->workTypeId !== $product->workTypeId()) {
$fields->ensureWorkTypeIsUsable($this->workTypes);
}
if ($fields->vatRateId !== $product->vatRateId()) {
$fields->ensureVatRateIsUsable($this->vatRates);
}
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);
$product->update($fields->name, $fields->unit, $fields->price, $fields->vatRateId, $fields->isConsumerProduct, $fields->duration, $fields->workTypeId);
$this->products->save($product);
});
}
@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Application\UseCases;
use App\Modules\Catalog\Domain\Contracts\VatRateRepository;
use App\Modules\Catalog\Domain\Exceptions\InvalidCatalogName;
use App\Modules\Catalog\Domain\Exceptions\VatRateNameTaken;
use App\Modules\Catalog\Domain\Exceptions\VatRateNotFound;
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Shared\Domain\Exceptions\InvalidVatPercentage;
use App\Modules\Shared\Domain\ValueObjects\VatPercentage;
/**
* Renames a VAT rate and changes its percentage, which then applies to every
* product using it. $makeDefault moves the default to this rate; the
* default cannot be cleared, only moved to another rate.
*/
final class UpdateVatRate
{
public function __construct(
private VatRateRepository $vatRates,
private TransactionManager $transactions,
) {}
/**
* @throws InvalidCatalogName
* @throws InvalidVatPercentage
* @throws VatRateNameTaken
* @throws VatRateNotFound
*/
public function execute(int $vatRateId, string $name, string $percentage, bool $makeDefault = false): void
{
$name = new CatalogName($name);
$percentage = VatPercentage::parse($percentage);
$this->transactions->run(function () use ($vatRateId, $name, $percentage, $makeDefault): void {
$vatRate = $this->vatRates->findById($vatRateId) ?? throw new VatRateNotFound($vatRateId);
if ($this->vatRates->isNameTaken($name->value, $vatRateId)) {
throw new VatRateNameTaken;
}
$vatRate->rename($name);
$vatRate->changePercentage($percentage);
if ($makeDefault) {
DefaultVatRate::moveTo($vatRate, $this->vatRates);
}
$this->vatRates->save($vatRate);
});
}
}