store/app/Models/Order.php

122 lines
3.0 KiB
PHP

<?php
namespace App\Models;
use App\Enums\OrderChannel;
use App\Enums\OrderStatus;
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;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
#[Guarded(['id'])]
#[Appends([
'subtotal_formatted',
'discount_formatted',
'marketplace_fee_formatted',
'net_amount_formatted',
'created_at_formatted',
'channel_label',
'price_type_label',
'status_label',
])]
class Order extends Model
{
use InteractsWithActivityLog;
use SoftDeletes;
protected function casts(): array
{
return [
'channel' => OrderChannel::class,
'price_type' => PriceType::class,
'status' => OrderStatus::class,
'subtotal' => 'integer',
'discount' => 'integer',
'marketplace_fee' => 'integer',
'net_amount' => 'integer',
];
}
public function subtotalFormatted(): Attribute
{
return Attribute::make(
get: fn () => 'Rp '.number_format($this->subtotal, 0, ',', '.'),
);
}
public function discountFormatted(): Attribute
{
return Attribute::make(
get: fn () => 'Rp '.number_format($this->discount, 0, ',', '.'),
);
}
public function marketplaceFeeFormatted(): Attribute
{
return Attribute::make(
get: fn () => 'Rp '.number_format($this->marketplace_fee, 0, ',', '.'),
);
}
public function netAmountFormatted(): Attribute
{
return Attribute::make(
get: fn () => 'Rp '.number_format($this->net_amount, 0, ',', '.'),
);
}
public function createdAtFormatted(): Attribute
{
return Attribute::make(
get: fn () => $this->created_at?->translatedFormat('l, d F Y H:i'),
);
}
public function channelLabel(): Attribute
{
return Attribute::make(
get: fn () => $this->channel->label(),
);
}
public function priceTypeLabel(): Attribute
{
return Attribute::make(
get: fn () => $this->price_type->label(),
);
}
public function statusLabel(): Attribute
{
return Attribute::make(
get: fn () => $this->status->label(),
);
}
public function customer(): BelongsTo
{
return $this->belongsTo(Customer::class);
}
public function items(): HasMany
{
return $this->hasMany(OrderItem::class);
}
public function cashTransaction(): BelongsTo
{
return $this->belongsTo(CashTransaction::class);
}
public function createdBy(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by_id');
}
}