dstpabuaran.com/app/Models/Order.php
Yoga Pangestu 91913633de feat: integrate Spatie Activitylog for activity tracking across models
- Added activity logging functionality to multiple models including CuttingMaterial, Employee, Invoice, and more.
- Implemented `getActivitylogOptions` method to configure logging behavior.
- Updated composer dependencies to include `spatie/laravel-activitylog`.
- Created a custom Activity model extending Spatie's Activity model.
- Added configuration file for activity logging settings.
2026-08-27 23:04:56 +07:00

225 lines
5.8 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\Image\Enums\Fit;
use Spatie\Activitylog\Support\LogOptions;
use Spatie\Activitylog\Models\Concerns\LogsActivity;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
#[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, LogsActivity, SoftDeletes;
protected function casts(): array
{
return [
'channel' => OrderChannel::class,
'price_type' => PriceType::class,
'status' => OrderStatus::class,
'payment_type' => PaymentType::class,
'subtotal' => 'integer',
'discount' => 'integer',
'nego_price' => 'integer',
'total_amount' => 'integer',
'cogs' => 'integer',
];
}
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logAll()
->logOnlyDirty()
->dontLogEmptyChanges();
}
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);
}
public function registerMediaCollections(): void
{
$this->addMediaCollection('photos');
}
public function registerMediaConversions(?Media $media = null): void
{
$this->addMediaConversion('thumb')
->fit(Fit::Contain, 150, 150);
}
}