Order.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 Illuminate\Database\Eloquent\Casts\Attribute;
  7. use Illuminate\Database\Eloquent\Model;
  8. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  9. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  10. use Illuminate\Database\Eloquent\Relations\HasMany;
  11. use Illuminate\Database\Eloquent\Relations\HasManyThrough;
  12. use Illuminate\Database\Eloquent\SoftDeletes;
  13. use Illuminate\Support\Facades\DB;
  14. class Order extends Model
  15. {
  16. use SoftDeletes;
  17. const DEFAULT_SORT_BY = 'created_at';
  18. const STATUS_NEW = 1;
  19. const STATUS_READY_TO_MOUNT = 2;
  20. const STATUS_READY_TO_HAND_OVER = 3;
  21. const STATUS_HANDED_OVER = 4;
  22. protected $fillable = [
  23. 'user_id',
  24. 'district_id',
  25. 'area_id',
  26. 'object_address',
  27. 'object_type_id',
  28. 'contract_date',
  29. 'contract_number',
  30. 'comment',
  31. 'installation_date',
  32. 'brigadier_id',
  33. 'order_status_id',
  34. 'tg_group_name',
  35. 'tg_group_link',
  36. 'ready_to_mount',
  37. ];
  38. public $appends = ['common_name', 'products_with_count'];
  39. public function products_sku(): HasMany
  40. {
  41. return $this->hasMany(ProductSKU::class, 'order_id', 'id');
  42. }
  43. public function products(): HasManyThrough
  44. {
  45. return $this->hasManyThrough(
  46. Product::class,
  47. ProductSKU::class,
  48. 'order_id',
  49. 'id',
  50. 'id',
  51. 'product_id');
  52. }
  53. public function user(): BelongsTo
  54. {
  55. return $this->belongsTo(User::class, 'user_id', 'id');
  56. }
  57. public function district(): BelongsTo
  58. {
  59. return $this->belongsTo(District::class);
  60. }
  61. public function area(): BelongsTo
  62. {
  63. return $this->belongsTo(Area::class);
  64. }
  65. public function objectType(): BelongsTo
  66. {
  67. return $this->belongsTo(ObjectType::class);
  68. }
  69. public function brigadier(): BelongsTo
  70. {
  71. return $this->belongsTo(User::class, 'brigadier_id', 'id');
  72. }
  73. public function orderStatus(): BelongsTo
  74. {
  75. return $this->belongsTo(OrderStatus::class);
  76. }
  77. public function getNeeds(): array
  78. {
  79. $needs = [];
  80. foreach ($this->products_sku as $sku) {
  81. $needs[$sku->product_id]['needs'] = (isset($needs[$sku->product_id])) ? $needs[$sku->product_id]['needs'] + 1 : 1;
  82. }
  83. foreach ($needs as $productId => $quantity) {
  84. $needs[$productId]['sku'] = MafOrder::query()
  85. ->where('maf_orders.product_id', $productId)
  86. ->sum('maf_orders.in_stock');
  87. }
  88. return $needs;
  89. }
  90. public function recalculateReadyToMount(): void
  91. {
  92. $result = true;
  93. foreach ($this->getNeeds() as $need) {
  94. if($need['sku'] < $need['needs']) {
  95. $result = false;
  96. break;
  97. }
  98. }
  99. if($this->order_status_id > self::STATUS_NEW) {
  100. $result = true;
  101. }
  102. $this->update(['ready_to_mount' => ($result) ? 'Да' : 'Нет']);
  103. }
  104. public function commonName(): Attribute
  105. {
  106. return Attribute::make(
  107. get: fn($value) => (string) $this->district->shortname . ', ' . $this->area->name . ', ' . $this->object_address,
  108. );
  109. }
  110. public function productsWithCount(): Attribute
  111. {
  112. $products = $this->products;
  113. $ret = [];
  114. foreach ($products as $product) {
  115. if(isset($ret[$product->id])) {
  116. $ret[$product->id]['count'] += 1;
  117. } else {
  118. $ret[$product->id] = [
  119. 'name' => $product->common_name,
  120. 'count' => 1,
  121. ];
  122. }
  123. }
  124. $s = '';
  125. foreach ($ret as $product) {
  126. $s .= $product['name'] . ' (' . $product['count'] . ')<br>';
  127. }
  128. return Attribute::make(
  129. get: fn($value) => (string) $s,
  130. );
  131. }
  132. }