| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace App\Models;
- use App\Helpers\Price;
- use App\Models\Scopes\YearScope;
- use Illuminate\Database\Eloquent\Attributes\ScopedBy;
- use Illuminate\Database\Eloquent\Casts\Attribute;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsToMany;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Illuminate\Support\Facades\DB;
- #[ScopedBy([YearScope::class])]
- class Product extends Model
- {
- use SoftDeletes;
- const DEFAULT_SORT_BY = 'article';
- protected $fillable = [
- 'year',
- 'article',
- 'name_tz',
- 'type_tz',
- 'nomenclature_number',
- 'sizes',
- 'manufacturer',
- 'unit',
- 'type',
- 'product_price',
- 'installation_price',
- 'total_price',
- 'manufacturer_name',
- 'note',
- ];
- protected $appends = ['product_price', 'installation_price', 'total_price',
- 'product_price_txt', 'installation_price_txt', 'total_price_txt',];
- protected function productPrice(): Attribute
- {
- return Attribute::make(
- get: fn(int $value) => (float) $value / 100,
- set: fn (float $value) => (int) ($value * 100),
- );
- }
- protected function installationPrice(): Attribute
- {
- return Attribute::make(
- get: fn(int $value) => (float) $value / 100,
- set: fn (float $value) => (int) ($value * 100),
- );
- }
- protected function totalPrice(): Attribute
- {
- return Attribute::make(
- get: fn(int $value) => (float) $value / 100,
- set: fn (float $value) => (int) ($value * 100),
- );
- }
- public function productPriceTxt(): Attribute
- {
- return Attribute::make(
- get: fn($value) => Price::format($this->product_price),
- );
- }
- public function installationPriceTxt(): Attribute
- {
- return Attribute::make(
- get: fn($value) => Price::format($this->installation_price),
- );
- }
- protected function totalPriceTxt(): Attribute
- {
- return Attribute::make(
- get: fn($value) => Price::format($this->total_price),
- );
- }
- public function commonName(): Attribute
- {
- return Attribute::make(
- get: fn($value) => $this->article . ', №' . $this->nomenclature_number,
- // get: fn($value) => (string) $this->name_tz . ' (арт.' . $this->article . ', №' . $this->nomenclature_number . ', ' . $this->year . 'г, ' . $this->product_price_txt . ')',
- );
- }
- /**
- * @return BelongsToMany
- */
- public function orders(): BelongsToMany
- {
- return $this->belongsToMany(Order::class, 'products_sku', 'order_id', 'product_id');
- }
- }
|