Order.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. const STATUS_COLOR = [
  49. self::STATUS_NEW => 'dark',
  50. self::STATUS_NOT_READY => 'dark',
  51. self::STATUS_READY_NO_MAF => '',
  52. self::STATUS_READY_TO_MOUNT => 'primary',
  53. self::STATUS_IN_MOUNT => 'info',
  54. self::STATUS_DUTY => 'warning',
  55. self::STATUS_READY_TO_HAND_OVER => 'primary',
  56. self::STATUS_NOT_HANDED_OVER_WITH_NOTES => 'warning',
  57. self::STATUS_HANDED_OVER_WITH_NOTES => 'info',
  58. self::STATUS_HANDED_OVER => 'success',
  59. self::STATUS_NO_MAF => 'secondary',
  60. self::STATUS_PROBLEM => 'danger',
  61. ];
  62. // set year attribute to current selected year
  63. protected static function boot(): void
  64. {
  65. parent::boot();
  66. static::creating(function($attributes) {
  67. if(!isset($attributes->year)) {
  68. $attributes->year = year();
  69. }
  70. });
  71. }
  72. protected $fillable = [
  73. 'year',
  74. 'name',
  75. 'user_id',
  76. 'district_id',
  77. 'area_id',
  78. 'object_address',
  79. 'object_type_id',
  80. 'comment',
  81. 'installation_date',
  82. 'ready_date',
  83. 'brigadier_id',
  84. 'order_status_id',
  85. 'tg_group_name',
  86. 'tg_group_link',
  87. 'ready_to_mount',
  88. ];
  89. public $appends = ['common_name', 'products_with_count'];
  90. public function products_sku(): HasMany
  91. {
  92. return $this->hasMany(ProductSKU::class, 'order_id', 'id');
  93. }
  94. public function products(): HasManyThrough
  95. {
  96. return $this->hasManyThrough(
  97. Product::class,
  98. ProductSKU::class,
  99. 'order_id',
  100. 'id',
  101. 'id',
  102. 'product_id');
  103. }
  104. public function user(): BelongsTo
  105. {
  106. return $this->belongsTo(User::class, 'user_id', 'id');
  107. }
  108. public function district(): BelongsTo
  109. {
  110. return $this->belongsTo(District::class);
  111. }
  112. public function area(): BelongsTo
  113. {
  114. return $this->belongsTo(Area::class);
  115. }
  116. public function objectType(): BelongsTo
  117. {
  118. return $this->belongsTo(ObjectType::class);
  119. }
  120. public function brigadier(): BelongsTo
  121. {
  122. return $this->belongsTo(User::class, 'brigadier_id', 'id');
  123. }
  124. public function orderStatus(): BelongsTo
  125. {
  126. return $this->belongsTo(OrderStatus::class);
  127. }
  128. public function photos(): BelongsToMany
  129. {
  130. return $this->belongsToMany(File::class, 'order_photo');
  131. }
  132. public function documents(): BelongsToMany
  133. {
  134. return $this->belongsToMany(File::class, 'order_document');
  135. }
  136. public function statements(): BelongsToMany
  137. {
  138. return $this->belongsToMany(File::class, 'order_statement');
  139. }
  140. public function getNeeds(): array
  141. {
  142. $needs = [];
  143. foreach ($this->products_sku as $sku) {
  144. if($sku->maf_order) continue;
  145. $needs[$sku->product_id]['needs'] = (isset($needs[$sku->product_id])) ? $needs[$sku->product_id]['needs'] + 1 : 1;
  146. }
  147. foreach ($needs as $productId => $quantity) {
  148. $needs[$productId]['sku'] = MafOrder::query()
  149. ->where('maf_orders.product_id', $productId)
  150. ->sum('maf_orders.in_stock');
  151. }
  152. return $needs;
  153. }
  154. public function recalculateReadyToMount(): void
  155. {
  156. $result = true;
  157. foreach ($this->getNeeds() as $need) {
  158. if($need['sku'] < $need['needs']) {
  159. $result = false;
  160. break;
  161. }
  162. }
  163. $this->update(['ready_to_mount' => ($result) ? 'Да' : 'Нет']);
  164. }
  165. public function autoChangeStatus(): void
  166. {
  167. if(($this->products_sku->count() < 1)
  168. && ($this->order_status_id !== self::STATUS_NEW)
  169. ) {
  170. $this->update(['order_status_id' => self::STATUS_NO_MAF]);
  171. return;
  172. }
  173. if(($this->order_status_id === self::STATUS_READY_TO_MOUNT)
  174. && ($this->brigadier_id !== null)
  175. && ($this->installation_date !== null)
  176. ) {
  177. $allMafConnected = true;
  178. foreach ($this->products_sku as $sku) {
  179. if($sku->maf_order) continue;
  180. $allMafConnected = false;
  181. }
  182. if($allMafConnected) {
  183. $this->update(['order_status_id' => self::STATUS_IN_MOUNT]);
  184. }
  185. }
  186. }
  187. public function commonName(): Attribute
  188. {
  189. return Attribute::make(
  190. get: fn($value) => (string) $this->object_address . ', ' . $this->area->name . ', ' . $this->district->shortname,
  191. );
  192. }
  193. public function productsWithCount(): Attribute
  194. {
  195. $products = $this->products_sku;
  196. $ret = [];
  197. foreach ($products as $product) {
  198. if(isset($ret[$product->product->id])) {
  199. $ret[$product->product->id]['count'] += 1;
  200. } else {
  201. $ret[$product->product->id] = [
  202. 'name' => $product->product->article,
  203. 'count' => 1,
  204. ];
  205. }
  206. if($product->maf_order?->order_number) {
  207. $ret[$product->product->id]['order_numbers'][] = $product->maf_order?->order_number;
  208. }
  209. }
  210. $s = '';
  211. foreach ($ret as $product) {
  212. ;
  213. $order_numbers = (isset($product['order_numbers'])) ? ' (' . implode(', ', array_unique($product['order_numbers'])) . ')' : '';
  214. $s .= '<div>' . $product['name'] . ' - ' . $product['count'] . $order_numbers . '</div>';
  215. }
  216. return Attribute::make(
  217. get: fn($value) => (string) $s,
  218. );
  219. }
  220. }