*/ use HasFactory; use SoftDeletes; public const DEFAULT_SORT_BY = 'article'; protected $fillable = [ 'image_file_id', 'article', 'calculator_name', 'kind', 'dimension_length', 'dimension_width', 'dimension_height', 'site_length', 'site_width', 'dimensions', 'fall_height', 'additional_info', 'dimension_unit', 'weight', 'volume', 'places', 'composition', 'age_group', 'max_users', 'unit', 'series', 'trademark', 'calculator_enabled', 'builders_price', 'wholesale_price', 'recommended_price', 'retail_price', 'project_price', 'project_with_installation_price', 'pik_price', 'recommended_plus_10_price', 'note', ]; protected $appends = [ 'image', 'calculator_enabled_txt', 'builders_price_txt', 'wholesale_price_txt', 'recommended_price_txt', 'retail_price_txt', 'project_price_txt', 'project_with_installation_price_txt', 'pik_price_txt', 'recommended_plus_10_price_txt', ]; protected function casts(): array { return [ 'fall_height' => 'decimal:3', 'weight' => 'decimal:3', 'volume' => 'decimal:3', 'places' => 'integer', 'max_users' => 'integer', 'calculator_enabled' => 'boolean', ]; } protected function buildersPrice(): Attribute { return $this->priceAttribute(); } protected function wholesalePrice(): Attribute { return $this->priceAttribute(); } protected function recommendedPrice(): Attribute { return $this->priceAttribute(); } protected function retailPrice(): Attribute { return $this->priceAttribute(); } protected function projectPrice(): Attribute { return $this->priceAttribute(); } protected function projectWithInstallationPrice(): Attribute { return $this->priceAttribute(); } protected function pikPrice(): Attribute { return $this->priceAttribute(); } protected function recommendedPlus10Price(): Attribute { return $this->priceAttribute(); } protected function calculatorEnabledTxt(): Attribute { return Attribute::make( get: fn (): string => match ($this->calculator_enabled) { true => 'да', false => 'нет', null => '', }, ); } protected function buildersPriceTxt(): Attribute { return $this->formattedPriceAttribute('builders_price'); } protected function wholesalePriceTxt(): Attribute { return $this->formattedPriceAttribute('wholesale_price'); } protected function recommendedPriceTxt(): Attribute { return $this->formattedPriceAttribute('recommended_price'); } protected function retailPriceTxt(): Attribute { return $this->formattedPriceAttribute('retail_price'); } protected function projectPriceTxt(): Attribute { return $this->formattedPriceAttribute('project_price'); } protected function projectWithInstallationPriceTxt(): Attribute { return $this->formattedPriceAttribute('project_with_installation_price'); } protected function pikPriceTxt(): Attribute { return $this->formattedPriceAttribute('pik_price'); } protected function recommendedPlus10PriceTxt(): Attribute { return $this->formattedPriceAttribute('recommended_plus_10_price'); } public function imageFile(): BelongsTo { return $this->belongsTo(File::class, 'image_file_id'); } public function documents(): BelongsToMany { return $this->belongsToMany( File::class, 'common_catalog_item_documents', )->withTimestamps(); } public function stockOrders(): HasMany { return $this->hasMany(StockOrder::class); } public function latestStockOrder(): HasOne { return $this->hasOne(StockOrder::class)->latestOfMany(); } public function stockReservations(): HasMany { return $this->hasMany(StockReservation::class); } public function stockMovements(): HasMany { return $this->hasMany(StockMovement::class); } protected function image(): Attribute { return Attribute::make( get: fn (): string => (string) ($this->imageFile?->thumbnail_link ?? ''), ); } private function priceAttribute(): Attribute { return Attribute::make( get: fn (mixed $value): ?float => $value === null ? null : (int) $value / 100, set: fn (mixed $value): ?int => $value === null || $value === '' ? null : (int) round((float) $value * 100), ); } private function formattedPriceAttribute(string $field): Attribute { return Attribute::make( get: fn (): string => isset($this->attributes[$field]) && $this->attributes[$field] !== null ? Price::format((int) $this->attributes[$field] / 100) : '-', ); } }