'array', 'settings' => 'array', 'can_ship' => 'boolean', 'can_pickup' => 'boolean', 'is_primary' => 'boolean', 'is_active' => 'boolean', ]; // Relationships public function entity(): BelongsTo { return $this->belongsTo(Entity::class); } public function inventory(): HasMany { return $this->hasMany(Inventory::class, 'warehouse_id'); } public function movements(): HasMany { return $this->hasMany(InventoryMovement::class, 'warehouse_id'); } // Address helpers public function getFullAddressAttribute(): string { $parts = array_filter([ $this->address_line1, $this->address_line2, $this->city, $this->county, $this->postcode, $this->country, ]); return implode(', ', $parts); } // Stock helpers /** * Get stock for a specific product. */ public function getStock(Product $product): ?Inventory { return $this->inventory() ->where('product_id', $product->id) ->first(); } /** * Get available stock (quantity - reserved). */ public function getAvailableStock(Product $product): int { $inventory = $this->getStock($product); if (! $inventory) { return 0; } return $inventory->getAvailableQuantity(); } /** * Check if product is in stock at this warehouse. */ public function hasStock(Product $product, int $quantity = 1): bool { return $this->getAvailableStock($product) >= $quantity; } // Operating hours /** * Check if warehouse is open at a given time. */ public function isOpenAt(\DateTimeInterface $dateTime): bool { if (! $this->operating_hours) { return true; // No hours defined = always open } $dayOfWeek = strtolower($dateTime->format('D')); $time = $dateTime->format('H:i'); $hours = $this->operating_hours[$dayOfWeek] ?? null; if (! $hours || ! isset($hours['open'], $hours['close'])) { return false; } return $time >= $hours['open'] && $time <= $hours['close']; } // Scopes public function scopeActive($query) { return $query->where('is_active', true); } public function scopePrimary($query) { return $query->where('is_primary', true); } public function scopeCanShip($query) { return $query->where('can_ship', true); } public function scopeForEntity($query, int $entityId) { return $query->where('entity_id', $entityId); } public function scopeOfType($query, string $type) { return $query->where('type', $type); } }