Viewing file: DashboardController.php (5.11 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use App\Models\Campaign; use App\Models\CampaignPerformance; use App\Models\Wallet; use App\Models\HilltopAccount; use Carbon\Carbon;
class DashboardController extends Controller { public function getKpis() { $user = Auth::user(); $userId = $user->id;
// Available Balance $wallet = $user->wallet; $balance = $wallet ? $wallet->balance : 0; // Calculate balance change (mock logic for now as we don't have historical balance snapshots easily accessible without complex queries) // Or we could check recent transactions. Let's keep it simple for now. $balanceChange = '+$0.00 this week'; $balanceChangeType = 'neutral';
// Active Campaigns $activeCampaignsCount = Campaign::where('user_id', $userId) ->where('status', 'active') ->count(); // Today's Spend $today = Carbon::today(); $todaysSpend = CampaignPerformance::whereHas('campaign', function ($query) use ($userId) { $query->where('user_id', $userId); }) ->where('date', $today) ->sum('spend'); // Yesterday's Spend for comparison $yesterday = Carbon::yesterday(); $yesterdaysSpend = CampaignPerformance::whereHas('campaign', function ($query) use ($userId) { $query->where('user_id', $userId); }) ->where('date', $yesterday) ->sum('spend');
$spendChange = 0; $spendChangeType = 'neutral'; if ($yesterdaysSpend > 0) { $spendChange = (($todaysSpend - $yesterdaysSpend) / $yesterdaysSpend) * 100; $spendChangeType = $spendChange > 0 ? 'negative' : 'positive'; // Higher spend is usually "negative" for wallet, but "positive" for scale. Let's stick to "spend increase" = neutral/info. // Actually, usually higher spend is "neutral" or "negative" if ROI isn't higher. Let's use neutral. // But the UI uses green for positive. Let's assume higher spend is good (scaling). $spendChangeType = $spendChange >= 0 ? 'positive' : 'neutral'; }
// Estimated Profit (Placeholder as we don't have revenue tracking yet) $estimatedProfit = 0; $profitChange = '0% this month'; $profitChangeType = 'neutral';
// Account Status $account = HilltopAccount::where('assigned_user_id', $userId)->first(); $accountData = null; if ($account) { $accountData = [ 'alias' => $account->alias, 'status' => 'Active' // Assuming if assigned it's active ]; }
return response()->json([ 'available_balance' => [ 'value' => '$' . number_format($balance, 2), 'change' => $balanceChange, 'change_type' => $balanceChangeType ], 'active_campaigns' => [ 'value' => (string)$activeCampaignsCount, 'change' => '', // Could calculate change if we tracked history 'change_type' => 'neutral' ], 'todays_spend' => [ 'value' => '$' . number_format($todaysSpend, 2), 'change' => ($spendChange >= 0 ? '+' : '') . number_format($spendChange, 1) . '% vs yesterday', 'change_type' => $spendChangeType ], 'estimated_profit' => [ 'value' => '$' . number_format($estimatedProfit, 2), 'change' => $profitChange, 'change_type' => $profitChangeType ], 'account_status' => $accountData ]); }
public function getCampaigns() { $userId = Auth::id();
$campaigns = Campaign::where('user_id', $userId) ->with(['performances' => function($query) { // We might want to limit performances or aggregate them }]) ->orderBy('created_at', 'desc') ->take(10) // Limit to recent 10 ->get();
$formattedCampaigns = $campaigns->map(function ($campaign) { // Aggregate stats $spend = $campaign->performances->sum('spend'); $impressions = $campaign->performances->sum('impressions'); $clicks = $campaign->performances->sum('clicks'); $ctr = $impressions > 0 ? ($clicks / $impressions) * 100 : 0;
return [ 'id' => $campaign->id, 'name' => $campaign->name, 'status' => $campaign->status, 'daily_cap' => '$' . number_format($campaign->daily_budget, 2), 'spend' => '$' . number_format($spend, 2), 'impressions' => number_format($impressions), 'clicks' => number_format($clicks), 'ctr' => number_format($ctr, 2) . '%', 'cost' => '$' . number_format($spend, 2) // Same as spend ]; });
return response()->json($formattedCampaigns); } }
|