113 lines
4.1 KiB
PHP
113 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Carbon\Carbon;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
// Query untuk mendapatkan data pendapatan per bulan
|
|
// Array of month names in Indonesian
|
|
$nama_bulan = [
|
|
'Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni',
|
|
'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember'
|
|
];
|
|
|
|
// Initialize an array to hold the monthly revenue data
|
|
$jumlah_pendapatan = [];
|
|
|
|
// Loop through each month to get the total revenue
|
|
for ($month = 1; $month <= 12; $month++) {
|
|
$total_pendapatan = DB::table('orders')
|
|
->leftJoin('order_items', 'orders.id', '=', 'order_items.order_id')
|
|
->leftJoin('products', 'products.id', '=', 'order_items.product_id')
|
|
->where('orders.status', 'delivered')
|
|
->whereMonth('orders.created_at', $month)
|
|
->whereYear('orders.created_at', Carbon::now()->year)
|
|
->sum('orders.total_cost');
|
|
|
|
$jumlah_pendapatan[] = (object)[
|
|
'total_pendapatan' => $total_pendapatan,
|
|
'bulan' => $nama_bulan[$month - 1],
|
|
'bulan_num' => $month
|
|
];
|
|
}
|
|
|
|
// Extracting labels and values for further use
|
|
$label = array_column($jumlah_pendapatan, 'bulan');
|
|
$jumlah_pendapatan_values = array_column($jumlah_pendapatan, 'total_pendapatan');
|
|
|
|
|
|
// dd($jumlah_pendapatan_values);
|
|
|
|
// total pendapatan
|
|
// $totalPendapatan = DB::table('orders')
|
|
// ->leftJoin('order_items', 'orders.id', '=', 'order_items.order_id')
|
|
// ->where('orders.status', 'delivered')
|
|
// ->whereDate('orders.transaction_time', date('Y-m-d'))
|
|
// ->select(
|
|
// DB::raw('SUM(order_items.quantity * order_items.price) as total_pemesanan')
|
|
// )->get();
|
|
|
|
$thirtyDaysAgo = Carbon::now()->subDays(30);
|
|
|
|
$jumlahUserAktif = DB::table('users')
|
|
->where('created_at', '>=', $thirtyDaysAgo)
|
|
->where('roles', 'member')
|
|
->count();
|
|
|
|
// Get the current date
|
|
$currentDate = Carbon::now()->toDateString();
|
|
|
|
// Count the number of transactions for today
|
|
$totalTransaksi = DB::table('orders')
|
|
->whereDate('created_at', $currentDate)
|
|
->count();
|
|
|
|
// dd($totalTransaksi);
|
|
|
|
|
|
|
|
$totalPemesanan = DB::table('orders')
|
|
->join('order_items', 'orders.id', '=', 'order_items.order_id')
|
|
->join('products', 'order_items.product_id', '=', 'products.id')
|
|
->select(
|
|
DB::raw('DATE(orders.transaction_time) as hari_pemesanan'),
|
|
DB::raw('SUM(order_items.quantity) as total_pemesanan')
|
|
)
|
|
->whereDate('orders.transaction_time', date('Y-m-d'))
|
|
->where('status', 'delivered')
|
|
->groupBy(DB::raw('DATE(orders.transaction_time)'))
|
|
->get();
|
|
|
|
// dd($totalPemesanan);
|
|
|
|
$totalPendapatanBulanan = DB::table('orders')
|
|
->leftJoin('order_items', 'orders.id', '=', 'order_items.order_id')
|
|
->where('orders.status', 'delivered')
|
|
->whereMonth('orders.transaction_time', now()->month)
|
|
->whereYear('orders.transaction_time', now()->year)
|
|
->select(
|
|
DB::raw('SUM(order_items.quantity * order_items.price) as total_pendapatan')
|
|
)
|
|
->value('total_pendapatan');
|
|
// dd($totalPendapatanBulanan);
|
|
|
|
$totalPendapatanHarian = DB::table('orders')
|
|
->where('orders.status', 'delivered')
|
|
->whereDate('orders.created_at', Carbon::today())
|
|
->sum('orders.total_cost');
|
|
|
|
// dd($totalPendapatanHarian);
|
|
|
|
return view('admin.dashboard.index', compact('totalPendapatanBulanan', 'jumlah_pendapatan_values', 'totalPendapatanHarian', 'label', 'jumlah_pendapatan', 'jumlahUserAktif', 'totalTransaksi'));
|
|
}
|
|
}
|