Files
Vasyka cbcf08b28c 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>
2026-06-05 05:15:01 +00:00

180 lines
5.8 KiB
PHP

<?php
namespace App\Models\Tenant;
use App\Models\Concerns\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class WorkOrderWork extends Model
{
use BelongsToTenant;
protected $table = 'wo_works';
protected $attributes = [
'mechanic_status' => 'pending',
'paused_seconds_total' => 0,
];
public const STATUSES = [
'todo' => 'De făcut',
'in_progress' => 'În lucru',
'done' => 'Finalizat',
];
public const MECHANIC_STATUSES = [
'pending' => 'În așteptare',
'in_progress' => 'În lucru',
'paused' => 'Pe pauză',
'done' => 'Finalizat',
'blocked' => 'Blocat',
];
public const BLOCK_REASONS = [
'missing_part' => 'Lipsă piesă',
'awaiting_approval' => 'Aștept aprobare client',
'broken_equipment' => 'Echipament defect',
'other' => 'Altă problemă',
];
protected $fillable = [
'company_id', 'work_order_id', 'labor_id', 'master_id',
'name', 'hours', 'price_per_hour', 'total', 'status', 'notes',
'requires_approval', 'approved_at', 'approval_token', 'declined_at',
'mechanic_status', 'mechanic_started_at', 'mechanic_done_at',
'actual_hours', 'paused_seconds_total', 'paused_at',
'block_reason', 'block_note',
];
protected $casts = [
'hours' => 'decimal:2',
'price_per_hour' => 'decimal:2',
'total' => 'decimal:2',
'requires_approval' => 'boolean',
'approved_at' => 'datetime',
'declined_at' => 'datetime',
'mechanic_started_at' => 'datetime',
'mechanic_done_at' => 'datetime',
'paused_at' => 'datetime',
'actual_hours' => 'decimal:2',
'paused_seconds_total' => 'integer',
];
// ── State machine ────────────────────────────────────────────
public function start(): void
{
if ($this->mechanic_status === 'done') return;
$this->forceFill([
'mechanic_status' => 'in_progress',
'mechanic_started_at' => $this->mechanic_started_at ?? now(),
'paused_at' => null,
'block_reason' => null,
'block_note' => null,
'status' => 'in_progress',
])->save();
}
public function pause(): void
{
if ($this->mechanic_status !== 'in_progress') return;
$this->forceFill([
'mechanic_status' => 'paused',
'paused_at' => now(),
])->save();
}
public function resume(): void
{
if ($this->mechanic_status !== 'paused') return;
$added = $this->paused_at ? $this->paused_at->diffInSeconds(now()) : 0;
$this->forceFill([
'mechanic_status' => 'in_progress',
'paused_seconds_total' => (int) $this->paused_seconds_total + (int) $added,
'paused_at' => null,
])->save();
}
public function markDone(): void
{
// If currently paused, count up till now as paused time before stopping.
if ($this->mechanic_status === 'paused' && $this->paused_at) {
$this->paused_seconds_total = (int) $this->paused_seconds_total + (int) $this->paused_at->diffInSeconds(now());
$this->paused_at = null;
}
$started = $this->mechanic_started_at ?? now();
$endedAt = now();
$elapsedSec = max(0, $started->diffInSeconds($endedAt) - (int) $this->paused_seconds_total);
$actualHours = round($elapsedSec / 3600, 2);
$this->forceFill([
'mechanic_status' => 'done',
'mechanic_done_at' => $endedAt,
'actual_hours' => $actualHours,
'status' => 'done',
'block_reason' => null,
])->save();
}
public function block(string $reason, ?string $note = null): void
{
if (! array_key_exists($reason, self::BLOCK_REASONS)) return;
$this->forceFill([
'mechanic_status' => 'blocked',
'block_reason' => $reason,
'block_note' => $note,
'paused_at' => null,
])->save();
}
/** 'green' if faster than norm, 'amber' if 30%+ over, 'red' if 100%+ over. */
public function efficiencyClass(): ?string
{
if ((float) $this->actual_hours <= 0 || (float) $this->hours <= 0) return null;
$ratio = (float) $this->actual_hours / (float) $this->hours;
return match (true) {
$ratio <= 1.0 => 'green',
$ratio <= 1.3 => 'amber',
default => 'red',
};
}
public function efficiencyPct(): ?int
{
if ((float) $this->actual_hours <= 0 || (float) $this->hours <= 0) return null;
return (int) round(100 * (float) $this->actual_hours / (float) $this->hours);
}
public function isPendingApproval(): bool
{
return $this->requires_approval && $this->approved_at === null && $this->declined_at === null;
}
public function workOrder(): BelongsTo
{
return $this->belongsTo(WorkOrder::class);
}
public function labor(): BelongsTo
{
return $this->belongsTo(Labor::class);
}
public function master(): BelongsTo
{
return $this->belongsTo(User::class, 'master_id');
}
protected static function booted(): void
{
static::saving(function (self $row) {
$row->total = round((float) $row->hours * (float) $row->price_per_hour, 2);
if ($row->requires_approval && empty($row->approval_token)) {
$row->approval_token = \Illuminate\Support\Str::random(24);
}
});
static::saved(fn (self $row) => $row->workOrder?->recalcTotal());
static::deleted(fn (self $row) => $row->workOrder?->recalcTotal());
}
}