47 lines
1.5 KiB
PHP
47 lines
1.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Observers;
|
||
|
|
|
||
|
|
use App\Models\ActivityLog;
|
||
|
|
use App\Models\CashRecord;
|
||
|
|
use Illuminate\Support\Facades\Auth;
|
||
|
|
|
||
|
|
class CashRecordObserver
|
||
|
|
{
|
||
|
|
public function created(CashRecord $record): void
|
||
|
|
{
|
||
|
|
ActivityLog::create([
|
||
|
|
'user_id' => Auth::id(),
|
||
|
|
'action' => 'created',
|
||
|
|
'model_type' => CashRecord::class,
|
||
|
|
'model_id' => $record->id,
|
||
|
|
'description' => "Transaksi kas baru: {$record->description} sebesar Rp " . number_format($record->amount, 0, ',', '.'),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function updated(CashRecord $record): void
|
||
|
|
{
|
||
|
|
// Setelah diverifikasi, tidak bisa diubah lagi
|
||
|
|
if ($record->getOriginal('verified_at') !== null) {
|
||
|
|
throw new \Exception('Transaksi yang sudah diverifikasi tidak dapat diubah.');
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($record->wasChanged('verified_by') && $record->verified_by !== null) {
|
||
|
|
ActivityLog::create([
|
||
|
|
'user_id' => Auth::id(),
|
||
|
|
'action' => 'verified',
|
||
|
|
'model_type' => CashRecord::class,
|
||
|
|
'model_id' => $record->id,
|
||
|
|
'description' => "Transaksi kas diverifikasi: {$record->description}",
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function deleting(CashRecord $record): void
|
||
|
|
{
|
||
|
|
if ($record->verified_at !== null) {
|
||
|
|
throw new \Exception('Transaksi yang sudah diverifikasi tidak dapat dihapus.');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|