Reservation.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. use Illuminate\Database\Eloquent\Relations\HasOne;
  7. /**
  8. * Резерв запчасти под рекламацию.
  9. *
  10. * Резерв уменьшает свободный остаток, но НЕ физический.
  11. * Физический остаток уменьшается только при списании (issue).
  12. *
  13. * Жизненный цикл:
  14. * 1. active - создан при добавлении запчасти в рекламацию
  15. * 2. issued - запчасть списана (отгружена)
  16. * 3. cancelled - резерв отменён (запчасть удалена из рекламации)
  17. */
  18. class Reservation extends Model
  19. {
  20. use HasFactory;
  21. const STATUS_ACTIVE = 'active';
  22. const STATUS_ISSUED = 'issued';
  23. const STATUS_CANCELLED = 'cancelled';
  24. const STATUS_NAMES = [
  25. self::STATUS_ACTIVE => 'Активен',
  26. self::STATUS_ISSUED => 'Списано',
  27. self::STATUS_CANCELLED => 'Отменён',
  28. ];
  29. protected $fillable = [
  30. 'spare_part_id',
  31. 'spare_part_order_id',
  32. 'reclamation_id',
  33. 'reserved_qty',
  34. 'with_documents',
  35. 'status',
  36. 'movement_id',
  37. ];
  38. protected $casts = [
  39. 'reserved_qty' => 'integer',
  40. 'with_documents' => 'boolean',
  41. ];
  42. // Отношения
  43. public function sparePart(): BelongsTo
  44. {
  45. return $this->belongsTo(SparePart::class);
  46. }
  47. public function sparePartOrder(): BelongsTo
  48. {
  49. return $this->belongsTo(SparePartOrder::class);
  50. }
  51. public function reclamation(): BelongsTo
  52. {
  53. return $this->belongsTo(Reclamation::class);
  54. }
  55. public function movement(): BelongsTo
  56. {
  57. return $this->belongsTo(InventoryMovement::class, 'movement_id');
  58. }
  59. // Аксессоры
  60. public function getStatusNameAttribute(): string
  61. {
  62. return self::STATUS_NAMES[$this->status] ?? $this->status;
  63. }
  64. public function isActive(): bool
  65. {
  66. return $this->status === self::STATUS_ACTIVE;
  67. }
  68. public function isIssued(): bool
  69. {
  70. return $this->status === self::STATUS_ISSUED;
  71. }
  72. public function isCancelled(): bool
  73. {
  74. return $this->status === self::STATUS_CANCELLED;
  75. }
  76. // Scopes
  77. public function scopeActive($query)
  78. {
  79. return $query->where('status', self::STATUS_ACTIVE);
  80. }
  81. public function scopeForReclamation($query, int $reclamationId)
  82. {
  83. return $query->where('reclamation_id', $reclamationId);
  84. }
  85. public function scopeForSparePart($query, int $sparePartId)
  86. {
  87. return $query->where('spare_part_id', $sparePartId);
  88. }
  89. public function scopeWithDocuments($query, bool $withDocs = true)
  90. {
  91. return $query->where('with_documents', $withDocs);
  92. }
  93. public function scopeFromOrder($query, int $orderId)
  94. {
  95. return $query->where('spare_part_order_id', $orderId);
  96. }
  97. }