cbcf08b28c
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>
94 lines
3.6 KiB
PHP
94 lines
3.6 KiB
PHP
<?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);
|
|
}
|
|
}
|