Order.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace App\Models;
  3. use App\Helpers\Price;
  4. use App\Models\Dictionary\Area;
  5. use App\Models\Dictionary\District;
  6. use App\Models\Scopes\YearScope;
  7. use Illuminate\Database\Eloquent\Attributes\ScopedBy;
  8. use Illuminate\Database\Eloquent\Casts\Attribute;
  9. use Illuminate\Database\Eloquent\Model;
  10. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  11. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  12. use Illuminate\Database\Eloquent\Relations\HasMany;
  13. use Illuminate\Database\Eloquent\Relations\HasManyThrough;
  14. use Illuminate\Database\Eloquent\SoftDeletes;
  15. use Illuminate\Support\Facades\DB;
  16. #[ScopedBy([YearScope::class])]
  17. class Order extends Model
  18. {
  19. use SoftDeletes;
  20. const DEFAULT_SORT_BY = 'created_at';
  21. const STATUS_NEW = 1;
  22. const STATUS_NOT_READY = 2;
  23. const STATUS_READY_NO_MAF = 3;
  24. const STATUS_READY_TO_MOUNT = 4;
  25. const STATUS_IN_MOUNT = 5;
  26. const STATUS_DUTY = 6;
  27. const STATUS_READY_TO_HAND_OVER = 7;
  28. const STATUS_NOT_HANDED_OVER_WITH_NOTES = 8;
  29. const STATUS_HANDED_OVER_WITH_NOTES = 9;
  30. const STATUS_HANDED_OVER = 10;
  31. const STATUS_NO_MAF = 11;
  32. const STATUS_PROBLEM = 12;
  33. //ПЛ не готова; ПЛ готова нет МАФ; Готов к монтажу; В монтаже; Долг;Готов к сдаче;Не сдан, замечания;Сдан с замечаниями;Сдан;Проблема
  34. const STATUS_NAMES = [
  35. self::STATUS_NEW => 'Новая',
  36. self::STATUS_NOT_READY => 'Не готова',
  37. self::STATUS_READY_NO_MAF => 'Готова, нет МАФ',
  38. self::STATUS_READY_TO_MOUNT => 'Готова к монтажу',
  39. self::STATUS_IN_MOUNT => 'В монтаже',
  40. self::STATUS_DUTY => 'Долг',
  41. self::STATUS_READY_TO_HAND_OVER => 'Готова к сдаче',
  42. self::STATUS_NOT_HANDED_OVER_WITH_NOTES => 'Не сдана, замечания',
  43. self::STATUS_HANDED_OVER_WITH_NOTES => 'Сдана с замечаниями',
  44. self::STATUS_HANDED_OVER => 'Сдана',
  45. self::STATUS_NO_MAF => 'Отсутствуют МАФ',
  46. self::STATUS_PROBLEM => 'Проблема',
  47. ];
  48. // set year attribute to current selected year
  49. protected static function boot(): void
  50. {
  51. parent::boot();
  52. static::creating(function($attributes) {
  53. if(!isset($attributes->year)) {
  54. $attributes->year = year();
  55. }
  56. });
  57. }
  58. protected $fillable = [
  59. 'year',
  60. 'user_id',
  61. 'district_id',
  62. 'area_id',
  63. 'object_address',
  64. 'object_type_id',
  65. 'comment',
  66. 'installation_date',
  67. 'ready_date',
  68. 'brigadier_id',
  69. 'order_status_id',
  70. 'tg_group_name',
  71. 'tg_group_link',
  72. 'ready_to_mount',
  73. ];
  74. public $appends = ['common_name', 'products_with_count'];
  75. public function products_sku(): HasMany
  76. {
  77. return $this->hasMany(ProductSKU::class, 'order_id', 'id');
  78. }
  79. public function products(): HasManyThrough
  80. {
  81. return $this->hasManyThrough(
  82. Product::class,
  83. ProductSKU::class,
  84. 'order_id',
  85. 'id',
  86. 'id',
  87. 'product_id');
  88. }
  89. public function user(): BelongsTo
  90. {
  91. return $this->belongsTo(User::class, 'user_id', 'id');
  92. }
  93. public function district(): BelongsTo
  94. {
  95. return $this->belongsTo(District::class);
  96. }
  97. public function area(): BelongsTo
  98. {
  99. return $this->belongsTo(Area::class);
  100. }
  101. public function objectType(): BelongsTo
  102. {
  103. return $this->belongsTo(ObjectType::class);
  104. }
  105. public function brigadier(): BelongsTo
  106. {
  107. return $this->belongsTo(User::class, 'brigadier_id', 'id');
  108. }
  109. public function orderStatus(): BelongsTo
  110. {
  111. return $this->belongsTo(OrderStatus::class);
  112. }
  113. public function getNeeds(): array
  114. {
  115. $needs = [];
  116. foreach ($this->products_sku as $sku) {
  117. if($sku->maf_order) continue;
  118. $needs[$sku->product_id]['needs'] = (isset($needs[$sku->product_id])) ? $needs[$sku->product_id]['needs'] + 1 : 1;
  119. }
  120. foreach ($needs as $productId => $quantity) {
  121. $needs[$productId]['sku'] = MafOrder::query()
  122. ->where('maf_orders.product_id', $productId)
  123. ->sum('maf_orders.in_stock');
  124. }
  125. return $needs;
  126. }
  127. public function recalculateReadyToMount(): void
  128. {
  129. $result = true;
  130. foreach ($this->getNeeds() as $need) {
  131. if($need['sku'] < $need['needs']) {
  132. $result = false;
  133. break;
  134. }
  135. }
  136. $this->update(['ready_to_mount' => ($result) ? 'Да' : 'Нет']);
  137. }
  138. public function autoChangeStatus(): void
  139. {
  140. if(($this->products_sku->count() < 1)
  141. && ($this->order_status_id !== self::STATUS_NEW)
  142. ) {
  143. $this->update(['order_status_id' => self::STATUS_NO_MAF]);
  144. return;
  145. }
  146. if(($this->order_status_id === self::STATUS_READY_TO_MOUNT)
  147. && ($this->brigadier_id !== null)
  148. && ($this->installation_date !== null)
  149. ) {
  150. $allMafConnected = true;
  151. foreach ($this->products_sku as $sku) {
  152. if($sku->maf_order) continue;
  153. $allMafConnected = false;
  154. }
  155. if($allMafConnected) {
  156. $this->update(['order_status_id' => self::STATUS_IN_MOUNT]);
  157. }
  158. }
  159. }
  160. public function commonName(): Attribute
  161. {
  162. return Attribute::make(
  163. get: fn($value) => (string) $this->object_address . ', ' . $this->area->name . ', ' . $this->district->shortname,
  164. );
  165. }
  166. public function productsWithCount(): Attribute
  167. {
  168. $products = $this->products_sku;
  169. $ret = [];
  170. foreach ($products as $product) {
  171. if(isset($ret[$product->product->id])) {
  172. $ret[$product->product->id]['count'] += 1;
  173. } else {
  174. $ret[$product->product->id] = [
  175. 'name' => $product->product->article,
  176. 'count' => 1,
  177. ];
  178. }
  179. if($product->maf_order?->order_number) {
  180. $ret[$product->product->id]['order_numbers'][] = $product->maf_order?->order_number;
  181. }
  182. }
  183. $s = '';
  184. foreach ($ret as $product) {
  185. ;
  186. $order_numbers = (isset($product['order_numbers'])) ? ' (' . implode(', ', array_unique($product['order_numbers'])) . ')' : '';
  187. $s .= '<div>' . $product['name'] . ' - ' . $product['count'] . $order_numbers . '</div>';
  188. }
  189. return Attribute::make(
  190. get: fn($value) => (string) $s,
  191. );
  192. }
  193. }