Order.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. 'ready_date',
  33. 'brigadier_id',
  34. 'order_status_id',
  35. 'tg_group_name',
  36. 'tg_group_link',
  37. 'ready_to_mount',
  38. ];
  39. public $appends = ['common_name', 'products_with_count'];
  40. public function products_sku(): HasMany
  41. {
  42. return $this->hasMany(ProductSKU::class, 'order_id', 'id');
  43. }
  44. public function products(): HasManyThrough
  45. {
  46. return $this->hasManyThrough(
  47. Product::class,
  48. ProductSKU::class,
  49. 'order_id',
  50. 'id',
  51. 'id',
  52. 'product_id');
  53. }
  54. public function user(): BelongsTo
  55. {
  56. return $this->belongsTo(User::class, 'user_id', 'id');
  57. }
  58. public function district(): BelongsTo
  59. {
  60. return $this->belongsTo(District::class);
  61. }
  62. public function area(): BelongsTo
  63. {
  64. return $this->belongsTo(Area::class);
  65. }
  66. public function objectType(): BelongsTo
  67. {
  68. return $this->belongsTo(ObjectType::class);
  69. }
  70. public function brigadier(): BelongsTo
  71. {
  72. return $this->belongsTo(User::class, 'brigadier_id', 'id');
  73. }
  74. public function orderStatus(): BelongsTo
  75. {
  76. return $this->belongsTo(OrderStatus::class);
  77. }
  78. public function getNeeds(): array
  79. {
  80. $needs = [];
  81. foreach ($this->products_sku as $sku) {
  82. $needs[$sku->product_id]['needs'] = (isset($needs[$sku->product_id])) ? $needs[$sku->product_id]['needs'] + 1 : 1;
  83. }
  84. foreach ($needs as $productId => $quantity) {
  85. $needs[$productId]['sku'] = MafOrder::query()
  86. ->where('maf_orders.product_id', $productId)
  87. ->sum('maf_orders.in_stock');
  88. }
  89. return $needs;
  90. }
  91. public function recalculateReadyToMount(): void
  92. {
  93. $result = true;
  94. foreach ($this->getNeeds() as $need) {
  95. if($need['sku'] < $need['needs']) {
  96. $result = false;
  97. break;
  98. }
  99. }
  100. if($this->order_status_id > self::STATUS_NEW) {
  101. $result = true;
  102. }
  103. $this->update(['ready_to_mount' => ($result) ? 'Да' : 'Нет']);
  104. }
  105. public function commonName(): Attribute
  106. {
  107. return Attribute::make(
  108. get: fn($value) => (string) $this->district->shortname . ', ' . $this->area->name . ', ' . $this->object_address,
  109. );
  110. }
  111. public function productsWithCount(): Attribute
  112. {
  113. $products = $this->products;
  114. $ret = [];
  115. foreach ($products as $product) {
  116. if(isset($ret[$product->id])) {
  117. $ret[$product->id]['count'] += 1;
  118. } else {
  119. $ret[$product->id] = [
  120. 'name' => $product->common_name,
  121. 'count' => 1,
  122. ];
  123. }
  124. }
  125. $s = '';
  126. foreach ($ret as $product) {
  127. $s .= $product['name'] . ' (' . $product['count'] . ')<br>';
  128. }
  129. return Attribute::make(
  130. get: fn($value) => (string) $s,
  131. );
  132. }
  133. }