Updated dashboard
This commit is contained in:
@@ -26,22 +26,41 @@ class DashboardController extends Controller
|
||||
->count('user_id');
|
||||
|
||||
// 3. Get Paid Subscribers & Breakdown by Plan Type AND Market Provider
|
||||
$paidBreakdown = UserSubscriber::where('user_subscribers.expired_at', '>=', now())
|
||||
->whereHas('subscribe', function (Builder $query) {
|
||||
$query->where('is_free', false);
|
||||
})
|
||||
$rawBreakdown = UserSubscriber::where('user_subscribers.expired_at', '>=', now())
|
||||
->join('subscribe_plans', 'user_subscribers.subscribe_plan_id', '=', 'subscribe_plans.id')
|
||||
->where('subscribe_plans.is_free', false) // Replaced redundant whereHas for better performance
|
||||
->selectRaw('subscribe_plans.name as plan_name, user_subscribers.market_provider, count(DISTINCT user_subscribers.user_id) as total')
|
||||
->groupBy('subscribe_plans.name', 'user_subscribers.market_provider')
|
||||
->get();
|
||||
|
||||
// Dynamically format the display name using the provider directly from the DB
|
||||
$paidBreakdown->each(function ($item) {
|
||||
$provider = !empty($item->market_provider) ? $item->market_provider : 'سایت / مستقیم';
|
||||
$item->display_name = $item->plan_name . ' (' . $provider . ')';
|
||||
});
|
||||
// 4. Safely group the results in PHP to fix split numbers (merging null, '', and 'web')
|
||||
$paidBreakdown = $rawBreakdown->map(function ($item) {
|
||||
$provider = strtolower(trim($item->market_provider));
|
||||
|
||||
// Total paid is the sum of the breakdown
|
||||
// Normalize the provider names
|
||||
if (empty($provider) || $provider === 'web') {
|
||||
$providerName = 'سایت';
|
||||
} elseif ($provider === 'bazar') {
|
||||
$providerName = 'بازار';
|
||||
} elseif ($provider === 'myket') {
|
||||
$providerName = 'مایکت';
|
||||
} else {
|
||||
$providerName = ucfirst($provider); // Dynamic fallback for any future gateways
|
||||
}
|
||||
|
||||
return [
|
||||
'display_name' => $item->plan_name . ' (' . $providerName . ')',
|
||||
'total' => $item->total
|
||||
];
|
||||
})->groupBy('display_name')->map(function ($group, $key) {
|
||||
// Merge any split rows (e.g. 'web' + null) into one accurate total sum
|
||||
return (object) [
|
||||
'display_name' => $key,
|
||||
'total' => $group->sum('total')
|
||||
];
|
||||
})->values();
|
||||
|
||||
// Total paid is the exact sum of the merged breakdown
|
||||
$paidSubscribers = $paidBreakdown->sum('total');
|
||||
|
||||
// 4. Fetch Recent Data (Limit to 5)
|
||||
|
||||
Reference in New Issue
Block a user