| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace App\Models;
- use App\Helpers\Price;
- use Illuminate\Database\Eloquent\Casts\Attribute;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Illuminate\Support\Facades\DB;
- class Product extends Model
- {
- use SoftDeletes;
- const DEFAULT_SORT_BY = 'article';
- protected $fillable = [
- 'year',
- 'name_tz',
- 'type_tz',
- 'nomenclature_number',
- 'sizes',
- 'manufacturer',
- 'unit',
- 'type',
- 'price_status',
- 'product_price',
- 'installation_price',
- 'service_price',
- 'total_price',
- 'manufacturer_name',
- 'article',
- 'note',
- ];
- protected $appends = ['product_price', 'installation_price', 'service_price', 'total_price',
- 'product_price_txt', 'installation_price_txt', 'service_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 servicePrice(): 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 servicePriceTxt(): Attribute
- {
- return Attribute::make(
- get: fn($value) => Price::format($this->service_price),
- );
- }
- protected function totalPriceTxt(): Attribute
- {
- return Attribute::make(
- get: fn($value) => Price::format($this->total_price),
- );
- }
- /**
- * @param $column
- * @return array
- */
- public static function getFilters($column): array
- {
- return DB::table('products')
- ->select($column)
- ->distinct()
- ->get()
- ->pluck($column)
- ->toArray();
- }
- }
|