Reclamation.php 4.1 KB

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