store/app/Models/ProductPrice.php

53 lines
1.2 KiB
PHP

<?php
namespace App\Models;
use App\Enums\PriceType;
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\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
#[Guarded(['id'])]
#[Appends(['price_formatted', 'price_input', 'type_label'])]
class ProductPrice extends Model
{
use InteractsWithActivityLog;
protected function casts(): array
{
return [
'type' => PriceType::class,
'price' => 'decimal:2',
];
}
public function priceFormatted(): Attribute
{
return Attribute::make(
get: fn () => 'Rp '.number_format((float) $this->price, 0, ',', '.'),
);
}
public function priceInput(): Attribute
{
return Attribute::make(
get: fn () => (string) (int) $this->price,
);
}
public function typeLabel(): Attribute
{
return Attribute::make(
get: fn () => $this->type->label(),
);
}
public function variant(): BelongsTo
{
return $this->belongsTo(ProductVariant::class, 'variant_id');
}
}