141 lines
4.0 KiB
PHP
141 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Carbon\Carbon;
|
|
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\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Spatie\Activitylog\LogOptions;
|
|
use Spatie\Activitylog\Models\Activity;
|
|
use Spatie\Activitylog\Traits\LogsActivity;
|
|
|
|
#[Guarded(['id'])]
|
|
#[Appends([
|
|
'base_salary_formatted',
|
|
'bonus_formatted',
|
|
'deduction_formatted',
|
|
'total_salary_formatted',
|
|
'period_month_formatted',
|
|
])]
|
|
class Payroll extends Model
|
|
{
|
|
use HasFactory, LogsActivity, SoftDeletes;
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'base_salary' => 'integer',
|
|
'bonus' => 'integer',
|
|
'deduction' => 'integer',
|
|
'total_salary' => 'integer',
|
|
'is_paid' => 'boolean',
|
|
];
|
|
}
|
|
|
|
protected function baseSalaryFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => 'Rp '.number_format($this->base_salary, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
protected function bonusFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => 'Rp '.number_format($this->bonus, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
protected function deductionFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => 'Rp '.number_format($this->deduction, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
protected function totalSalaryFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => 'Rp '.number_format($this->total_salary, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
protected function periodMonthFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->period_month ? Carbon::parse($this->period_month)->translatedFormat('F Y') : null,
|
|
);
|
|
}
|
|
|
|
public function adjustments(): HasMany
|
|
{
|
|
return $this->hasMany(PayrollAdjustment::class);
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function getActivitylogOptions(): LogOptions
|
|
{
|
|
return LogOptions::defaults()
|
|
->logOnly(['base_salary', 'bonus', 'deduction', 'total_salary', 'is_paid', 'period_month'])
|
|
->logOnlyDirty()
|
|
->useLogName('Penggajian');
|
|
}
|
|
|
|
public function tapActivity(Activity $activity, string $eventName)
|
|
{
|
|
$activity->description = match ($eventName) {
|
|
'created' => 'TAMBAH',
|
|
'updated' => 'UBAH',
|
|
'deleted' => 'HAPUS',
|
|
default => $activity->description,
|
|
};
|
|
|
|
if (isset($activity->properties['attributes'])) {
|
|
$attributeMap = [
|
|
'base_salary' => 'Gaji Pokok',
|
|
'bonus' => 'Bonus',
|
|
'deduction' => 'Potongan',
|
|
'total_salary' => 'Total Gaji',
|
|
'is_paid' => 'Status Pembayaran',
|
|
'period_month' => 'Periode',
|
|
];
|
|
|
|
$properties = $activity->properties->toArray();
|
|
|
|
$localizeValues = function ($attrs) use ($attributeMap) {
|
|
$newAttrs = [];
|
|
foreach ($attrs as $key => $value) {
|
|
$label = $attributeMap[$key] ?? $key;
|
|
$displayValue = $value;
|
|
|
|
if ($key === 'is_paid') {
|
|
$displayValue = $value ? 'Dibayar' : 'Belum Dibayar';
|
|
}
|
|
|
|
$newAttrs[$label] = $displayValue;
|
|
}
|
|
|
|
return $newAttrs;
|
|
};
|
|
|
|
$properties['attributes'] = $localizeValues($properties['attributes']);
|
|
|
|
if (isset($properties['old'])) {
|
|
$properties['old'] = $localizeValues($properties['old']);
|
|
}
|
|
|
|
$activity->properties = collect($properties);
|
|
}
|
|
}
|
|
}
|