MIF_E31220412/app/Http/Controllers/Admin/DashboardController.php

182 lines
6.2 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Paket;
use App\Models\Sewa;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
class DashboardController extends Controller
{
public function index()
{
// Mengambil statistik
$stats = [
'users' => User::where('role', 'customer')->count(),
'packages' => Paket::count(),
'rentals' => Sewa::whereIn('status', ['disetujui', 'selesai'])->count(),
'revenue' => Sewa::where('status', 'selesai')->sum('total_harga')
];
// Mengambil aktivitas terbaru
$activities = $this->getRecentActivities();
// Mengambil pesanan yang dibatalkan terbaru (untuk notifikasi)
$recentCancellations = Sewa::with(['user', 'paket'])
->where('status', 'dibatalkan')
->whereNotNull('detail_status')
->latest()
->take(5)
->get();
return view('admin.dashboard', compact('stats', 'activities', 'recentCancellations'));
}
private function getRecentActivities()
{
$activities = [];
// Aktivitas sewa terbaru
$recentRentals = Sewa::with(['user', 'paket'])
->whereIn('status', ['disetujui', 'selesai'])
->latest()
->take(5)
->get();
foreach ($recentRentals as $rental) {
$activities[] = [
'title' => 'Sewa Baru',
'description' => "{$rental->user->name} menyewa paket {$rental->paket->nama_paket}",
'time' => Carbon::parse($rental->created_at)->diffForHumans(),
'color' => 'blue',
'icon' => 'M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2'
];
}
// Aktivitas pembayaran
$recentPayments = Sewa::where('status', 'selesai')
->latest()
->take(5)
->get();
foreach ($recentPayments as $payment) {
$activities[] = [
'title' => 'Pembayaran Selesai',
'description' => "Pembayaran sewa paket {$payment->paket->nama_paket} oleh {$payment->user->name}",
'time' => Carbon::parse($payment->updated_at)->diffForHumans(),
'color' => 'green',
'icon' => 'M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z'
];
}
// Aktivitas pembatalan
$recentCancellations = Sewa::with(['user', 'paket'])
->where('status', 'dibatalkan')
->latest()
->take(3)
->get();
foreach ($recentCancellations as $cancellation) {
$activities[] = [
'title' => 'Pesanan Dibatalkan',
'description' => "{$cancellation->user->name} membatalkan pesanan paket {$cancellation->paket->nama_paket}",
'time' => Carbon::parse($cancellation->updated_at)->diffForHumans(),
'color' => 'red',
'icon' => 'M6 18L18 6M6 6l12 12'
];
}
// Urutkan berdasarkan waktu terbaru
usort($activities, function($a, $b) {
return strtotime($b['time']) - strtotime($a['time']);
});
return array_slice($activities, 0, 5);
}
/**
* Menampilkan halaman daftar pesanan yang dibatalkan
*/
public function cancelledOrders()
{
if (Auth::user()->tipe_pengguna !== 'admin') {
return redirect()->route('dashboard')->with('error', 'Akses ditolak.');
}
$cancelledOrders = Sewa::with(['user', 'paket', 'kota'])
->where('status', 'dibatalkan')
->orderBy('updated_at', 'desc')
->paginate(15);
return view('admin.cancelled-orders', compact('cancelledOrders'));
}
/**
* Menampilkan detail pesanan yang dibatalkan
*/
public function showCancelledOrder($id)
{
if (Auth::user()->tipe_pengguna !== 'admin') {
return redirect()->route('dashboard')->with('error', 'Akses ditolak.');
}
$sewa = Sewa::with(['user', 'paket', 'kota'])->findOrFail($id);
if ($sewa->status !== 'dibatalkan') {
return redirect()->route('admin.cancelled-orders')->with('error', 'Pesanan ini tidak dibatalkan.');
}
return view('admin.cancelled-order-detail', compact('sewa'));
}
/**
* API untuk mendapatkan jumlah pesanan yang dibatalkan (untuk notifikasi)
*/
public function getCancelledOrdersCount()
{
if (Auth::user()->tipe_pengguna !== 'admin') {
return response()->json(['error' => 'Unauthorized'], 403);
}
$count = Sewa::where('status', 'dibatalkan')
->whereNotNull('detail_status')
->where('updated_at', '>=', now()->subDays(7)) // Hanya 7 hari terakhir
->count();
return response()->json(['count' => $count]);
}
/**
* API untuk mendapatkan notifikasi pembatalan terbaru
*/
public function getRecentCancellations()
{
if (Auth::user()->tipe_pengguna !== 'admin') {
return response()->json(['error' => 'Unauthorized'], 403);
}
$recentCancellations = Sewa::with(['user', 'paket'])
->where('status', 'dibatalkan')
->whereNotNull('detail_status')
->where('updated_at', '>=', now()->subHours(24)) // Hanya 24 jam terakhir
->orderBy('updated_at', 'desc')
->take(5)
->get()
->map(function ($cancellation) {
return [
'id' => $cancellation->id,
'user_name' => $cancellation->user->name ?? 'N/A',
'paket_name' => $cancellation->paket->nama ?? 'N/A',
'alasan' => $cancellation->detail_status,
'cancelled_at' => $cancellation->updated_at->diffForHumans(),
'total_harga' => number_format($cancellation->total_harga, 0, ',', '.')
];
});
return response()->json(['cancellations' => $recentCancellations]);
}
}