fix: null check budget, cegah duplikasi approval/vote, tambah related_type di votes
This commit is contained in:
@@ -4,7 +4,6 @@ namespace App\Models;
|
|||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use App\Models\Activity;
|
|
||||||
|
|
||||||
class CashRecord extends Model
|
class CashRecord extends Model
|
||||||
{
|
{
|
||||||
|
|||||||
+1
-1
@@ -10,7 +10,7 @@ class Vote extends Model
|
|||||||
{
|
{
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'title', 'description', 'type',
|
'title', 'description', 'type',
|
||||||
'related_id', 'status', 'deadline', 'created_by',
|
'related_id', 'related_type', 'status', 'deadline', 'created_by',
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
|
|||||||
@@ -47,23 +47,29 @@ class ActivityObserver
|
|||||||
|
|
||||||
// Threshold budget
|
// Threshold budget
|
||||||
$budget = $activity->budget;
|
$budget = $activity->budget;
|
||||||
if ($budget >= 500_000 && $budget <= 2_000_000) {
|
if ($budget !== null && $budget >= 500_000 && $budget <= 2_000_000) {
|
||||||
Approval::create([
|
Approval::firstOrCreate(
|
||||||
'model_type' => Activity::class,
|
['model_type' => Activity::class, 'model_id' => $activity->id],
|
||||||
'model_id' => $activity->id,
|
['required_approvals' => 1, 'status' => 'pending']
|
||||||
'required_approvals' => 1,
|
);
|
||||||
'status' => 'pending',
|
} elseif ($budget !== null && $budget > 2_000_000) {
|
||||||
]);
|
$exists = Vote::where('related_type', Activity::class)
|
||||||
} elseif ($budget > 2_000_000) {
|
->where('related_id', $activity->id)
|
||||||
Vote::create([
|
->where('type', 'finance')
|
||||||
'title' => "Persetujuan Budget Kegiatan: {$activity->title}",
|
->exists();
|
||||||
'description' => "Budget kegiatan senilai Rp " . number_format($budget, 0, ',', '.') . " memerlukan persetujuan voting.",
|
|
||||||
'type' => 'finance',
|
if (! $exists) {
|
||||||
'related_id' => $activity->id,
|
Vote::create([
|
||||||
'status' => 'open',
|
'title' => "Persetujuan Budget Kegiatan: {$activity->title}",
|
||||||
'deadline' => now()->addDays(3),
|
'description' => "Budget kegiatan senilai Rp " . number_format($budget, 0, ',', '.') . " memerlukan persetujuan voting.",
|
||||||
'created_by' => Auth::id() ?? $activity->created_by,
|
'type' => 'finance',
|
||||||
]);
|
'related_id' => $activity->id,
|
||||||
|
'related_type' => Activity::class,
|
||||||
|
'status' => 'open',
|
||||||
|
'deadline' => now()->addDays(3),
|
||||||
|
'created_by' => Auth::id() ?? $activity->created_by,
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,13 +41,14 @@ class CashRecordObserver
|
|||||||
// Threshold: > 2jt → buat voting + notif semua anggota
|
// Threshold: > 2jt → buat voting + notif semua anggota
|
||||||
if ($record->amount > 2_000_000) {
|
if ($record->amount > 2_000_000) {
|
||||||
Vote::create([
|
Vote::create([
|
||||||
'title' => "Persetujuan Transaksi: {$record->description}",
|
'title' => "Persetujuan Transaksi: {$record->description}",
|
||||||
'description' => "Transaksi senilai Rp " . number_format($record->amount, 0, ',', '.') . " memerlukan persetujuan voting.",
|
'description' => "Transaksi senilai Rp " . number_format($record->amount, 0, ',', '.') . " memerlukan persetujuan voting.",
|
||||||
'type' => 'finance',
|
'type' => 'finance',
|
||||||
'related_id' => $record->id,
|
'related_id' => $record->id,
|
||||||
'status' => 'open',
|
'related_type' => CashRecord::class,
|
||||||
'deadline' => now()->addDays(3),
|
'status' => 'open',
|
||||||
'created_by' => Auth::id() ?? $record->created_by,
|
'deadline' => now()->addDays(3),
|
||||||
|
'created_by' => Auth::id() ?? $record->created_by,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
NotificationService::toRole('ketua',
|
NotificationService::toRole('ketua',
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('votes', function (Blueprint $table) {
|
||||||
|
$table->string('related_type')->nullable()->after('related_id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('votes', function (Blueprint $table) {
|
||||||
|
$table->dropColumn('related_type');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user