Order.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 $appends = ['common_name'];
  39. public function products_sku(): HasMany
  40. {
  41. return $this->hasMany(ProductSKU::class, 'order_id', 'id');
  42. }
  43. public function products(): HasManyThrough
  44. {
  45. return $this->hasManyThrough(Product::class, ProductSKU::class, 'order_id', 'id');
  46. }
  47. public function maf_orders(): BelongsTo
  48. {
  49. return $this->belongsTo(MafOrder::class, 'maf_order_id');
  50. }
  51. public function user(): BelongsTo
  52. {
  53. return $this->belongsTo(User::class, 'user_id', 'id');
  54. }
  55. public function district(): BelongsTo
  56. {
  57. return $this->belongsTo(District::class);
  58. }
  59. public function area(): BelongsTo
  60. {
  61. return $this->belongsTo(Area::class);
  62. }
  63. public function objectType(): BelongsTo
  64. {
  65. return $this->belongsTo(ObjectType::class);
  66. }
  67. public function brigadier(): BelongsTo
  68. {
  69. return $this->belongsTo(User::class, 'brigadier_id', 'id');
  70. }
  71. public function orderStatus(): BelongsTo
  72. {
  73. return $this->belongsTo(OrderStatus::class);
  74. }
  75. public function getNeeds(): array
  76. {
  77. $needs = [];
  78. foreach ($this->products_sku as $sku) {
  79. $needs[$sku->product_id]['needs'] = (isset($needs[$sku->product_id])) ? $needs[$sku->product_id]['needs'] + 1 : 1;
  80. }
  81. foreach ($needs as $productId => $quantity) {
  82. $needs[$productId]['sku'] = MafOrder::query()
  83. ->where('maf_orders.product_id', $productId)
  84. ->sum('maf_orders.in_stock');
  85. }
  86. return $needs;
  87. }
  88. public function recalculateReadyToMount(): void
  89. {
  90. $result = true;
  91. foreach ($this->getNeeds() as $need) {
  92. if($need['sku'] < $need['needs']) {
  93. $result = false;
  94. break;
  95. }
  96. }
  97. if($this->order_status_id > self::STATUS_NEW) {
  98. $result = true;
  99. }
  100. $this->update(['ready_to_mount' => ($result) ? 'Да' : 'Нет']);
  101. }
  102. public function commonName(): Attribute
  103. {
  104. return Attribute::make(
  105. get: fn($value) => (string) $this->district->shortname . ', ' . $this->area->name . ', ' . $this->object_address,
  106. );
  107. }
  108. }