diff --git a/app/Http/Controllers/Admin/DashboardController.php b/app/Http/Controllers/Admin/DashboardController.php index 8bbc4a1..cc714a0 100644 --- a/app/Http/Controllers/Admin/DashboardController.php +++ b/app/Http/Controllers/Admin/DashboardController.php @@ -34,7 +34,7 @@ class DashboardController extends Controller ->orderBy(DB::raw("DATE(created_at)")) ->pluck('count', 'date') ->toArray(); - $customReferralCode = CustomReferralCode::where('is_active',1)->first(); + $customReferralCode = CustomReferralCode::where('is_active', 1)->first(); $start_date = now()->subDays(7)->startOfDay(); $end_date = now()->endOfDay(); @@ -148,28 +148,48 @@ class DashboardController extends Controller 'next_payment_date' => $transaction->next_payment_date ? Carbon::parse($transaction->next_payment_date)->format('m/d/Y') : null, ]; })->toArray(); - $refrralUsers = ReferralUsers::with(['referredUser','refeersUser'])->latest()->get(); + $response = ReferralUsers::with(['referredUser', 'refeersUser'])->latest()->get()->toArray(); - return view('Admin.dashboard', compact( - 'customReferralCode', - 'customerCount', - 'restaurantCount', - 'dataMonthlyWithType3', - 'dataMonthlyWithType4', - 'dataQuarterlyWithType3', - 'dataQuarterlyWithType4', - 'dataYearlyWithType3', - 'dataYearlyWithType4', - 'formatted_transactions', - 'dailyData', - 'formattedDailyData', - 'defaultData', - 'formattedDefaultData', - 'quarterlyData', - 'yearlyData', - 'refrralUsers' - ) + $referralData = []; + + foreach ($response as $item) { + $principalId = $item['iam_principal_xid']; + + if (!isset($referralData[$principalId])) { + $referralData[$principalId] = [ + 'referred_user' => $item['referred_user'], + 'refeers_users' => [] + ]; + } + + // Add the refeers_user to the respective principalId + if (isset($item['refeers_user'])) { + $referralData[$principalId]['refeers_users'][] = $item['refeers_user']; + } + } + + return view( + 'Admin.dashboard', + compact( + 'customReferralCode', + 'customerCount', + 'restaurantCount', + 'dataMonthlyWithType3', + 'dataMonthlyWithType4', + 'dataQuarterlyWithType3', + 'dataQuarterlyWithType4', + 'dataYearlyWithType3', + 'dataYearlyWithType4', + 'formatted_transactions', + 'dailyData', + 'formattedDailyData', + 'defaultData', + 'formattedDefaultData', + 'quarterlyData', + 'yearlyData', + 'referralData' + ) ); } @@ -188,19 +208,17 @@ class DashboardController extends Controller try { DB::beginTransaction(); - if($request->id){ + if ($request->id) { CustomReferralCode::updateOrCreate(['id', $request->id], [ 'referral_code' => $request->referral_code, ]); - }else{ - CustomReferralCode::updateOrCreate([ - 'referral_code' => $request->referral_code, - ]); - } + } else { + CustomReferralCode::updateOrCreate([ + 'referral_code' => $request->referral_code, + ]); + } DB::commit(); return response()->json(['status_code' => 200]); - - } catch (\Exception $e) { DB::rollBack(); \Log::error('Storing Referral Code function failed: ' . $e->getMessage()); diff --git a/resources/views/Admin/dashboard.blade.php b/resources/views/Admin/dashboard.blade.php index 78db781..8bf228c 100644 --- a/resources/views/Admin/dashboard.blade.php +++ b/resources/views/Admin/dashboard.blade.php @@ -18,40 +18,44 @@