input('bulan', date('m')); $tahun = $request->input('tahun', date('Y')); // Data utama $cashier = DB::table('cashiers') ->whereMonth('createdAt', $bulan) ->whereYear('createdAt', $tahun) ->get(); $totalPendapatan = $cashier->sum('total'); $totalTransaksi = $cashier->sum('jumlah_item'); $jumlahHariAktif = $cashier ->map(fn($item) => date('Y-m-d', strtotime($item->createdAt))) ->unique() ->count(); $rataRataPerHari = $jumlahHariAktif > 0 ? $totalPendapatan / $jumlahHariAktif : 0; $transaksiRataRataPerHari = $jumlahHariAktif > 0 ? $totalTransaksi / $jumlahHariAktif : 0; $totalProduk = DB::table('products')->count(); $produkBaru = DB::table('products') ->whereMonth('createdAt', $bulan) ->whereYear('createdAt', $tahun) ->count(); $pendapatanPerBulan = []; for ($i = 1; $i <= 12; $i++) { $total = DB::table('cashiers') ->whereMonth('createdAt', $i) ->whereYear('createdAt', $tahun) ->sum('total'); $pendapatanPerBulan[] = $total; } // Penjualan Tertinggi tanpa filter brand $penjualanTertinggi = DB::table('cashiers') ->join('products', 'cashiers.produk_id', '=', 'products.id') ->select( 'products.nama_produk as nama_produk', DB::raw('SUM(cashiers.jumlah_item) as total_terjual') ) ->whereMonth('cashiers.createdAt', $bulan) ->whereYear('cashiers.createdAt', $tahun) ->groupBy('products.id', 'products.nama_produk') ->orderByDesc('total_terjual') ->limit(6) ->get(); return view('dashboard', compact( 'bulan', 'tahun', 'totalPendapatan', 'totalTransaksi', 'rataRataPerHari', 'transaksiRataRataPerHari', 'totalProduk', 'produkBaru', 'pendapatanPerBulan', 'penjualanTertinggi' )); } }