Files
autocrm/app/Models/Tenant/Part.php
T
Vasyka 1d5ea6d261 feat: Calendar Vizual v2 (Pod×Days matrix) + hidden markup
Implements 2 of the biggest items from /tmp/service/new docs:

== Calendar Vizual v2 (from 02-prototip-calendar-vizual.html) ==

Replaces the FullCalendar week view (the one that visually collapsed after
Livewire re-renders) with a server-rendered matrix that the harness
already drives through Livewire — no third-party JS to clash with Filament.

Layout: 8-column CSS grid (1 row-label + 7 days). Rows are either Posts
(Pod 1, Pod 2…) or active masters depending on toolbar switch. Each
cell holds 0..N event cards.

Per-cell load badge (top-right):
  hours_planned / capacity  →  badge color (gray <50%, orange 50–90%, red ≥90%)

Drag-drop: HTML5 native, Alpine.js holds the dragEventId, moveEvent($id,
$toRowId, $toDate) in PHP updates either post_id or master_id (depending
on groupBy mode) plus date — works seamlessly when re-grouping.

KPI bar (4 cards above toolbar):
- Ore programate X / Y · % capacity
- Fișe deschise (orange)
- Confirmate X/Y (green) + confirmation rate
- No-show alert (red) — scheduled events <24h away that are still unconfirmed

Toolbar:
- ◀ Week ▶ + Astăzi (reset)
- Date label "01 — 07 iunie 2026"
- Grupare switch: Pod ↔ Mecanic
- Filtru: master dropdown + status dropdown (Confirmate/Neconfirmate/În lucru)

Today column highlighted blue; Sunday column hatched as closed
(non-interactive, no drop target); Saturday muted as weekend.

Event card color = master.color (deterministic, matches profile setting),
shown as left border + background tint. Title = client name; meta =
"VW Passat · CIU 001"; time = "08:00–12:00 · V.".

Click empty cell → quick-create panel (right slide-in) with date+pod
pre-filled. Click event → detail panel with Client/Phone/Auto/Plate/
Master/Pod + delete + edit.

Legend section at bottom (mecanici dots, load colors, day states).

== Hidden Markup (from gap-analysis.md #3) ==

Adds `hidden_markup_pct` decimal to parts. Customer documents continue
to show the standard sell_price; the hidden markup is an internal margin
indicator used for B2B contracts and corporate analytics.

Part::internalCostWithHiddenMarkup() returns buy_price * (1 + pct/100).
Falls back to buy_price when pct is null. Decimal:2 cast so persistence
round-trips cleanly.

== Schema migration ==

Idempotent (hasColumn guards):
- posts.hours_per_day decimal(5,1) default 10
- posts.description varchar(255) nullable
- parts.hidden_markup_pct decimal(5,2) nullable

== Tests ==

+11 new in CalendarBoardV2Test (8) + HiddenMarkupTest (3):
- get_days returns 7 days with today flagged + Sunday closed + Saturday weekend
- get_rows returns posts when grouped by post + with capacity
- get_rows returns masters when grouped by master + Fără maistru fallback row
- matrix places events in correct cells + sums hours
- move_event reassigns post_id and date
- create_appt inserts appointment via panel form
- stats compute utilization from events (8h / 60h capacity = 13%)
- status filter narrows to confirmed only
- hidden_markup applies pct correctly + falls back to buy_price + persists

Suite: 196 passed (551 assertions). Was 185.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-04 21:50:22 +00:00

161 lines
4.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Models\Tenant;
use App\Models\Concerns\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class Part extends Model implements HasMedia
{
use BelongsToTenant, InteractsWithMedia, SoftDeletes;
public function registerMediaCollections(): void
{
// Multi-image gallery (catalog uses imageUrl() = first; detail page renders all).
$this->addMediaCollection('image');
}
public function imageUrl(): ?string
{
$m = $this->getFirstMedia('image');
if (! $m) return null;
if (! @file_exists($m->getPath())) return null;
return $m->getUrl();
}
/** @return list<string> All published image URLs (excluding any whose file is missing). */
public function imageUrls(): array
{
return $this->getMedia('image')
->filter(fn ($m) => @file_exists($m->getPath()))
->map(fn ($m) => $m->getUrl())
->values()->all();
}
public const CATEGORIES = [
'Ulei', 'Filtre', 'Frâne', 'Suspensie', 'Lichide',
'Distribuție', 'Anvelope', 'Electrică', 'Caroserie', 'Altele',
];
protected $fillable = [
'company_id', 'name', 'article', 'brand', 'category',
'qty', 'qty_reserved', 'unit', 'min_qty',
'buy_price', 'sell_price', 'hidden_markup_pct',
'location', 'barcode', 'preferred_supplier_id',
'is_active', 'is_published', 'notes',
];
protected $casts = [
'qty' => 'decimal:2',
'qty_reserved' => 'decimal:3',
'min_qty' => 'decimal:2',
'buy_price' => 'decimal:2',
'sell_price' => 'decimal:2',
'hidden_markup_pct' => 'decimal:2',
'is_active' => 'boolean',
'is_published' => 'boolean',
];
/** Internal cost+hidden markup (NOT shown to customer). Used for margin analytics + B2B contract pricing. */
public function internalCostWithHiddenMarkup(): float
{
$base = (float) $this->buy_price;
$pct = (float) ($this->hidden_markup_pct ?: 0);
return round($base * (1 + $pct / 100), 2);
}
public function preferredSupplier(): BelongsTo
{
return $this->belongsTo(Supplier::class, 'preferred_supplier_id');
}
public function batches(): HasMany
{
return $this->hasMany(PartBatch::class);
}
public function reservations(): HasMany
{
return $this->hasMany(PartReservation::class);
}
public function events(): HasMany
{
return $this->hasMany(WarehouseEvent::class);
}
public function priceHistory(): HasMany
{
return $this->hasMany(SupplierPartPrice::class);
}
public function crossRefs(): HasMany
{
return $this->hasMany(PartCrossRef::class);
}
public function scopePublished($q)
{
return $q->where('is_active', true)->where('is_published', true);
}
/**
* Search published parts by free text against name / article / brand and
* any registered cross-reference article. Returns a query builder.
*/
public static function searchPublished(?string $term)
{
$q = static::published();
if ($term = trim((string) $term)) {
$like = '%' . $term . '%';
$q->where(function ($w) use ($like, $term) {
$w->where('name', 'like', $like)
->orWhere('article', 'like', $like)
->orWhere('brand', 'like', $like)
->orWhereHas('crossRefs', fn ($c) => $c->where('cross_article', 'like', $like));
});
}
return $q;
}
/** Live total across all batches of all warehouses (source of truth). */
public function qtyOnHand(?int $warehouseId = null): float
{
$q = $this->batches()->newQuery()->where('part_id', $this->id);
if ($warehouseId) $q->where('warehouse_id', $warehouseId);
return (float) $q->sum('qty_remaining');
}
/** Available for new reservations = on hand already reserved. */
public function qtyAvailable(?int $warehouseId = null): float
{
return max(0.0, $this->qtyOnHand($warehouseId) - (float) $this->qty_reserved);
}
public function isLow(): bool
{
return (float) $this->qty <= (float) $this->min_qty;
}
public function isOut(): bool
{
return (float) $this->qty <= 0;
}
/**
* Legacy direct-stock adjustment.
* NOTE: this only moves the cached `qty` column. Real stock changes
* should go through WarehouseService so batches + events stay in sync.
*/
public function adjustStock(float $delta, ?string $reason = null): void
{
$this->qty = max(0, (float) $this->qty + $delta);
$this->save();
}
}