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
@@ -64,20 +64,88 @@
<span>{{ $col['items']->count() }}</span>
</div>
@forelse ($col['items'] as $wo)
<a class="mb-card" href="{{ route('filament.tenant.resources.work-orders.edit', $wo) }}">
<div class="mb-card" style="cursor:default;">
<div style="display:flex;justify-content:space-between;align-items:center;">
<span class="num">#{{ $wo->number }}</span>
<a href="{{ route('filament.tenant.resources.work-orders.edit', $wo) }}" class="num" style="text-decoration:none;color:#3b82f6;">#{{ $wo->number }}</a>
<span class="plate">{{ $wo->vehicle?->plate ?? '—' }}</span>
</div>
<div class="client">{{ $wo->client?->name ?? 'fără client' }} · {{ $wo->vehicle?->make }} {{ $wo->vehicle?->model }}</div>
@if ($wo->complaint)
<div class="complaint">{{ $wo->complaint }}</div>
@endif
</a>
@php $works = $this->getWorksFor($wo->id) @endphp
@if (! empty($works))
<div style="margin-top:8px; border-top:1px solid #f3f4f6; padding-top:8px;">
@foreach ($works as $w)
<div style="font-size:12px; margin-bottom:6px; padding:6px 8px; background:#f9fafb; border-radius:6px;">
@php
$ms = $w->mechanic_status ?: 'pending';
$stColor = ['pending' => '#94a3b8', 'in_progress' => '#3b82f6', 'paused' => '#f59e0b', 'done' => '#10b981', 'blocked' => '#dc2626'][$ms];
$eff = $w->efficiencyClass();
$effColor = ['green' => '#10b981', 'amber' => '#f59e0b', 'red' => '#dc2626'][$eff ?? ''] ?? null;
@endphp
<div style="display:flex;justify-content:space-between;align-items:flex-start;gap:6px;">
<div style="flex:1;">
<span style="font-weight:600;">{{ $w->name }}</span>
<div style="margin-top:3px; display:flex; gap:4px; align-items:center; flex-wrap:wrap;">
<span style="background:{{ $stColor }}; color:white; padding:1px 6px; border-radius:4px; font-size:10px; font-weight:600;">{{ \App\Models\Tenant\WorkOrderWork::MECHANIC_STATUSES[$ms] }}</span>
@if ($w->hours > 0)
<span style="color:#6b7280; font-size:10px;">{{ rtrim(rtrim(number_format($w->hours, 2), '0'), '.') }}h norm</span>
@endif
@if ($w->actual_hours > 0 && $effColor)
<span style="color:{{ $effColor }}; font-weight:600; font-size:10px;">{{ rtrim(rtrim(number_format($w->actual_hours, 2), '0'), '.') }}h real ({{ $w->efficiencyPct() }}%)</span>
@endif
@if ($w->block_reason)
<span style="color:#dc2626; font-size:10px;"> {{ \App\Models\Tenant\WorkOrderWork::BLOCK_REASONS[$w->block_reason] ?? $w->block_reason }}</span>
@endif
</div>
</div>
</div>
<div style="display:flex; gap:4px; margin-top:6px; flex-wrap:wrap;">
@if (in_array($ms, ['pending', 'blocked']))
<button wire:click="startWork({{ $w->id }})" style="background:#3b82f6;color:white;border:none;padding:4px 8px;border-radius:4px;font-size:11px;cursor:pointer;"> Start</button>
@endif
@if ($ms === 'in_progress')
<button wire:click="pauseWork({{ $w->id }})" style="background:#f59e0b;color:white;border:none;padding:4px 8px;border-radius:4px;font-size:11px;cursor:pointer;"> Pauză</button>
<button wire:click="doneWork({{ $w->id }})" style="background:#10b981;color:white;border:none;padding:4px 8px;border-radius:4px;font-size:11px;cursor:pointer;"> Done</button>
<button wire:click="openBlockModal({{ $w->id }})" style="background:#fff;color:#dc2626;border:1px solid #dc2626;padding:4px 8px;border-radius:4px;font-size:11px;cursor:pointer;">🔴 Blochez</button>
@endif
@if ($ms === 'paused')
<button wire:click="resumeWork({{ $w->id }})" style="background:#3b82f6;color:white;border:none;padding:4px 8px;border-radius:4px;font-size:11px;cursor:pointer;"> Reia</button>
<button wire:click="doneWork({{ $w->id }})" style="background:#10b981;color:white;border:none;padding:4px 8px;border-radius:4px;font-size:11px;cursor:pointer;"> Done</button>
@endif
</div>
</div>
@endforeach
</div>
@endif
</div>
@empty
<div class="mb-empty"></div>
@endforelse
</div>
@endforeach
</div>
{{-- BLOCK REASON MODAL --}}
@if ($blockingWorkId)
<div style="position:fixed; inset:0; background:rgba(0,0,0,0.5); z-index:9999; display:flex; align-items:center; justify-content:center;" wire:click="$set('blockingWorkId', null)">
<div style="background:white; border-radius:12px; padding:20px; max-width:400px; width:90%;" wire:click.stop>
<h2 style="font-size:16px; font-weight:600; margin-bottom:12px; color:#dc2626;">🔴 Blochez lucrarea</h2>
<p style="font-size:13px; color:#4b5563; margin-bottom:14px;">Selectează motivul pentru care lucrarea nu poate continua. Va fi vizibil managerului.</p>
<label style="font-size:11px; color:#6b7280; text-transform:uppercase; font-weight:600; display:block; margin-bottom:4px;">Motiv *</label>
<select wire:model="blockReason" style="width:100%; padding:8px 10px; border:1px solid #cbd5e1; border-radius:6px; font-size:14px; margin-bottom:12px;">
@foreach (\App\Models\Tenant\WorkOrderWork::BLOCK_REASONS as $k => $v)
<option value="{{ $k }}">{{ $v }}</option>
@endforeach
</select>
<label style="font-size:11px; color:#6b7280; text-transform:uppercase; font-weight:600; display:block; margin-bottom:4px;">Detalii (opțional)</label>
<textarea wire:model="blockNote" rows="2" style="width:100%; padding:8px 10px; border:1px solid #cbd5e1; border-radius:6px; font-size:14px; margin-bottom:14px;" placeholder="Ex: Lipsește filtrul Mann W712/83"></textarea>
<div style="display:flex; gap:8px;">
<button wire:click="$set('blockingWorkId', null)" style="flex:1; padding:8px; background:white; color:#4b5563; border:1px solid #cbd5e1; border-radius:6px; cursor:pointer;">Anulează</button>
<button wire:click="confirmBlock" style="flex:1; padding:8px; background:#dc2626; color:white; border:none; border-radius:6px; cursor:pointer; font-weight:600;">Blochez</button>
</div>
</div>
</div>
@endif
</x-filament-panels::page>