61 lines
1.5 KiB
PHP
61 lines
1.5 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\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
#[Guarded(['id'])]
|
|
#[Appends(['price_formatted', 'cost_per_unit_formatted', 'type_label'])]
|
|
class CuttingResultPrice extends Model
|
|
{
|
|
use HasFactory;
|
|
use InteractsWithActivityLog;
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'price_type' => PriceType::class,
|
|
'price' => 'integer',
|
|
'cost_per_unit' => 'integer',
|
|
];
|
|
}
|
|
|
|
public function cutting(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Cutting::class);
|
|
}
|
|
|
|
public function productVariant(): BelongsTo
|
|
{
|
|
return $this->belongsTo(ProductVariant::class);
|
|
}
|
|
|
|
public function priceFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => 'Rp '.number_format($this->price, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
public function costPerUnitFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => 'Rp '.number_format($this->cost_per_unit, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
public function typeLabel(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->price_type->label(),
|
|
);
|
|
}
|
|
}
|