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')]]);
});
@@ -0,0 +1,55 @@
<?php
use App\Models\User;
use App\Modules\Catalog\Application\UseCases\SetMileageRate;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ProductModel;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use App\Modules\Shared\Application\Contracts\Settings;
use App\Modules\Users\Domain\Enums\MoneyPermission;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
ProductModel::factory()->consumer()->create(['name' => 'Passikuvat', 'price_cents' => 1800]);
ProductModel::factory()->consumer()->inactive()->create(['name' => 'Vanha passikuva']);
ProductModel::factory()->create(['name' => 'Roll-up', 'price_cents' => 12000]);
WorkTypeModel::query()->where('name', 'painotyö')->update(['hourly_rate_cents' => 7500]);
app(Settings::class)->put(SetMileageRate::SETTING_KEY, '59');
});
it('shows consumer prices with VAT but no other prices to an employee without the price permission', function () {
$this->actingAs(User::factory()->employee()->create())
->get(route('catalog.price-list'))
->assertOk()
->assertSee('Passikuvat')
->assertSee("18,00\u{00A0}€", false)
->assertSee("25,5\u{00A0}%", false)
->assertDontSee('Vanha passikuva')
->assertDontSee('Roll-up')
->assertDontSee('painotyö')
->assertDontSee("75,00\u{00A0}€", false)
->assertDontSee("0,59\u{00A0}€", false)
->assertSee(__('catalog.price_list.no_permission'));
});
it('shows every price to an employee with the price permission and to management', function (Closure $makeUser) {
$this->actingAs($makeUser())
->get(route('catalog.price-list'))
->assertOk()
->assertSee('Roll-up')
->assertSee("120,00\u{00A0}€", false)
->assertSee("150,60\u{00A0}€", false)
->assertSee('painotyö')
->assertSee("75,00\u{00A0}€", false)
->assertSee("0,59\u{00A0}€", false);
})->with([
'employee with prices' => [fn () => User::factory()->employee()->withMoneyPermissions(MoneyPermission::Prices)->create()],
'management' => [fn () => User::factory()->management()->create()],
]);
it('keeps subcontractors out of the price list', function () {
$this->actingAs(User::factory()->subcontractor()->create())
->get(route('catalog.price-list'))
->assertForbidden();
});