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
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace App\Modules\Shared\Domain\Exceptions;
final class InvalidVatPercentage extends DomainException
{
public function __construct()
{
parent::__construct('A VAT rate must be 0–100 % with at most two decimals.');
}
}
@@ -55,6 +55,24 @@ final readonly class Money
return new self((int) $matches[1] * 100 + (int) $fraction);
}
/**
* This amount without VAT plus the VAT, rounded half up to the cent.
*/
public function addVat(VatPercentage $vat): self
{
return new self(intdiv($this->cents * (10_000 + $vat->basisPoints) * 2 + 10_000, 20_000));
}
/**
* The amount without VAT when this amount includes it, rounded half up to the cent.
*/
public function removeVat(VatPercentage $vat): self
{
$divisor = 10_000 + $vat->basisPoints;
return new self(intdiv($this->cents * 10_000 * 2 + $divisor, $divisor * 2));
}
/**
* Finnish notation with the currency, e.g. "1 200,50 €".
*/
@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace App\Modules\Shared\Domain\ValueObjects;
use App\Modules\Shared\Domain\Exceptions\InvalidVatPercentage;
/**
* A VAT rate such as 25,5 %, stored in hundredths of a percent (2550) so
* that calculations stay in whole numbers.
*/
final readonly class VatPercentage
{
public const int MAX_BASIS_POINTS = 10_000;
public int $basisPoints;
public function __construct(int $basisPoints)
{
if ($basisPoints < 0 || $basisPoints > self::MAX_BASIS_POINTS) {
throw new InvalidVatPercentage;
}
$this->basisPoints = $basisPoints;
}
/**
* Parses "25,5", "25.5", "0" or "25,5 %".
*
* @throws InvalidVatPercentage
*/
public static function parse(string $value): self
{
$value = str_replace(',', '.', preg_replace('/[\s\x{00A0}%]+/u', '', $value) ?? '');
if (preg_match('/^(\d{1,3})(?:\.(\d{1,2}))?$/', $value, $matches) !== 1) {
throw new InvalidVatPercentage;
}
return new self((int) $matches[1] * 100 + (int) str_pad($matches[2] ?? '', 2, '0'));
}
/**
* Finnish notation without trailing zeros, e.g. "25,5 %" or "0 %".
*/
public function format(): string
{
return $this->inputValue()."\u{00A0}%";
}
/**
* The percentage for an input field, e.g. "25,5".
*/
public function inputValue(): string
{
return str_replace('.', ',', rtrim(rtrim(number_format($this->basisPoints / 100, 2, '.', ''), '0'), '.'));
}
}