70ca2fa74a
Client sees only Total. Salary is calculated from salary_base = client_price
× (1 − margin/100). Margin never appears in customer-facing surfaces (PDF,
tracking JSON, portal).
Terminology: "marjă internă" — internal profit margin. NOT VAT/TVA. Never
called NDS/TVA anywhere in the code to avoid confusion with real Moldova
tax reporting (Doc 19/1C integration).
== Configuration ==
Fallback chain (in MarginResolver::resolve):
1. WorkOrder.override_margin_pct — per-Fișă for special contracts/VIP
2. User.internal_margin_pct — per-mechanic (main setting)
3. Company.settings.default_internal_margin_pct — tenant default
4. 0.0 — no margin
Example (mechanic Andrei with 20% margin):
User enters price_per_hour = 250 for 1h diagnosis
→ total = 250 (what client sees, goes into PDF)
→ salary_base = 250 × 0.80 = 200 (what mechanic gets salaried on)
→ applied_margin_pct = 20 (frozen)
If admin later changes Andrei's margin to 40%, the row's salary_base does
NOT change — history is immutable. Only new rows use the new margin.
Solves the retroactive-recompute problem for closed payroll periods.
== salary_base freeze semantics ==
wo_works gains 2 columns:
salary_base decimal(10,2) nullable
applied_margin_pct decimal(5,2) nullable
Frozen at save time by WorkOrderWork::saving hook. Recomputes only if
total OR master_id changes (i.e., someone actively edits the price or
reassigns the mechanic — in those cases we WANT the salary_base to
follow). Legacy rows (before this feature) have null salary_base;
PayrollCalculator falls back to total for them.
== PayrollCalculator uses salary_base ==
Previously: sum(wo_works.total) × works_pct → gave the mechanic a cut
of the price INCLUDING margin.
Now: sum(salary_base ?? total) × works_pct → the cut is from the
labor rate excluding margin.
Impact: for a 250 lei diagnosis at 20% margin with 50% payroll cut, the
mechanic gets 200 × 50% = 100 lei (was 250 × 50% = 125 lei). The shop
keeps the 50 lei margin regardless of the payroll %.
== RBAC gate ==
New permission FINANCE_VIEW_INTERNAL_MARGIN. Assigned to owner + admin +
manager + accountant in seed matrix. Not granted to mechanic,
receptionist, or viewer — those roles never see the "Bază salariu"
disclosure line or the margin % fields.
== UI surfaces ==
UserResource — new "Salariu & marjă" section (visible only with
FINANCE_VIEW_INTERNAL_MARGIN):
- Tarif orar (MDL)
- Marjă internă (%) with helper text explaining the -X% semantics
- Placeholder tells manager the exact formula
WorkOrderResource form — new override_margin_pct field in the "Plată &
total" section, gated by same permission. Helper text: "Doar pentru
cazuri speciale. Lasă gol pentru a folosi marja mecanicului."
WorksRelationManager (WO edit page) — Total column now shows a gray
subtitle line "Bază salariu: 200.00 MDL · marjă 20%" ONLY for users
with FINANCE_VIEW_INTERNAL_MARGIN. Everyone else sees just Total.
== Contract tests: NO leak ==
InternalMarginTest verifies with black-box grepping that:
- WorkOrderPdfService::generate output contains NONE of
{salary_base, internal_margin, applied_margin_pct, marja intern,
Bază salariu}
- /api/track/{token} JSON payload contains NONE of the same terms
- wo_parts table has no salary_base column (margin ONLY on labor)
- Changing mechanic.internal_margin_pct after work is saved does NOT
rewrite the historical salary_base (frozen)
- WO override wins over mechanic margin (contract-priced clients)
- Fallback chain: WO → mechanic → company default → 0
== Suite ==
298 passed (828 assertions). Was 285. +13 InternalMarginTest.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
133 lines
5.7 KiB
PHP
133 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Auth\Permissions;
|
|
use App\Models\Central\Company;
|
|
use App\Models\Central\Plan;
|
|
use App\Models\Tenant\User;
|
|
use App\Services\RbacSeeder;
|
|
use App\Tenancy\TenantManager;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Spatie\Permission\Models\Permission;
|
|
use Spatie\Permission\Models\Role;
|
|
use Spatie\Permission\PermissionRegistrar;
|
|
use Tests\TestCase;
|
|
|
|
class RbacTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private Company $company;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$plan = Plan::firstOrCreate(['slug' => 'test'], ['name' => 'T', 'price' => 0, 'features' => []]);
|
|
$this->company = Company::create([
|
|
'plan_id' => $plan->id, 'slug' => 'rbac-' . uniqid(),
|
|
'name' => 'RBAC Co', 'status' => 'active',
|
|
]);
|
|
app(TenantManager::class)->setCurrent($this->company);
|
|
app(RbacSeeder::class)->seedTenantRoles($this->company->id);
|
|
app(PermissionRegistrar::class)->setPermissionsTeamId($this->company->id);
|
|
}
|
|
|
|
public function test_seeder_creates_51_permissions(): void
|
|
{
|
|
$this->assertEquals(52, Permission::where('guard_name', 'web')->count());
|
|
}
|
|
|
|
public function test_seeder_creates_7_roles_per_tenant(): void
|
|
{
|
|
$roles = Role::where('company_id', $this->company->id)->pluck('name')->toArray();
|
|
sort($roles);
|
|
$this->assertEquals(['accountant', 'admin', 'manager', 'mechanic', 'owner', 'receptionist', 'viewer'], $roles);
|
|
}
|
|
|
|
public function test_owner_role_has_all_permissions(): void
|
|
{
|
|
$owner = Role::where('company_id', $this->company->id)->where('name', 'owner')->first();
|
|
$this->assertEquals(52, $owner->permissions->count());
|
|
}
|
|
|
|
public function test_mechanic_role_has_minimal_permissions(): void
|
|
{
|
|
$mechanic = Role::where('company_id', $this->company->id)->where('name', 'mechanic')->first();
|
|
$perms = $mechanic->permissions->pluck('name')->toArray();
|
|
$this->assertContains(Permissions::WORK_ORDERS_VIEW_OWN_ASSIGNED, $perms);
|
|
$this->assertContains(Permissions::INVENTORY_VIEW, $perms);
|
|
$this->assertNotContains(Permissions::WORK_ORDERS_VIEW_ALL, $perms);
|
|
$this->assertNotContains(Permissions::FINANCE_VIEW_OVERVIEW, $perms);
|
|
$this->assertNotContains(Permissions::ADMIN_USERS_MANAGE, $perms);
|
|
}
|
|
|
|
public function test_accountant_can_see_finance_but_not_admin(): void
|
|
{
|
|
$accountant = Role::where('company_id', $this->company->id)->where('name', 'accountant')->first();
|
|
$perms = $accountant->permissions->pluck('name')->toArray();
|
|
$this->assertContains(Permissions::FINANCE_VIEW_OVERVIEW, $perms);
|
|
$this->assertContains(Permissions::FINANCE_VIEW_PL, $perms);
|
|
$this->assertContains(Permissions::SALARIES_CALCULATE, $perms);
|
|
$this->assertNotContains(Permissions::ADMIN_USERS_MANAGE, $perms);
|
|
$this->assertNotContains(Permissions::WORK_ORDERS_DELETE, $perms);
|
|
}
|
|
|
|
public function test_user_can_method_returns_true_when_role_has_permission(): void
|
|
{
|
|
$user = User::create(['name' => 'M', 'email' => 'm-' . uniqid() . '@e.com', 'password' => bcrypt('x'), 'role' => 'mechanic', 'status' => 'active']);
|
|
$user->syncRoles(['mechanic']);
|
|
|
|
$this->assertTrue($user->canDo(Permissions::WORK_ORDERS_VIEW_OWN_ASSIGNED));
|
|
$this->assertFalse($user->canDo(Permissions::FINANCE_VIEW_OVERVIEW));
|
|
}
|
|
|
|
public function test_admin_bypasses_permission_check(): void
|
|
{
|
|
$admin = User::create(['name' => 'A', 'email' => 'a-' . uniqid() . '@e.com', 'password' => bcrypt('x'), 'role' => 'admin', 'status' => 'active']);
|
|
$admin->syncRoles(['admin']);
|
|
|
|
// Admin gets the bypass even if a permission is not explicitly granted
|
|
$this->assertTrue($admin->canDo('some.permission.that.does.not.exist'));
|
|
$this->assertTrue($admin->canDo(Permissions::FINANCE_DELETE_PAYMENT));
|
|
}
|
|
|
|
public function test_owner_helper_returns_true_for_owner_role_user(): void
|
|
{
|
|
$user = User::create(['name' => 'O', 'email' => 'o-' . uniqid() . '@e.com', 'password' => bcrypt('x'), 'role' => 'owner', 'status' => 'active']);
|
|
$this->assertTrue($user->isOwner());
|
|
$this->assertTrue($user->isAdmin()); // owner counts as admin for canDo bypass
|
|
}
|
|
|
|
public function test_sync_users_to_roles_maps_legacy_role_strings(): void
|
|
{
|
|
$u1 = User::create(['name' => 'X', 'email' => 'x@e.com', 'password' => bcrypt('x'), 'role' => 'parts_manager', 'status' => 'active']);
|
|
$u2 = User::create(['name' => 'Y', 'email' => 'y@e.com', 'password' => bcrypt('x'), 'role' => 'master', 'status' => 'active']);
|
|
$u3 = User::create(['name' => 'Z', 'email' => 'z@e.com', 'password' => bcrypt('x'), 'role' => 'user', 'status' => 'active']);
|
|
|
|
app(RbacSeeder::class)->syncUsersToRoles($this->company->id);
|
|
|
|
$u1->refresh(); $u2->refresh(); $u3->refresh();
|
|
// parts_manager → manager
|
|
$this->assertTrue($u1->hasRole('manager'));
|
|
// master → mechanic
|
|
$this->assertTrue($u2->hasRole('mechanic'));
|
|
// user → viewer
|
|
$this->assertTrue($u3->hasRole('viewer'));
|
|
}
|
|
|
|
public function test_two_factor_helper_reflects_app_authentication_secret(): void
|
|
{
|
|
$user = User::create(['name' => 'T', 'email' => 't@e.com', 'password' => bcrypt('x'), 'role' => 'admin', 'status' => 'active']);
|
|
$this->assertFalse($user->hasTwoFactorEnabled());
|
|
|
|
$user->saveAppAuthenticationSecret('FAKEBASE32SECRET====');
|
|
$user->refresh();
|
|
$this->assertTrue($user->hasTwoFactorEnabled());
|
|
|
|
$user->saveAppAuthenticationSecret(null);
|
|
$user->refresh();
|
|
$this->assertFalse($user->hasTwoFactorEnabled());
|
|
}
|
|
}
|