- 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.
89 lines
2.1 KiB
PHP
89 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\RawMaterialUnit;
|
|
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\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Spatie\Activitylog\Support\LogOptions;
|
|
use Spatie\Activitylog\Models\Concerns\LogsActivity;
|
|
|
|
#[Appends(['formatted_name', 'unit_label'])]
|
|
#[Guarded(['id'])]
|
|
class RawMaterial extends Model
|
|
{
|
|
use HasFactory, LogsActivity, SoftDeletes;
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'unit' => RawMaterialUnit::class,
|
|
'is_active' => 'boolean',
|
|
];
|
|
}
|
|
|
|
public function getActivitylogOptions(): LogOptions
|
|
{
|
|
return LogOptions::defaults()
|
|
->logAll()
|
|
->logOnlyDirty()
|
|
->dontLogEmptyChanges();
|
|
}
|
|
|
|
protected function formattedName(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => ucfirst($this->name),
|
|
);
|
|
}
|
|
|
|
protected function unitLabel(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->unit->label(),
|
|
);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function active(Builder $query): void
|
|
{
|
|
$query->where('is_active', true);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function nonactive(Builder $query): void
|
|
{
|
|
$query->where('is_active', false);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function kg(Builder $query): void
|
|
{
|
|
$query->where('unit', RawMaterialUnit::KG);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function meter(Builder $query): void
|
|
{
|
|
$query->where('unit', RawMaterialUnit::METER);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function yard(Builder $query): void
|
|
{
|
|
$query->where('unit', RawMaterialUnit::YARD);
|
|
}
|
|
|
|
public function rawMaterialPrices(): HasMany
|
|
{
|
|
return $this->hasMany(RawMaterialPrice::class);
|
|
}
|
|
}
|