| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- namespace App\Models;
- use App\Helpers\Price;
- use App\Models\Dictionary\Area;
- use App\Models\Dictionary\District;
- use Illuminate\Database\Eloquent\Casts\Attribute;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\BelongsToMany;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use Illuminate\Database\Eloquent\Relations\HasManyThrough;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Illuminate\Support\Facades\DB;
- class Order extends Model
- {
- use SoftDeletes;
- const DEFAULT_SORT_BY = 'created_at';
- const STATUS_NEW = 1;
- const STATUS_READY_TO_MOUNT = 2;
- const STATUS_READY_TO_HAND_OVER = 3;
- const STATUS_HANDED_OVER = 4;
- protected $fillable = [
- 'user_id',
- 'district_id',
- 'area_id',
- 'object_address',
- 'object_type_id',
- 'contract_date',
- 'contract_number',
- 'comment',
- 'installation_date',
- 'brigadier_id',
- 'order_status_id',
- 'tg_group_name',
- 'tg_group_link',
- 'ready_to_mount',
- ];
- public $appends = ['common_name'];
- public function products_sku(): HasMany
- {
- return $this->hasMany(ProductSKU::class, 'order_id', 'id');
- }
- public function products(): HasManyThrough
- {
- return $this->hasManyThrough(Product::class, ProductSKU::class, 'order_id', 'id');
- }
- public function maf_orders(): BelongsTo
- {
- return $this->belongsTo(MafOrder::class, 'maf_order_id');
- }
- public function user(): BelongsTo
- {
- return $this->belongsTo(User::class, 'user_id', 'id');
- }
- public function district(): BelongsTo
- {
- return $this->belongsTo(District::class);
- }
- public function area(): BelongsTo
- {
- return $this->belongsTo(Area::class);
- }
- public function objectType(): BelongsTo
- {
- return $this->belongsTo(ObjectType::class);
- }
- public function brigadier(): BelongsTo
- {
- return $this->belongsTo(User::class, 'brigadier_id', 'id');
- }
- public function orderStatus(): BelongsTo
- {
- return $this->belongsTo(OrderStatus::class);
- }
- public function getNeeds(): array
- {
- $needs = [];
- foreach ($this->products_sku as $sku) {
- $needs[$sku->product_id]['needs'] = (isset($needs[$sku->product_id])) ? $needs[$sku->product_id]['needs'] + 1 : 1;
- }
- foreach ($needs as $productId => $quantity) {
- $needs[$productId]['sku'] = MafOrder::query()
- ->where('maf_orders.product_id', $productId)
- ->sum('maf_orders.in_stock');
- }
- return $needs;
- }
- public function recalculateReadyToMount(): void
- {
- $result = true;
- foreach ($this->getNeeds() as $need) {
- if($need['sku'] < $need['needs']) {
- $result = false;
- break;
- }
- }
- if($this->order_status_id > self::STATUS_NEW) {
- $result = true;
- }
- $this->update(['ready_to_mount' => ($result) ? 'Да' : 'Нет']);
- }
- public function commonName(): Attribute
- {
- return Attribute::make(
- get: fn($value) => (string) $this->district->shortname . ', ' . $this->area->name . ', ' . $this->object_address,
- );
- }
- }
|