Add the profitability report and the internal hourly cost

Profitability by job and service unit from a period's hours, expenses
and the created billing materials of its months. Hours are costed at one
internal hourly cost, set on the price list maintenance page; a job of
several service units is shared evenly between them.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-25 08:50:30 +02:00
co-authored by Claude Sonnet 5.5
parent 8079c272d2
commit 8aa62c8e02
31 changed files with 1110 additions and 1 deletions
@@ -24,4 +24,9 @@ interface ReportCatalog
* @return array<int, string>
*/
public function serviceUnits(): array;
/**
* The internal cost of an hour of work in cents (VAT 0), null when not set.
*/
public function internalHourlyCostCents(): ?int;
}
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace App\Modules\Reports\Application\Contracts;
use App\Modules\Reports\Application\DTOs\JobRevenue;
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
/**
* What was billed, from the Billing module.
*/
interface ReportRevenue
{
/**
* Amounts billed by the created billing materials of the billing months
* the period touches, by job.
*
* @return array<int, JobRevenue> Keyed by job id.
*/
public function byJob(ReportPeriod $period): array;
}
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\Modules\Reports\Application\DTOs;
use App\Modules\Reports\Domain\ValueObjects\Profit;
/**
* A job's profitability in the period.
*/
final readonly class JobProfit
{
public function __construct(
public ReportJob $job,
public Profit $profit,
) {}
}
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace App\Modules\Reports\Application\DTOs;
/**
* A job's billed amounts without VAT in cents: for the work (hours, fixed
* price, monthly fees) and for expenses, and the billed hours in quarters.
*/
final readonly class JobRevenue
{
public function __construct(
public int $jobId,
public int $workCents,
public int $expenseCents,
public int $billedQuarters,
) {}
}
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace App\Modules\Reports\Application\DTOs;
use App\Modules\Reports\Domain\ValueObjects\Profit;
/**
* Profitability by job (by job number) and by service unit (catalog
* order), the total, and the internal hourly cost used (null when not set,
* and then there are no hour costs or margins).
*/
final readonly class ProfitabilityReport
{
/**
* @param list<JobProfit> $jobs
* @param list<ServiceUnitProfit> $serviceUnits
*/
public function __construct(
public array $jobs,
public array $serviceUnits,
public Profit $total,
public ?int $hourlyCostCents,
) {}
}
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace App\Modules\Reports\Application\DTOs;
use App\Modules\Reports\Domain\ValueObjects\Profit;
/**
* A service unit's profitability in the period: its jobs' figures, a job
* of several service units shared evenly between them.
*/
final readonly class ServiceUnitProfit
{
public function __construct(
public int $serviceUnitId,
public string $name,
public int $jobCount,
public Profit $profit,
) {}
}
@@ -0,0 +1,102 @@
<?php
declare(strict_types=1);
namespace App\Modules\Reports\Application\UseCases;
use App\Modules\Reports\Application\Contracts\ReportCatalog;
use App\Modules\Reports\Application\Contracts\ReportExpenses;
use App\Modules\Reports\Application\Contracts\ReportHours;
use App\Modules\Reports\Application\Contracts\ReportJobs;
use App\Modules\Reports\Application\Contracts\ReportRevenue;
use App\Modules\Reports\Application\DTOs\JobProfit;
use App\Modules\Reports\Application\DTOs\ProfitabilityReport;
use App\Modules\Reports\Application\DTOs\ServiceUnitProfit;
use App\Modules\Reports\Domain\ValueObjects\Profit;
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
/**
* Profitability by job and by service unit (spec 13) from what happened
* in a period: hours worked and expenses spent in it, and what the created
* billing materials of its billing months billed. Hours cost the company's
* one internal hourly cost. A job of several service units is shared
* evenly between them. Internal jobs are left out unless asked for.
*/
final readonly class ProfitabilityByJob
{
public function __construct(
private ReportHours $hours,
private ReportExpenses $expenses,
private ReportRevenue $revenue,
private ReportJobs $jobs,
private ReportCatalog $catalog,
) {}
public function execute(ReportPeriod $period, bool $includeInternal = false): ProfitabilityReport
{
$quarters = [];
foreach ($this->hours->between($period) as $entry) {
$quarters[$entry->jobId] = ($quarters[$entry->jobId] ?? 0) + $entry->quarters;
}
$expenseCosts = $this->expenses->costsByJob($period);
$revenue = $this->revenue->byJob($period);
$hourlyCost = $this->catalog->internalHourlyCostCents();
$jobIds = array_values(array_unique([...array_keys($quarters), ...array_keys($expenseCosts), ...array_keys($revenue)]));
$jobs = $this->jobs->infoOf($jobIds);
$rows = [];
foreach ($jobs as $job) {
if ($job->isInternal && ! $includeInternal) {
continue;
}
$billed = $revenue[$job->id] ?? null;
$rows[] = new JobProfit($job, Profit::of(
$quarters[$job->id] ?? 0,
$billed->billedQuarters ?? 0,
$billed->workCents ?? 0,
$billed->expenseCents ?? 0,
$expenseCosts[$job->id] ?? 0,
$hourlyCost,
));
}
usort($rows, fn (JobProfit $a, JobProfit $b): int => strcmp($a->job->number, $b->job->number));
$total = array_reduce($rows, fn (Profit $sum, JobProfit $row): Profit => $sum->plus($row->profit), Profit::of(0, 0, 0, 0, 0, $hourlyCost));
return new ProfitabilityReport($rows, $this->byServiceUnit($rows, $hourlyCost), $total, $hourlyCost);
}
/**
* @param list<JobProfit> $rows
* @return list<ServiceUnitProfit>
*/
private function byServiceUnit(array $rows, ?int $hourlyCost): array
{
$names = $this->catalog->serviceUnits();
$profits = [];
$jobCounts = [];
foreach ($rows as $row) {
$unitIds = $row->job->serviceUnitIds === [] ? [0] : $row->job->serviceUnitIds;
foreach ($row->profit->split(count($unitIds)) as $index => $share) {
$unitId = $unitIds[$index];
$profits[$unitId] = ($profits[$unitId] ?? Profit::of(0, 0, 0, 0, 0, $hourlyCost))->plus($share);
$jobCounts[$unitId] = ($jobCounts[$unitId] ?? 0) + 1;
}
}
$result = [];
foreach ([...array_keys($names), 0] as $unitId) {
if (isset($profits[$unitId])) {
$result[] = new ServiceUnitProfit($unitId, $names[$unitId] ?? '', $jobCounts[$unitId], $profits[$unitId]);
}
}
return $result;
}
}