hours->unbilledUntil($day); $jobs = $this->jobs->infoOf(array_values(array_unique(array_map(fn (UnbilledEntry $entry): int => $entry->jobId, $entries)))); $workTypes = $this->catalog->workTypes(); $byJob = []; foreach ($entries as $entry) { $job = $jobs[$entry->jobId] ?? null; if ($job === null || ! $job->isBillable) { continue; } if ($serviceUnitId !== null && ! in_array($serviceUnitId, $job->serviceUnitIds, true)) { continue; } $byJob[$job->id][] = $entry; } $rows = []; $valueByServiceUnit = []; foreach ($byJob as $jobId => $jobEntries) { $job = $jobs[$jobId]; $lines = []; $missingRate = false; foreach ($jobEntries as $entry) { $workType = $workTypes[$entry->workTypeId] ?? null; $rate = $workType?->rateCents; $value = null; if ($job->billsHours) { if ($rate === null) { $missingRate = true; } else { // Rounded half up to a cent, as on invoice lines. $value = intdiv(($entry->approvedQuarters + $entry->unapprovedQuarters) * $rate * 2 + 4, 8); $unit = $workType->serviceUnitName; $valueByServiceUnit[$unit] = ($valueByServiceUnit[$unit] ?? 0) + $value; } } $lines[] = new UnbilledWorkType($workType->name ?? '', $workType->serviceUnitName ?? '', $entry->approvedQuarters, $entry->unapprovedQuarters, $rate, $value); } $rows[] = new UnbilledJob( $job, array_sum(array_map(fn (UnbilledEntry $entry): int => $entry->approvedQuarters, $jobEntries)), array_sum(array_map(fn (UnbilledEntry $entry): int => $entry->unapprovedQuarters, $jobEntries)), $job->billsHours ? array_sum(array_map(fn (UnbilledWorkType $line): int => $line->valueCents ?? 0, $lines)) : null, $missingRate, min(array_map(fn (UnbilledEntry $entry): string => $entry->oldestDay, $jobEntries)), $lines, ); } usort($rows, fn (UnbilledJob $a, UnbilledJob $b): int => [$a->oldestDay, $a->job->number] <=> [$b->oldestDay, $b->job->number]); arsort($valueByServiceUnit); return new UnbilledReport( $rows, array_sum(array_map(fn (UnbilledJob $row): int => $row->approvedQuarters, $rows)), array_sum(array_map(fn (UnbilledJob $row): int => $row->unapprovedQuarters, $rows)), array_sum(array_map(fn (UnbilledJob $row): int => $row->valueCents ?? 0, $rows)), $valueByServiceUnit, $this->catalog->serviceUnits(), ); } }