diff --git a/spk_kontrakan/app/Http/Controllers/DashboardController.php b/spk_kontrakan/app/Http/Controllers/DashboardController.php index 1980316..9273e4a 100644 --- a/spk_kontrakan/app/Http/Controllers/DashboardController.php +++ b/spk_kontrakan/app/Http/Controllers/DashboardController.php @@ -8,6 +8,7 @@ use App\Models\Booking; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Auth; class DashboardController extends Controller { @@ -19,17 +20,22 @@ class DashboardController extends Controller */ public function index() { + $admin = auth('admin')->user() ?? Auth::user(); + $adminId = $admin->id; + $isSuperAdmin = $admin->role === 'super_admin'; + // ========== CACHE STATISTIK (5 menit) ========== - $stats = Cache::remember('dashboard_stats', 300, function () { + $stats = Cache::remember('dashboard_stats_admin_' . $adminId, 300, function () use ($adminId, $isSuperAdmin) { return [ - 'jumlahKontrakan' => Kontrakan::count(), - 'jumlahLaundry' => Laundry::count(), - 'jumlahKriteria' => Kriteria::count(), + 'jumlahKontrakan' => Kontrakan::where('admin_id', $adminId)->count(), + 'jumlahLaundry' => $isSuperAdmin ? Laundry::count() : 0, + 'jumlahKriteria' => $isSuperAdmin ? Kriteria::count() : 0, ]; }); // ========== DATA TERBARU (No Cache) ========== - $recentKontrakan = Kontrakan::latest() + $recentKontrakan = Kontrakan::where('admin_id', $adminId) + ->latest() ->select('id', 'nama', 'alamat', 'harga', 'jarak', 'jumlah_kamar', 'created_at') // Only needed columns ->take(5) ->get(); @@ -45,10 +51,10 @@ public function index() ->get(); // ========== DATA CHART (Cache 10 menit) ========== - $chartData = Cache::remember('dashboard_charts', 600, function () { + $chartData = Cache::remember('dashboard_charts_admin_' . $adminId, 600, function () use ($adminId, $isSuperAdmin) { // 1. Harga Kontrakan (Top 5) - $hargaKontrakan = Kontrakan::select('nama', 'harga') + $hargaKontrakan = Kontrakan::where('admin_id', $adminId)->select('nama', 'harga') ->orderBy('harga', 'desc') ->take(5) ->get(); @@ -67,6 +73,7 @@ public function index() // 3. Distribusi Jarak (Single Query dengan CASE) $jarakKontrakan = DB::table('kontrakans') + ->where('admin_id', $adminId) ->selectRaw(" SUM(CASE WHEN jarak <= 500 THEN 1 ELSE 0 END) as dekat, SUM(CASE WHEN jarak > 500 AND jarak <= 1000 THEN 1 ELSE 0 END) as sedang, @@ -84,6 +91,7 @@ public function index() // 4. Statistik Aggregate (Single Query) $kontrakanStats = DB::table('kontrakans') + ->where('admin_id', $adminId) ->selectRaw(' AVG(harga) as avg_harga, AVG(jarak) as avg_jarak, @@ -94,7 +102,7 @@ public function index() ->first(); // 5. Top Kontrakan by Harga - $topKontrakan = Kontrakan::select('nama', 'harga', 'jumlah_kamar', 'jarak') + $topKontrakan = Kontrakan::where('admin_id', $adminId)->select('nama', 'harga', 'jumlah_kamar', 'jarak') ->orderBy('harga', 'desc') ->take(5) ->get(); @@ -147,6 +155,7 @@ public function index() // ========== INSIGHT REAL-TIME (No Cache) ========== $realtimeJarakKontrakan = DB::table('kontrakans') + ->where('admin_id', $adminId) ->selectRaw(" SUM(CASE WHEN jarak <= 500 THEN 1 ELSE 0 END) as dekat, SUM(CASE WHEN jarak > 500 AND jarak <= 1000 THEN 1 ELSE 0 END) as sedang, @@ -155,6 +164,7 @@ public function index() ->first(); $realtimeKontrakanStats = DB::table('kontrakans') + ->where('admin_id', $adminId) ->selectRaw(' AVG(harga) as avg_harga, AVG(jarak) as avg_jarak, @@ -167,7 +177,17 @@ public function index() // ========== TAMBAHAN DATA YANG HILANG ========== $additionalData = [ // Data booking - 'totalBookings' => Booking::count() ?? 0, + 'totalBookings' => Booking::whereHas('kontrakan', function ($query) use ($adminId) { + $query->where('admin_id', $adminId); + })->count(), + + 'bookingMenunggu' => Booking::whereHas('kontrakan', function ($query) use ($adminId) { + $query->where('admin_id', $adminId); + })->whereRaw('LOWER(status) = ?', ['menunggu'])->count(), + + 'bookingSelesai' => Booking::whereHas('kontrakan', function ($query) use ($adminId) { + $query->where('admin_id', $adminId); + })->whereRaw('LOWER(status) = ?', ['selesai'])->count(), // Data admin (dari tabel admins) 'totalAdmins' => \App\Models\Admin::where('role', 'admin')->count() ?? 1, @@ -190,6 +210,13 @@ public function index() 'avgKamarKontrakan' => $realtimeKontrakanStats->avg_kamar ?? 0, 'minHargaKontrakan' => $realtimeKontrakanStats->min_harga ?? 0, 'maxHargaKontrakan' => $realtimeKontrakanStats->max_harga ?? 0, + 'recentBookings' => Booking::whereHas('kontrakan', function ($query) use ($adminId) { + $query->where('admin_id', $adminId); + }) + ->with('kontrakan:id,nama,admin_id') + ->latest() + ->take(5) + ->get(), 'recentKontrakan' => $recentKontrakan, 'recentLaundry' => $recentLaundry, ]); diff --git a/spk_kontrakan/resources/views/dashboard/index.blade.php b/spk_kontrakan/resources/views/dashboard/index.blade.php index 4916448..eab0653 100644 --- a/spk_kontrakan/resources/views/dashboard/index.blade.php +++ b/spk_kontrakan/resources/views/dashboard/index.blade.php @@ -395,8 +395,8 @@
-

📊 Dashboard Analytics

-

Selamat datang, {{ Auth::user()->name }}! 👋

+

Dashboard Admin

+

Selamat datang, {{ Auth::user()->name }}! Kelola data kontrakan dan booking dengan lebih mudah.

{{ now()->format('d M Y') }}
{{ $jumlahKontrakan }} kontrakan
@@ -421,7 +421,7 @@
-
+
@@ -446,7 +446,7 @@ @if(!$isSuperAdmin) -
+
@@ -454,7 +454,7 @@

Total Booking

{{ $totalBookings }}

- Update real-time + Data booking terbaru

@@ -470,6 +470,48 @@ @endif + +
+
+
+
+
+

Booking Menunggu

+

{{ $bookingMenunggu ?? 0 }}

+

Perlu dikonfirmasi

+
+
+ +
+
+ + Cek Booking + +
+
+
+ + +
+
+
+
+
+

Booking Selesai

+

{{ $bookingSelesai ?? 0 }}

+

Transaksi selesai

+
+
+ +
+
+ + Lihat Riwayat + +
+
+
+ @if($isSuperAdmin)
@@ -787,7 +829,78 @@ @if($isSuperAdmin)
-
+ + +
+
+
+
+
+ Booking Terbaru +
+ Menampilkan booking terbaru dari kontrakan milik admin yang login. +
+ + Lihat Semua + +
+ +
+ @if(isset($recentBookings) && $recentBookings->count() > 0) +
+ + + + + + + + + + + @foreach($recentBookings as $booking) + + + + + + + @endforeach + +
Nama PenyewaKontrakanStatusTanggal
+ {{ $booking->user->name ?? $booking->nama_penyewa ?? 'Mahasiswa' }} + + {{ $booking->kontrakan->nama ?? '-' }} + + @php + $status = strtolower($booking->status ?? 'menunggu'); + $badgeClass = match($status) { + 'selesai' => 'success', + 'dikonfirmasi' => 'primary', + 'ditempati' => 'info', + 'ditolak' => 'danger', + default => 'warning' + }; + @endphp + + {{ ucfirst($booking->status ?? 'Menunggu') }} + + + {{ $booking->created_at ? $booking->created_at->format('d M Y') : '-' }} +
+
+ @else +
+ +
Belum ada booking masuk
+

Booking dari mahasiswa akan tampil di bagian ini.

+
+ @endif +
+
+
+ +
@@ -1014,6 +1127,7 @@ @if($isSuperAdmin) +@if($isSuperAdmin)