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:
@@ -6,6 +6,7 @@ namespace App\Modules\Catalog\Application\Contracts;
|
||||
|
||||
use App\Modules\Catalog\Application\DTOs\HourlyRateGroup;
|
||||
use App\Modules\Catalog\Application\DTOs\ProductSummary;
|
||||
use App\Modules\Catalog\Application\DTOs\VatRateSummary;
|
||||
|
||||
/**
|
||||
* Read side of the price list: hourly rates, products and the mileage rate.
|
||||
@@ -31,8 +32,24 @@ interface PriceListQueries
|
||||
|
||||
public function findProduct(int $id): ?ProductSummary;
|
||||
|
||||
/**
|
||||
* Active consumer products, whose prices with VAT every user may see.
|
||||
*
|
||||
* @return list<ProductSummary>
|
||||
*/
|
||||
public function consumerProducts(): array;
|
||||
|
||||
/**
|
||||
* Price of a driven kilometre in cents (VAT 0), null when not set.
|
||||
*/
|
||||
public function mileageRateCents(): ?int;
|
||||
|
||||
/**
|
||||
* @return list<VatRateSummary>
|
||||
*/
|
||||
public function vatRates(bool $includeInactive = true): array;
|
||||
|
||||
public function findVatRate(int $id): ?VatRateSummary;
|
||||
|
||||
public function defaultVatRate(): ?VatRateSummary;
|
||||
}
|
||||
|
||||
@@ -7,7 +7,8 @@ namespace App\Modules\Catalog\Application\DTOs;
|
||||
use App\Modules\Catalog\Domain\Enums\ProductUnit;
|
||||
|
||||
/**
|
||||
* A product's fields as the user typed them: price "12,50", duration "0,25".
|
||||
* A product's fields as the user typed them: price "12,50" (with VAT for a
|
||||
* consumer product, otherwise without), duration "0,25".
|
||||
*/
|
||||
final readonly class ProductInput
|
||||
{
|
||||
@@ -15,6 +16,7 @@ final readonly class ProductInput
|
||||
public string $name,
|
||||
public ProductUnit $unit,
|
||||
public string $price,
|
||||
public int $vatRateId,
|
||||
public bool $isConsumerProduct,
|
||||
public string $duration = '',
|
||||
public ?int $workTypeId = null,
|
||||
|
||||
@@ -7,7 +7,10 @@ namespace App\Modules\Catalog\Application\DTOs;
|
||||
use App\Modules\Catalog\Domain\Enums\ProductUnit;
|
||||
|
||||
/**
|
||||
* A product with its price in cents (VAT 0) and the hours its sale books.
|
||||
* A product with its prices in cents and the hours its sale books.
|
||||
* $priceCents is the price as entered: with VAT when $priceIncludesVat
|
||||
* (consumer products), otherwise without. $netCents and $grossCents are
|
||||
* both, one of them calculated with the product's VAT rate.
|
||||
*/
|
||||
final readonly class ProductSummary
|
||||
{
|
||||
@@ -16,6 +19,11 @@ final readonly class ProductSummary
|
||||
public string $name,
|
||||
public ProductUnit $unit,
|
||||
public int $priceCents,
|
||||
public bool $priceIncludesVat,
|
||||
public int $netCents,
|
||||
public int $grossCents,
|
||||
public int $vatRateId,
|
||||
public int $vatBasisPoints,
|
||||
public bool $isConsumerProduct,
|
||||
public int $durationQuarters,
|
||||
public ?int $workTypeId,
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Catalog\Application\DTOs;
|
||||
|
||||
/**
|
||||
* A VAT rate with its percentage in hundredths of a percent (2550 = 25,5 %).
|
||||
*/
|
||||
final readonly class VatRateSummary
|
||||
{
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public string $name,
|
||||
public int $basisPoints,
|
||||
public bool $isDefault,
|
||||
public bool $isActive,
|
||||
) {}
|
||||
}
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user