Stage 8 — Smart Pricing Engine: contextual coefficients

Contextual multipliers layered on top of base MarkupRule pricing, applied
per work-order line based on vehicle, client and urgency.

Schema:
- pricing_coefficients (multiplier, conditions JSON, priority, stackable)
- vehicles.vehicle_class (sedan/suv/commercial/hybrid/ev/premium)
- clients.is_vip
- work_orders.urgency (normal/urgent/express)

PricingEngine::quote(Part, Vehicle?, Client?, urgency):
- base = MarkupRule on buy_price (fallback sell_price or buy×1.30)
- context: class (explicit or inferred hybrid/ev from fuel), age, vip, urgency
- stackable coefficients all multiply; non-stackable take only the highest
- returns {base, final, applied[]} breakdown

PricingCoefficient::matches(ctx) — classes/age range/vip/urgency conditions
(empty = always applies).

Filament:
- PricingCoefficientResource with condition builder (classes, age, vip, urgency)
- vehicle_class select, client is_vip toggle, WO urgency select
- "Preț inteligent" action on WO parts shows breakdown + applies sell_price

Tests (6 new):
- base-only without coefficients; age coefficient gating; VIP; express urgency;
  stackable multiply vs non-stackable highest-wins; hybrid inferred from fuel

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-28 05:40:27 +00:00
parent 954ba8f059
commit c90c35d930
16 changed files with 580 additions and 3 deletions
@@ -85,6 +85,32 @@ class PartsRelationManager extends RelationManager
Actions\CreateAction::make(),
])
->actions([
Actions\Action::make('smart_price')
->label('Preț inteligent')
->icon('heroicon-m-sparkles')
->color('primary')
->visible(fn (WorkOrderPart $r) => (bool) $r->part_id)
->modalHeading('Preț contextual')
->modalSubmitActionLabel('Aplică prețul')
->modalContent(function (WorkOrderPart $r) {
$wo = $r->workOrder;
$part = $r->part;
$quote = app(\App\Services\Pricing\PricingEngine::class)->quote(
$part, $wo?->vehicle, $wo?->client, $wo?->urgency ?? 'normal'
);
return view('filament.tenant.smart-price', ['quote' => $quote, 'item' => $r]);
})
->action(function (WorkOrderPart $r) {
$wo = $r->workOrder;
$quote = app(\App\Services\Pricing\PricingEngine::class)->quote(
$r->part, $wo?->vehicle, $wo?->client, $wo?->urgency ?? 'normal'
);
$r->sell_price = $quote['final'];
$r->save();
Notification::make()
->title('Preț actualizat: ' . number_format($quote['final'], 2) . ' MDL')
->success()->send();
}),
Actions\Action::make('issue_now')
->label('Eliberează')
->icon('heroicon-m-arrow-up-on-square')