51 lines
1.5 KiB
PHP
51 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Product extends Model
|
|
{
|
|
protected $fillable = ['category_id','name','slug','description','base_price','sale_price','tax_rate','weight','active','featured','track_stock','sort_order','delivery_note'];
|
|
|
|
protected $casts = ['active' => 'boolean', 'featured' => 'boolean', 'track_stock' => 'boolean', 'base_price' => 'decimal:2', 'sale_price' => 'decimal:2', 'tax_rate' => 'decimal:2'];
|
|
|
|
public function isOnSale(): bool
|
|
{
|
|
return $this->sale_price !== null && (float)$this->sale_price < (float)$this->base_price;
|
|
}
|
|
|
|
public function getEffectivePriceAttribute(): float
|
|
{
|
|
return $this->isOnSale() ? (float)$this->sale_price : (float)$this->base_price;
|
|
}
|
|
|
|
public function category(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Category::class);
|
|
}
|
|
|
|
public function images(): HasMany
|
|
{
|
|
return $this->hasMany(ProductImage::class)->orderBy('sort_order');
|
|
}
|
|
|
|
public function variants(): HasMany
|
|
{
|
|
return $this->hasMany(ProductVariant::class);
|
|
}
|
|
|
|
/** Effektiver Bruttopreis (Basispreis, wenn keine Variante gewählt) */
|
|
public function getDisplayPriceAttribute(): float
|
|
{
|
|
return (float) $this->base_price;
|
|
}
|
|
|
|
public function getNetPriceAttribute(): float
|
|
{
|
|
return round($this->base_price / (1 + $this->tax_rate / 100), 2);
|
|
}
|
|
}
|