Order.php 9.1 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. ];
  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 reclamations(): HasMany
  142. {
  143. return $this->hasMany(Reclamation::class);
  144. }
  145. public function getNeeds(): array
  146. {
  147. $needs = [];
  148. foreach ($this->products_sku as $sku) {
  149. if($sku->maf_order) continue;
  150. $needs[$sku->product_id]['needs'] = (isset($needs[$sku->product_id])) ? $needs[$sku->product_id]['needs'] + 1 : 1;
  151. }
  152. foreach ($needs as $productId => $quantity) {
  153. $needs[$productId]['sku'] = MafOrder::query()
  154. ->where('maf_orders.product_id', $productId)
  155. ->sum('maf_orders.in_stock');
  156. }
  157. return $needs;
  158. }
  159. public function recalculateReadyToMount(): void
  160. {
  161. $result = true;
  162. foreach ($this->getNeeds() as $need) {
  163. if($need['sku'] < $need['needs']) {
  164. $result = false;
  165. break;
  166. }
  167. }
  168. $this->update(['ready_to_mount' => ($result) ? 'Да' : 'Нет']);
  169. }
  170. public function autoChangeStatus(): void
  171. {
  172. if(($this->products_sku->count() < 1)
  173. && ($this->order_status_id !== self::STATUS_NEW)
  174. ) {
  175. $this->update(['order_status_id' => self::STATUS_NO_MAF]);
  176. return;
  177. }
  178. if(($this->order_status_id === self::STATUS_READY_TO_MOUNT)
  179. && ($this->brigadier_id !== null)
  180. && ($this->installation_date !== null)
  181. ) {
  182. $allMafConnected = true;
  183. foreach ($this->products_sku as $sku) {
  184. if($sku->maf_order) continue;
  185. $allMafConnected = false;
  186. }
  187. if($allMafConnected) {
  188. $this->update(['order_status_id' => self::STATUS_IN_MOUNT]);
  189. }
  190. }
  191. }
  192. public function commonName(): Attribute
  193. {
  194. return Attribute::make(
  195. get: fn($value) => (string) $this->object_address . ', ' . $this->area->name . ', ' . $this->district->shortname,
  196. );
  197. }
  198. public function productsWithCount(): Attribute
  199. {
  200. $products = $this->products_sku;
  201. $ret = [];
  202. foreach ($products as $product) {
  203. if(isset($ret[$product->product->id])) {
  204. $ret[$product->product->id]['count'] += 1;
  205. } else {
  206. $ret[$product->product->id] = [
  207. 'name' => $product->product->article,
  208. 'count' => 1,
  209. ];
  210. }
  211. if($product->maf_order?->order_number) {
  212. $ret[$product->product->id]['order_numbers'][] = $product->maf_order?->order_number;
  213. }
  214. }
  215. $s = '';
  216. $openTag = '<div>';
  217. $closeTag = '</div>';
  218. foreach ($ret as $product) {
  219. $order_numbers = (isset($product['order_numbers'])) ? ' (' . implode(', ', array_unique($product['order_numbers'])) . ')' : '';
  220. $s .= $openTag . $product['name'] . ' - ' . $product['count'] . $order_numbers . $closeTag;
  221. }
  222. return Attribute::make(
  223. get: fn($value) => (string) $s,
  224. );
  225. }
  226. public function isAllMafConnected(): bool
  227. {
  228. foreach($this->products_sku as $sku) {
  229. if($sku->maf_order_id) continue;
  230. return false;
  231. }
  232. return true;
  233. }
  234. public function canCreateHandover(): bool
  235. {
  236. if($this->photos->count() === 0) {
  237. return false;
  238. }
  239. foreach($this->products_sku as $sku) {
  240. if($sku->maf_order_id === null) return false;
  241. if($sku->passport_id === null) return false;
  242. if($sku->rfid === null) return false;
  243. if($sku->manufacture_date === null) return false;
  244. if($sku->factory_number === null) return false;
  245. if($sku->product->passport_name === null) return false;
  246. if($sku->product->statement_name === null) return false;
  247. if($sku->product->certificate_id === null) return false;
  248. if($sku->product->service_life === null) return false;
  249. if($sku->product->certificate_number === null) return false;
  250. if($sku->product->certificate_date === null) return false;
  251. if($sku->product->certificate_issuer === null) return false;
  252. if($sku->product->certificate_type === null) return false;
  253. }
  254. return true;
  255. }
  256. }