Reservation.php 3.1 KB

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