Files
autocrm/app/Models/Tenant/Part.php
T
Vasyka a2026f640a Stage 6 — Purchase System: partial receipt + supplier analytics
Schema:
- purchase_items.qty_received (backfilled from `received` boolean)
- purchases.warehouse_id (target warehouse FK)
- supplier_part_prices (price history per supplier/part with purchase ref)
- New status `partial` between ordered and received

Purchase ↔ Warehouse integration:
- Purchase::receiveItem(item, qty, warehouse?) — routes through
  WarehouseService::receive: creates batch + receipt event + supplier price row
- Purchase::receiveAllRemaining(warehouse?) — receives all outstanding lines
- Purchase::recomputeStatus() — auto: ordered → partial → received

Old flat markReceived() removed — every receipt now writes batches + ledger.

Filament:
- Purchase list: progress %, partial badge, warehouse picker on form
- ItemsRelationManager: per-line "Recepționează" with qty + warehouse modal,
  qty_received shown as "X.XX / Y.YY" with colour
- PartResource: new PriceHistoryRelationManager (supplier price history)
- SupplierResource: derived columns onTimeRate / avgDeliveryDays / spend(90d)
  + "Rerating" action

Analytics:
- App\Services\Warehouse\SupplierAnalytics (onTimeRate, avgDeliveryDays,
  spend, count, computedRating)
- `suppliers:rate` artisan command + weekly schedule (Mon 04:00)
- Computed rating: 70% on-time + 20% volume + 10% speed bonus

Tests (6 new, all pass):
- Partial receipt of 3/10 → status=partial + 1 batch + 1 price row
- receiveAllRemaining → status=received with received_at set
- Over-receive throws InvalidArgumentException
- Two partial receipts (4+6) → 2 batches FIFO + status=received
- onTimeRate 50% with 1 on-time + 1 late
- computedRating null when <2 deliveries

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 19:37:12 +00:00

97 lines
2.7 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;
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);
}
public function priceHistory(): HasMany
{
return $this->hasMany(SupplierPartPrice::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();
}
}