100 lines
2.5 KiB
PHP
100 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Concerns\HasModuleMedia;
|
|
use App\Models\Concerns\InteractsWithActivityLog;
|
|
use Illuminate\Database\Eloquent\Attributes\Appends;
|
|
use Illuminate\Database\Eloquent\Attributes\Guarded;
|
|
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\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Spatie\MediaLibrary\HasMedia;
|
|
|
|
#[Guarded(['id'])]
|
|
#[Appends(['stock_formatted', 'reject_stock_formatted', 'retail_stock_formatted'])]
|
|
class ProductVariant extends Model implements HasMedia
|
|
{
|
|
// 1. Use Trait
|
|
use HasFactory, HasModuleMedia, InteractsWithActivityLog, SoftDeletes;
|
|
|
|
private const MIN_STOCK = 5;
|
|
|
|
// 2. Casting
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'reject_stock' => 'integer',
|
|
'stock' => 'integer',
|
|
'retail_stock' => 'integer',
|
|
];
|
|
}
|
|
|
|
// 4. Attribute
|
|
public function rejectStockFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => number_format($this->reject_stock, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
public function stockFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => number_format($this->stock, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
public function retailStockFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => number_format($this->retail_stock, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
// 5. Other Methods
|
|
public static function mediaModuleName(): string
|
|
{
|
|
return 'product';
|
|
}
|
|
|
|
public static function minStock(): int
|
|
{
|
|
return self::MIN_STOCK;
|
|
}
|
|
|
|
public function registerMediaCollections(): void
|
|
{
|
|
$this->addMediaCollection('images');
|
|
}
|
|
|
|
// 6. Relation
|
|
public function cuttingResultPrices(): HasMany
|
|
{
|
|
return $this->hasMany(CuttingResultPrice::class);
|
|
}
|
|
|
|
public function cuttingResults(): HasMany
|
|
{
|
|
return $this->hasMany(CuttingResult::class);
|
|
}
|
|
|
|
public function orderItems(): HasMany
|
|
{
|
|
return $this->hasMany(OrderItem::class);
|
|
}
|
|
|
|
public function prices(): HasMany
|
|
{
|
|
return $this->hasMany(ProductPrice::class, 'variant_id');
|
|
}
|
|
|
|
public function product(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Product::class);
|
|
}
|
|
}
|