Order.php 7.9 KB

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