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:
@@ -3,6 +3,7 @@
|
||||
use App\Models\User;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ProductModel;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\VatRateModel;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
@@ -28,6 +29,8 @@ it('has no accessibility issues on the catalog pages', function (string $page, s
|
||||
'price list' => route('catalog.prices'),
|
||||
'new product' => route('catalog.products.create'),
|
||||
'edit product' => route('catalog.products.edit', $product->id),
|
||||
'edit VAT rate' => route('catalog.vat-rates.edit', VatRateModel::query()->where('is_default', true)->value('id')),
|
||||
'read-only price list' => route('catalog.price-list'),
|
||||
};
|
||||
|
||||
$visit = visit($url);
|
||||
@@ -36,7 +39,7 @@ it('has no accessibility issues on the catalog pages', function (string $page, s
|
||||
|
||||
$visit->assertNoJavaScriptErrors()
|
||||
->assertNoAccessibilityIssues(level: 2);
|
||||
})->with(['catalog', 'new service unit', 'new service', 'edit work type', 'price list', 'new product', 'edit product'])->with([
|
||||
})->with(['catalog', 'new service unit', 'new service', 'edit work type', 'price list', 'new product', 'edit product', 'edit VAT rate', 'read-only price list'])->with([
|
||||
'desktop, light' => ['desktop', 'light'],
|
||||
'desktop, dark' => ['desktop', 'dark'],
|
||||
'mobile, light' => ['mobile', 'light'],
|
||||
@@ -75,3 +78,21 @@ it('saves an hourly rate with the keyboard and announces it', function () {
|
||||
|
||||
expect($workType->refresh()->hourly_rate_cents)->toBe(8550);
|
||||
});
|
||||
|
||||
it('has no accessibility issues on the price list of an employee without the price permission', function (string $device, string $colorScheme) {
|
||||
$this->actingAs(User::factory()->employee()->create());
|
||||
ProductModel::factory()->consumer()->create(['name' => 'Passikuvat']);
|
||||
|
||||
$visit = visit(route('catalog.price-list'));
|
||||
$visit = $device === 'mobile' ? $visit->on()->mobile() : $visit->on()->desktop();
|
||||
$visit = $colorScheme === 'dark' ? $visit->inDarkMode() : $visit->inLightMode();
|
||||
|
||||
$visit->assertSee('Passikuvat')
|
||||
->assertNoJavaScriptErrors()
|
||||
->assertNoAccessibilityIssues(level: 2);
|
||||
})->with([
|
||||
'desktop, light' => ['desktop', 'light'],
|
||||
'desktop, dark' => ['desktop', 'dark'],
|
||||
'mobile, light' => ['mobile', 'light'],
|
||||
'mobile, dark' => ['mobile', 'dark'],
|
||||
]);
|
||||
|
||||
@@ -26,6 +26,7 @@ class InMemoryProductRepository implements ProductRepository
|
||||
$product->name(),
|
||||
$product->unit(),
|
||||
$product->price(),
|
||||
$product->vatRateId(),
|
||||
$product->isConsumerProduct(),
|
||||
$product->duration(),
|
||||
$product->workTypeId(),
|
||||
|
||||
@@ -43,6 +43,7 @@ class InMemoryUserAccountRepository implements UserAccountRepository
|
||||
$user->isActive(),
|
||||
$user->employmentEndsOn(),
|
||||
$user->supervisorId(),
|
||||
$user->moneyPermissions(),
|
||||
);
|
||||
|
||||
return clone $this->users[$id];
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Modules\Catalog\Domain\Contracts\VatRateRepository;
|
||||
use App\Modules\Catalog\Domain\Entities\VatRate;
|
||||
use App\Modules\Catalog\Domain\Enums\MoveDirection;
|
||||
|
||||
class InMemoryVatRateRepository implements VatRateRepository
|
||||
{
|
||||
/** @var array<int, VatRate> */
|
||||
public array $items = [];
|
||||
|
||||
private int $nextId = 1;
|
||||
|
||||
public function findById(int $id): ?VatRate
|
||||
{
|
||||
return isset($this->items[$id]) ? clone $this->items[$id] : null;
|
||||
}
|
||||
|
||||
public function findDefault(): ?VatRate
|
||||
{
|
||||
foreach ($this->items as $vatRate) {
|
||||
if ($vatRate->isDefault()) {
|
||||
return clone $vatRate;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function save(VatRate $vatRate): VatRate
|
||||
{
|
||||
$id = $vatRate->id() ?? $this->nextId++;
|
||||
$this->items[$id] = VatRate::reconstitute($id, $vatRate->name(), $vatRate->percentage(), $vatRate->isDefault(), $vatRate->isActive(), $vatRate->sortOrder());
|
||||
|
||||
return clone $this->items[$id];
|
||||
}
|
||||
|
||||
public function isNameTaken(string $name, ?int $exceptId = null): bool
|
||||
{
|
||||
foreach ($this->items as $id => $vatRate) {
|
||||
if ($id !== $exceptId && mb_strtolower($vatRate->name()->value) === mb_strtolower($name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function nextSortOrder(): int
|
||||
{
|
||||
return array_reduce($this->items, fn (int $max, VatRate $vatRate): int => max($max, $vatRate->sortOrder()), 0) + 1;
|
||||
}
|
||||
|
||||
public function neighbour(VatRate $vatRate, MoveDirection $direction): ?VatRate
|
||||
{
|
||||
$candidates = array_values(array_filter($this->items, fn (VatRate $other): bool => $direction === MoveDirection::Up
|
||||
? $other->sortOrder() < $vatRate->sortOrder()
|
||||
: $other->sortOrder() > $vatRate->sortOrder()));
|
||||
|
||||
usort($candidates, fn (VatRate $a, VatRate $b): int => $direction === MoveDirection::Up
|
||||
? $b->sortOrder() <=> $a->sortOrder()
|
||||
: $a->sortOrder() <=> $b->sortOrder());
|
||||
|
||||
return isset($candidates[0]) ? clone $candidates[0] : null;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use App\Modules\Users\Domain\Enums\MoneyPermission;
|
||||
use App\Modules\Users\Infrastructure\Persistence\Eloquent\UserMoneyPermissionsModel;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Livewire\Livewire;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
it('lets admins and management see all money, employees and interns only by permission, subcontractors none', function (Closure $makeUser, array $abilities) {
|
||||
$user = $makeUser();
|
||||
|
||||
foreach (['prices', 'expenses', 'quotes', 'billing'] as $ability) {
|
||||
expect(Gate::forUser($user)->allows('money.'.$ability))->toBe(in_array($ability, $abilities, true), $ability);
|
||||
}
|
||||
})->with([
|
||||
'admin' => [fn () => User::factory()->admin()->create(), ['prices', 'expenses', 'quotes', 'billing']],
|
||||
'management' => [fn () => User::factory()->management()->create(), ['prices', 'expenses', 'quotes', 'billing']],
|
||||
'employee without permissions' => [fn () => User::factory()->employee()->create(), []],
|
||||
'employee with prices and quotes' => [fn () => User::factory()->employee()->withMoneyPermissions(MoneyPermission::Prices, MoneyPermission::Quotes)->create(), ['prices', 'quotes']],
|
||||
'intern with billing' => [fn () => User::factory()->intern()->withMoneyPermissions(MoneyPermission::Billing)->create(), ['billing']],
|
||||
'subcontractor with a stray permission' => [fn () => User::factory()->subcontractor()->withMoneyPermissions(MoneyPermission::Prices)->create(), []],
|
||||
]);
|
||||
|
||||
it('saves money permissions on the user form for an employee', function () {
|
||||
Notification::fake();
|
||||
$this->actingAs(User::factory()->admin()->create());
|
||||
$employee = User::factory()->employee()->create();
|
||||
|
||||
Livewire::test('users::form', ['user' => $employee->id])
|
||||
->assertSee('Rahamäärien näkyvyys')
|
||||
->set('form.money_permissions', ['prices', 'expenses'])
|
||||
->call('save')
|
||||
->assertHasNoErrors();
|
||||
|
||||
expect($employee->refresh()->moneyPermissions?->permissions())->toBe([MoneyPermission::Prices, MoneyPermission::Expenses]);
|
||||
|
||||
Livewire::test('users::form', ['user' => $employee->id])
|
||||
->assertSet('form.money_permissions', ['prices', 'expenses'])
|
||||
->set('form.role', 'management')
|
||||
->assertDontSee('Rahamäärien näkyvyys')
|
||||
->call('save')
|
||||
->assertHasNoErrors();
|
||||
|
||||
expect(UserMoneyPermissionsModel::query()->where('user_id', $employee->id)->sole()->permissions())->toBe([]);
|
||||
});
|
||||
|
||||
it('rejects an unknown money permission', function () {
|
||||
$this->actingAs(User::factory()->admin()->create());
|
||||
$employee = User::factory()->employee()->create();
|
||||
|
||||
Livewire::test('users::form', ['user' => $employee->id])
|
||||
->set('form.money_permissions', ['salaries'])
|
||||
->call('save')
|
||||
->assertHasErrors(['form.money_permissions']);
|
||||
});
|
||||
@@ -3,27 +3,35 @@
|
||||
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\AddVatRate;
|
||||
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\SetVatRateActive;
|
||||
use App\Modules\Catalog\Application\UseCases\UpdateProduct;
|
||||
use App\Modules\Catalog\Application\UseCases\UpdateVatRate;
|
||||
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\DefaultVatRateMustBeActive;
|
||||
use App\Modules\Catalog\Domain\Exceptions\InvalidProductDuration;
|
||||
use App\Modules\Catalog\Domain\Exceptions\ProductNameTaken;
|
||||
use App\Modules\Catalog\Domain\Exceptions\ProductNeedsWorkType;
|
||||
use App\Modules\Catalog\Domain\Exceptions\VatRateNameTaken;
|
||||
use App\Modules\Catalog\Domain\Exceptions\VatRateNotFound;
|
||||
use App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
|
||||
use App\Modules\Shared\Domain\Exceptions\InvalidVatPercentage;
|
||||
use Tests\Fakes\FakeSettings;
|
||||
use Tests\Fakes\FakeTransactionManager;
|
||||
use Tests\Fakes\InMemoryProductRepository;
|
||||
use Tests\Fakes\InMemoryServiceRepository;
|
||||
use Tests\Fakes\InMemoryServiceUnitRepository;
|
||||
use Tests\Fakes\InMemoryVatRateRepository;
|
||||
use Tests\Fakes\InMemoryWorkTypeRepository;
|
||||
|
||||
beforeEach(function () {
|
||||
@@ -32,6 +40,7 @@ beforeEach(function () {
|
||||
$this->workTypes = new InMemoryWorkTypeRepository;
|
||||
$this->products = new InMemoryProductRepository;
|
||||
$this->settings = new FakeSettings;
|
||||
$this->vatRates = new InMemoryVatRateRepository;
|
||||
$transactions = new FakeTransactionManager;
|
||||
|
||||
$this->addWorkType = new AddWorkType($units, $services, $this->workTypes, $transactions);
|
||||
@@ -39,8 +48,14 @@ beforeEach(function () {
|
||||
$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->addProduct = new AddProduct($this->products, $this->workTypes, $this->vatRates, $transactions);
|
||||
$this->updateProduct = new UpdateProduct($this->products, $this->workTypes, $this->vatRates, $transactions);
|
||||
$this->addVatRate = new AddVatRate($this->vatRates, $transactions);
|
||||
$this->updateVatRate = new UpdateVatRate($this->vatRates, $transactions);
|
||||
$this->setVatRateActive = new SetVatRateActive($this->vatRates, $transactions);
|
||||
|
||||
$this->general = $this->addVatRate->execute('Yleinen', '25,5', makeDefault: true);
|
||||
$this->reduced = $this->addVatRate->execute('Alennettu', '10');
|
||||
$this->setProductActive = new SetProductActive($this->products, $transactions);
|
||||
$this->moveProduct = new MoveProduct($this->products, $transactions);
|
||||
|
||||
@@ -48,9 +63,9 @@ beforeEach(function () {
|
||||
$this->passPhotos = $this->addWorkType->execute($consumer, null, 'passikuvat');
|
||||
});
|
||||
|
||||
function productInput(string $name, string $price = '15', string $duration = '', ?int $workTypeId = null): ProductInput
|
||||
function productInput(string $name, string $price = '15', string $duration = '', ?int $workTypeId = null, int $vatRateId = 1): ProductInput
|
||||
{
|
||||
return new ProductInput($name, ProductUnit::Piece, $price, true, $duration, $workTypeId);
|
||||
return new ProductInput($name, ProductUnit::Piece, $price, $vatRateId, true, $duration, $workTypeId);
|
||||
}
|
||||
|
||||
it('sets, changes and removes a work type hourly rate', function () {
|
||||
@@ -124,3 +139,35 @@ it('moves and deactivates products', function () {
|
||||
$this->setProductActive->execute($first, false);
|
||||
expect($this->products->findById($first)?->isActive())->toBeFalse();
|
||||
});
|
||||
|
||||
it('keeps exactly one default VAT rate', function () {
|
||||
$this->updateVatRate->execute($this->reduced, 'Alennettu', '10', makeDefault: true);
|
||||
|
||||
expect($this->vatRates->findById($this->reduced)?->isDefault())->toBeTrue()
|
||||
->and($this->vatRates->findById($this->general)?->isDefault())->toBeFalse();
|
||||
|
||||
$this->updateVatRate->execute($this->reduced, 'Alennettu', '14');
|
||||
|
||||
expect($this->vatRates->findById($this->reduced)?->isDefault())->toBeTrue()
|
||||
->and($this->vatRates->findById($this->reduced)?->percentage()->basisPoints)->toBe(1400);
|
||||
});
|
||||
|
||||
it('enforces VAT rate rules', function (Closure $change, string $exception) {
|
||||
expect(fn () => $change->call($this))->toThrow($exception);
|
||||
})->with([
|
||||
'default stays in use' => [fn () => $this->setVatRateActive->execute($this->general, false), DefaultVatRateMustBeActive::class],
|
||||
'name taken, ignoring case' => [fn () => $this->addVatRate->execute('yleinen', '24'), VatRateNameTaken::class],
|
||||
'percentage at most 100' => [fn () => $this->addVatRate->execute('Liikaa', '101'), InvalidVatPercentage::class],
|
||||
'unknown rate for a product' => [fn () => $this->addProduct->execute(productInput('Tuloste', vatRateId: 99)), VatRateNotFound::class],
|
||||
]);
|
||||
|
||||
it('does not let an inactive VAT rate be chosen anew but keeps it on a product', function () {
|
||||
$id = $this->addProduct->execute(productInput('Passikuvat', vatRateId: $this->reduced));
|
||||
$this->setVatRateActive->execute($this->reduced, false);
|
||||
|
||||
$this->updateProduct->execute($id, productInput('Passikuvat', '20', vatRateId: $this->reduced));
|
||||
expect($this->products->findById($id)?->vatRateId())->toBe($this->reduced);
|
||||
|
||||
expect(fn () => $this->addProduct->execute(productInput('Tuloste', vatRateId: $this->reduced)))
|
||||
->toThrow(VatRateNotFound::class);
|
||||
});
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
<?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);
|
||||
@@ -28,3 +30,29 @@ it('formats with a no-break space and the currency', function () {
|
||||
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']);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
use App\Modules\Shared\Domain\ValueObjects\EmailAddress;
|
||||
use App\Modules\Users\Domain\Entities\UserAccount;
|
||||
use App\Modules\Users\Domain\Enums\MoneyPermission;
|
||||
use App\Modules\Users\Domain\Enums\Role;
|
||||
use App\Modules\Users\Domain\Exceptions\InternRequiresEndDateAndSupervisor;
|
||||
use App\Modules\Users\Domain\Exceptions\InvalidSupervisor;
|
||||
@@ -72,3 +73,13 @@ it('does not let an inactive user log in', function () {
|
||||
expect($user->canLogInOn(new DateTimeImmutable('2026-01-01')))->toBeFalse()
|
||||
->and($user->isActiveAdmin())->toBeFalse();
|
||||
});
|
||||
|
||||
it('keeps money permissions only for employees and interns', function () {
|
||||
$user = UserAccount::register(new PersonName('Eero'), new EmailAddress('[email protected]'), Role::Employee, moneyPermissions: [MoneyPermission::Quotes, MoneyPermission::Prices]);
|
||||
|
||||
expect($user->moneyPermissions())->toBe([MoneyPermission::Prices, MoneyPermission::Quotes]);
|
||||
|
||||
$user->changeDetails($user->name(), $user->email(), Role::Management, true, null, null, [MoneyPermission::Prices]);
|
||||
|
||||
expect($user->moneyPermissions())->toBe([]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user