dress/app/Models/Order.php

148 lines
4.5 KiB
PHP

<?php
namespace App\Models;
use App\Enums\OrderChannel;
use App\Enums\OrderStatus;
use App\Enums\PaymentMethod;
use App\Observers\OrderObserver;
use Illuminate\Database\Eloquent\Attributes\Appends;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
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\Activitylog\LogOptions;
use Spatie\Activitylog\Models\Activity;
use Spatie\Activitylog\Traits\LogsActivity;
#[Guarded(['id'])]
#[Appends(['cogs_formatted', 'subtotal_formatted', 'discount_formatted', 'payment_formatted', 'total_formatted', 'created_at_formatted'])]
#[ObservedBy([OrderObserver::class])]
class Order extends Model
{
use HasFactory, LogsActivity, SoftDeletes;
protected function casts(): array
{
return [
'cogs' => 'integer',
'subtotal' => 'integer',
'discount' => 'integer',
'payment' => 'integer',
'total' => 'integer',
'payment_method' => PaymentMethod::class,
'order_status' => OrderStatus::class,
'order_channel' => OrderChannel::class,
];
}
protected function cogsFormatted(): Attribute
{
return Attribute::make(
get: fn() => 'Rp ' . number_format($this->cogs, 0, ',', '.'),
);
}
protected function subtotalFormatted(): Attribute
{
return Attribute::make(
get: fn() => 'Rp ' . number_format($this->subtotal, 0, ',', '.'),
);
}
protected function discountFormatted(): Attribute
{
return Attribute::make(
get: fn() => 'Rp ' . number_format($this->discount, 0, ',', '.'),
);
}
protected function paymentFormatted(): Attribute
{
return Attribute::make(
get: fn() => 'Rp ' . number_format($this->payment, 0, ',', '.'),
);
}
protected function totalFormatted(): Attribute
{
return Attribute::make(
get: fn() => 'Rp ' . number_format($this->total, 0, ',', '.'),
);
}
protected function createdAtFormatted(): Attribute
{
return Attribute::make(
get: fn() => $this->created_at->translatedFormat('d F Y H:i'),
);
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function items(): HasMany
{
return $this->hasMany(OrderItem::class);
}
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logOnly(['order_number', 'customer_name', 'cogs', 'subtotal', 'discount', 'payment', 'total', 'payment_method', 'order_status', 'order_channel'])
->logOnlyDirty()
->useLogName('Pesanan');
}
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 = [
'order_number' => 'Nomor Order',
'customer_name' => 'Nama Pelanggan',
'cogs' => 'Modal',
'subtotal' => 'Subtotal',
'discount' => 'Diskon',
'payment' => 'Bayar',
'total' => 'Total',
'payment_method' => 'Metode Pembayaran',
'order_status' => 'Status Pesanan',
'order_channel' => 'Saluran Pesanan',
];
$properties = $activity->properties->toArray();
$localizeValues = function ($attrs) use ($attributeMap) {
$newAttrs = [];
foreach ($attrs as $key => $value) {
$label = $attributeMap[$key] ?? $key;
$newAttrs[$label] = $value;
}
return $newAttrs;
};
$properties['attributes'] = $localizeValues($properties['attributes']);
if (isset($properties['old'])) {
$properties['old'] = $localizeValues($properties['old']);
}
$activity->properties = collect($properties);
}
}
}