StockReservation.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Models;
  4. use Illuminate\Database\Eloquent\Builder;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. class StockReservation extends Model
  8. {
  9. public const STATUS_ACTIVE = 'active';
  10. public const STATUS_ISSUED = 'issued';
  11. public const STATUS_CANCELLED = 'cancelled';
  12. public const STATUS_NAMES = [
  13. self::STATUS_ACTIVE => 'Забронировано',
  14. self::STATUS_ISSUED => 'Списано',
  15. self::STATUS_CANCELLED => 'Отменено',
  16. ];
  17. protected $fillable = [
  18. 'common_catalog_item_id',
  19. 'stock_order_id',
  20. 'manager_id',
  21. 'quantity',
  22. 'status',
  23. 'note',
  24. 'created_by',
  25. 'issued_at',
  26. 'cancelled_at',
  27. ];
  28. protected function casts(): array
  29. {
  30. return [
  31. 'common_catalog_item_id' => 'integer',
  32. 'stock_order_id' => 'integer',
  33. 'manager_id' => 'integer',
  34. 'quantity' => 'integer',
  35. 'created_by' => 'integer',
  36. 'issued_at' => 'datetime',
  37. 'cancelled_at' => 'datetime',
  38. ];
  39. }
  40. public function item(): BelongsTo
  41. {
  42. return $this->belongsTo(CommonCatalogItem::class, 'common_catalog_item_id');
  43. }
  44. public function order(): BelongsTo
  45. {
  46. return $this->belongsTo(StockOrder::class, 'stock_order_id');
  47. }
  48. public function manager(): BelongsTo
  49. {
  50. return $this->belongsTo(User::class, 'manager_id');
  51. }
  52. public function creator(): BelongsTo
  53. {
  54. return $this->belongsTo(User::class, 'created_by');
  55. }
  56. public function scopeActive(Builder $query): Builder
  57. {
  58. return $query->where('status', self::STATUS_ACTIVE);
  59. }
  60. public function isActive(): bool
  61. {
  62. return $this->status === self::STATUS_ACTIVE;
  63. }
  64. }