Files
Vasyka 67da97178d Batch 1: Procentaj + Finanțe consolidat + Recomandări
═══ Procentaj (markup rules) ═══
- markup_rules table cu type (category/brand/range), key, range_from/to, markup_pct, priority
- MarkupRule::bestForPart($part) — rezolvare brand → category → range → 30% default
- MarkupRule::applyToPart($part) — recalc sell_price = buy_price × (1 + pct/100)
- Filament resource sub Depozit cu form dinamic per tip
- Action 'Aplică toate regulile la stoc' — recalc tot catalogul (chunk 100)

═══ Finanțe consolidat ═══
- Custom Page /app/finance cu 4 tab-uri:
  • Overview: încasări/cheltuieli/profit/datorii (4 cards)
  • Cashflow: bar chart per zi (verde=in, roșu=out) + Net total
  • P&L: venituri (manopere + piese) vs costuri (cost piese + cheltuieli pe categorie)
    + profit net + marjă %
  • Balance: active (cash net + datorii + stoc), all-time totals
- Period filter: lună / luna trecută / an / 30 zile

═══ Recomandări ═══
- Custom Page /app/recommendations 4 sectiuni:
  • Clienți pierduți (>6 luni fără WO + are istoric)
  • Mașini km>100k (sugestie revizie)
  • Fișe neplătite (rest > 0)
  • VIP fără contact >30 zile

Total tenant routes: 100.
2026-05-07 15:30:04 +00:00

73 lines
2.1 KiB
PHP

<?php
namespace App\Models\Tenant;
use App\Models\Concerns\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
class MarkupRule extends Model
{
use BelongsToTenant;
public const TYPES = [
'category' => 'Categorie',
'brand' => 'Brand',
'range' => 'Interval preț',
];
protected $fillable = [
'company_id', 'type', 'key',
'range_from', 'range_to', 'markup_pct',
'priority', 'is_active',
];
protected $casts = [
'range_from' => 'decimal:2',
'range_to' => 'decimal:2',
'markup_pct' => 'decimal:2',
'is_active' => 'boolean',
];
/**
* Find best matching rule for a given Part. Priority order:
* 1) brand match 2) category match 3) range (buy_price intersect) 4) default 30%.
*/
public static function bestForPart(Part $part): ?self
{
// Brand exact
if ($part->brand) {
$r = static::where('type', 'brand')
->where('is_active', true)
->where('key', $part->brand)
->orderBy('priority')->first();
if ($r) return $r;
}
// Category exact
if ($part->category) {
$r = static::where('type', 'category')
->where('is_active', true)
->where('key', $part->category)
->orderBy('priority')->first();
if ($r) return $r;
}
// Range (buy_price ∈ [from, to))
$price = (float) $part->buy_price;
$r = static::where('type', 'range')
->where('is_active', true)
->where('range_from', '<=', $price)
->where(function ($q) use ($price) {
$q->whereNull('range_to')->orWhere('range_to', '>', $price);
})
->orderBy('priority')->orderByDesc('range_from')->first();
return $r;
}
public static function applyToPart(Part $part): bool
{
$rule = static::bestForPart($part);
$pct = $rule?->markup_pct ?? 30;
$part->sell_price = round((float) $part->buy_price * (1 + (float) $pct / 100), 2);
return $part->save();
}
}