| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <?php
- declare(strict_types=1);
- namespace App\Models;
- use App\Helpers\Price;
- use Database\Factories\CommonCatalogItemFactory;
- use Illuminate\Database\Eloquent\Casts\Attribute;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- 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\HasOne;
- use Illuminate\Database\Eloquent\SoftDeletes;
- class CommonCatalogItem extends Model
- {
- /** @use HasFactory<CommonCatalogItemFactory> */
- 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)
- : '-',
- );
- }
- }
|