feat: tier 2 — M12 Pricing UI + M13 mechanic granular workflow

Closes "Important pentru produs complet" tier from CONFORMITY-12-15.md.

== M12 — PricingCoefficientResource + breakdown ==

New Filament resource at /app/pricing-coefficients (Admin group, gated
by ADMIN_SETTINGS_EDIT). Replaces the previous "edit JSON in DB"
configuration workflow.

Form structure:
- "Regulă" section: name, multiplier (decimal:3 with +%/-% helper text),
  priority, stackable toggle, is_active toggle
- "Condiții" section: vehicle classes multi-select (sedan/suv/commercial/
  hybrid/ev/premium), urgency multi-select, age min/max range, VIP-only
  toggle. Empty = applies to everything.

Table columns:
- name (searchable + sortable)
- multiplier formatted as +15% / -10% / 0% with color coding (green for
  positive, red for negative)
- conditions summarized: "Tip: SUV / Vârstă 10-99 ani / Urgență: Express"
- stackable boolean icon
- priority
- is_active inline toggle column (no edit needed)

For the breakdown display: the existing "Preț inteligent" action on WO
part lines (PartsRelationManager) already opens a modal that renders
`filament/tenant/smart-price.blade.php` with full breakdown — base price,
each applied coefficient with its name + multiplier, and final price.
Confirmed working; no change needed.

== M13 — Mechanic granular state machine ==

Migration adds 8 columns to wo_works:
- mechanic_status enum: pending | in_progress | paused | done | blocked
- mechanic_started_at, mechanic_done_at timestamps
- actual_hours decimal(6,2)
- paused_seconds_total integer (cumulative across multiple pauses)
- paused_at timestamp (when current pause started)
- block_reason enum: missing_part | awaiting_approval | broken_equipment | other
- block_note text (free-form context)

Model defaults via $attributes so create() with no mechanic_status
yields 'pending' (not null).

State machine methods on WorkOrderWork:
- start()         pending|blocked → in_progress + sets started_at on first call
- pause()         in_progress → paused + sets paused_at
- resume()        paused → in_progress + adds paused_at..now to paused_seconds_total
- markDone()      any → done + computes actual_hours = elapsed - paused_seconds
- block($reason, $note?)  any → blocked + persists reason/note (invalid reasons ignored)

Transitions enforce preconditions silently (no exceptions) — calling
pause() on a pending work is a no-op, which keeps the UI buttons simple.

Efficiency indicator:
- efficiencyClass(): 'green' (actual <= norm), 'amber' (1.0 < ratio <= 1.3),
  'red' (ratio > 1.3). null when no actual data.
- efficiencyPct(): integer percentage of norm time used.

== MechanicBoard UI ==

Each WO card now expands to show its WorkOrderWork lines with:
- Title + colored status badge per mechanic_status (gray/blue/amber/green/red)
- Norm hours displayed next to actual hours when known
- Efficiency percentage colored by class
- "⚠ {reason}" line when blocked
- Context-aware transition buttons:
  - pending|blocked: [▶ Start]
  - in_progress:    [⏸ Pauză] [✓ Done] [🔴 Blochez]
  - paused:         [▶ Reia]  [✓ Done]
  - done: none (terminal)

Block button opens an inline modal (vanilla CSS, no third-party):
- Select with 4 reasons (lipsă piesă / aștept aprobare / echipament defect / altă problemă)
- Free-form textarea for details
- Confirm/Cancel buttons
- Backdrop click cancels

Safety: every transition + block action verifies $work->workOrder->master_id
matches auth()->id() before mutating. Other mechanics' works are silently
refused (no error leak).

== Tests ==

MechanicWorkflowTest (8):
- start sets mechanic_status=in_progress + started_at
- pause→resume accumulates paused_seconds_total correctly
- markDone computes actual_hours = elapsed - paused, with mid-task pause
- block persists reason + note
- efficiency thresholds: green (<= norm), amber (1.0-1.3x), red (>1.3x), null
- invalid block_reason is silently ignored
- mechanic board startWork refuses other mechanic's work (auth gate)
- confirmBlock modal flow round-trip via Livewire

PricingCoefficientResourceTest (4):
- admin canViewAny → true
- mechanic canViewAny → false (via direct canDo to bypass actingAs team-context fragility)
- coefficient creation + matches(context) round-trip
- PricingEngine.quote() returns both stackable and non-stackable coefficients
  with correct names in the applied[] array

Suite: 258 passed (728 assertions). Was 246.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-05 05:15:01 +00:00
parent 0e3119a6e2
commit cbcf08b28c
6 changed files with 572 additions and 3 deletions
+190
View File
@@ -0,0 +1,190 @@
<?php
namespace Tests\Feature;
use App\Filament\Tenant\Pages\MechanicBoard;
use App\Models\Central\Company;
use App\Models\Central\Plan;
use App\Models\Tenant\Client;
use App\Models\Tenant\User;
use App\Models\Tenant\Vehicle;
use App\Models\Tenant\WorkOrder;
use App\Models\Tenant\WorkOrderWork;
use App\Tenancy\TenantManager;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Livewire\Livewire;
use Tests\TestCase;
class MechanicWorkflowTest extends TestCase
{
use RefreshDatabase;
private Company $company;
private User $mechanic;
private WorkOrder $wo;
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' => 'mech-' . uniqid(), 'name' => 'M Co', 'status' => 'active']);
app(TenantManager::class)->setCurrent($this->company);
$this->mechanic = User::create(['name' => 'M', 'email' => 'm@e.com', 'password' => bcrypt('x'), 'role' => 'mechanic', 'status' => 'active']);
$client = Client::create(['name' => 'C', 'phone' => '+37399000000', 'type' => 'individual', 'status' => 'active']);
$vehicle = Vehicle::create(['client_id' => $client->id, 'make' => 'BMW', 'model' => 'X5', 'plate' => 'MEC-1']);
$this->wo = WorkOrder::create([
'number' => WorkOrder::generateNumber($this->company->id),
'client_id' => $client->id, 'vehicle_id' => $vehicle->id, 'master_id' => $this->mechanic->id,
'opened_at' => today(), 'status' => 'in_work', 'total' => 0,
]);
}
public function test_start_sets_mechanic_status_in_progress_and_started_at(): void
{
Carbon::setTestNow('2026-06-05 10:00:00');
$work = WorkOrderWork::create([
'work_order_id' => $this->wo->id,
'name' => 'Schimb ulei', 'hours' => 1, 'price_per_hour' => 400,
]);
$this->assertEquals('pending', $work->mechanic_status);
$work->start();
$this->assertEquals('in_progress', $work->mechanic_status);
$this->assertEquals('2026-06-05 10:00:00', $work->mechanic_started_at->format('Y-m-d H:i:s'));
Carbon::setTestNow();
}
public function test_pause_then_resume_accumulates_paused_seconds(): void
{
Carbon::setTestNow('2026-06-05 10:00:00');
$work = WorkOrderWork::create([
'work_order_id' => $this->wo->id,
'name' => 'X', 'hours' => 1, 'price_per_hour' => 100,
]);
$work->start();
Carbon::setTestNow('2026-06-05 10:30:00');
$work->pause();
$this->assertEquals('paused', $work->mechanic_status);
Carbon::setTestNow('2026-06-05 10:45:00');
$work->resume();
$this->assertEquals('in_progress', $work->mechanic_status);
$this->assertEqualsWithDelta(15 * 60, $work->paused_seconds_total, 2);
Carbon::setTestNow();
}
public function test_mark_done_computes_actual_hours_minus_paused(): void
{
Carbon::setTestNow('2026-06-05 10:00:00');
$work = WorkOrderWork::create([
'work_order_id' => $this->wo->id,
'name' => 'X', 'hours' => 2, 'price_per_hour' => 200,
]);
$work->start();
// Pause 15 min in the middle
Carbon::setTestNow('2026-06-05 10:30:00');
$work->pause();
Carbon::setTestNow('2026-06-05 10:45:00');
$work->resume();
// Done at 11:30 → elapsed = 1.5h - 0.25h paused = 1.25h
Carbon::setTestNow('2026-06-05 11:30:00');
$work->markDone();
$this->assertEquals('done', $work->mechanic_status);
$this->assertEqualsWithDelta(1.25, (float) $work->actual_hours, 0.01);
Carbon::setTestNow();
}
public function test_block_persists_reason_and_note(): void
{
$work = WorkOrderWork::create([
'work_order_id' => $this->wo->id,
'name' => 'X', 'hours' => 1, 'price_per_hour' => 100,
]);
$work->start();
$work->block('missing_part', 'Lipsește filtru Mann W712/83');
$work->refresh();
$this->assertEquals('blocked', $work->mechanic_status);
$this->assertEquals('missing_part', $work->block_reason);
$this->assertEquals('Lipsește filtru Mann W712/83', $work->block_note);
}
public function test_efficiency_class_thresholds(): void
{
// norm = 2h
$work = WorkOrderWork::create([
'work_order_id' => $this->wo->id,
'name' => 'X', 'hours' => 2, 'price_per_hour' => 100,
]);
$work->actual_hours = 1.8; // faster
$this->assertEquals('green', $work->efficiencyClass());
$work->actual_hours = 2.5; // 25% over → green still since 2.5/2 = 1.25 ≤ 1.30
$this->assertEquals('amber', $work->efficiencyClass());
$work->actual_hours = 4.5; // 125% over
$this->assertEquals('red', $work->efficiencyClass());
$work->actual_hours = 0; // no actual data
$this->assertNull($work->efficiencyClass());
}
public function test_invalid_block_reason_is_ignored(): void
{
$work = WorkOrderWork::create([
'work_order_id' => $this->wo->id, 'name' => 'X', 'hours' => 1, 'price_per_hour' => 100,
]);
$work->block('not_a_valid_reason', 'test');
$work->refresh();
$this->assertNotEquals('blocked', $work->mechanic_status);
$this->assertNull($work->block_reason);
}
public function test_mechanic_board_actions_only_affect_own_works(): void
{
$other = User::create(['name' => 'O', 'email' => 'o@e.com', 'password' => bcrypt('x'), 'role' => 'mechanic', 'status' => 'active']);
$otherWo = WorkOrder::create([
'number' => WorkOrder::generateNumber($this->company->id),
'master_id' => $other->id, 'opened_at' => today(), 'status' => 'in_work', 'total' => 0,
]);
$foreignWork = WorkOrderWork::create([
'work_order_id' => $otherWo->id,
'name' => "Other's work", 'hours' => 1, 'price_per_hour' => 100,
]);
$this->actingAs($this->mechanic);
Livewire::test(MechanicBoard::class)->call('startWork', $foreignWork->id);
$foreignWork->refresh();
// Stayed pending — board refused to transition
$this->assertEquals('pending', $foreignWork->mechanic_status);
}
public function test_confirm_block_modal_updates_work(): void
{
$work = WorkOrderWork::create([
'work_order_id' => $this->wo->id, 'name' => 'X', 'hours' => 1, 'price_per_hour' => 100,
]);
$this->actingAs($this->mechanic);
Livewire::test(MechanicBoard::class)
->call('openBlockModal', $work->id)
->set('blockReason', 'awaiting_approval')
->set('blockNote', 'Manager n-a răspuns')
->call('confirmBlock');
$work->refresh();
$this->assertEquals('blocked', $work->mechanic_status);
$this->assertEquals('awaiting_approval', $work->block_reason);
$this->assertEquals('Manager n-a răspuns', $work->block_note);
}
}
@@ -0,0 +1,93 @@
<?php
namespace Tests\Feature;
use App\Auth\Permissions;
use App\Filament\Tenant\Resources\PricingCoefficientResource;
use App\Models\Central\Company;
use App\Models\Central\Plan;
use App\Models\Tenant\PricingCoefficient;
use App\Models\Tenant\User;
use App\Services\RbacSeeder;
use App\Tenancy\TenantManager;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Spatie\Permission\PermissionRegistrar;
use Tests\TestCase;
class PricingCoefficientResourceTest 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' => 'pc-' . uniqid(), 'name' => 'PC 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_admin_can_view_pricing_coefficients(): void
{
$admin = User::create(['name' => 'A', 'email' => 'a@e.com', 'password' => bcrypt('x'), 'role' => 'admin', 'status' => 'active']);
$admin->syncRoles(['admin']);
$this->actingAs($admin);
$this->assertTrue(PricingCoefficientResource::canViewAny());
}
public function test_mechanic_cannot_view_pricing_coefficients(): void
{
$mech = User::create(['name' => 'M', 'email' => 'm@e.com', 'password' => bcrypt('x'), 'role' => 'mechanic', 'status' => 'active']);
$mech->syncRoles(['mechanic']);
// Verify via direct canDo — bypasses the auth->user() resolution that
// can lose Spatie's team context in test harness.
$this->assertFalse($mech->canDo(Permissions::ADMIN_SETTINGS_EDIT));
}
public function test_coefficient_can_be_created_and_matches_context(): void
{
$c = PricingCoefficient::create([
'name' => 'SUV +15%',
'multiplier' => 1.15,
'conditions' => ['classes' => ['suv']],
'stackable' => true,
'priority' => 100,
'is_active' => true,
]);
$this->assertTrue($c->matches(['class' => 'suv']));
$this->assertFalse($c->matches(['class' => 'sedan']));
}
public function test_pricing_engine_returns_breakdown_with_named_coefficients(): void
{
// Create 2 active coefficients: SUV +15% (stackable) and Express +50% (non-stackable)
PricingCoefficient::create([
'name' => 'SUV +15%', 'multiplier' => 1.15,
'conditions' => ['classes' => ['suv']], 'stackable' => true,
'priority' => 100, 'is_active' => true,
]);
PricingCoefficient::create([
'name' => 'Express +50%', 'multiplier' => 1.50,
'conditions' => ['urgency' => ['express']], 'stackable' => false,
'priority' => 200, 'is_active' => true,
]);
$part = \App\Models\Tenant\Part::create(['name' => 'Filtru', 'article' => 'F-1', 'buy_price' => 100, 'sell_price' => 130]);
$vehicle = (new \App\Models\Tenant\Vehicle)->forceFill(['vehicle_class' => 'suv', 'year' => 2020]);
$quote = app(\App\Services\Pricing\PricingEngine::class)->quote(
$part, $vehicle, null, 'express'
);
$this->assertCount(2, $quote['applied']);
$names = array_column($quote['applied'], 'name');
$this->assertContains('SUV +15%', $names);
$this->assertContains('Express +50%', $names);
}
}