hours->between($period); $jobs = $this->jobs->infoOf(array_values(array_unique(array_map(fn (HoursEntry $entry): int => $entry->jobId, $entries)))); $workTypes = $this->catalog->workTypes(); $names = $this->people->namesByIds(array_values(array_unique(array_map(fn (HoursEntry $entry): int => $entry->userId, $entries)))); $customers = []; $lines = []; foreach ($entries as $entry) { $job = $jobs[$entry->jobId] ?? null; if ($job === null) { continue; } $customers[$job->customerId] = $job->customerName; if ($serviceUnitId !== null && ! in_array($serviceUnitId, $job->serviceUnitIds, true)) { continue; } if ($customerId !== null && $job->customerId !== $customerId) { continue; } $workType = $workTypes[$entry->workTypeId] ?? null; $lines[] = new HoursLine( $entry->userId, $names[$entry->userId] ?? '', $job->id, $job->number, $job->name, $job->customerName, $job->isInternal, $workType->name ?? '', $workType->serviceUnitName ?? '', $entry->quarters, $entry->unapprovedQuarters, $entry->billedQuarters, ); } usort($lines, fn (HoursLine $a, HoursLine $b): int => [$a->userName, $a->userId, $a->jobNumber, $a->workTypeName] <=> [$b->userName, $b->userId, $b->jobNumber, $b->workTypeName]); asort($customers, SORT_NATURAL | SORT_FLAG_CASE); $byPerson = []; foreach ($lines as $line) { $byPerson[$line->userId][] = $line; } $people = array_map(fn (array $personLines): PersonHours => $this->person($personLines), array_values($byPerson)); return new HoursReport($people, $this->total($people), $lines, $customers, $this->catalog->serviceUnits()); } /** * @param non-empty-list $lines */ private function person(array $lines): PersonHours { $byWorkType = []; $byJob = []; $jobLabels = []; foreach ($lines as $line) { $byWorkType[$line->workTypeName] = ($byWorkType[$line->workTypeName] ?? 0) + $line->quarters; $byJob[$line->jobId] = ($byJob[$line->jobId] ?? 0) + $line->quarters; $jobLabels[$line->jobId] = $line->jobNumber.' '.$line->jobName; } arsort($byWorkType); arsort($byJob); $workTypeRows = []; foreach ($byWorkType as $label => $quarters) { $workTypeRows[] = new HoursBreakdown((string) $label, $quarters); } $jobRows = []; foreach ($byJob as $jobId => $quarters) { $jobRows[] = new HoursBreakdown($jobLabels[$jobId], $quarters, $jobId); } return new PersonHours( $lines[0]->userId, $lines[0]->userName, self::sum($lines, fn (HoursLine $line): int => $line->quarters), self::sum($lines, fn (HoursLine $line): int => $line->isInternal ? 0 : $line->quarters), self::sum($lines, fn (HoursLine $line): int => $line->isInternal ? $line->quarters : 0), self::sum($lines, fn (HoursLine $line): int => $line->unapprovedQuarters), self::sum($lines, fn (HoursLine $line): int => $line->billedQuarters), $workTypeRows, $jobRows, ); } /** * @param list $people */ private function total(array $people): PersonHours { return new PersonHours( 0, '', self::sum($people, fn (PersonHours $person): int => $person->quarters), self::sum($people, fn (PersonHours $person): int => $person->customerQuarters), self::sum($people, fn (PersonHours $person): int => $person->internalQuarters), self::sum($people, fn (PersonHours $person): int => $person->unapprovedQuarters), self::sum($people, fn (PersonHours $person): int => $person->billedQuarters), ); } /** * @template T * * @param list $items * @param callable(T): int $value */ private static function sum(array $items, callable $value): int { return array_sum(array_map($value, $items)); } }