Reclamation.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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\BelongsToMany;
  7. use Illuminate\Database\Eloquent\Relations\HasMany;
  8. class Reclamation extends Model
  9. {
  10. use HasFactory;
  11. const DEFAULT_SORT_BY = 'created_at';
  12. const STATUS_NEW = 1;
  13. const STATUS_WAIT = 2;
  14. const STATUS_IN_WORK = 3;
  15. const STATUS_SUBSCRIBE_ACT = 4;
  16. const STATUS_DONE = 5;
  17. const STATUS_SENT = 6;
  18. const STATUS_DO_DOCS = 7;
  19. const STATUS_HANDOVER_TO_CHECK = 8;
  20. const STATUS_PAID = 9;
  21. const STATUS_CLOSED_NO_PAY = 10;
  22. const STATUS_NAMES = [
  23. self::STATUS_NEW => 'Новая',
  24. self::STATUS_WAIT => 'В ожидании',
  25. self::STATUS_IN_WORK => 'В работе',
  26. self::STATUS_SUBSCRIBE_ACT => 'Подпись акта',
  27. self::STATUS_DONE => 'Исправлена',
  28. self::STATUS_SENT => 'Закрыта, оплачивается',
  29. self::STATUS_DO_DOCS => 'Подготовка документов',
  30. self::STATUS_HANDOVER_TO_CHECK => 'Передана на проверку',
  31. self::STATUS_PAID => 'Оплачена',
  32. self::STATUS_CLOSED_NO_PAY => 'Закрыта, не оплачивается',
  33. ];
  34. public const BRIGADIER_VISIBLE_STATUS_IDS = [
  35. self::STATUS_NEW,
  36. self::STATUS_WAIT,
  37. self::STATUS_IN_WORK,
  38. self::STATUS_SUBSCRIBE_ACT,
  39. ];
  40. protected $fillable = [
  41. 'order_id',
  42. 'reclamation_type_id',
  43. 'user_id',
  44. 'status_id',
  45. 'reason',
  46. 'guarantee',
  47. 'whats_done',
  48. 'create_date',
  49. 'finish_date',
  50. 'start_work_date',
  51. 'work_days',
  52. 'brigadier_id',
  53. 'comment',
  54. 'factory_reclamation_number',
  55. ];
  56. public function order(): BelongsTo
  57. {
  58. return $this->belongsTo(Order::class)->withoutGlobalScope(\App\Models\Scopes\YearScope::class);
  59. }
  60. public function status(): BelongsTo
  61. {
  62. return $this->belongsTo(ReclamationStatus::class);
  63. }
  64. public function reclamationType(): BelongsTo
  65. {
  66. return $this->belongsTo(ReclamationType::class);
  67. }
  68. public function isDkr(): bool
  69. {
  70. return $this->reclamationType?->code === ReclamationType::CODE_DKR;
  71. }
  72. public function skus(): BelongsToMany
  73. {
  74. return $this->belongsToMany(
  75. ProductSKU::class,
  76. 'reclamation_product_sku',
  77. 'reclamation_id',
  78. 'product_sku_id')
  79. ->withoutGlobalScope(\App\Models\Scopes\YearScope::class);
  80. }
  81. public function user(): BelongsTo
  82. {
  83. return $this->belongsTo(User::class);
  84. }
  85. public function brigadier(): BelongsTo
  86. {
  87. return $this->belongsTo(User::class, 'brigadier_id', 'id');
  88. }
  89. public function photos_before(): BelongsToMany
  90. {
  91. return $this->belongsToMany(File::class, 'reclamation_photo_before');
  92. }
  93. public function photos_after(): BelongsToMany
  94. {
  95. return $this->belongsToMany(File::class, 'reclamation_photo_after');
  96. }
  97. public function documents(): BelongsToMany
  98. {
  99. return $this->belongsToMany(File::class, 'reclamation_document');
  100. }
  101. public function acts(): BelongsToMany
  102. {
  103. return $this->belongsToMany(File::class, 'reclamation_act');
  104. }
  105. public function details(): HasMany
  106. {
  107. return $this->hasMany(ReclamationDetail::class);
  108. }
  109. public function spareParts(): BelongsToMany
  110. {
  111. return $this->belongsToMany(SparePart::class, 'reclamation_spare_part')
  112. ->withPivot('quantity', 'with_documents', 'status', 'reserved_qty', 'issued_qty')
  113. ->withTimestamps();
  114. }
  115. /**
  116. * Резервы запчастей для этой рекламации
  117. */
  118. public function sparePartReservations(): HasMany
  119. {
  120. return $this->hasMany(Reservation::class);
  121. }
  122. /**
  123. * Дефициты запчастей для этой рекламации
  124. */
  125. public function sparePartShortages(): HasMany
  126. {
  127. return $this->hasMany(Shortage::class);
  128. }
  129. /**
  130. * Активные резервы запчастей
  131. */
  132. public function activeReservations(): HasMany
  133. {
  134. return $this->hasMany(Reservation::class)->where('status', Reservation::STATUS_ACTIVE);
  135. }
  136. /**
  137. * Открытые дефициты запчастей
  138. */
  139. public function openShortages(): HasMany
  140. {
  141. return $this->hasMany(Shortage::class)->where('status', Shortage::STATUS_OPEN);
  142. }
  143. public function chatMessages(): HasMany
  144. {
  145. return $this->hasMany(ChatMessage::class)->orderBy('created_at');
  146. }
  147. public static function visibleStatusIdsForBrigadier(): array
  148. {
  149. return self::BRIGADIER_VISIBLE_STATUS_IDS;
  150. }
  151. }