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
+2 -1
View File
@@ -18,7 +18,7 @@ class Client extends Model
'phone', 'phone_alt', 'email',
'telegram', 'telegram_chat_id', 'whatsapp', 'viber',
'notify_prefs',
'source', 'marketing_channel', 'status',
'source', 'marketing_channel', 'status', 'is_vip',
'balance', 'discount_pct', 'notes',
'assigned_to', 'last_contact_at',
];
@@ -28,6 +28,7 @@ class Client extends Model
'discount_pct' => 'decimal:2',
'last_contact_at' => 'datetime',
'notify_prefs' => 'array',
'is_vip' => 'boolean',
];
/** Normalize a phone number to E.164-ish digits for matching. */
+79
View File
@@ -0,0 +1,79 @@
<?php
namespace App\Models\Tenant;
use App\Models\Concerns\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
class PricingCoefficient extends Model
{
use BelongsToTenant;
public const VEHICLE_CLASSES = [
'sedan' => 'Sedan / Hatchback',
'suv' => 'SUV / Crossover',
'commercial' => 'Comercial (van/camion)',
'hybrid' => 'Hibrid',
'ev' => 'Electric (EV)',
'premium' => 'Premium / Lux',
];
public const URGENCY = [
'normal' => 'Normal',
'urgent' => 'Urgent',
'express' => 'Express',
];
protected $fillable = [
'company_id', 'name', 'multiplier', 'conditions',
'priority', 'stackable', 'is_active',
];
protected $casts = [
'multiplier' => 'decimal:3',
'conditions' => 'array',
'stackable' => 'boolean',
'is_active' => 'boolean',
];
/**
* Does this coefficient apply to the given pricing context?
*
* @param array{class?:?string, age?:?int, vip?:bool, urgency?:string} $ctx
*/
public function matches(array $ctx): bool
{
$c = (array) $this->conditions;
// Vehicle class — if rule lists classes, context class must be among them.
$classes = (array) ($c['classes'] ?? []);
if (! empty($classes)) {
if (empty($ctx['class']) || ! in_array($ctx['class'], $classes, true)) {
return false;
}
}
// Vehicle age range.
if (isset($c['age_min']) && $c['age_min'] !== null && $c['age_min'] !== '') {
if (($ctx['age'] ?? null) === null || $ctx['age'] < (int) $c['age_min']) return false;
}
if (isset($c['age_max']) && $c['age_max'] !== null && $c['age_max'] !== '') {
if (($ctx['age'] ?? null) === null || $ctx['age'] > (int) $c['age_max']) return false;
}
// VIP requirement (true = only VIP, false/null = ignore).
if (! empty($c['client_vip'])) {
if (empty($ctx['vip'])) return false;
}
// Urgency — if rule lists urgencies, context must match.
$urg = (array) ($c['urgency'] ?? []);
if (! empty($urg)) {
if (empty($ctx['urgency']) || ! in_array($ctx['urgency'], $urg, true)) {
return false;
}
}
return true;
}
}
+1 -1
View File
@@ -15,7 +15,7 @@ class Vehicle extends Model
protected $fillable = [
'company_id', 'client_id',
'make', 'model', 'year', 'vin', 'plate',
'engine', 'gearbox', 'fuel', 'mileage', 'color', 'notes',
'engine', 'gearbox', 'fuel', 'vehicle_class', 'mileage', 'color', 'notes',
];
public function client(): BelongsTo
+1 -1
View File
@@ -38,7 +38,7 @@ class WorkOrder extends Model implements HasMedia
'client_id', 'vehicle_id', 'master_id', 'deal_id', 'appointment_id',
'opened_at', 'closed_at', 'mileage_in', 'mileage_out',
'complaint', 'diagnosis', 'recommendations',
'status', 'pay_status', 'approved', 'approved_at',
'status', 'urgency', 'pay_status', 'approved', 'approved_at',
'discount_pct', 'total',
'eta_at', 'tracking_token',
];