Order.php 2.9 KB

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