Files
petriandClaude Opus 5.5 bdd8d446d8 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]>
2026-11-24 07:51:19 +02:00

59 lines
2.3 KiB
PHP

<?php
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
use App\Modules\Shared\Domain\Exceptions\InvalidVatPercentage;
use App\Modules\Shared\Domain\ValueObjects\Money;
use App\Modules\Shared\Domain\ValueObjects\VatPercentage;
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');
});
it('adds and removes VAT, rounding half up to the cent', function (int $net, string $vat, int $gross) {
$percentage = VatPercentage::parse($vat);
expect(Money::fromCents($net)->addVat($percentage)->cents)->toBe($gross);
})->with([
'general rate' => [1434, '25,5', 1800],
'rounds half up' => [2, '25', 3],
'reduced rate' => [1000, '13,5', 1135],
'zero rate' => [999, '0', 999],
]);
it('works out the price without VAT from a consumer price', function () {
expect(Money::fromCents(1800)->removeVat(VatPercentage::parse('25,5'))->cents)->toBe(1434)
->and(Money::fromCents(1000)->removeVat(VatPercentage::parse('0'))->cents)->toBe(1000);
});
it('parses and formats VAT percentages', function () {
expect(VatPercentage::parse('25,5 %')->basisPoints)->toBe(2550)
->and(VatPercentage::parse('13.5')->format())->toBe("13,5\u{00A0}%")
->and(VatPercentage::parse('0')->inputValue())->toBe('0');
});
it('rejects invalid VAT percentages', function (string $typed) {
VatPercentage::parse($typed);
})->throws(InvalidVatPercentage::class)->with(['', '-1', '100,01', '12,345', 'abc']);