Order.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 Illuminate\Database\Eloquent\Casts\Attribute;
  7. use Illuminate\Database\Eloquent\Model;
  8. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  9. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  10. use Illuminate\Database\Eloquent\Relations\HasMany;
  11. use Illuminate\Database\Eloquent\Relations\HasManyThrough;
  12. use Illuminate\Database\Eloquent\SoftDeletes;
  13. use Illuminate\Support\Facades\DB;
  14. class Order extends Model
  15. {
  16. use SoftDeletes;
  17. const DEFAULT_SORT_BY = 'created_at';
  18. const STATUS_NEW = 1;
  19. const STATUS_READY_TO_MOUNT = 2;
  20. const STATUS_READY_TO_HAND_OVER = 3;
  21. const STATUS_HANDED_OVER = 4;
  22. protected $fillable = [
  23. 'user_id',
  24. 'district_id',
  25. 'area_id',
  26. 'object_address',
  27. 'object_type_id',
  28. 'contract_date',
  29. 'contract_number',
  30. 'comment',
  31. 'installation_date',
  32. 'brigadier_id',
  33. 'order_status_id',
  34. 'tg_group_name',
  35. 'tg_group_link',
  36. 'ready_to_mount',
  37. ];
  38. public function products_sku(): HasMany
  39. {
  40. return $this->hasMany(ProductSKU::class, 'order_id', 'id');
  41. }
  42. public function products(): HasManyThrough
  43. {
  44. return $this->hasManyThrough(Product::class, ProductSKU::class, 'order_id', 'id');
  45. }
  46. public function maf_orders(): BelongsTo
  47. {
  48. return $this->belongsTo(MafOrder::class, 'maf_order_id');
  49. }
  50. public function user(): BelongsTo
  51. {
  52. return $this->belongsTo(User::class, 'user_id', 'id');
  53. }
  54. public function district(): BelongsTo
  55. {
  56. return $this->belongsTo(District::class);
  57. }
  58. public function area(): BelongsTo
  59. {
  60. return $this->belongsTo(Area::class);
  61. }
  62. public function objectType(): BelongsTo
  63. {
  64. return $this->belongsTo(ObjectType::class);
  65. }
  66. public function brigadier(): BelongsTo
  67. {
  68. return $this->belongsTo(User::class, 'brigadier_id', 'id');
  69. }
  70. public function orderStatus(): BelongsTo
  71. {
  72. return $this->belongsTo(OrderStatus::class);
  73. }
  74. public function getNeeds(): array
  75. {
  76. $needs = [];
  77. foreach ($this->products_sku as $sku) {
  78. $needs[$sku->product_id]['needs'] = (isset($needs[$sku->product_id])) ? $needs[$sku->product_id]['needs'] + 1 : 1;
  79. }
  80. foreach ($needs as $productId => $quantity) {
  81. $needs[$productId]['sku'] = MafOrder::query()
  82. ->where('maf_orders.product_id', $productId)
  83. ->sum('maf_orders.in_stock');
  84. }
  85. return $needs;
  86. }
  87. public function recalculateReadyToMount(): void
  88. {
  89. $result = true;
  90. foreach ($this->getNeeds() as $need) {
  91. if($need['sku'] < $need['needs']) {
  92. $result = false;
  93. break;
  94. }
  95. }
  96. if($this->order_status_id > self::STATUS_NEW) {
  97. $result = true;
  98. }
  99. $this->update(['ready_to_mount' => ($result) ? 'Да' : 'Нет']);
  100. }
  101. }