File size: 9,786 Bytes
70ba896 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Services\AnalyticsService;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Carbon\Carbon;
class DashboardController extends Controller
{
protected AnalyticsService $analyticsService;
public function __construct(AnalyticsService $analyticsService)
{
$this->analyticsService = $analyticsService;
}
/**
* Get revenue data with time period filtering
*/
public function revenue(Request $request): JsonResponse
{
$period = $request->get('period', 'daily');
$date = $request->get('date') ? Carbon::parse($request->get('date')) : Carbon::now();
$data = $this->analyticsService->getAggregatedData($period, $date);
$todayRevenue = $this->analyticsService->getTodayRevenue();
return response()->json([
'success' => true,
'data' => [
'chart_data' => $data,
'summary' => $todayRevenue,
'period' => $period,
'date' => $date->format('Y-m-d')
]
]);
}
/**
* Get profit data with trend calculations
*/
public function profit(Request $request): JsonResponse
{
$period = $request->get('period', 'today');
$date = $request->get('date') ? Carbon::parse($request->get('date')) : Carbon::now();
$profitData = $this->analyticsService->getProfitData($period);
$chartData = $this->analyticsService->getAggregatedData($period === 'today' ? 'daily' : $period, $date);
// Format chart data for profit visualization
$formattedChartData = array_map(function ($item) {
return [
'label' => $item['hour'] ?? $item['day_name'] ?? $item['day'] ?? $item['date'],
'revenue' => $item['revenue'],
'profit' => $item['profit'],
'profit_margin' => $item['revenue'] > 0 ? (($item['profit'] / $item['revenue']) * 100) : 0
];
}, $chartData);
return response()->json([
'success' => true,
'data' => [
'chart_data' => $formattedChartData,
'summary' => $profitData,
'period' => $period,
'date' => $date->format('Y-m-d')
]
]);
}
/**
* Get sales analytics with product performance
*/
public function sales(Request $request): JsonResponse
{
$limit = $request->get('limit', 10);
$topProducts = $this->analyticsService->getTopProducts($limit);
$period = $request->get('period', 'daily');
$date = $request->get('date') ? Carbon::parse($request->get('date')) : Carbon::now();
$salesData = $this->analyticsService->getAggregatedData($period, $date);
// Format sales chart data
$formattedSalesData = array_map(function ($item) {
return [
'label' => $item['hour'] ?? $item['day_name'] ?? $item['day'] ?? $item['date'],
'orders' => $item['orders'],
'revenue' => $item['revenue'],
'avg_order_value' => $item['orders'] > 0 ? ($item['revenue'] / $item['orders']) : 0
];
}, $salesData);
return response()->json([
'success' => true,
'data' => [
'top_products' => $topProducts,
'sales_chart' => $formattedSalesData,
'period' => $period,
'date' => $date->format('Y-m-d')
]
]);
}
/**
* Get customer analytics with new vs returning metrics
*/
public function customers(Request $request): JsonResponse
{
$customerAnalytics = $this->analyticsService->getCustomerAnalytics();
$period = $request->get('period', 'daily');
$date = $request->get('date') ? Carbon::parse($request->get('date')) : Carbon::now();
$customerData = $this->analyticsService->getAggregatedData($period, $date);
// Format customer chart data
$formattedCustomerData = array_map(function ($item) {
return [
'label' => $item['hour'] ?? $item['day_name'] ?? $item['day'] ?? $item['date'],
'customers' => $item['customers'],
'orders' => $item['orders'],
'conversion_rate' => $item['customers'] > 0 ? (($item['orders'] / $item['customers']) * 100) : 0
];
}, $customerData);
return response()->json([
'success' => true,
'data' => [
'analytics' => $customerAnalytics,
'chart_data' => $formattedCustomerData,
'period' => $period,
'date' => $date->format('Y-m-d')
]
]);
}
/**
* Get all dashboard metrics summary with period support
*/
public function summary(Request $request): JsonResponse
{
$period = $request->get('period', 'daily');
$date = $request->get('date') ? Carbon::parse($request->get('date')) : Carbon::now();
// Get period-specific data
$revenueData = $this->getRevenueForPeriod($period, $date);
$profitData = $this->analyticsService->getProfitData($period);
$customerAnalytics = $this->analyticsService->getCustomerAnalytics();
$orderStats = $this->getOrdersForPeriod($period, $date);
$productStats = $this->analyticsService->getProductStats();
$topProducts = $this->analyticsService->getTopProducts(5);
return response()->json([
'success' => true,
'data' => [
'revenue' => $revenueData,
'profit' => $profitData,
'orders' => $orderStats,
'customers' => $customerAnalytics,
'products' => $productStats,
'top_products' => $topProducts,
'period' => $period,
'date' => $date->format('Y-m-d'),
'last_updated' => Carbon::now()->toISOString()
]
]);
}
/**
* Get revenue data for specific period
*/
private function getRevenueForPeriod(string $period, Carbon $date): array
{
switch ($period) {
case 'weekly':
$current = \App\Models\Order::whereBetween('created_at', [
$date->copy()->startOfWeek(),
$date->copy()->endOfWeek()
])->sum('total_amount');
$previous = \App\Models\Order::whereBetween('created_at', [
$date->copy()->subWeek()->startOfWeek(),
$date->copy()->subWeek()->endOfWeek()
])->sum('total_amount');
break;
case 'monthly':
$current = \App\Models\Order::whereBetween('created_at', [
$date->copy()->startOfMonth(),
$date->copy()->endOfMonth()
])->sum('total_amount');
$previous = \App\Models\Order::whereBetween('created_at', [
$date->copy()->subMonth()->startOfMonth(),
$date->copy()->subMonth()->endOfMonth()
])->sum('total_amount');
break;
default: // daily
$current = \App\Models\Order::whereDate('created_at', $date)->sum('total_amount');
$previous = \App\Models\Order::whereDate('created_at', $date->copy()->subDay())->sum('total_amount');
break;
}
$percentageChange = $previous > 0 ? (($current - $previous) / $previous) * 100 : 0;
return [
'current' => $current,
'previous' => $previous,
'percentage_change' => round($percentageChange, 2),
'trend' => $percentageChange >= 0 ? 'up' : 'down'
];
}
/**
* Get orders data for specific period
*/
private function getOrdersForPeriod(string $period, Carbon $date): array
{
switch ($period) {
case 'weekly':
$current = \App\Models\Order::whereBetween('created_at', [
$date->copy()->startOfWeek(),
$date->copy()->endOfWeek()
])->count();
$previous = \App\Models\Order::whereBetween('created_at', [
$date->copy()->subWeek()->startOfWeek(),
$date->copy()->subWeek()->endOfWeek()
])->count();
break;
case 'monthly':
$current = \App\Models\Order::whereBetween('created_at', [
$date->copy()->startOfMonth(),
$date->copy()->endOfMonth()
])->count();
$previous = \App\Models\Order::whereBetween('created_at', [
$date->copy()->subMonth()->startOfMonth(),
$date->copy()->subMonth()->endOfMonth()
])->count();
break;
default: // daily
$current = \App\Models\Order::whereDate('created_at', $date)->count();
$previous = \App\Models\Order::whereDate('created_at', $date->copy()->subDay())->count();
break;
}
$percentageChange = $previous > 0 ? (($current - $previous) / $previous) * 100 : 0;
return [
'current' => $current,
'previous' => $previous,
'percentage_change' => round($percentageChange, 2),
'trend' => $percentageChange >= 0 ? 'up' : 'down'
];
}
}
|