'Заказан', self::STATUS_IN_STOCK => 'На складе', self::STATUS_SHIPPED => 'Отгружено', ]; protected $fillable = [ 'common_catalog_item_id', 'order_number', 'status', 'ordered_quantity', 'available_quantity', 'note', 'user_id', ]; protected function casts(): array { return [ 'common_catalog_item_id' => 'integer', 'ordered_quantity' => 'integer', 'available_quantity' => 'integer', 'user_id' => 'integer', ]; } public function item(): BelongsTo { return $this->belongsTo(CommonCatalogItem::class, 'common_catalog_item_id'); } public function user(): BelongsTo { return $this->belongsTo(User::class); } public function reservations(): HasMany { return $this->hasMany(StockReservation::class); } public function movements(): HasMany { return $this->hasMany(StockMovement::class); } public function getStatusNameAttribute(): string { return self::STATUS_NAMES[$this->status] ?? $this->status; } public function getReservedQuantityAttribute(): int { if (array_key_exists('active_reservations_sum_quantity', $this->attributes)) { return (int) $this->attributes['active_reservations_sum_quantity']; } return (int) $this->reservations()->active()->sum('quantity'); } public function getFreeQuantityAttribute(): int { return max(0, $this->available_quantity - $this->reserved_quantity); } }