162 lines
4.4 KiB
PHP
162 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\PriceType;
|
|
use Illuminate\Database\Eloquent\Attributes\Appends;
|
|
use Illuminate\Database\Eloquent\Attributes\Guarded;
|
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
|
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\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Spatie\Activitylog\LogOptions;
|
|
use Spatie\Activitylog\Models\Activity;
|
|
use Spatie\Activitylog\Traits\LogsActivity;
|
|
use Spatie\MediaLibrary\HasMedia;
|
|
use Spatie\MediaLibrary\InteractsWithMedia;
|
|
use Spatie\Sluggable\HasSlug;
|
|
use Spatie\Sluggable\SlugOptions;
|
|
|
|
#[Guarded(['id'])]
|
|
#[Appends(['thumbnail_url', 'images_data', 'retail_price'])]
|
|
class Product extends Model implements HasMedia
|
|
{
|
|
use HasFactory, HasSlug, InteractsWithMedia, LogsActivity, SoftDeletes;
|
|
|
|
protected function retailPrice(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->prices->where('price_type', PriceType::RETAIL)->first()?->price ?: 0,
|
|
);
|
|
}
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'is_active' => 'boolean',
|
|
'stock' => 'integer',
|
|
];
|
|
}
|
|
|
|
#[Scope]
|
|
protected function active(Builder $query): void
|
|
{
|
|
$query->where('is_active', true);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function inactive(Builder $query): void
|
|
{
|
|
$query->where('is_active', false);
|
|
}
|
|
|
|
public function getSlugOptions(): SlugOptions
|
|
{
|
|
return SlugOptions::create()
|
|
->generateSlugsFrom('name')
|
|
->saveSlugsTo('slug');
|
|
}
|
|
|
|
public function registerMediaCollections(): void
|
|
{
|
|
$this->addMediaCollection('thumbnail')
|
|
->singleFile();
|
|
|
|
$this->addMediaCollection('images');
|
|
}
|
|
|
|
protected function thumbnailUrl(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->getFirstMediaUrl('thumbnail') ?: null,
|
|
);
|
|
}
|
|
|
|
protected function imagesData(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->getMedia('images')->map(fn ($media) => [
|
|
'id' => $media->id,
|
|
'url' => $media->getUrl(),
|
|
])->toArray(),
|
|
);
|
|
}
|
|
|
|
public function categories(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Category::class);
|
|
}
|
|
|
|
public function prices(): HasMany
|
|
{
|
|
return $this->hasMany(ProductPrice::class);
|
|
}
|
|
|
|
public function orderItems(): HasMany
|
|
{
|
|
return $this->hasMany(OrderItem::class);
|
|
}
|
|
|
|
public function purchaseItems(): HasMany
|
|
{
|
|
return $this->hasMany(PurchaseItem::class);
|
|
}
|
|
|
|
public function getActivitylogOptions(): LogOptions
|
|
{
|
|
return LogOptions::defaults()
|
|
->logOnly(['name', 'description', 'stock', 'is_active'])
|
|
->logOnlyDirty()
|
|
->useLogName('Produk');
|
|
}
|
|
|
|
public function tapActivity(Activity $activity, string $eventName)
|
|
{
|
|
$activity->description = match ($eventName) {
|
|
'created' => 'TAMBAH',
|
|
'updated' => 'UBAH',
|
|
'deleted' => 'HAPUS',
|
|
default => $activity->description,
|
|
};
|
|
|
|
if (isset($activity->properties['attributes'])) {
|
|
$attributeMap = [
|
|
'name' => 'Nama',
|
|
'description' => 'Deskripsi',
|
|
'stock' => 'Stok',
|
|
'is_active' => 'Status',
|
|
];
|
|
|
|
$properties = $activity->properties->toArray();
|
|
|
|
$localizeValues = function ($attrs) use ($attributeMap) {
|
|
$newAttrs = [];
|
|
foreach ($attrs as $key => $value) {
|
|
$label = $attributeMap[$key] ?? $key;
|
|
$displayValue = $value;
|
|
|
|
if ($key === 'is_active') {
|
|
$displayValue = $value ? 'Aktif' : 'Tidak Aktif';
|
|
}
|
|
|
|
$newAttrs[$label] = $displayValue;
|
|
}
|
|
|
|
return $newAttrs;
|
|
};
|
|
|
|
$properties['attributes'] = $localizeValues($properties['attributes']);
|
|
|
|
if (isset($properties['old'])) {
|
|
$properties['old'] = $localizeValues($properties['old']);
|
|
}
|
|
|
|
$activity->properties = collect($properties);
|
|
}
|
|
}
|
|
}
|