2026-04-03 04:34:21 +07:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Observers;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Models\ActivityLog;
|
2026-04-03 05:02:33 +07:00
|
|
|
|
use App\Models\Approval;
|
2026-04-03 04:34:21 +07:00
|
|
|
|
use App\Models\CashRecord;
|
2026-04-03 05:02:33 +07:00
|
|
|
|
use App\Models\Vote;
|
2026-04-03 08:12:12 +07:00
|
|
|
|
use App\Services\NotificationService;
|
2026-04-03 04:34:21 +07:00
|
|
|
|
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, ',', '.'),
|
|
|
|
|
|
]);
|
2026-04-03 05:02:33 +07:00
|
|
|
|
|
2026-04-03 08:12:12 +07:00
|
|
|
|
// Threshold: 500rb–2jt → buat approval ketua + notif
|
2026-04-03 05:02:33 +07:00
|
|
|
|
if ($record->amount >= 500_000 && $record->amount <= 2_000_000) {
|
2026-04-03 08:12:12 +07:00
|
|
|
|
$approval = Approval::create([
|
2026-04-03 05:02:33 +07:00
|
|
|
|
'model_type' => CashRecord::class,
|
|
|
|
|
|
'model_id' => $record->id,
|
|
|
|
|
|
'required_approvals' => 1,
|
|
|
|
|
|
'status' => 'pending',
|
|
|
|
|
|
]);
|
2026-04-03 08:12:12 +07:00
|
|
|
|
|
|
|
|
|
|
NotificationService::toRole('ketua',
|
|
|
|
|
|
'Transaksi Kas Butuh Persetujuan',
|
|
|
|
|
|
"Transaksi \"{$record->description}\" senilai Rp " . number_format($record->amount, 0, ',', '.') . " menunggu persetujuan Anda.",
|
|
|
|
|
|
'warning',
|
|
|
|
|
|
route('filament.admin.resources.cash-records.index')
|
|
|
|
|
|
);
|
2026-04-03 05:02:33 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-03 08:12:12 +07:00
|
|
|
|
// Threshold: > 2jt → buat voting + notif semua anggota
|
2026-04-03 05:02:33 +07:00
|
|
|
|
if ($record->amount > 2_000_000) {
|
|
|
|
|
|
Vote::create([
|
|
|
|
|
|
'title' => "Persetujuan Transaksi: {$record->description}",
|
|
|
|
|
|
'description' => "Transaksi senilai Rp " . number_format($record->amount, 0, ',', '.') . " memerlukan persetujuan voting.",
|
|
|
|
|
|
'type' => 'finance',
|
|
|
|
|
|
'related_id' => $record->id,
|
|
|
|
|
|
'status' => 'open',
|
|
|
|
|
|
'deadline' => now()->addDays(3),
|
2026-04-03 11:31:12 +07:00
|
|
|
|
'created_by' => Auth::id() ?? $record->created_by,
|
2026-04-03 05:02:33 +07:00
|
|
|
|
]);
|
2026-04-03 08:12:12 +07:00
|
|
|
|
|
|
|
|
|
|
NotificationService::toRole('ketua',
|
|
|
|
|
|
'Voting Transaksi Besar Dibuat',
|
|
|
|
|
|
"Transaksi \"{$record->description}\" senilai Rp " . number_format($record->amount, 0, ',', '.') . " memerlukan voting.",
|
|
|
|
|
|
'warning',
|
|
|
|
|
|
route('filament.admin.resources.votes.index')
|
|
|
|
|
|
);
|
2026-04-03 05:02:33 +07:00
|
|
|
|
}
|
2026-04-03 04:34:21 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function updated(CashRecord $record): void
|
|
|
|
|
|
{
|
|
|
|
|
|
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) {
|
2026-04-03 08:51:47 +07:00
|
|
|
|
\Filament\Notifications\Notification::make()
|
|
|
|
|
|
->title('Tidak dapat dihapus')
|
|
|
|
|
|
->body('Transaksi yang sudah diverifikasi tidak dapat dihapus.')
|
|
|
|
|
|
->danger()
|
|
|
|
|
|
->send();
|
|
|
|
|
|
|
|
|
|
|
|
abort(403);
|
2026-04-03 04:34:21 +07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|