- 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.
126 lines
3.3 KiB
PHP
126 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\EmploymentStatus;
|
|
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\Activitylog\Support\LogOptions;
|
|
use Spatie\Activitylog\Models\Concerns\LogsActivity;
|
|
|
|
#[Appends(['formatted_base_salary', 'employment_status_label', 'formatted_join_date', 'formatted_resign_date'])]
|
|
#[Guarded(['id'])]
|
|
class Employee extends Model
|
|
{
|
|
use HasFactory, LogsActivity, SoftDeletes;
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'employment_status' => EmploymentStatus::class,
|
|
'base_salary' => 'integer',
|
|
'join_date' => 'date:Y-m-d',
|
|
'resign_date' => 'date:Y-m-d',
|
|
];
|
|
}
|
|
|
|
public function getActivitylogOptions(): LogOptions
|
|
{
|
|
return LogOptions::defaults()
|
|
->logAll()
|
|
->logOnlyDirty()
|
|
->dontLogEmptyChanges();
|
|
}
|
|
|
|
protected function formattedBaseSalary(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => 'Rp '.number_format($this->base_salary, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
protected function employmentStatusLabel(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->employment_status->label(),
|
|
);
|
|
}
|
|
|
|
protected function formattedJoinDate(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->join_date?->translatedFormat('l, d F Y'),
|
|
);
|
|
}
|
|
|
|
protected function formattedResignDate(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->resign_date?->translatedFormat('l, d F Y'),
|
|
);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function contract(Builder $query): void
|
|
{
|
|
$query->where('employment_status', EmploymentStatus::CONTRACT);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function fullTime(Builder $query): void
|
|
{
|
|
$query->where('employment_status', EmploymentStatus::FULL_TIME);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function internship(Builder $query): void
|
|
{
|
|
$query->where('employment_status', EmploymentStatus::INTERNSHIP);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function partTime(Builder $query): void
|
|
{
|
|
$query->where('employment_status', EmploymentStatus::PART_TIME);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function resigned(Builder $query): void
|
|
{
|
|
$query->where('employment_status', EmploymentStatus::RESIGNED);
|
|
}
|
|
|
|
public function attendances(): HasMany
|
|
{
|
|
return $this->hasMany(Attendance::class);
|
|
}
|
|
|
|
public function employeeAdvances(): HasMany
|
|
{
|
|
return $this->hasMany(EmployeeAdvance::class);
|
|
}
|
|
|
|
public function leaveRequests(): HasMany
|
|
{
|
|
return $this->hasMany(LeaveRequest::class);
|
|
}
|
|
|
|
public function payrolls(): HasMany
|
|
{
|
|
return $this->hasMany(Payroll::class);
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
}
|