Order.php 10 KB

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