154 lines
3.9 KiB
PHP
154 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\OrderChannel;
|
|
use App\Enums\OrderStatus;
|
|
use App\Enums\PaymentType;
|
|
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\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\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
#[Guarded(['id'])]
|
|
#[Appends([
|
|
'subtotal_formatted',
|
|
'discount_formatted',
|
|
'total_amount_formatted',
|
|
'created_at_formatted',
|
|
'channel_label',
|
|
'payment_type_label',
|
|
'price_type_label',
|
|
'status_label',
|
|
'creator_name',
|
|
'marketing_name',
|
|
])]
|
|
class Order extends Model
|
|
{
|
|
use HasFactory, InteractsWithActivityLog, SoftDeletes;
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'channel' => OrderChannel::class,
|
|
'price_type' => PriceType::class,
|
|
'payment_type' => PaymentType::class,
|
|
'is_affiliate' => 'boolean',
|
|
'status' => OrderStatus::class,
|
|
'subtotal' => 'integer',
|
|
'discount' => 'integer',
|
|
'marketplace_settings_snapshot' => 'array',
|
|
'total_amount' => 'integer',
|
|
];
|
|
}
|
|
|
|
public function cashTransaction(): BelongsTo
|
|
{
|
|
return $this->belongsTo(CashTransaction::class);
|
|
}
|
|
|
|
public function createdBy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'created_by_id');
|
|
}
|
|
|
|
public function customer(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Customer::class);
|
|
}
|
|
|
|
public function items(): HasMany
|
|
{
|
|
return $this->hasMany(OrderItem::class);
|
|
}
|
|
|
|
public function marketing(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'marketing_id');
|
|
}
|
|
|
|
public function channelLabel(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->channel->label(),
|
|
);
|
|
}
|
|
|
|
public function createdAtFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->created_at?->translatedFormat('l, d F Y H:i'),
|
|
);
|
|
}
|
|
|
|
public function discountFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => 'Rp '.number_format($this->discount, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
public function totalAmountFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => 'Rp '.number_format($this->total_amount, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
public function paymentTypeLabel(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->payment_type->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 creatorName(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->createdBy?->profile?->full_name ?? $this->createdBy?->username ?? '-',
|
|
);
|
|
}
|
|
|
|
public function marketingName(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->marketing?->profile?->full_name ?? $this->marketing?->username ?? '-',
|
|
);
|
|
}
|
|
|
|
public function subtotalFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => 'Rp '.number_format($this->subtotal, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
#[Scope]
|
|
public function completed(Builder $query): void
|
|
{
|
|
$query->where('status', OrderStatus::COMPLETED);
|
|
}
|
|
}
|