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
@@ -0,0 +1,57 @@
<?php
namespace App\Modules\Reports\Presentation\Csv;
use App\Modules\Reports\Application\UseCases\ProfitabilityByJob;
use App\Modules\Reports\Presentation\ReportFormat;
use Illuminate\Http\Request;
/**
* Profitability as CSV: a row per job, amounts in cents (VAT 0); the hour
* cost and margin are empty when no internal hourly cost is set.
*/
class ProfitabilityCsv implements ReportCsv
{
public function __construct(private ProfitabilityByJob $report) {}
public function fileName(Request $request): string
{
$period = CsvFilters::period($request);
return __('reports.profitability.file').'-'.$period->from.'-'.$period->to;
}
public function headers(): array
{
return array_values(__('reports.profitability.csv'));
}
public function rows(Request $request): iterable
{
$report = $this->report->execute(CsvFilters::period($request), CsvFilters::flag($request, 'sisaiset'));
$serviceUnits = [];
foreach ($report->serviceUnits as $unit) {
$serviceUnits[$unit->serviceUnitId] = $unit->name;
}
foreach ($report->jobs as $row) {
$profit = $row->profit;
yield [
$row->job->number,
$row->job->name,
$row->job->customerName,
implode(', ', array_map(fn (int $id): string => $serviceUnits[$id] ?? '', $row->job->serviceUnitIds)),
$row->job->paymentBasis === null ? '' : __('jobs.payment_bases.'.$row->job->paymentBasis),
ReportFormat::csvHours($profit->quarters),
ReportFormat::csvHours($profit->billedQuarters),
$profit->workCents,
$profit->expenseRevenueCents,
$profit->expenseCostCents,
$profit->hourCostCents ?? '',
$profit->marginCents() ?? '',
];
}
}
}
@@ -4,6 +4,7 @@ namespace App\Modules\Reports\Presentation\Http\Controllers;
use App\Modules\Reports\Domain\Exceptions\InvalidReportPeriod;
use App\Modules\Reports\Presentation\Csv\HoursCsv;
use App\Modules\Reports\Presentation\Csv\ProfitabilityCsv;
use App\Modules\Reports\Presentation\Csv\ReimbursementsCsv;
use App\Modules\Reports\Presentation\Csv\ReportCsv;
use App\Modules\Reports\Presentation\Csv\SalesCsv;
@@ -21,7 +22,7 @@ use Symfony\Component\HttpFoundation\StreamedResponse;
class DownloadReport
{
/** The reports that have a CSV, by their URL key. */
public const array REPORTS = ['hours', 'unbilled', 'sales', 'reimbursements'];
public const array REPORTS = ['hours', 'unbilled', 'profitability', 'sales', 'reimbursements'];
public function __invoke(string $report, Request $request): StreamedResponse
{
@@ -75,6 +76,7 @@ class DownloadReport
return match ($report) {
'hours' => app(HoursCsv::class),
'unbilled' => app(UnbilledCsv::class),
'profitability' => app(ProfitabilityCsv::class),
'sales' => app(SalesCsv::class),
'reimbursements' => app(ReimbursementsCsv::class),
default => abort(404),
@@ -0,0 +1,217 @@
<?php
use App\Modules\Reports\Application\DTOs\ProfitabilityReport;
use App\Modules\Reports\Application\UseCases\ProfitabilityByJob;
use App\Modules\Reports\Domain\ValueObjects\Profit;
use App\Modules\Reports\Presentation\Livewire\Concerns\ReportPage;
use App\Modules\Users\Application\Contracts\Clock;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Url;
use Livewire\Component;
/**
* Profitability by service unit and by job (spec 13) in a period: what
* was billed less the expenses' purchase costs and the hours at the
* internal hourly cost (set on the price list maintenance page).
*/
new class extends Component
{
use ReportPage;
#[Url(as: 'sisaiset', except: false)]
public bool $includeInternal = false;
public function mount(Clock $clock): void
{
$this->startPeriod($clock, 'this_year');
}
#[Computed]
public function report(): ?ProfitabilityReport
{
$period = $this->period();
return $period === null ? null : app(ProfitabilityByJob::class)->execute($period, $this->includeInternal);
}
public function updated(): void
{
$this->authorize('reports.view');
unset($this->report);
}
public function margin(Profit $profit): string
{
$margin = $profit->marginCents();
return $margin === null ? '–' : $this->money($margin);
}
public function marginPercent(Profit $profit): string
{
$ratio = $profit->marginRatio();
return $ratio === null ? '–' : $this->percent($ratio);
}
public function hourCost(Profit $profit): string
{
return $profit->hourCostCents === null ? '–' : $this->money($profit->hourCostCents);
}
public function effectiveRate(Profit $profit): string
{
$cents = $profit->effectiveHourlyCents();
return $cents === null ? '–' : $this->money($cents);
}
public function render(): mixed
{
return $this->view()->title(__('reports.profitability.title'));
}
};
?>
<div class="mx-auto max-w-7xl space-y-6 p-4 sm:p-6">
<div class="space-y-2">
<h1 class="text-2xl font-semibold">{{ __('reports.profitability.title') }}</h1>
<p class="max-w-3xl text-sm">{{ __('reports.profitability.intro') }}</p>
</div>
<x-reports.period-filter :presets="$this->periodPresets()" :valid="$this->period !== null" />
<x-checkbox id="include-internal" wire:model.live="includeInternal" :label="__('reports.profitability.include_internal')" />
@php($report = $this->report)
@if ($report !== null)
<div class="flex flex-wrap items-center justify-between gap-3">
<h2 class="text-xl font-semibold" role="status" aria-live="polite">{{ $this->periodLabel() }}</h2>
<x-button :href="$this->csvUrl('profitability', ['sisaiset' => $includeInternal ? '1' : ''])" subtle color="black" icon="arrow-down-tray" :text="__('reports.download_csv')" />
</div>
@if ($report->hourlyCostCents === null)
<div class="flex items-start gap-2 rounded-lg border border-amber-700 p-4 text-sm dark:border-amber-400">
<x-icon name="exclamation-triangle" class="size-5 shrink-0" aria-hidden="true" />
<p>
{{ __('reports.profitability.no_cost') }}
@can('catalog.manage')
<x-link :href="route('catalog.prices').'#internal-cost-heading'">{{ __('reports.profitability.set_cost') }}</x-link>
@endcan
</p>
</div>
@else
<p class="text-sm">{{ __('reports.profitability.cost_used', ['cost' => $this->money($report->hourlyCostCents)]) }}</p>
@endif
@if ($report->jobs === [])
<p>{{ __('reports.profitability.none') }}</p>
@else
<section aria-labelledby="units-heading" class="space-y-4">
<h2 id="units-heading" class="text-xl font-semibold">{{ __('reports.profitability.by_service_unit') }}</h2>
<p class="text-sm">{{ __('reports.profitability.split_note') }}</p>
<div class="overflow-x-auto">
<table class="w-full text-sm">
<caption class="sr-only">{{ __('reports.profitability.units_table', ['period' => $this->periodLabel()]) }}</caption>
<thead>
<tr class="border-b border-zinc-300 text-left dark:border-zinc-600">
<th scope="col" class="py-2 pr-3">{{ __('reports.profitability.service_unit') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.profitability.jobs') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.profitability.hours') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.profitability.revenue') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.profitability.costs') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.profitability.margin') }}</th>
<th scope="col" class="py-2 text-right">{{ __('reports.profitability.margin_percent') }}</th>
</tr>
</thead>
<tbody class="divide-y divide-zinc-200 dark:divide-zinc-700">
@foreach ($report->serviceUnits as $unit)
<tr wire:key="unit-{{ $unit->serviceUnitId }}">
<th scope="row" class="py-2 pr-3 text-left font-normal">{{ $unit->name !== '' ? $unit->name : __('reports.profitability.no_service_unit') }}</th>
<td class="py-2 pr-3 text-right">{{ $unit->jobCount }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($unit->profit->quarters) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->money($unit->profit->revenueCents()) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->money($unit->profit->expenseCostCents + ($unit->profit->hourCostCents ?? 0)) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->margin($unit->profit) }}</td>
<td class="py-2 text-right whitespace-nowrap">{{ $this->marginPercent($unit->profit) }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@if ($report->hourlyCostCents !== null)
<x-reports.chart
:title="__('reports.profitability.chart')"
:summary="implode(', ', array_map(fn ($unit) => $unit->name.' '.$this->margin($unit->profit), $report->serviceUnits))"
:labels="array_map(fn ($unit) => $unit->name, $report->serviceUnits)"
:values="array_map(fn ($unit) => ($unit->profit->marginCents() ?? 0) / 100, $report->serviceUnits)"
:formatter="fn (float $value) => number_format($value, 0, ',', ' ').' €'"
/>
@endif
</section>
<section aria-labelledby="jobs-heading" class="space-y-4">
<h2 id="jobs-heading" class="text-xl font-semibold">{{ __('reports.profitability.by_job') }}</h2>
<div class="overflow-x-auto">
<table class="w-full text-sm">
<caption class="sr-only">{{ __('reports.profitability.jobs_table', ['period' => $this->periodLabel()]) }}</caption>
<thead>
<tr class="border-b border-zinc-300 text-left align-bottom dark:border-zinc-600">
<th scope="col" class="py-2 pr-3">{{ __('reports.profitability.job') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.profitability.hours') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.profitability.billed_hours') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.profitability.work_revenue') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.profitability.expense_revenue') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.profitability.expense_cost') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.profitability.hour_cost') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.profitability.margin') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.profitability.margin_percent') }}</th>
<th scope="col" class="py-2 text-right">{{ __('reports.profitability.effective_rate') }}</th>
</tr>
</thead>
<tbody class="divide-y divide-zinc-200 dark:divide-zinc-700">
@foreach ($report->jobs as $row)
<tr wire:key="job-{{ $row->job->id }}" class="align-top">
<th scope="row" class="py-2 pr-3 text-left font-normal">
<x-link :href="route('jobs.show', $row->job->id)" navigate>{{ $row->job->number }} {{ $row->job->name }}</x-link>
<span class="block text-xs text-zinc-600 dark:text-zinc-300">
{{ $row->job->customerName }}@if ($row->job->paymentBasis !== null), {{ __('jobs.payment_bases.'.$row->job->paymentBasis) }}@endif
@if ($row->job->quotedAmountCents !== null), {{ __('reports.profitability.quoted', ['amount' => $this->money($row->job->quotedAmountCents)]) }}@endif
</span>
</th>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($row->profit->quarters) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($row->profit->billedQuarters) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->money($row->profit->workCents) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->money($row->profit->expenseRevenueCents) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->money($row->profit->expenseCostCents) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hourCost($row->profit) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->margin($row->profit) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->marginPercent($row->profit) }}</td>
<td class="py-2 text-right whitespace-nowrap">{{ $this->effectiveRate($row->profit) }}</td>
</tr>
@endforeach
</tbody>
<tfoot>
<tr class="border-t-2 border-zinc-400 font-semibold dark:border-zinc-500">
<th scope="row" class="py-2 pr-3 text-left">{{ __('reports.total') }}</th>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($report->total->quarters) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($report->total->billedQuarters) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->money($report->total->workCents) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->money($report->total->expenseRevenueCents) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->money($report->total->expenseCostCents) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hourCost($report->total) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->margin($report->total) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->marginPercent($report->total) }}</td>
<td class="py-2 text-right whitespace-nowrap">{{ $this->effectiveRate($report->total) }}</td>
</tr>
</tfoot>
</table>
</div>
</section>
@endif
@endif
</div>
@@ -9,6 +9,7 @@ Route::middleware(['web', 'auth', 'can:reports.view'])
->group(function () {
Route::livewire('/hours', 'reports::hours')->name('hours');
Route::livewire('/unbilled', 'reports::unbilled')->name('unbilled');
Route::livewire('/profitability', 'reports::profitability')->name('profitability');
Route::livewire('/sales', 'reports::sales')->name('sales');
Route::livewire('/reimbursements', 'reports::reimbursements')->name('reimbursements');
Route::get('/{report}/csv', DownloadReport::class)->whereIn('report', DownloadReport::REPORTS)->name('csv');