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,6 +3,7 @@
use App\Models\User;
use App\Modules\Catalog\Application\Contracts\PriceListQueries;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ProductModel;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\VatRateModel;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
@@ -29,9 +30,11 @@ it('shows the price list pages to admins and management only', function (string
'subcontractor' => ['subcontractor', 403],
]);
it('shows the price list menu link only to those who may manage it', function () {
$this->get(route('customers.index'))->assertSee('Hinnasto');
$this->actingAs(User::factory()->create())->get(route('customers.index'))->assertDontSee('Hinnasto');
it('links the menu to the management page or the read-only price list by role', function () {
$this->get(route('customers.index'))->assertSee(route('catalog.prices'), false);
$this->actingAs(User::factory()->employee()->create())->get(route('customers.index'))
->assertSee(route('catalog.price-list'), false)
->assertDontSee(route('catalog.prices'), false);
});
it('saves and removes hourly rates from the grid', function () {
@@ -178,3 +181,66 @@ it('saves the mileage rate', function () {
->call('saveMileageRate')
->assertHasErrors(['mileageRate']);
});
it('manages VAT rates and keeps one default', function () {
$general = VatRateModel::query()->where('is_default', true)->sole();
Livewire::test('catalog::vat-rate-form')
->set('name', 'Alennettu 14 %')
->set('percentage', '14')
->set('makeDefault', true)
->call('save')
->assertHasNoErrors()
->assertRedirect(route('catalog.prices'));
$new = VatRateModel::query()->where('name', 'Alennettu 14 %')->sole();
expect($new->basis_points)->toBe(1400)
->and($new->is_default)->toBeTrue()
->and($general->refresh()->is_default)->toBeFalse();
Livewire::test('catalog::vat-rate-form', ['vatRate' => $general->id])
->assertSet('percentage', '25,5')
->set('percentage', '101')
->call('save')
->assertHasErrors(['percentage' => [__('catalog.errors.invalid_vat_percentage')]]);
});
it('does not take the default VAT rate out of use', function () {
$general = VatRateModel::query()->where('is_default', true)->sole();
Livewire::test('catalog::prices')
->call('toggleActive', 'vat_rate', $general->id)
->assertSet('errorMessage', __('catalog.errors.default_vat_rate_active', ['name' => 'Yleinen']));
expect($general->refresh()->is_active)->toBeTrue();
});
it('stores a consumer price with VAT and shows both prices', function () {
$reduced = VatRateModel::query()->where('basis_points', 1000)->sole();
Livewire::test('catalog::product-form')
->assertSet('form.vat_rate_id', (string) VatRateModel::query()->where('is_default', true)->value('id'))
->set('form.name', 'Passikuvat')
->set('form.is_consumer_product', true)
->assertSee('Hinta sis. ALV')
->set('form.price', '18')
->assertSee("Veroton hinta: 14,34\u{00A0}€")
->set('form.vat_rate_id', (string) $reduced->id)
->call('save')
->assertHasNoErrors();
$product = app(PriceListQueries::class)->findProduct(ProductModel::query()->sole()->id);
expect([$product->priceCents, $product->grossCents, $product->netCents, $product->vatBasisPoints])->toBe([1800, 1800, 1636, 1000]);
});
it('does not let an inactive VAT rate be chosen for a product', function () {
$inactive = VatRateModel::factory()->inactive()->create();
Livewire::test('catalog::product-form')
->set('form.name', 'Tuloste')
->set('form.price', '2')
->set('form.vat_rate_id', (string) $inactive->id)
->call('save')
->assertHasErrors(['form.vat_rate_id' => [__('catalog.errors.vat_rate_unavailable')]]);
});