dstpabuaran.com/app/Models/Order.php
Yoga Pangestu 5961a60585 feat: add labels and formatting attributes to various models and enums
- Added labels for leave request statuses in LeaveRequestStatus enum.
- Introduced label methods for PayrollAdjustmentType, PayrollPeriodStatus, PayrollStatus, RawMaterialUnit, and StokOpnameStatus enums.
- Implemented formatted attributes for Attendance, CashAccount, CashTransaction, Category, Customer, Cutting, Employee, EmployeeAdvance, Expense, LeaveRequest, Order, OrderItem, Payroll, PayrollAdjustment, PayrollPeriod, Product, ProductPrice, ProductVariant, Purchase, PurchaseItem, RawMaterial, RawMaterialPrice, Restock, RestockItem, StokOpname, StokOpnameItem, Supplier, User, and UserProfile models.
- Enhanced phone number formatting for Customer and Supplier models.
- Added date formatting for various models to improve readability.
2026-08-04 15:31:41 +07:00

204 lines
5.2 KiB
PHP

<?php
namespace App\Models;
use App\Enums\OrderChannel;
use App\Enums\OrderStatus;
use App\Enums\PaymentType;
use App\Enums\PriceType;
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;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
#[Appends(['channel_label', 'formatted_cogs', 'formatted_discount', 'formatted_nego_price', 'payment_type_label', 'status_label', 'formatted_subtotal', 'formatted_total_amount'])]
#[Guarded(['id'])]
class Order extends Model implements HasMedia
{
use HasFactory, InteractsWithMedia, SoftDeletes;
protected function casts(): array
{
return [
'channel' => OrderChannel::class,
'price_type' => PriceType::class,
'status' => OrderStatus::class,
'payment_type' => PaymentType::class,
'is_affiliate' => 'boolean',
'subtotal' => 'integer',
'discount' => 'integer',
'nego_price' => 'integer',
'total_amount' => 'integer',
'cogs' => 'integer',
'marketplace_settings_snapshot' => 'array',
];
}
protected function channelLabel(): Attribute
{
return Attribute::make(
get: fn () => $this->channel->label(),
);
}
protected function formattedCogs(): Attribute
{
return Attribute::make(
get: fn () => 'Rp ' . number_format($this->cogs, 0, ',', '.'),
);
}
protected function formattedDiscount(): Attribute
{
return Attribute::make(
get: fn () => 'Rp ' . number_format($this->discount, 0, ',', '.'),
);
}
protected function formattedNegoPrice(): Attribute
{
return Attribute::make(
get: fn () => 'Rp ' . number_format($this->nego_price, 0, ',', '.'),
);
}
protected function paymentTypeLabel(): Attribute
{
return Attribute::make(
get: fn () => $this->payment_type->label(),
);
}
protected function statusLabel(): Attribute
{
return Attribute::make(
get: fn () => $this->status->label(),
);
}
protected function formattedSubtotal(): Attribute
{
return Attribute::make(
get: fn () => 'Rp ' . number_format($this->subtotal, 0, ',', '.'),
);
}
protected function formattedTotalAmount(): Attribute
{
return Attribute::make(
get: fn () => 'Rp ' . number_format($this->total_amount, 0, ',', '.'),
);
}
#[Scope]
protected function cancelled(Builder $query): void
{
$query->where('status', OrderStatus::CANCELLED);
}
#[Scope]
protected function cash(Builder $query): void
{
$query->where('payment_type', PaymentType::CASH);
}
#[Scope]
protected function completed(Builder $query): void
{
$query->where('status', OrderStatus::COMPLETED);
}
#[Scope]
protected function pending(Builder $query): void
{
$query->where('status', OrderStatus::PENDING);
}
#[Scope]
protected function processing(Builder $query): void
{
$query->where('status', OrderStatus::PROCESSING);
}
#[Scope]
protected function qris(Builder $query): void
{
$query->where('payment_type', PaymentType::QRIS);
}
#[Scope]
protected function refunded(Builder $query): void
{
$query->where('status', OrderStatus::REFUNDED);
}
#[Scope]
protected function retail(Builder $query): void
{
$query->where('price_type', PriceType::RETAIL);
}
#[Scope]
protected function shopee(Builder $query): void
{
$query->where('channel', OrderChannel::SHOPEE);
}
#[Scope]
protected function store(Builder $query): void
{
$query->where('channel', OrderChannel::STORE);
}
#[Scope]
protected function tiktok(Builder $query): void
{
$query->where('channel', OrderChannel::TIKTOK);
}
#[Scope]
protected function transfer(Builder $query): void
{
$query->where('payment_type', PaymentType::TRANSFER);
}
#[Scope]
protected function wholesale(Builder $query): void
{
$query->where('price_type', PriceType::WHOLESALE);
}
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 marketing(): BelongsTo
{
return $this->belongsTo(User::class, 'marketing_id');
}
public function orderItems(): HasMany
{
return $this->hasMany(OrderItem::class);
}
}