Hours on customers' billable jobs that no billing material has taken, by job and work type up to a day, valued at the hourly rates for jobs billed by the hour, with a chart by service unit and CSV. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
102 lines
4.0 KiB
PHP
102 lines
4.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Reports\Application\UseCases;
|
|
|
|
use App\Modules\Reports\Application\Contracts\ReportCatalog;
|
|
use App\Modules\Reports\Application\Contracts\ReportHours;
|
|
use App\Modules\Reports\Application\Contracts\ReportJobs;
|
|
use App\Modules\Reports\Application\DTOs\UnbilledEntry;
|
|
use App\Modules\Reports\Application\DTOs\UnbilledJob;
|
|
use App\Modules\Reports\Application\DTOs\UnbilledReport;
|
|
use App\Modules\Reports\Application\DTOs\UnbilledWorkType;
|
|
|
|
/**
|
|
* Unbilled hours (spec 13): the hours worked on customers' billable jobs
|
|
* up to a day that no billing material has taken yet, approved or not.
|
|
* Their value is the hours at the work types' hourly rates for jobs billed
|
|
* by their hours; a fixed price or monthly fee already covers the hours of
|
|
* other jobs. Internal and not billable jobs are left out.
|
|
*/
|
|
final readonly class UnbilledHoursReport
|
|
{
|
|
public function __construct(
|
|
private ReportHours $hours,
|
|
private ReportJobs $jobs,
|
|
private ReportCatalog $catalog,
|
|
) {}
|
|
|
|
public function execute(string $day, ?int $serviceUnitId = null): UnbilledReport
|
|
{
|
|
$entries = $this->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(),
|
|
);
|
|
}
|
|
}
|