90 lines
2.4 KiB
PHP
90 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Concerns;
|
|
|
|
use App\Models\FormHistory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
trait LogsFormHistory
|
|
{
|
|
private function logCreated(Model $model, string $module, array $newValues): void
|
|
{
|
|
$this->createFormHistory(
|
|
module: $module,
|
|
event: 'created',
|
|
description: "{$module} ditambahkan",
|
|
newValues: $newValues,
|
|
);
|
|
}
|
|
|
|
private function logUpdated(Model $model, string $module, array $oldValues, array $newValues): void
|
|
{
|
|
$this->createFormHistory(
|
|
module: $module,
|
|
event: 'updated',
|
|
description: "{$module} diperbarui",
|
|
newValues: $newValues,
|
|
oldValues: $oldValues,
|
|
);
|
|
}
|
|
|
|
private function logDeleted(Model $model, string $module, array $oldValues): void
|
|
{
|
|
$this->createFormHistory(
|
|
module: $module,
|
|
event: 'deleted',
|
|
description: "{$module} dihapus",
|
|
oldValues: $oldValues,
|
|
);
|
|
}
|
|
|
|
private function createFormHistory(
|
|
string $module,
|
|
string $event,
|
|
string $description,
|
|
array $newValues = [],
|
|
array $oldValues = [],
|
|
): void {
|
|
$attributeChanges = array_filter([
|
|
'new' => $newValues ?: null,
|
|
'old' => $oldValues ?: null,
|
|
]);
|
|
|
|
FormHistory::create([
|
|
'causer_id' => Auth::id(),
|
|
'module' => $module,
|
|
'event' => $event,
|
|
'description' => $description,
|
|
'attribute_changes' => $attributeChanges ?: null,
|
|
]);
|
|
}
|
|
|
|
private function formatCurrency(int $value): string
|
|
{
|
|
return 'Rp ' . number_format($value, 0, ',', '.');
|
|
}
|
|
|
|
private function formatDate(?string $date, string $format = 'l, d F Y'): ?string
|
|
{
|
|
return $date ? \Carbon\Carbon::parse($date)->translatedFormat($format) : null;
|
|
}
|
|
|
|
private function formatDateTime(?string $datetime): ?string
|
|
{
|
|
return $datetime ? \Carbon\Carbon::parse($datetime)->translatedFormat('l, d F Y H:i') : null;
|
|
}
|
|
|
|
private function formatBoolean(?bool $value): ?string
|
|
{
|
|
return $value === null ? null : ($value ? 'Ya' : 'Tidak');
|
|
}
|
|
|
|
private function resolveRelation(Model $model, string $relation, string $attribute): ?string
|
|
{
|
|
$related = $model->{$relation};
|
|
|
|
return $related?->{$attribute} ?? null;
|
|
}
|
|
}
|