$agents keyed by id * @return array{ * direct_agent_id: int|null, * direct_agent_label: string|null, * parent_agent_id: int|null, * parent_agent_label: string|null, * } */ public function agentLineLabels(?int $directAgentId, Collection $agents): array { if ($directAgentId === null || $directAgentId <= 0) { return [ 'direct_agent_id' => null, 'direct_agent_label' => null, 'parent_agent_id' => null, 'parent_agent_label' => null, ]; } $direct = $agents->get($directAgentId); $directLabel = $this->formatAgent($direct, $directAgentId); $parentId = $direct !== null ? (int) ($direct->parent_id ?? 0) : 0; $parent = $parentId > 0 ? $agents->get($parentId) : null; return [ 'direct_agent_id' => $directAgentId, 'direct_agent_label' => $directLabel, 'parent_agent_id' => $parentId > 0 ? $parentId : null, 'parent_agent_label' => $parentId > 0 ? $this->formatAgent($parent, $parentId) : null, ]; } /** * @param list $agentIds * @return Collection */ public function loadAgents(array $agentIds): Collection { $ids = array_values(array_unique(array_filter($agentIds, static fn (int $id): bool => $id > 0))); if ($ids === []) { return collect(); } $rows = DB::table('agent_nodes')->whereIn('id', $ids)->get()->keyBy('id'); $parentIds = $rows ->pluck('parent_id') ->map(static fn ($id): int => (int) $id) ->filter(static fn (int $id): bool => $id > 0) ->unique() ->values() ->all(); $missingParents = array_diff($parentIds, $ids); if ($missingParents !== []) { $parents = DB::table('agent_nodes')->whereIn('id', $missingParents)->get()->keyBy('id'); foreach ($parents as $id => $row) { $rows->put((int) $id, $row); } } return $rows; } /** * @param list $ticketItemIds * @return array */ public function loadTicketRefs(array $ticketItemIds): array { $ids = array_values(array_unique(array_filter($ticketItemIds, static fn (int $id): bool => $id > 0))); if ($ids === []) { return []; } $map = []; foreach (DB::table('ticket_items as ti') ->leftJoin('draws as d', 'd.id', '=', 'ti.draw_id') ->whereIn('ti.id', $ids) ->select(['ti.id', 'ti.play_code', 'ti.actual_deduct_amount', 'd.draw_no']) ->get() as $row) { $map[(int) $row->id] = [ 'ticket_item_id' => (int) $row->id, 'play_code' => $row->play_code !== null ? (string) $row->play_code : null, 'draw_no' => $row->draw_no !== null ? (string) $row->draw_no : null, 'actual_deduct_amount' => (int) ($row->actual_deduct_amount ?? 0), ]; } return $map; } public function formatAgent(?object $agent, int $fallbackId): string { if ($agent === null) { return "agent#{$fallbackId}"; } $name = trim((string) ($agent->name ?? '')); $code = trim((string) ($agent->code ?? '')); if ($name !== '' && $code !== '') { return "{$name} ({$code})"; } return $name !== '' ? $name : ($code !== '' ? $code : "agent#{$fallbackId}"); } public function formatPlayerUsername(?object $player): ?string { if ($player === null) { return null; } $username = trim((string) ($player->username ?? '')); return $username !== '' ? $username : null; } public function formatPlayerSiteId(?object $player): ?string { if ($player === null) { return null; } $sitePlayerId = trim((string) ($player->site_player_id ?? '')); return $sitePlayerId !== '' ? $sitePlayerId : null; } public function formatCounterpartyLabel(string $type, int $id, Collection $agents): string { if ($type === 'platform' || $id <= 0) { return 'platform'; } if ($type === 'agent') { return $this->formatAgent($agents->get($id), $id); } return "{$type}#{$id}"; } }