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
@@ -3,27 +3,35 @@
use App\Modules\Catalog\Application\DTOs\ProductInput;
use App\Modules\Catalog\Application\UseCases\AddProduct;
use App\Modules\Catalog\Application\UseCases\AddServiceUnit;
use App\Modules\Catalog\Application\UseCases\AddVatRate;
use App\Modules\Catalog\Application\UseCases\AddWorkType;
use App\Modules\Catalog\Application\UseCases\MoveProduct;
use App\Modules\Catalog\Application\UseCases\SetCatalogItemActive;
use App\Modules\Catalog\Application\UseCases\SetHourlyRate;
use App\Modules\Catalog\Application\UseCases\SetMileageRate;
use App\Modules\Catalog\Application\UseCases\SetProductActive;
use App\Modules\Catalog\Application\UseCases\SetVatRateActive;
use App\Modules\Catalog\Application\UseCases\UpdateProduct;
use App\Modules\Catalog\Application\UseCases\UpdateVatRate;
use App\Modules\Catalog\Application\UseCases\UpdateWorkType;
use App\Modules\Catalog\Domain\Enums\CatalogLevel;
use App\Modules\Catalog\Domain\Enums\MoveDirection;
use App\Modules\Catalog\Domain\Enums\ProductUnit;
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
use App\Modules\Catalog\Domain\Exceptions\DefaultVatRateMustBeActive;
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\VatRateNameTaken;
use App\Modules\Catalog\Domain\Exceptions\VatRateNotFound;
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
use App\Modules\Shared\Domain\Exceptions\InvalidVatPercentage;
use Tests\Fakes\FakeSettings;
use Tests\Fakes\FakeTransactionManager;
use Tests\Fakes\InMemoryProductRepository;
use Tests\Fakes\InMemoryServiceRepository;
use Tests\Fakes\InMemoryServiceUnitRepository;
use Tests\Fakes\InMemoryVatRateRepository;
use Tests\Fakes\InMemoryWorkTypeRepository;
beforeEach(function () {
@@ -32,6 +40,7 @@ beforeEach(function () {
$this->workTypes = new InMemoryWorkTypeRepository;
$this->products = new InMemoryProductRepository;
$this->settings = new FakeSettings;
$this->vatRates = new InMemoryVatRateRepository;
$transactions = new FakeTransactionManager;
$this->addWorkType = new AddWorkType($units, $services, $this->workTypes, $transactions);
@@ -39,8 +48,14 @@ beforeEach(function () {
$this->setWorkTypeActive = new SetCatalogItemActive($units, $services, $this->workTypes, $transactions);
$this->setHourlyRate = new SetHourlyRate($this->workTypes, $transactions);
$this->setMileageRate = new SetMileageRate($this->settings);
$this->addProduct = new AddProduct($this->products, $this->workTypes, $transactions);
$this->updateProduct = new UpdateProduct($this->products, $this->workTypes, $transactions);
$this->addProduct = new AddProduct($this->products, $this->workTypes, $this->vatRates, $transactions);
$this->updateProduct = new UpdateProduct($this->products, $this->workTypes, $this->vatRates, $transactions);
$this->addVatRate = new AddVatRate($this->vatRates, $transactions);
$this->updateVatRate = new UpdateVatRate($this->vatRates, $transactions);
$this->setVatRateActive = new SetVatRateActive($this->vatRates, $transactions);
$this->general = $this->addVatRate->execute('Yleinen', '25,5', makeDefault: true);
$this->reduced = $this->addVatRate->execute('Alennettu', '10');
$this->setProductActive = new SetProductActive($this->products, $transactions);
$this->moveProduct = new MoveProduct($this->products, $transactions);
@@ -48,9 +63,9 @@ beforeEach(function () {
$this->passPhotos = $this->addWorkType->execute($consumer, null, 'passikuvat');
});
function productInput(string $name, string $price = '15', string $duration = '', ?int $workTypeId = null): ProductInput
function productInput(string $name, string $price = '15', string $duration = '', ?int $workTypeId = null, int $vatRateId = 1): ProductInput
{
return new ProductInput($name, ProductUnit::Piece, $price, true, $duration, $workTypeId);
return new ProductInput($name, ProductUnit::Piece, $price, $vatRateId, true, $duration, $workTypeId);
}
it('sets, changes and removes a work type hourly rate', function () {
@@ -124,3 +139,35 @@ it('moves and deactivates products', function () {
$this->setProductActive->execute($first, false);
expect($this->products->findById($first)?->isActive())->toBeFalse();
});
it('keeps exactly one default VAT rate', function () {
$this->updateVatRate->execute($this->reduced, 'Alennettu', '10', makeDefault: true);
expect($this->vatRates->findById($this->reduced)?->isDefault())->toBeTrue()
->and($this->vatRates->findById($this->general)?->isDefault())->toBeFalse();
$this->updateVatRate->execute($this->reduced, 'Alennettu', '14');
expect($this->vatRates->findById($this->reduced)?->isDefault())->toBeTrue()
->and($this->vatRates->findById($this->reduced)?->percentage()->basisPoints)->toBe(1400);
});
it('enforces VAT rate rules', function (Closure $change, string $exception) {
expect(fn () => $change->call($this))->toThrow($exception);
})->with([
'default stays in use' => [fn () => $this->setVatRateActive->execute($this->general, false), DefaultVatRateMustBeActive::class],
'name taken, ignoring case' => [fn () => $this->addVatRate->execute('yleinen', '24'), VatRateNameTaken::class],
'percentage at most 100' => [fn () => $this->addVatRate->execute('Liikaa', '101'), InvalidVatPercentage::class],
'unknown rate for a product' => [fn () => $this->addProduct->execute(productInput('Tuloste', vatRateId: 99)), VatRateNotFound::class],
]);
it('does not let an inactive VAT rate be chosen anew but keeps it on a product', function () {
$id = $this->addProduct->execute(productInput('Passikuvat', vatRateId: $this->reduced));
$this->setVatRateActive->execute($this->reduced, false);
$this->updateProduct->execute($id, productInput('Passikuvat', '20', vatRateId: $this->reduced));
expect($this->products->findById($id)?->vatRateId())->toBe($this->reduced);
expect(fn () => $this->addProduct->execute(productInput('Tuloste', vatRateId: $this->reduced)))
->toThrow(VatRateNotFound::class);
});