- Removed unnecessary ordering by 'created_at' in variant loading across multiple services and models. - Implemented global scopes for ordering product variants and raw material prices by 'name' and 'variant', respectively. - Enhanced code readability and maintainability by simplifying query structures in ProductController, OrderService, and others.
109 lines
2.8 KiB
PHP
109 lines
2.8 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\Builder;
|
|
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\Relations\MorphMany;
|
|
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;
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::addGlobalScope('ordered', function (Builder $query): void {
|
|
$query->orderBy('name');
|
|
});
|
|
}
|
|
|
|
// 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 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)->withTrashed();
|
|
}
|
|
|
|
public function stockMutations(): MorphMany
|
|
{
|
|
return $this->morphMany(StockMutation::class, 'stockable');
|
|
}
|
|
}
|