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(); $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(); } $defaultData = Subscriptions::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(); $months = range(1, 12); $formattedDefaultData = []; foreach ($months as $month) { $formattedDefaultData[$month] = isset($defaultData[$month]) ? $defaultData[$month] : 0; } $quarterlyData = Subscriptions::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; } } // Sort the array by quarter keys ksort($quarterlyData); // Fetching data for yearly option $yearlyData = Subscriptions::select(DB::raw("COUNT(*) as count"), DB::raw("YEAR(created_at) as year")) ->groupBy(DB::raw("YEAR(created_at)")) ->pluck('count', 'year') ->toArray(); $dataMonthlyWithType3 = IamPrincipal::select(DB::raw("COUNT(*) as count"), DB::raw("MONTH(created_at) as month")) ->where('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(); $dataMonthlyWithType4 = Subscriptions::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(); // Fill missing months with zeros $months = range(1, 12); $dataMonthlyWithType3 = array_replace(array_fill_keys($months, 0), $dataMonthlyWithType3); $dataMonthlyWithType4 = array_replace(array_fill_keys($months, 0), $dataMonthlyWithType4); // Quarterly data $dataQuarterlyWithType3 = IamPrincipal::select(DB::raw("COUNT(*) as count"), DB::raw("QUARTER(created_at) as quarter")) ->where('principal_type_xid', 3) ->groupBy(DB::raw("QUARTER(created_at)")) ->orderBy(DB::raw("QUARTER(created_at)")) ->pluck('count', 'quarter') ->toArray(); $dataQuarterlyWithType4 = Subscriptions::select(DB::raw("COUNT(*) as count"), DB::raw("QUARTER(created_at) as quarter")) ->groupBy(DB::raw("QUARTER(created_at)")) ->orderBy(DB::raw("QUARTER(created_at)")) ->pluck('count', 'quarter') ->toArray(); // Fill missing quarters with zeros $quarters = range(1, 4); $dataQuarterlyWithType3 = array_replace(array_fill_keys($quarters, 0), $dataQuarterlyWithType3); $dataQuarterlyWithType4 = array_replace(array_fill_keys($quarters, 0), $dataQuarterlyWithType4); // Yearly data $dataYearlyWithType3 = IamPrincipal::select(DB::raw("COUNT(*) as count"), DB::raw("YEAR(created_at) as year")) ->where('principal_type_xid', 3) ->groupBy(DB::raw("YEAR(created_at)")) ->pluck('count', 'year') ->toArray(); $dataYearlyWithType4 = Subscriptions::select(DB::raw("COUNT(*) as count"), DB::raw("YEAR(created_at) as year")) ->groupBy(DB::raw("YEAR(created_at)")) ->pluck('count', 'year') ->toArray(); $customerCount = IamPrincipal::where('principal_type_xid', '=', 3)->count(); $restaurantCount = Subscriptions::where('is_active', 1)->count(); $dateTime = now(); $formattedDateTime = $dateTime->format('Y-m-d H:i:s'); $recent_transaction = Subscriptions::where('next_payment_date', '>=', $formattedDateTime)->with('subscription')->orderBy('id', 'desc')->get()->toArray(); return view('Admin.dashboard', compact( 'customerCount', 'restaurantCount', 'dataMonthlyWithType3', 'dataMonthlyWithType4', 'dataQuarterlyWithType3', 'dataQuarterlyWithType4', 'dataYearlyWithType3', 'dataYearlyWithType4', 'recent_transaction', 'dailyData', 'formattedDailyData', 'defaultData', 'formattedDefaultData', 'quarterlyData', 'yearlyData' )); } /* Created By : Sayali Parab Created at : 08 July 2024 Use : To Get Excel. */ public function exportRecentTransactions(Request $request) { try { if ($request->has('all_id')) { $recentTransaction = Subscriptions::with('subscription')->get()->toArray(); return Excel::download(new DashboardExportUser($recentTransaction), 'recent_transactions.xlsx'); } $ids = $request->input('selected_id'); $fileName = 'selected_customer_transaction_data.xlsx'; return Excel::download(new DashboardSelectedExportUser($ids), $fileName); } catch (\Exception $e) { return response()->json(['error' => 'Export failed. Something went wrong.'], 500); } } }