Reclamation.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. 'user_id',
  43. 'status_id',
  44. 'reason',
  45. 'guarantee',
  46. 'whats_done',
  47. 'create_date',
  48. 'finish_date',
  49. 'start_work_date',
  50. 'work_days',
  51. 'brigadier_id',
  52. 'comment',
  53. 'factory_reclamation_number',
  54. ];
  55. public function order(): BelongsTo
  56. {
  57. return $this->belongsTo(Order::class)->withoutGlobalScope(\App\Models\Scopes\YearScope::class);
  58. }
  59. public function status(): BelongsTo
  60. {
  61. return $this->belongsTo(ReclamationStatus::class);
  62. }
  63. public function skus(): BelongsToMany
  64. {
  65. return $this->belongsToMany(
  66. ProductSKU::class,
  67. 'reclamation_product_sku',
  68. 'reclamation_id',
  69. 'product_sku_id')
  70. ->withoutGlobalScope(\App\Models\Scopes\YearScope::class);
  71. }
  72. public function user(): BelongsTo
  73. {
  74. return $this->belongsTo(User::class);
  75. }
  76. public function brigadier(): BelongsTo
  77. {
  78. return $this->belongsTo(User::class, 'brigadier_id', 'id');
  79. }
  80. public function photos_before(): BelongsToMany
  81. {
  82. return $this->belongsToMany(File::class, 'reclamation_photo_before');
  83. }
  84. public function photos_after(): BelongsToMany
  85. {
  86. return $this->belongsToMany(File::class, 'reclamation_photo_after');
  87. }
  88. public function documents(): BelongsToMany
  89. {
  90. return $this->belongsToMany(File::class, 'reclamation_document');
  91. }
  92. public function acts(): BelongsToMany
  93. {
  94. return $this->belongsToMany(File::class, 'reclamation_act');
  95. }
  96. public function details(): HasMany
  97. {
  98. return $this->hasMany(ReclamationDetail::class);
  99. }
  100. public function spareParts(): BelongsToMany
  101. {
  102. return $this->belongsToMany(SparePart::class, 'reclamation_spare_part')
  103. ->withPivot('quantity', 'with_documents', 'status', 'reserved_qty', 'issued_qty')
  104. ->withTimestamps();
  105. }
  106. /**
  107. * Резервы запчастей для этой рекламации
  108. */
  109. public function sparePartReservations(): HasMany
  110. {
  111. return $this->hasMany(Reservation::class);
  112. }
  113. /**
  114. * Дефициты запчастей для этой рекламации
  115. */
  116. public function sparePartShortages(): HasMany
  117. {
  118. return $this->hasMany(Shortage::class);
  119. }
  120. /**
  121. * Активные резервы запчастей
  122. */
  123. public function activeReservations(): HasMany
  124. {
  125. return $this->hasMany(Reservation::class)->where('status', Reservation::STATUS_ACTIVE);
  126. }
  127. /**
  128. * Открытые дефициты запчастей
  129. */
  130. public function openShortages(): HasMany
  131. {
  132. return $this->hasMany(Shortage::class)->where('status', Shortage::STATUS_OPEN);
  133. }
  134. public function chatMessages(): HasMany
  135. {
  136. return $this->hasMany(ChatMessage::class)->orderBy('created_at');
  137. }
  138. public static function visibleStatusIdsForBrigadier(): array
  139. {
  140. return self::BRIGADIER_VISIBLE_STATUS_IDS;
  141. }
  142. }