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 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
|
|
|
|
|
|
|
|
|
|
// Threshold: 500rb–2jt → buat approval ketua
|
|
|
|
|
|
if ($record->amount >= 500_000 && $record->amount <= 2_000_000) {
|
|
|
|
|
|
Approval::create([
|
|
|
|
|
|
'model_type' => CashRecord::class,
|
|
|
|
|
|
'model_id' => $record->id,
|
|
|
|
|
|
'required_approvals' => 1,
|
|
|
|
|
|
'status' => 'pending',
|
|
|
|
|
|
]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Threshold: > 2jt → buat voting
|
|
|
|
|
|
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),
|
|
|
|
|
|
'created_by' => Auth::id(),
|
|
|
|
|
|
]);
|
|
|
|
|
|
}
|
2026-04-03 04:34:21 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function updated(CashRecord $record): void
|
|
|
|
|
|
{
|
|
|
|
|
|
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) {
|
2026-04-03 05:02:33 +07:00
|
|
|
|
// Pastikan threshold terpenuhi sebelum verifikasi
|
|
|
|
|
|
if ($record->amount >= 500_000 && $record->amount <= 2_000_000) {
|
|
|
|
|
|
$approval = Approval::where('model_type', CashRecord::class)
|
|
|
|
|
|
->where('model_id', $record->id)
|
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
|
|
if (! $approval || $approval->status !== 'approved') {
|
|
|
|
|
|
throw new \Exception('Transaksi ini memerlukan persetujuan ketua sebelum diverifikasi.');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ($record->amount > 2_000_000) {
|
|
|
|
|
|
$vote = Vote::where('type', 'finance')
|
|
|
|
|
|
->where('related_id', $record->id)
|
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
|
|
$total = $vote?->items()->count() ?? 0;
|
|
|
|
|
|
$approve = $vote?->items()->where('choice', 'approve')->count() ?? 0;
|
|
|
|
|
|
|
|
|
|
|
|
if (! $vote || $vote->status !== 'closed' || $total === 0 || ($approve / $total) <= 0.5) {
|
|
|
|
|
|
throw new \Exception('Transaksi ini memerlukan voting dengan mayoritas setuju sebelum diverifikasi.');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-03 04:34:21 +07:00
|
|
|
|
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.');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|