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:
2026-11-24 07:30:49 +02:00
co-authored by Claude Opus 5.5
parent 1acdaa5b19
commit dc2612cf46
58 changed files with 2577 additions and 32 deletions
@@ -71,7 +71,7 @@ it('moves a work type to another service, last there', function () {
$this->addWorkType->execute($this->media, $this->photo, 'kuvaus');
$editing = $this->addWorkType->execute($this->media, $this->video, 'editointi');
$this->updateWorkType->execute($editing, $this->photo, 'editointi');
$this->updateWorkType->execute($editing, $this->photo, 'editointi', '');
$workType = $this->workTypes->findById($editing);
expect($workType?->serviceId())->toBe($this->photo)
@@ -0,0 +1,126 @@
<?php
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\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\UpdateProduct;
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\InvalidProductDuration;
use App\Modules\Catalog\Domain\Exceptions\ProductNameTaken;
use App\Modules\Catalog\Domain\Exceptions\ProductNeedsWorkType;
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
use Tests\Fakes\FakeSettings;
use Tests\Fakes\FakeTransactionManager;
use Tests\Fakes\InMemoryProductRepository;
use Tests\Fakes\InMemoryServiceRepository;
use Tests\Fakes\InMemoryServiceUnitRepository;
use Tests\Fakes\InMemoryWorkTypeRepository;
beforeEach(function () {
$units = new InMemoryServiceUnitRepository;
$services = new InMemoryServiceRepository;
$this->workTypes = new InMemoryWorkTypeRepository;
$this->products = new InMemoryProductRepository;
$this->settings = new FakeSettings;
$transactions = new FakeTransactionManager;
$this->addWorkType = new AddWorkType($units, $services, $this->workTypes, $transactions);
$this->updateWorkType = new UpdateWorkType($units, $services, $this->workTypes, $transactions);
$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->setProductActive = new SetProductActive($this->products, $transactions);
$this->moveProduct = new MoveProduct($this->products, $transactions);
$consumer = (new AddServiceUnit($units, $transactions))->execute('Kuluttajapalvelut');
$this->passPhotos = $this->addWorkType->execute($consumer, null, 'passikuvat');
});
function productInput(string $name, string $price = '15', string $duration = '', ?int $workTypeId = null): ProductInput
{
return new ProductInput($name, ProductUnit::Piece, $price, true, $duration, $workTypeId);
}
it('sets, changes and removes a work type hourly rate', function () {
$rate = $this->addWorkType->execute(1, null, 'ohjelmointityö', '85');
expect($this->workTypes->findById($rate)?->hourlyRate()?->cents)->toBe(8500);
$this->updateWorkType->execute($rate, null, 'ohjelmointityö', '110,50');
expect($this->workTypes->findById($rate)?->hourlyRate()?->cents)->toBe(11050);
expect($this->setHourlyRate->execute($rate, ''))->toBeNull()
->and($this->workTypes->findById($rate)?->hourlyRate())->toBeNull();
});
it('rejects an invalid hourly rate without saving it', function () {
$this->setHourlyRate->execute($this->passPhotos, '12,345');
})->throws(InvalidMoneyAmount::class);
it('stores and removes the mileage rate', function () {
expect($this->setMileageRate->execute('0,59')?->cents)->toBe(59)
->and($this->settings->get(SetMileageRate::SETTING_KEY))->toBe('59');
$this->setMileageRate->execute('');
expect($this->settings->get(SetMileageRate::SETTING_KEY))->toBeNull();
});
it('adds products last with their price and automatic hours', function () {
$this->addProduct->execute(productInput('Tuloste A4', '1,50'));
$id = $this->addProduct->execute(productInput('Passikuvat', '18', '0,25', $this->passPhotos));
$saved = $this->products->findById($id);
expect($saved?->sortOrder())->toBe(2)
->and($saved?->price()->cents)->toBe(1800)
->and($saved?->duration()->quarters)->toBe(1)
->and($saved?->workTypeId())->toBe($this->passPhotos);
});
it('enforces product rules', function (Closure $add, string $exception) {
$this->addProduct->execute(productInput('Passikuvat'));
expect(fn () => $add->call($this))->toThrow($exception);
})->with([
'name taken, ignoring case' => [fn () => $this->addProduct->execute(productInput('passikuvat')), ProductNameTaken::class],
'price required' => [fn () => $this->addProduct->execute(productInput('Tuloste', '')), InvalidMoneyAmount::class],
'duration needs a work type' => [fn () => $this->addProduct->execute(productInput('Tuloste', '1', '0,5')), ProductNeedsWorkType::class],
'duration in quarters' => [fn () => $this->addProduct->execute(productInput('Tuloste', '1', '0,1', $this->passPhotos)), InvalidProductDuration::class],
'unknown work type' => [fn () => $this->addProduct->execute(productInput('Tuloste', '1', '0,5', 999)), CatalogItemNotFound::class],
]);
it('keeps an inactive work type on update but does not let it be chosen anew', function () {
$id = $this->addProduct->execute(productInput('Passikuvat', '18', '0,25', $this->passPhotos));
$this->setWorkTypeActive->execute(CatalogLevel::WorkType, $this->passPhotos, false);
$this->updateProduct->execute($id, productInput('Passikuvat', '20', '0,25', $this->passPhotos));
expect($this->products->findById($id)?->price()->cents)->toBe(2000);
$other = $this->addProduct->execute(productInput('Tuloste'));
expect(fn () => $this->updateProduct->execute($other, productInput('Tuloste', '1', '0,25', $this->passPhotos)))
->toThrow(CatalogItemNotFound::class);
});
it('moves and deactivates products', function () {
$first = $this->addProduct->execute(productInput('Passikuvat'));
$second = $this->addProduct->execute(productInput('Tuloste'));
expect($this->moveProduct->execute($second, MoveDirection::Up))->toBeTrue()
->and($this->products->findById($second)?->sortOrder())->toBe(1)
->and($this->moveProduct->execute($second, MoveDirection::Up))->toBeFalse();
$this->setProductActive->execute($first, false);
expect($this->products->findById($first)?->isActive())->toBeFalse();
});
+30
View File
@@ -0,0 +1,30 @@
<?php
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
use App\Modules\Shared\Domain\ValueObjects\Money;
it('parses euro amounts typed in Finnish or English notation', function (string $typed, int $cents) {
expect(Money::parse($typed)?->cents)->toBe($cents);
})->with([
'whole euros' => ['85', 8500],
'comma decimals' => ['85,50', 8550],
'one decimal' => ['85.5', 8550],
'thousands with a space' => ['1 200,00', 120000],
'with the euro sign' => ['0,59 €', 59],
'zero' => ['0', 0],
]);
it('treats an empty value as no amount', function () {
expect(Money::parse(' '))->toBeNull();
});
it('rejects amounts that are not euros with at most two decimals', function (string $typed) {
Money::parse($typed);
})->throws(InvalidMoneyAmount::class)->with(['-5', '1,234', 'abc', '1.000,00', '1000001']);
it('formats with a no-break space and the currency', function () {
$money = Money::fromCents(120050);
expect($money->format())->toBe("1\u{00A0}200,50\u{00A0}€")
->and($money->inputValue())->toBe('1200,50');
});