expenses->employeePaid($period); $jobs = $this->jobs->infoOf(array_values(array_unique(array_map(fn (ReimbursementEntry $entry): int => $entry->jobId, $entries)))); $names = $this->people->namesByIds(array_values(array_unique(array_map(fn (ReimbursementEntry $entry): int => $entry->userId, $entries)))); $byPerson = []; foreach ($entries as $entry) { $job = $jobs[$entry->jobId] ?? null; $byPerson[$entry->userId][] = new ReimbursementLine($entry, $job->number ?? '', $job->name ?? ''); } $people = []; foreach ($byPerson as $userId => $lines) { $people[] = new PersonReimbursements( $userId, $names[$userId] ?? '', $lines, array_sum(array_map(fn (ReimbursementLine $line): int => $line->expense->kilometerTenths ?? 0, $lines)), array_sum(array_map(fn (ReimbursementLine $line): int => $line->expense->costCents, $lines)), ); } usort($people, fn (PersonReimbursements $a, PersonReimbursements $b): int => strnatcasecmp($a->name, $b->name)); return new ReimbursementReport( $people, array_sum(array_map(fn (PersonReimbursements $person): int => $person->kilometerTenths, $people)), array_sum(array_map(fn (PersonReimbursements $person): int => $person->costCents, $people)), ); } }