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
+2
View File
@@ -4,6 +4,7 @@ namespace Database\Factories;
use App\Modules\Catalog\Domain\Enums\ProductUnit;
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\Database\Eloquent\Factories\Factory;
@@ -23,6 +24,7 @@ class ProductFactory extends Factory
'name' => fake()->unique()->words(2, true),
'unit' => ProductUnit::Piece->value,
'price_cents' => fake()->numberBetween(100, 10000),
'vat_rate_id' => fn (): int => VatRateModel::query()->where('is_default', true)->value('id') ?? VatRateModel::factory()->create(['is_default' => true])->id,
'is_consumer_product' => false,
'auto_hours' => 0,
'work_type_id' => null,
+15
View File
@@ -3,7 +3,9 @@
namespace Database\Factories;
use App\Models\User;
use App\Modules\Users\Domain\Enums\MoneyPermission;
use App\Modules\Users\Domain\Enums\Role;
use App\Modules\Users\Infrastructure\Persistence\Eloquent\UserMoneyPermissionsModel;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
@@ -84,4 +86,17 @@ class UserFactory extends Factory
{
return $this->state(fn (): array => ['is_active' => false]);
}
/**
* Personal money permissions (they apply to employees and interns).
*/
public function withMoneyPermissions(MoneyPermission ...$permissions): static
{
return $this->afterCreating(function (User $user) use ($permissions): void {
UserMoneyPermissionsModel::query()->updateOrCreate(
['user_id' => $user->id],
UserMoneyPermissionsModel::columnsFor(array_values($permissions)),
);
});
}
}
+33
View File
@@ -0,0 +1,33 @@
<?php
namespace Database\Factories;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\VatRateModel;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<VatRateModel>
*/
class VatRateFactory extends Factory
{
protected $model = VatRateModel::class;
/**
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => fake()->unique()->words(2, true),
'basis_points' => fake()->randomElement([0, 1000, 1350, 2550]),
'is_default' => false,
'is_active' => true,
'sort_order' => fake()->unique()->numberBetween(100, 100000),
];
}
public function inactive(): static
{
return $this->state(fn (): array => ['is_active' => false]);
}
}
@@ -0,0 +1,47 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* VAT rates in hundredths of a percent (2550 = 25,5 %). One rate is the
* default, used for hours, expenses and mileage. Rows are never
* deleted, only deactivated.
*/
public function up(): void
{
Schema::create('vat_rates', function (Blueprint $table) {
$table->id();
$table->string('name', 100);
$table->unsignedInteger('basis_points');
$table->boolean('is_default')->default(false);
$table->boolean('is_active')->default(true);
$table->unsignedInteger('sort_order');
$table->timestamps();
$table->index('sort_order');
});
$now = now();
DB::table('vat_rates')->insert(array_map(
fn (array $rate, int $index): array => $rate + ['is_active' => true, 'sort_order' => $index + 1, 'created_at' => $now, 'updated_at' => $now],
[
['name' => 'Yleinen', 'basis_points' => 2550, 'is_default' => true],
['name' => 'Alennettu 13,5 %', 'basis_points' => 1350, 'is_default' => false],
['name' => 'Alennettu 10 %', 'basis_points' => 1000, 'is_default' => false],
['name' => 'Veroton', 'basis_points' => 0, 'is_default' => false],
],
[0, 1, 2, 3],
));
}
public function down(): void
{
Schema::dropIfExists('vat_rates');
}
};
@@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Every product has a VAT rate; existing products get the default one.
* The column stays nullable in the database, the domain requires it.
*/
public function up(): void
{
Schema::table('products', function (Blueprint $table) {
$table->foreignId('vat_rate_id')->nullable()->after('price_cents')->constrained()->restrictOnDelete();
});
$defaultId = DB::table('vat_rates')->where('is_default', true)->value('id');
DB::table('products')->update(['vat_rate_id' => $defaultId]);
}
public function down(): void
{
Schema::table('products', function (Blueprint $table) {
$table->dropConstrainedForeignId('vat_rate_id');
});
}
};
@@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Personal permissions of employees and interns to see money amounts.
* Admins and management see all amounts by role.
*/
public function up(): void
{
Schema::create('user_money_permissions', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->unique()->constrained()->cascadeOnDelete();
$table->boolean('can_see_prices')->default(false);
$table->boolean('can_see_expenses')->default(false);
$table->boolean('can_see_quotes')->default(false);
$table->boolean('can_see_billing')->default(false);
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('user_money_permissions');
}
};