- 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.
73 lines
1.9 KiB
PHP
73 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\InvoiceStatus;
|
|
use Illuminate\Database\Eloquent\Attributes\Appends;
|
|
use Illuminate\Database\Eloquent\Attributes\Guarded;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Spatie\Activitylog\Support\LogOptions;
|
|
use Spatie\Activitylog\Models\Concerns\LogsActivity;
|
|
|
|
#[Appends(['formatted_amount', 'formatted_date', 'formatted_due_date', 'status_label'])]
|
|
#[Guarded(['id'])]
|
|
class Invoice extends Model
|
|
{
|
|
use HasFactory, LogsActivity, SoftDeletes;
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'amount' => 'integer',
|
|
'date' => 'date:Y-m-d',
|
|
'due_date' => 'date:Y-m-d',
|
|
'status' => InvoiceStatus::class,
|
|
];
|
|
}
|
|
|
|
public function getActivitylogOptions(): LogOptions
|
|
{
|
|
return LogOptions::defaults()
|
|
->logAll()
|
|
->logOnlyDirty()
|
|
->dontLogEmptyChanges();
|
|
}
|
|
|
|
protected function formattedAmount(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => 'Rp '.number_format($this->amount, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
protected function formattedDate(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->date?->translatedFormat('l, d F Y'),
|
|
);
|
|
}
|
|
|
|
protected function formattedDueDate(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->due_date?->translatedFormat('l, d F Y'),
|
|
);
|
|
}
|
|
|
|
protected function statusLabel(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->status->label(),
|
|
);
|
|
}
|
|
|
|
public function items(): HasMany
|
|
{
|
|
return $this->hasMany(InvoiceItem::class);
|
|
}
|
|
}
|