Order.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. 'install_days',
  90. ];
  91. public $appends = ['common_name', 'products_with_count'];
  92. public function products_sku(): HasMany
  93. {
  94. return $this->hasMany(ProductSKU::class, 'order_id', 'id');
  95. }
  96. public function products(): HasManyThrough
  97. {
  98. return $this->hasManyThrough(
  99. Product::class,
  100. ProductSKU::class,
  101. 'order_id',
  102. 'id',
  103. 'id',
  104. 'product_id');
  105. }
  106. public function user(): BelongsTo
  107. {
  108. return $this->belongsTo(User::class, 'user_id', 'id');
  109. }
  110. public function district(): BelongsTo
  111. {
  112. return $this->belongsTo(District::class);
  113. }
  114. public function area(): BelongsTo
  115. {
  116. return $this->belongsTo(Area::class);
  117. }
  118. public function objectType(): BelongsTo
  119. {
  120. return $this->belongsTo(ObjectType::class);
  121. }
  122. public function brigadier(): BelongsTo
  123. {
  124. return $this->belongsTo(User::class, 'brigadier_id', 'id');
  125. }
  126. public function orderStatus(): BelongsTo
  127. {
  128. return $this->belongsTo(OrderStatus::class);
  129. }
  130. public function photos(): BelongsToMany
  131. {
  132. return $this->belongsToMany(File::class, 'order_photo');
  133. }
  134. public function documents(): BelongsToMany
  135. {
  136. return $this->belongsToMany(File::class, 'order_document');
  137. }
  138. public function statements(): BelongsToMany
  139. {
  140. return $this->belongsToMany(File::class, 'order_statement');
  141. }
  142. public function reclamations(): HasMany
  143. {
  144. return $this->hasMany(Reclamation::class);
  145. }
  146. public function getNeeds(): array
  147. {
  148. $needs = [];
  149. foreach ($this->products_sku as $sku) {
  150. if($sku->maf_order) continue;
  151. $needs[$sku->product_id]['needs'] = (isset($needs[$sku->product_id])) ? $needs[$sku->product_id]['needs'] + 1 : 1;
  152. }
  153. foreach ($needs as $productId => $quantity) {
  154. $needs[$productId]['sku'] = MafOrder::query()
  155. ->where('maf_orders.product_id', $productId)
  156. ->sum('maf_orders.in_stock');
  157. }
  158. return $needs;
  159. }
  160. public function recalculateReadyToMount(): void
  161. {
  162. $result = true;
  163. foreach ($this->getNeeds() as $need) {
  164. if($need['sku'] < $need['needs']) {
  165. $result = false;
  166. break;
  167. }
  168. }
  169. $this->update(['ready_to_mount' => ($result) ? 'Да' : 'Нет']);
  170. }
  171. public function autoChangeStatus(): void
  172. {
  173. if(($this->products_sku->count() < 1)
  174. && ($this->order_status_id !== self::STATUS_NEW)
  175. ) {
  176. $this->update(['order_status_id' => self::STATUS_NO_MAF]);
  177. return;
  178. }
  179. if(($this->order_status_id === self::STATUS_READY_TO_MOUNT)
  180. && ($this->brigadier_id !== null)
  181. && ($this->installation_date !== null)
  182. ) {
  183. $allMafConnected = true;
  184. foreach ($this->products_sku as $sku) {
  185. if($sku->maf_order) continue;
  186. $allMafConnected = false;
  187. }
  188. if($allMafConnected) {
  189. $this->update(['order_status_id' => self::STATUS_IN_MOUNT]);
  190. }
  191. }
  192. }
  193. public function commonName(): Attribute
  194. {
  195. return Attribute::make(
  196. get: fn($value) => (string) $this->object_address . ', ' . $this->area->name . ', ' . $this->district->shortname,
  197. );
  198. }
  199. public function productsWithCount(): Attribute
  200. {
  201. $products = $this->products_sku;
  202. $ret = [];
  203. foreach ($products as $product) {
  204. if(isset($ret[$product->product->id])) {
  205. $ret[$product->product->id]['count'] += 1;
  206. } else {
  207. $ret[$product->product->id] = [
  208. 'name' => $product->product->article,
  209. 'count' => 1,
  210. ];
  211. }
  212. if($product->maf_order?->order_number) {
  213. $ret[$product->product->id]['order_numbers'][] = $product->maf_order?->order_number;
  214. }
  215. }
  216. $s = '';
  217. $openTag = '<div>';
  218. $closeTag = '</div>';
  219. foreach ($ret as $product) {
  220. $order_numbers = (isset($product['order_numbers'])) ? ' (' . implode(', ', array_unique($product['order_numbers'])) . ')' : '';
  221. $s .= $openTag . $product['name'] . ' - ' . $product['count'] . $order_numbers . $closeTag;
  222. }
  223. return Attribute::make(
  224. get: fn($value) => (string) $s,
  225. );
  226. }
  227. public function isAllMafConnected(): bool
  228. {
  229. foreach($this->products_sku as $sku) {
  230. if($sku->maf_order_id) continue;
  231. return false;
  232. }
  233. return true;
  234. }
  235. public function canCreateHandover(): bool
  236. {
  237. if($this->photos->count() === 0) {
  238. return false;
  239. }
  240. foreach($this->products_sku as $sku) {
  241. if($sku->maf_order_id === null) return false;
  242. if($sku->passport_id === null) return false;
  243. if($sku->rfid === null) return false;
  244. if($sku->manufacture_date === null) return false;
  245. if($sku->factory_number === null) return false;
  246. if($sku->product->passport_name === null) return false;
  247. if($sku->product->statement_name === null) return false;
  248. if($sku->product->certificate_id === null) return false;
  249. if($sku->product->service_life === null) return false;
  250. if($sku->product->certificate_number === null) return false;
  251. if($sku->product->certificate_date === null) return false;
  252. if($sku->product->certificate_issuer === null) return false;
  253. if($sku->product->certificate_type === null) return false;
  254. }
  255. return true;
  256. }
  257. }