426156fe45
Schema: - warehouses (multi-warehouse, code unique per company, is_default) - part_batches (lot per receipt, qty_in/qty_remaining, buy_price, FIFO-indexed) - warehouse_events (immutable ledger: opening/receipt/issue/transfer/adjustment/write_off) - part_reservations (per-WO allocations from specific batches, active/consumed/released) - companies.default_warehouse_id + parts.qty_reserved Backfill: 1 default warehouse + 1 opening batch per existing part per company. WarehouseService: - receive / issue (FIFO) / reserve / release / consume / transfer / adjust - DB::transaction + lockForUpdate on batch rows - InsufficientStockException with requested + available context - Auto-syncs parts.qty as aggregate cache (source of truth = sum(qty_remaining)) WO integration: - WorkOrderPart created/updated → reserve from FIFO batches - WorkOrderPart deleted → release - WorkOrder status=done → consume reservations into issue events - WorkOrder status=cancelled → release reservations Filament: - WarehouseResource (CRUD) - BatchesRelationManager on PartResource (FIFO list with qty_remaining + cost) - "Recepție" action on parts list → calls WarehouseService::receive - qty_reserved column added on parts list Tests (8 new, all pass): - receipt creates batch + event - FIFO order verified across 3 batches with different received_at - InsufficientStockException on over-issue - Reservations block other reservations but don't deplete on-hand - WO done consumes; WO cancelled releases - Batches tenant-isolated - Transfer between warehouses with weighted-avg cost Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
92 lines
2.6 KiB
PHP
92 lines
2.6 KiB
PHP
<?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;
|
||
|
||
class Part extends Model
|
||
{
|
||
use BelongsToTenant, SoftDeletes;
|
||
|
||
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',
|
||
'location', 'barcode', 'preferred_supplier_id',
|
||
'is_active', 'notes',
|
||
];
|
||
|
||
protected $casts = [
|
||
'qty' => 'decimal:2',
|
||
'qty_reserved' => 'decimal:3',
|
||
'min_qty' => 'decimal:2',
|
||
'buy_price' => 'decimal:2',
|
||
'sell_price' => 'decimal:2',
|
||
'is_active' => 'boolean',
|
||
];
|
||
|
||
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);
|
||
}
|
||
|
||
/** 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();
|
||
}
|
||
}
|