lotteryAdmin(); abort_if($admin === null, 401); $periodId = (int) $request->query('settlement_period_id', 0); $adminSiteId = (int) $request->query('admin_site_id', 0); $query = DB::table('settlement_bills as sb') ->leftJoin('settlement_periods as sp', 'sp.id', '=', 'sb.settlement_period_id') ->select([ 'sb.*', 'sp.period_start', 'sp.period_end', 'sp.admin_site_id', ]) ->orderByDesc('sb.id'); if ($periodId > 0) { $query->where('sb.settlement_period_id', $periodId); } if ($adminSiteId > 0) { $query->where('sp.admin_site_id', $adminSiteId); } $billType = (string) $request->query('bill_type', ''); if ($billType !== '') { $query->where('sb.bill_type', $billType); } $scope = (string) $request->query('scope', ''); match ($scope) { 'pending_confirm' => $query->where('sb.status', 'pending_confirm'), 'awaiting_payment' => $query ->whereIn('sb.status', ['confirmed', 'partial_paid', 'overdue']) ->where('sb.unpaid_amount', '>', 0), 'settled' => $query->where('sb.status', 'settled'), 'adjustment' => $query->whereIn('sb.bill_type', ['adjustment', 'reversal']), default => null, }; AdminAgentSettlementScope::applyToBillsQuery($query, $admin, 'sb'); /** @var Collection $items */ $items = $query->limit(200)->get(); return ApiResponse::success([ 'items' => $this->enrichBillRows($items), ]); } /** * @param Collection $items * @return list> */ private function enrichBillRows(Collection $items): array { if ($items->isEmpty()) { return []; } $playerIds = []; $agentIds = []; foreach ($items as $row) { if ((string) $row->owner_type === 'player') { $playerIds[] = (int) $row->owner_id; } elseif ((string) $row->owner_type === 'agent') { $agentIds[] = (int) $row->owner_id; } if ((string) $row->counterparty_type === 'agent' && (int) $row->counterparty_id > 0) { $agentIds[] = (int) $row->counterparty_id; } } $players = $playerIds !== [] ? DB::table('players') ->whereIn('id', array_unique($playerIds)) ->select(['id', 'username', 'site_player_id', 'funding_mode', 'auth_source']) ->get() ->keyBy('id') : collect(); $agents = $agentIds !== [] ? DB::table('agent_nodes')->whereIn('id', array_unique($agentIds))->get()->keyBy('id') : collect(); $out = []; foreach ($items as $row) { $item = (array) $row; $item['owner_label'] = $this->resolvePartyLabel( (string) $row->owner_type, (int) $row->owner_id, $players, $agents, ); $item['counterparty_label'] = $this->resolvePartyLabel( (string) $row->counterparty_type, (int) $row->counterparty_id, $players, $agents, ); if ((string) $row->owner_type === 'player') { $player = $players->get((int) $row->owner_id); $item['owner_funding_mode'] = $player !== null ? (string) ($player->funding_mode ?? '') : null; $item['owner_auth_source'] = $player !== null ? $player->auth_source : null; } $out[] = $item; } return $out; } /** * @param Collection $players * @param Collection $agents */ private function resolvePartyLabel( string $type, int $id, Collection $players, Collection $agents, ): string { if ($type === 'platform' || $id <= 0) { return 'platform'; } if ($type === 'player') { $player = $players->get($id); return $player !== null ? (string) ($player->username ?: $player->site_player_id) : "player#{$id}"; } if ($type === 'agent') { $agent = $agents->get($id); return $agent !== null ? (string) ($agent->name ?: $agent->code) : "agent#{$id}"; } return "{$type}#{$id}"; } }