Add Reports module with the hours by person report
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]>
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Reports\Application\Contracts;
|
||||
|
||||
use App\Modules\Reports\Application\DTOs\ReportWorkType;
|
||||
|
||||
/**
|
||||
* Service units, work types and their prices, from the Catalog module.
|
||||
*/
|
||||
interface ReportCatalog
|
||||
{
|
||||
/**
|
||||
* Every work type, inactive ones included.
|
||||
*
|
||||
* @return array<int, ReportWorkType>
|
||||
*/
|
||||
public function workTypes(): array;
|
||||
|
||||
/**
|
||||
* Every service unit's name in catalog order, inactive ones included.
|
||||
*
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public function serviceUnits(): array;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Reports\Application\Contracts;
|
||||
|
||||
use App\Modules\Reports\Application\DTOs\HoursEntry;
|
||||
use App\Modules\Reports\Application\DTOs\UnbilledEntry;
|
||||
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
|
||||
|
||||
/**
|
||||
* Recorded hours, from the TimeTracking module.
|
||||
*/
|
||||
interface ReportHours
|
||||
{
|
||||
/**
|
||||
* Hours worked in the period by user, job and work type.
|
||||
*
|
||||
* @return list<HoursEntry>
|
||||
*/
|
||||
public function between(ReportPeriod $period): array;
|
||||
|
||||
/**
|
||||
* Unbilled hours worked on or before the day (Y-m-d) by job and work type.
|
||||
*
|
||||
* @return list<UnbilledEntry>
|
||||
*/
|
||||
public function unbilledUntil(string $day): array;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Reports\Application\Contracts;
|
||||
|
||||
use App\Modules\Reports\Application\DTOs\ReportJob;
|
||||
|
||||
/**
|
||||
* Customer jobs, from the Jobs and Customers modules.
|
||||
*/
|
||||
interface ReportJobs
|
||||
{
|
||||
/**
|
||||
* @param list<int> $ids
|
||||
* @return array<int, ReportJob> Keyed by id; unknown ids are left out.
|
||||
*/
|
||||
public function infoOf(array $ids): array;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Reports\Application\Contracts;
|
||||
|
||||
/**
|
||||
* Users' names, from the Users module.
|
||||
*/
|
||||
interface ReportPeople
|
||||
{
|
||||
/**
|
||||
* @param list<int> $ids
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public function namesByIds(array $ids): array;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Reports\Application\DTOs;
|
||||
|
||||
/**
|
||||
* A part of a person's hours, e.g. one work type or one job, in quarter hours.
|
||||
*/
|
||||
final readonly class HoursBreakdown
|
||||
{
|
||||
public function __construct(
|
||||
public string $label,
|
||||
public int $quarters,
|
||||
public ?int $jobId = null,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Reports\Application\DTOs;
|
||||
|
||||
/**
|
||||
* A user's hours on a job's work type in quarter hours: all, those waiting
|
||||
* for approval and those billed.
|
||||
*/
|
||||
final readonly class HoursEntry
|
||||
{
|
||||
public function __construct(
|
||||
public int $userId,
|
||||
public int $jobId,
|
||||
public int $workTypeId,
|
||||
public int $quarters,
|
||||
public int $unapprovedQuarters,
|
||||
public int $billedQuarters,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Reports\Application\DTOs;
|
||||
|
||||
/**
|
||||
* One row of the hours report: a person's hours on a job's work type, in
|
||||
* quarter hours.
|
||||
*/
|
||||
final readonly class HoursLine
|
||||
{
|
||||
public function __construct(
|
||||
public int $userId,
|
||||
public string $userName,
|
||||
public int $jobId,
|
||||
public string $jobNumber,
|
||||
public string $jobName,
|
||||
public string $customerName,
|
||||
public bool $isInternal,
|
||||
public string $workTypeName,
|
||||
public string $serviceUnitName,
|
||||
public int $quarters,
|
||||
public int $unapprovedQuarters,
|
||||
public int $billedQuarters,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Reports\Application\DTOs;
|
||||
|
||||
/**
|
||||
* Hours by person: a row per person by name, their total, the rows behind
|
||||
* them, and the customers and service units the hours can be narrowed to.
|
||||
*/
|
||||
final readonly class HoursReport
|
||||
{
|
||||
/**
|
||||
* @param list<PersonHours> $people
|
||||
* @param list<HoursLine> $lines
|
||||
* @param array<int, string> $customers
|
||||
* @param array<int, string> $serviceUnits
|
||||
*/
|
||||
public function __construct(
|
||||
public array $people,
|
||||
public PersonHours $total,
|
||||
public array $lines,
|
||||
public array $customers,
|
||||
public array $serviceUnits,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Reports\Application\DTOs;
|
||||
|
||||
/**
|
||||
* A person's hours in the period in quarter hours: in all, on customers'
|
||||
* and on internal jobs, waiting for approval and billed, broken down by
|
||||
* work type and by job (most hours first).
|
||||
*/
|
||||
final readonly class PersonHours
|
||||
{
|
||||
/**
|
||||
* @param list<HoursBreakdown> $byWorkType
|
||||
* @param list<HoursBreakdown> $byJob
|
||||
*/
|
||||
public function __construct(
|
||||
public int $userId,
|
||||
public string $name,
|
||||
public int $quarters,
|
||||
public int $customerQuarters,
|
||||
public int $internalQuarters,
|
||||
public int $unapprovedQuarters,
|
||||
public int $billedQuarters,
|
||||
public array $byWorkType = [],
|
||||
public array $byJob = [],
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Reports\Application\DTOs;
|
||||
|
||||
/**
|
||||
* A customer job in a report. Status and payment basis are the Jobs
|
||||
* module's keys (e.g. "done", "hourly"). A job bills its hours when its
|
||||
* hours are priced by the hourly rates (hourly or price list work), and is
|
||||
* billable when it is a customer's job that is billed at all.
|
||||
*/
|
||||
final readonly class ReportJob
|
||||
{
|
||||
/**
|
||||
* @param list<int> $serviceUnitIds
|
||||
*/
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public string $number,
|
||||
public string $name,
|
||||
public int $customerId,
|
||||
public string $customerName,
|
||||
public bool $isInternal,
|
||||
public string $status,
|
||||
public ?string $paymentBasis,
|
||||
public bool $isBillable,
|
||||
public bool $billsHours,
|
||||
public ?int $quotedAmountCents,
|
||||
public array $serviceUnitIds,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Reports\Application\DTOs;
|
||||
|
||||
/**
|
||||
* A work type with its service unit and hourly rate in cents (VAT 0).
|
||||
*/
|
||||
final readonly class ReportWorkType
|
||||
{
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public string $name,
|
||||
public int $serviceUnitId,
|
||||
public string $serviceUnitName,
|
||||
public ?int $rateCents,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Reports\Application\DTOs;
|
||||
|
||||
/**
|
||||
* A job's unbilled hours on a work type in quarter hours, with the first
|
||||
* day worked (Y-m-d).
|
||||
*/
|
||||
final readonly class UnbilledEntry
|
||||
{
|
||||
public function __construct(
|
||||
public int $jobId,
|
||||
public int $workTypeId,
|
||||
public int $approvedQuarters,
|
||||
public int $unapprovedQuarters,
|
||||
public string $oldestDay,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
<?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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user