Order.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. 'contract_date',
  66. 'contract_number',
  67. 'comment',
  68. 'installation_date',
  69. 'ready_date',
  70. 'brigadier_id',
  71. 'order_status_id',
  72. 'tg_group_name',
  73. 'tg_group_link',
  74. 'ready_to_mount',
  75. ];
  76. public $appends = ['common_name', 'products_with_count'];
  77. public function products_sku(): HasMany
  78. {
  79. return $this->hasMany(ProductSKU::class, 'order_id', 'id');
  80. }
  81. public function products(): HasManyThrough
  82. {
  83. return $this->hasManyThrough(
  84. Product::class,
  85. ProductSKU::class,
  86. 'order_id',
  87. 'id',
  88. 'id',
  89. 'product_id');
  90. }
  91. public function user(): BelongsTo
  92. {
  93. return $this->belongsTo(User::class, 'user_id', 'id');
  94. }
  95. public function district(): BelongsTo
  96. {
  97. return $this->belongsTo(District::class);
  98. }
  99. public function area(): BelongsTo
  100. {
  101. return $this->belongsTo(Area::class);
  102. }
  103. public function objectType(): BelongsTo
  104. {
  105. return $this->belongsTo(ObjectType::class);
  106. }
  107. public function brigadier(): BelongsTo
  108. {
  109. return $this->belongsTo(User::class, 'brigadier_id', 'id');
  110. }
  111. public function orderStatus(): BelongsTo
  112. {
  113. return $this->belongsTo(OrderStatus::class);
  114. }
  115. public function getNeeds(): array
  116. {
  117. $needs = [];
  118. foreach ($this->products_sku as $sku) {
  119. if($sku->maf_order) continue;
  120. $needs[$sku->product_id]['needs'] = (isset($needs[$sku->product_id])) ? $needs[$sku->product_id]['needs'] + 1 : 1;
  121. }
  122. foreach ($needs as $productId => $quantity) {
  123. $needs[$productId]['sku'] = MafOrder::query()
  124. ->where('maf_orders.product_id', $productId)
  125. ->sum('maf_orders.in_stock');
  126. }
  127. return $needs;
  128. }
  129. public function recalculateReadyToMount(): void
  130. {
  131. $result = true;
  132. foreach ($this->getNeeds() as $need) {
  133. if($need['sku'] < $need['needs']) {
  134. $result = false;
  135. break;
  136. }
  137. }
  138. $this->update(['ready_to_mount' => ($result) ? 'Да' : 'Нет']);
  139. }
  140. public function autoChangeStatus(): void
  141. {
  142. if(($this->products_sku->count() < 1)
  143. && ($this->order_status_id !== self::STATUS_NEW)
  144. ) {
  145. $this->update(['order_status_id' => self::STATUS_NO_MAF]);
  146. return;
  147. }
  148. if(($this->order_status_id === self::STATUS_READY_TO_MOUNT)
  149. && ($this->brigadier_id !== null)
  150. && ($this->installation_date !== null)
  151. ) {
  152. $allMafConnected = true;
  153. foreach ($this->products_sku as $sku) {
  154. if($sku->maf_order) continue;
  155. $allMafConnected = false;
  156. }
  157. if($allMafConnected) {
  158. $this->update(['order_status_id' => self::STATUS_IN_MOUNT]);
  159. }
  160. }
  161. }
  162. public function commonName(): Attribute
  163. {
  164. return Attribute::make(
  165. get: fn($value) => (string) $this->object_address . ', ' . $this->area->name . ', ' . $this->district->shortname,
  166. );
  167. }
  168. public function productsWithCount(): Attribute
  169. {
  170. $products = $this->products_sku;
  171. $ret = [];
  172. foreach ($products as $product) {
  173. if(isset($ret[$product->product->id])) {
  174. $ret[$product->product->id]['count'] += 1;
  175. } else {
  176. $ret[$product->product->id] = [
  177. 'name' => $product->product->article,
  178. 'count' => 1,
  179. ];
  180. }
  181. if($product->maf_order?->order_number) {
  182. $ret[$product->product->id]['order_numbers'][] = $product->maf_order?->order_number;
  183. }
  184. }
  185. $s = '';
  186. foreach ($ret as $product) {
  187. ;
  188. $order_numbers = (isset($product['order_numbers'])) ? ' (' . implode(', ', array_unique($product['order_numbers'])) . ')' : '';
  189. $s .= '<div>' . $product['name'] . ' - ' . $product['count'] . $order_numbers . '</div>';
  190. }
  191. return Attribute::make(
  192. get: fn($value) => (string) $s,
  193. );
  194. }
  195. }