Management's reports (spec 13) get their own module that reads the other modules through ports. The first report sums hours by person in a period, narrowed by service unit or customer, with breakdowns, a chart and CSV. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
158 lines
5.6 KiB
PHP
158 lines
5.6 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\Contracts\ReportPeople;
|
|
use App\Modules\Reports\Application\DTOs\HoursBreakdown;
|
|
use App\Modules\Reports\Application\DTOs\HoursEntry;
|
|
use App\Modules\Reports\Application\DTOs\HoursLine;
|
|
use App\Modules\Reports\Application\DTOs\HoursReport;
|
|
use App\Modules\Reports\Application\DTOs\PersonHours;
|
|
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
|
|
|
|
/**
|
|
* Hours by person in a period (spec 13), optionally narrowed to the jobs
|
|
* of one service unit or one customer. Internal work is work on the
|
|
* internal customer's jobs.
|
|
*/
|
|
final readonly class HoursByPerson
|
|
{
|
|
public function __construct(
|
|
private ReportHours $hours,
|
|
private ReportJobs $jobs,
|
|
private ReportCatalog $catalog,
|
|
private ReportPeople $people,
|
|
) {}
|
|
|
|
public function execute(ReportPeriod $period, ?int $serviceUnitId = null, ?int $customerId = null): HoursReport
|
|
{
|
|
$entries = $this->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<HoursLine> $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<PersonHours> $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<T> $items
|
|
* @param callable(T): int $value
|
|
*/
|
|
private static function sum(array $items, callable $value): int
|
|
{
|
|
return array_sum(array_map($value, $items));
|
|
}
|
|
}
|