Notifikasi
+{{ $notification->created_at->diffForHumans() }}
+Belum ada notifikasi
+ @endforelse +diff --git a/app/Http/Controllers/NotificationController.php b/app/Http/Controllers/NotificationController.php new file mode 100644 index 0000000..d1184eb --- /dev/null +++ b/app/Http/Controllers/NotificationController.php @@ -0,0 +1,16 @@ +user()->profile->cabang->notifications; + Notification::where('to', auth()->user()->profile->cabang->id)->update(['is_read' => true]); + return view('notification.index', compact('notifications')); + } +} diff --git a/app/Http/Controllers/Rotasi/PengajuanController.php b/app/Http/Controllers/Rotasi/PengajuanController.php index c077e74..8e5114b 100644 --- a/app/Http/Controllers/Rotasi/PengajuanController.php +++ b/app/Http/Controllers/Rotasi/PengajuanController.php @@ -55,7 +55,7 @@ public function input(Request $request) if (!auth()->user()->profile->cabang_id) { return redirect()->back()->with('invalid', 'Anda tidak memiliki cabang')->withInput(); } - if ($request->lokasi_awal_id !== auth()->user()->profile->cabang_id) { + if ($request->lokasi_awal_id != auth()->user()->profile->cabang_id) { return redirect()->back()->with('invalid', 'Cabang asal tidak sesuai')->withInput(); } } diff --git a/app/Http/Controllers/Rotasi/SelektifAdminController.php b/app/Http/Controllers/Rotasi/SelektifAdminController.php index 0f34c4a..cbc8fc8 100644 --- a/app/Http/Controllers/Rotasi/SelektifAdminController.php +++ b/app/Http/Controllers/Rotasi/SelektifAdminController.php @@ -5,6 +5,7 @@ use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Models\Cabang; +use App\Models\Notification; use App\Models\Pengajuan; use App\Models\Personel; use Carbon\Carbon; @@ -65,12 +66,30 @@ public function selektif($id, Request $request) $request->validate([ 'status' => 'required|in:dapat,tidak,diterima' ]); + DB::beginTransaction(); $pengajuan = Pengajuan::find($id); if (!$pengajuan) { return response()->json(['message' => 'Data not found'], 404); } + $notification = Notification::create([ + 'status' => $request->status, + 'to' => $pengajuan->lokasi_awal_id, + 'cabang_asal_id' => $pengajuan->lokasi_awal_id, + 'cabang_tujuan_id' => $pengajuan->lokasi_tujuan_id, + 'pengajuan_id' => $pengajuan->id + ]); + $notification->save(); + if ($pengajuan->lokasi_awal_id != $pengajuan->lokasi_tujuan_id) { + $notification = Notification::create([ + 'status' => $request->status, + 'to' => $pengajuan->lokasi_tujuan_id, + 'cabang_asal_id' => $pengajuan->lokasi_awal_id, + 'cabang_tujuan_id' => $pengajuan->lokasi_tujuan_id, + 'pengajuan_id' => $pengajuan->id + ]); + $notification->save(); + } if ($request->status == 'dapat') { - DB::beginTransaction(); $personel = Personel::where('nik', $pengajuan->nik)->first(); if (!$personel) { $personel = new Personel(); @@ -97,11 +116,11 @@ public function selektif($id, Request $request) $pengajuan->primary->status = 'tidak_dapat'; $pengajuan->primary->save(); } - if ($pengajuan->primary->secondary) { + if ($pengajuan->primary && $pengajuan->primary->secondary && $pengajuan->primary->secondary_id != $pengajuan->id) { $pengajuan->primary->secondary->status = 'tidak_dapat'; $pengajuan->primary->secondary->save(); } - if ($pengajuan->primary->th3) { + if ($pengajuan->primary && $pengajuan->primary->th3 && $pengajuan->primary->th3_id != $pengajuan->id) { $pengajuan->primary->th3->status = 'tidak_dapat'; $pengajuan->primary->th3->save(); } @@ -114,7 +133,6 @@ public function selektif($id, Request $request) 'keterangan' => 'required', 'rekomendasi' => 'required' ]); - DB::beginTransaction(); $pengajuan->status = 'tidak_dapat'; $pengajuan->keteranganPenolakan()->create([ 'tipe' => 'keterangan_penolakan', @@ -128,7 +146,6 @@ public function selektif($id, Request $request) DB::commit(); return redirect()->back()->with('success', 'Data berhasil diupdate'); } else if ($request->status == 'diterima') { - DB::beginTransaction(); $pengajuan->status = 'diterima'; if ($pengajuan->posisi_sekarang == "ACO") { $pengajuan->lokasiAwal->jumlah_personel_aco -= 1; diff --git a/app/Models/Cabang.php b/app/Models/Cabang.php index d44d9e5..c16d0f6 100644 --- a/app/Models/Cabang.php +++ b/app/Models/Cabang.php @@ -32,6 +32,19 @@ class Cabang extends Model 'frms_ats_system', ]; + public function notifications() + { + return $this->hasMany(Notification::class, 'to'); + } + + public function notreadnotifications() + { + return $this->hasMany( + Notification::class, + 'to' + )->where("is_read", false); + } + public function coord() { return $this->hasOne(CabangCoord::class); diff --git a/app/Models/Notification.php b/app/Models/Notification.php new file mode 100644 index 0000000..525406d --- /dev/null +++ b/app/Models/Notification.php @@ -0,0 +1,35 @@ +belongsTo(User::class); + } + + public function pengajuan() + { + return $this->belongsTo(Pengajuan::class); + } + + public function notread() + { + return $this->where("is_read", false)->get(); + } +} diff --git a/database/migrations/2024_10_30_104942_create_notifications_table.php b/database/migrations/2024_10_30_104942_create_notifications_table.php new file mode 100644 index 0000000..7a7a59f --- /dev/null +++ b/database/migrations/2024_10_30_104942_create_notifications_table.php @@ -0,0 +1,33 @@ +id(); + $table->string("status"); + $table->foreignId("to")->constrained("cabangs")->onDelete("cascade"); + $table->boolean("is_read")->default(false); + $table->foreignId("cabang_asal_id")->constrained("cabangs")->onDelete("cascade"); + $table->foreignId("cabang_tujuan_id")->constrained("cabangs")->onDelete("cascade"); + $table->foreignId("pengajuan_id")->constrained("pengajuans")->onDelete("cascade"); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('notifications'); + } +}; diff --git a/resources/views/components/header.blade.php b/resources/views/components/header.blade.php index cd51e1e..38d229e 100644 --- a/resources/views/components/header.blade.php +++ b/resources/views/components/header.blade.php @@ -17,6 +17,14 @@ class="max-h-0 duration-300 absolute flex flex-col gap-1 bg-white mt-4 text-gray
{{ $notification->created_at->diffForHumans() }}
+Belum ada notifikasi
+ @endforelse +