Reservation.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Models;
  3. use App\Models\Scopes\YearScope;
  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. const STATUS_ACTIVE = 'active';
  21. const STATUS_ISSUED = 'issued';
  22. const STATUS_CANCELLED = 'cancelled';
  23. const STATUS_NAMES = [
  24. self::STATUS_ACTIVE => 'Активен',
  25. self::STATUS_ISSUED => 'Списано',
  26. self::STATUS_CANCELLED => 'Отменён',
  27. ];
  28. protected $fillable = [
  29. 'spare_part_id',
  30. 'spare_part_order_id',
  31. 'reclamation_id',
  32. 'reserved_qty',
  33. 'with_documents',
  34. 'status',
  35. 'movement_id',
  36. ];
  37. protected $casts = [
  38. 'reserved_qty' => 'integer',
  39. 'with_documents' => 'boolean',
  40. ];
  41. // Отношения
  42. public function sparePart(): BelongsTo
  43. {
  44. return $this->belongsTo(SparePart::class);
  45. }
  46. public function sparePartOrder(): BelongsTo
  47. {
  48. return $this->belongsTo(SparePartOrder::class)->withoutGlobalScope(YearScope::class);
  49. }
  50. public function reclamation(): BelongsTo
  51. {
  52. return $this->belongsTo(Reclamation::class);
  53. }
  54. public function movement(): BelongsTo
  55. {
  56. return $this->belongsTo(InventoryMovement::class, 'movement_id');
  57. }
  58. // Аксессоры
  59. public function getStatusNameAttribute(): string
  60. {
  61. return self::STATUS_NAMES[$this->status] ?? $this->status;
  62. }
  63. public function isActive(): bool
  64. {
  65. return $this->status === self::STATUS_ACTIVE;
  66. }
  67. public function isIssued(): bool
  68. {
  69. return $this->status === self::STATUS_ISSUED;
  70. }
  71. public function isCancelled(): bool
  72. {
  73. return $this->status === self::STATUS_CANCELLED;
  74. }
  75. // Scopes
  76. public function scopeActive($query)
  77. {
  78. return $query->where('status', self::STATUS_ACTIVE);
  79. }
  80. public function scopeForReclamation($query, int $reclamationId)
  81. {
  82. return $query->where('reclamation_id', $reclamationId);
  83. }
  84. public function scopeForSparePart($query, int $sparePartId)
  85. {
  86. return $query->where('spare_part_id', $sparePartId);
  87. }
  88. public function scopeWithDocuments($query, bool $withDocs = true)
  89. {
  90. return $query->where('with_documents', $withDocs);
  91. }
  92. public function scopeFromOrder($query, int $orderId)
  93. {
  94. return $query->where('spare_part_order_id', $orderId);
  95. }
  96. }