MIF_E31222467/pos-smartphone-fix/app/Http/Controllers/DashboardController.php

84 lines
2.3 KiB
PHP

<?php
namespace App\Http\Controllers;
// use App\Models\Sample;
// use App\Models\Minggu;
// use App\Models\Bulan;
// use App\Models\Hasil;
// use App\Models\Brand;
// use App\Models\Training;
// use App\Models\Testing;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class DashboardController extends Controller
{
public function index(Request $request)
{
$bulan = $request->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'
));
}
}