Files
cheerstothe_season_2.0/app/Http/Controllers/Admin/DashboardController.php

156 lines
6.6 KiB
PHP
Raw Normal View History

2024-05-23 15:20:21 +05:30
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
2024-05-29 14:53:06 +05:30
use App\Models\IamPrincipal;
use Illuminate\Support\Facades\DB;
use App\Models\ManageRestaurant;
2024-05-23 15:20:21 +05:30
class DashboardController extends Controller
{
2024-05-29 14:53:06 +05:30
// public function showDashboard(){
2024-05-23 15:20:21 +05:30
2024-05-29 14:53:06 +05:30
// return view('Admin.dashboard');
// }
public function showDashboard()
{
// Fetching data for sorting by day
// $dailyData = OrderedPassport::select(DB::raw("COUNT(*) as count"), DB::raw("DATE(created_at) as date"))
// ->whereBetween('created_at', [now()->subDays(7), now()]) // Fetch data for the last 7 days
// ->groupBy(DB::raw("DATE(created_at)"))
// ->orderBy(DB::raw("DATE(created_at)"))
// ->pluck('count', 'date')
// ->toArray();
// Ensure that $dailyData contains zeros for days with no data
$start_date = now()->subDays(7)->startOfDay();
$end_date = now()->endOfDay();
$formattedDailyData = [];
while ($start_date <= $end_date) {
$formattedDailyData[$start_date->format('Y-m-d')] = isset($dailyData[$start_date->format('Y-m-d')]) ? $dailyData[$start_date->format('Y-m-d')] : 0;
$start_date->addDay();
}
// Default sales chart data (monthly)
// $defaultData = OrderedPassport::select(DB::raw("COUNT(*) as count"), DB::raw("MONTH(created_at) as month"))
// ->whereYear('created_at', date('Y'))
// ->groupBy(DB::raw("MONTH(created_at)"))
// ->orderBy(DB::raw("MONTH(created_at)"))
// ->pluck('count', 'month')
// ->toArray();
// Ensure that $defaultData contains zeros for months with no data
$months = range(1, 12);
$formattedDefaultData = [];
foreach ($months as $month) {
$formattedDefaultData[$month] = isset($defaultData[$month]) ? $defaultData[$month] : 0;
}
// $quarterlyData = OrderedPassport::select(
// DB::raw("COUNT(*) as count"),
// DB::raw("QUARTER(created_at) as quarter")
// )
// ->whereYear('created_at', date('Y'))
// ->groupBy(DB::raw("QUARTER(created_at)"))
// ->orderBy(DB::raw("QUARTER(created_at)"))
// ->pluck('count', 'quarter')
// ->toArray();
// Ensure that $quarterlyData contains zeros for quarters with no data
for ($i = 1; $i <= 4; $i++) {
if (!isset($quarterlyData[$i])) {
$quarterlyData[$i] = 0;
}
}
// Fetching data for yearly option
// $yearlyData = OrderedPassport::select(DB::raw("COUNT(*) as count"), DB::raw("YEAR(created_at) as year"))
// ->groupBy(DB::raw("YEAR(created_at)"))
// ->pluck('count', 'year')
// ->toArray();
//
// Monthly data
$dataMonthlyWithType3 = IamPrincipal::select(DB::raw("COUNT(*) as count"), DB::raw("MONTH(created_at) as month"))
->whereIn('principal_type_xid', [3])
->whereYear('created_at', date('Y'))
->groupBy(DB::raw("MONTH(created_at)"))
->orderBy(DB::raw("MONTH(created_at)"))
->pluck('count', 'month')
->toArray();
2024-05-29 17:13:40 +05:30
// dd($dataMonthlyWithType3);
2024-05-29 14:53:06 +05:30
$dataMonthlyWithType4 = IamPrincipal::select(DB::raw("COUNT(*) as count"), DB::raw("MONTH(created_at) as month"))
->whereIn('principal_type_xid', [4])
->whereYear('created_at', date('Y'))
->groupBy(DB::raw("MONTH(created_at)"))
->orderBy(DB::raw("MONTH(created_at)"))
->pluck('count', 'month')
->toArray();
// Quarterly data
$dataQuarterlyWithType3 = IamPrincipal::select(
DB::raw("COUNT(*) as count"),
DB::raw("QUARTER(created_at) as quarter")
)
->whereIn('principal_type_xid', [3])
->groupBy(DB::raw("QUARTER(created_at)"))
->orderBy(DB::raw("QUARTER(created_at)"))
->pluck('count', 'quarter')
->toArray();
for ($i = 1; $i <= 4; $i++) {
if (!isset($dataQuarterlyWithType3[$i])) {
$dataQuarterlyWithType3[$i] = 0;
}
}
$dataQuarterlyWithType4 = IamPrincipal::select(
DB::raw("COUNT(*) as count"),
DB::raw("QUARTER(created_at) as quarter")
)
->whereIn('principal_type_xid', [4])
->groupBy(DB::raw("QUARTER(created_at)"))
->orderBy(DB::raw("QUARTER(created_at)"))
->pluck('count', 'quarter')
->toArray();
for ($i = 1; $i <= 4; $i++) {
if (!isset($dataQuarterlyWithType4[$i])) {
$dataQuarterlyWithType4[$i] = 0;
}
}
// Yearly data
$dataYearlyWithType3 = IamPrincipal::select(DB::raw("COUNT(*) as count"), DB::raw("YEAR(created_at) as year"))
->whereIn('principal_type_xid', [3])
->groupBy(DB::raw("YEAR(created_at)"))
->pluck('count', 'year')
->toArray();
// dd($dataYearlyWithType3);
$dataYearlyWithType4 = IamPrincipal::select(DB::raw("COUNT(*) as count"), DB::raw("YEAR(created_at) as year"))
->whereIn('principal_type_xid', [4])
->groupBy(DB::raw("YEAR(created_at)"))
->pluck('count', 'year')
->toArray();
$customerCount = IamPrincipal::where('principal_type_xid', '=', 3)->count();
// $activePassports = ManagePassport::where('is_active', 1)->take(12)->get();
// $restaurantCount = ManageRestaurant::where('is_redeem', 1)->count();
$restaurantCount = ManageRestaurant::where('is_active', 1)->count();
// $recent_transaction = OrderedPassport::with('order', 'order_passport', 'carts.passport', 'iamPrincipal')->get()->toArray();
// $datas = MyPassportVoucher::with('passportVouchers', 'passportData', 'customer')->get()->toArray();
// Pass the data to the view
// return view('Admin.dashboard', compact('customerCount', 'activePassports', 'restaurantCount', 'recent_transaction', 'datas', 'formattedDefaultData', 'quarterlyData', 'yearlyData', 'dataMonthlyWithType3', 'dataMonthlyWithType4', 'dataQuarterlyWithType3', 'dataQuarterlyWithType4', 'dataYearlyWithType3', 'dataYearlyWithType4','formattedDailyData'));
return view('Admin.dashboard', compact('customerCount','restaurantCount','dataMonthlyWithType3','dataMonthlyWithType4','dataQuarterlyWithType3', 'dataQuarterlyWithType4', 'dataYearlyWithType3', 'dataYearlyWithType4'));
2024-05-23 15:20:21 +05:30
}
}