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:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user