Add the unbilled hours report

Hours on customers' billable jobs that no billing material has taken,
by job and work type up to a day, valued at the hourly rates for jobs
billed by the hour, with a chart by service unit and CSV.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-25 08:41:43 +02:00
co-authored by Claude Sonnet 5.5
parent 64f1e7c54a
commit a57b383e4d
17 changed files with 697 additions and 45 deletions
@@ -0,0 +1,191 @@
<?php
use App\Modules\Reports\Application\DTOs\UnbilledJob;
use App\Modules\Reports\Application\DTOs\UnbilledReport;
use App\Modules\Reports\Application\UseCases\UnbilledHoursReport;
use App\Modules\Reports\Domain\Exceptions\InvalidReportPeriod;
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
use App\Modules\Reports\Presentation\Livewire\Concerns\FormatsReports;
use App\Modules\Users\Application\Contracts\Clock;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Url;
use Livewire\Component;
/**
* Unbilled hours (spec 13): hours on customers' billable jobs up to a day
* that no billing material has taken yet, by job and work type, with
* their value at the hourly rates when the job bills its hours.
*/
new class extends Component
{
use FormatsReports;
#[Url(as: 'paiva', except: '')]
public string $day = '';
#[Url(as: 'yksikko', except: '')]
public string $serviceUnit = '';
public function mount(Clock $clock): void
{
$this->authorize('reports.view');
if (! $this->dayIsValid()) {
$this->day = $clock->today()->format('Y-m-d');
}
}
#[Computed]
public function report(): ?UnbilledReport
{
if (! $this->dayIsValid()) {
return null;
}
return app(UnbilledHoursReport::class)->execute($this->day, ctype_digit($this->serviceUnit) ? (int) $this->serviceUnit : null);
}
public function updated(): void
{
$this->authorize('reports.view');
unset($this->report);
}
public function dayIsValid(): bool
{
try {
new ReportPeriod($this->day, $this->day);
return true;
} catch (InvalidReportPeriod) {
return false;
}
}
public function csv(): string
{
return route('reports.csv', array_filter(['report' => 'unbilled', 'paiva' => $this->day, 'yksikko' => $this->serviceUnit]));
}
public function valueText(UnbilledJob $row): string
{
if ($row->valueCents === null) {
return __('reports.unbilled.in_price');
}
return $this->money($row->valueCents).($row->missingRate ? ' '.__('reports.unbilled.missing_rate_short') : '');
}
public function render(): mixed
{
return $this->view()->title(__('reports.unbilled.title'));
}
};
?>
<div class="mx-auto max-w-6xl space-y-6 p-4 sm:p-6">
<div class="space-y-2">
<h1 class="text-2xl font-semibold">{{ __('reports.unbilled.title') }}</h1>
<p class="max-w-3xl text-sm">{{ __('reports.unbilled.intro') }}</p>
</div>
@php($report = $this->report)
<div class="grid max-w-2xl gap-4 sm:grid-cols-2">
<x-input id="report-day" wire:model.live.change="day" type="date" :label="__('reports.unbilled.day')" required />
@if ($report !== null)
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
<x-select.native wire:model.live="serviceUnit" :label="__('reports.filters.service_unit')" :aria-label="__('reports.filters.service_unit')">
<option value="">{{ __('reports.filters.all_service_units') }}</option>
@foreach ($report->serviceUnits as $id => $name)
<option value="{{ $id }}">{{ $name }}</option>
@endforeach
</x-select.native>
@endif
</div>
@if ($report === null)
<p role="alert" class="flex items-center gap-2 text-sm font-medium text-red-700 dark:text-red-300">
<x-icon name="exclamation-triangle" class="size-5" aria-hidden="true" />
{{ __('reports.unbilled.invalid_day') }}
</p>
@else
<div class="flex flex-wrap items-center justify-between gap-3">
<h2 class="text-xl font-semibold" role="status" aria-live="polite">{{ __('reports.unbilled.until', ['day' => $this->dateLabel($day)]) }}</h2>
<x-button :href="$this->csv()" subtle color="black" icon="arrow-down-tray" :text="__('reports.download_csv')" />
</div>
@if ($report->jobs === [])
<p>{{ __('reports.unbilled.none') }}</p>
@else
<div class="overflow-x-auto">
<table class="w-full text-sm">
<caption class="sr-only">{{ __('reports.unbilled.table', ['day' => $this->dateLabel($day)]) }}</caption>
<thead>
<tr class="border-b border-zinc-300 text-left dark:border-zinc-600">
<th scope="col" class="py-2 pr-3">{{ __('reports.unbilled.job') }}</th>
<th scope="col" class="py-2 pr-3">{{ __('reports.unbilled.status') }}</th>
<th scope="col" class="py-2 pr-3">{{ __('reports.unbilled.oldest') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.unbilled.approved') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.unbilled.unapproved') }}</th>
<th scope="col" class="py-2 text-right">{{ __('reports.unbilled.value') }}</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 }}</span>
<details class="mt-1">
<summary class="cursor-pointer text-xs font-medium focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary-600">
{{ __('reports.unbilled.by_work_type', ['job' => $row->job->number]) }}
</summary>
<ul class="mt-1 space-y-0.5 text-xs">
@foreach ($row->workTypes as $line)
<li>
{{ __('reports.unbilled.work_type_line', [
'name' => $line->name,
'approved' => $this->hours($line->approvedQuarters),
'unapproved' => $this->hours($line->unapprovedQuarters),
]) }}@if ($line->valueCents !== null), {{ $this->money($line->valueCents) }}@elseif ($row->job->billsHours) ({{ __('reports.unbilled.missing_rate') }})@endif
</li>
@endforeach
</ul>
</details>
</th>
<td class="py-2 pr-3">
{{ __('jobs.statuses.'.$row->job->status) }}
@if ($row->job->paymentBasis !== null)
<span class="block text-xs text-zinc-600 dark:text-zinc-300">{{ __('jobs.payment_bases.'.$row->job->paymentBasis) }}</span>
@endif
</td>
<td class="py-2 pr-3 whitespace-nowrap">{{ $this->dateLabel($row->oldestDay) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($row->approvedQuarters) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($row->unapprovedQuarters) }}</td>
<td class="py-2 text-right">{{ $this->valueText($row) }}</td>
</tr>
@endforeach
</tbody>
<tfoot>
<tr class="border-t-2 border-zinc-400 font-semibold dark:border-zinc-500">
<th scope="row" colspan="3" class="py-2 pr-3 text-left">{{ __('reports.total') }}</th>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($report->approvedQuarters) }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($report->unapprovedQuarters) }}</td>
<td class="py-2 text-right whitespace-nowrap">{{ $this->money($report->valueCents) }}</td>
</tr>
</tfoot>
</table>
</div>
<x-reports.chart
:title="__('reports.unbilled.chart')"
:summary="implode(', ', array_map(fn ($unit, $cents) => $unit.' '.$this->money($cents), array_keys($report->valueByServiceUnit), $report->valueByServiceUnit))"
:labels="array_keys($report->valueByServiceUnit)"
:values="array_map(fn ($cents) => $cents / 100, array_values($report->valueByServiceUnit))"
:formatter="fn (float $value) => number_format($value, 0, ',', ' ').' €'"
/>
@endif
@endif
</div>