| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- declare(strict_types=1);
- namespace App\Models;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use Illuminate\Database\Eloquent\SoftDeletes;
- class StockOrder extends Model
- {
- use HasFactory, SoftDeletes;
- public const STATUS_ORDERED = 'ordered';
- public const STATUS_IN_STOCK = 'in_stock';
- public const STATUS_SHIPPED = 'shipped';
- public const STATUS_NAMES = [
- self::STATUS_ORDERED => 'Заказан',
- 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);
- }
- }
|