|null */ public function build(AdminUser $admin): ?array { if (! $admin->hasPermissionCode('dashboard.view')) { return null; } $node = $admin->primaryAgentNode(); if ($node === null) { return null; } $profile = AgentProfile::query()->where('agent_node_id', $node->id)->first(); $subtreeIds = AgentNode::query() ->where('path', 'like', $node->path.'%') ->pluck('id') ->map(static fn ($id): int => (int) $id) ->all(); $directChildCount = AgentNode::query() ->where('parent_id', $node->id) ->count(); $directPlayerCount = 0; if (Schema::hasColumn('players', 'agent_node_id')) { $directPlayerCount = Player::query() ->where('agent_node_id', $node->id) ->count(); } $pendingBillStats = $this->pendingBillStats($admin, $subtreeIds); $scope = AdminScopeContextResolver::fromValues($admin, requestedAgentNodeId: (int) $node->id); $today = now()->toDateString(); $sevenDayFrom = now()->subDays(6)->toDateString(); $todayTotals = $this->reportQuery->periodFinanceTotals($today, $today, $scope); $sevenDayTotals = $this->reportQuery->periodFinanceTotals($sevenDayFrom, $today, $scope); $currencyCode = $this->reportQuery->resolvePeriodCurrencyCode($today, $today, $scope) ?? $this->reportQuery->resolvePeriodCurrencyCode($sevenDayFrom, $today, $scope); $teamPlayerStats = $this->teamPlayerStats($subtreeIds); $todayActivityStats = $this->todayActivityStats($subtreeIds, $today); $topAgentToday = $this->topAgentToday($scope, $today, (int) $node->id); return [ 'agent_node_id' => (int) $node->id, 'agent_code' => (string) $node->code, 'agent_name' => (string) $node->name, 'depth' => (int) $node->depth, 'credit_limit' => (int) ($profile?->credit_limit ?? 0), 'allocated_credit' => (int) ($profile?->allocated_credit ?? 0), 'used_credit' => (int) ($profile?->used_credit ?? 0), 'available_credit' => max( 0, (int) ($profile?->credit_limit ?? 0) - (int) ($profile?->allocated_credit ?? 0), ), 'total_share_rate' => (float) ($profile?->total_share_rate ?? 0), 'settlement_cycle' => (string) ($profile?->settlement_cycle ?? 'weekly'), 'can_create_child_agent' => (bool) ($profile?->can_create_child_agent ?? false), 'can_create_player' => (bool) ($profile?->can_create_player ?? false), 'direct_child_count' => $directChildCount, 'subtree_agent_count' => count($subtreeIds), 'direct_player_count' => $directPlayerCount, 'team_player_count' => $teamPlayerStats['count'], 'active_player_count_today' => $todayActivityStats['player_count'], 'bet_order_count_today' => $todayActivityStats['order_count'], 'today_bet_minor' => $todayTotals['total_bet_minor'], 'today_payout_minor' => $todayTotals['total_payout_minor'], 'today_profit_minor' => $todayTotals['approx_house_gross_minor'], 'seven_day_bet_minor' => $sevenDayTotals['total_bet_minor'], 'seven_day_payout_minor' => $sevenDayTotals['total_payout_minor'], 'seven_day_profit_minor' => $sevenDayTotals['approx_house_gross_minor'], 'currency_code' => $currencyCode, 'pending_bill_count' => $pendingBillStats['count'], 'pending_unpaid_minor' => $pendingBillStats['unpaid_minor'], 'latest_bet_at' => $todayActivityStats['latest_bet_at'], 'top_agent_today' => $topAgentToday, ]; } /** * @param list $subtreeIds * @return array{count: int, unpaid_minor: int} */ private function pendingBillStats(AdminUser $admin, array $subtreeIds): array { if ($subtreeIds === []) { return ['count' => 0, 'unpaid_minor' => 0]; } $query = DB::table('settlement_bills') ->where('bill_type', 'agent') ->where('owner_type', 'agent') ->whereIn('owner_id', $subtreeIds) ->whereIn('status', ['pending', 'pending_confirm', 'partial']); AdminAgentSettlementScope::applyToBillsQuery($query, $admin); return [ 'count' => (int) $query->count(), 'unpaid_minor' => (int) $query->sum('unpaid_amount'), ]; } /** * @param list $subtreeIds * @return array{count: int} */ private function teamPlayerStats(array $subtreeIds): array { if ($subtreeIds === [] || ! Schema::hasColumn('players', 'agent_node_id')) { return ['count' => 0]; } return [ 'count' => (int) Player::query() ->whereIn('agent_node_id', $subtreeIds) ->count(), ]; } /** * @param list $subtreeIds * @return array{player_count: int, order_count: int, latest_bet_at: ?string} */ private function todayActivityStats(array $subtreeIds, string $today): array { if ($subtreeIds === []) { return [ 'player_count' => 0, 'order_count' => 0, 'latest_bet_at' => null, ]; } $base = DB::table('ticket_orders as o') ->join('players as p', 'p.id', '=', 'o.player_id') ->whereIn('p.agent_node_id', $subtreeIds) ->whereDate('o.created_at', $today); $latestBetAt = (clone $base)->max('o.created_at'); return [ 'player_count' => (int) (clone $base)->distinct('o.player_id')->count('o.player_id'), 'order_count' => (int) (clone $base)->count(), 'latest_bet_at' => $latestBetAt !== null ? Carbon::parse((string) $latestBetAt)->toIso8601String() : null, ]; } /** * @return array|null */ private function topAgentToday(AdminScopeContext $scope, string $today, int $currentAgentId): ?array { $rows = $this->reportQuery->agentRankingRows($today, $today, null, 5, $scope); foreach ($rows as $row) { if ((int) ($row['agent_node_id'] ?? 0) === $currentAgentId) { return $row; } } return $rows[0] ?? null; } }