Add the expense reimbursements report

The expenses employees paid themselves in a period, by person with
kilometres and amounts, printable and as CSV for payroll.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-25 08:46:22 +02:00
co-authored by Claude Sonnet 5.5
parent 568871c9dc
commit 8079c272d2
21 changed files with 639 additions and 1 deletions
@@ -0,0 +1,46 @@
<?php
namespace App\Modules\Reports\Presentation\Csv;
use App\Modules\Reports\Application\UseCases\ReimbursementsReport;
use App\Modules\Reports\Presentation\ReportFormat;
use Illuminate\Http\Request;
/**
* Expense reimbursements as CSV: a row per expense, kilometres with a
* decimal comma and the cost in cents (VAT 0).
*/
class ReimbursementsCsv implements ReportCsv
{
public function __construct(private ReimbursementsReport $report) {}
public function fileName(Request $request): string
{
$period = CsvFilters::period($request);
return __('reports.reimbursements.file').'-'.$period->from.'-'.$period->to;
}
public function headers(): array
{
return array_values(__('reports.reimbursements.csv'));
}
public function rows(Request $request): iterable
{
foreach ($this->report->execute(CsvFilters::period($request))->people as $person) {
foreach ($person->lines as $line) {
yield [
$person->name,
$line->expense->spentOn,
__('expenses.categories.'.$line->expense->category),
ReportFormat::expenseText($line->expense),
$line->jobNumber,
$line->jobName,
$line->expense->kilometerTenths === null ? '' : ReportFormat::csvKilometers($line->expense->kilometerTenths),
$line->expense->costCents,
];
}
}
}
}
@@ -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\ReimbursementsCsv;
use App\Modules\Reports\Presentation\Csv\ReportCsv;
use App\Modules\Reports\Presentation\Csv\SalesCsv;
use App\Modules\Reports\Presentation\Csv\UnbilledCsv;
@@ -20,7 +21,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'];
public const array REPORTS = ['hours', 'unbilled', 'sales', 'reimbursements'];
public function __invoke(string $report, Request $request): StreamedResponse
{
@@ -75,6 +76,7 @@ class DownloadReport
'hours' => app(HoursCsv::class),
'unbilled' => app(UnbilledCsv::class),
'sales' => app(SalesCsv::class),
'reimbursements' => app(ReimbursementsCsv::class),
default => abort(404),
};
}
@@ -0,0 +1,125 @@
<?php
use App\Modules\Reports\Application\DTOs\ReimbursementEntry;
use App\Modules\Reports\Application\DTOs\ReimbursementReport;
use App\Modules\Reports\Application\UseCases\ReimbursementsReport;
use App\Modules\Reports\Presentation\Livewire\Concerns\ReportPage;
use App\Modules\Reports\Presentation\ReportFormat;
use App\Modules\Users\Application\Contracts\Clock;
use Livewire\Attributes\Computed;
use Livewire\Component;
/**
* Expense reimbursements (spec 10, 13): the expenses employees paid
* themselves in a period, by person, to print or download for payroll.
* Paying them is outside this system.
*/
new class extends Component
{
use ReportPage;
public function mount(Clock $clock): void
{
$this->startPeriod($clock, 'previous_month');
}
#[Computed]
public function report(): ?ReimbursementReport
{
$period = $this->period();
return $period === null ? null : app(ReimbursementsReport::class)->execute($period);
}
public function updated(): void
{
$this->authorize('reports.view');
unset($this->report);
}
public function kilometers(?int $tenths): string
{
return $tenths === null ? '' : ReportFormat::kilometers($tenths);
}
public function what(ReimbursementEntry $expense): string
{
return ReportFormat::expenseText($expense);
}
public function render(): mixed
{
return $this->view()->title(__('reports.reimbursements.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.reimbursements.title') }}</h1>
<p class="max-w-3xl text-sm">{{ __('reports.reimbursements.intro') }}</p>
</div>
<x-reports.period-filter class="print:hidden" :presets="$this->periodPresets()" :valid="$this->period !== null" />
@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>
<div class="flex flex-wrap gap-2 print:hidden">
<x-button x-on:click="window.print()" subtle color="black" icon="printer" :text="__('reports.reimbursements.print')" />
<x-button :href="$this->csvUrl('reimbursements')" subtle color="black" icon="arrow-down-tray" :text="__('reports.download_csv')" />
</div>
</div>
@if ($report->people === [])
<p>{{ __('reports.reimbursements.none') }}</p>
@else
<p class="font-semibold">
{{ __('reports.reimbursements.grand_total', ['amount' => $this->money($report->costCents), 'km' => $this->kilometers($report->kilometerTenths)]) }}
</p>
@foreach ($report->people as $person)
<section wire:key="person-{{ $person->userId }}" aria-labelledby="person-{{ $person->userId }}" class="space-y-2 break-inside-avoid">
<h3 id="person-{{ $person->userId }}" class="text-lg font-semibold">{{ $person->name }}</h3>
<div class="overflow-x-auto">
<table class="w-full text-sm">
<caption class="sr-only">{{ __('reports.reimbursements.table', ['name' => $person->name, '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.reimbursements.day') }}</th>
<th scope="col" class="py-2 pr-3">{{ __('reports.reimbursements.category') }}</th>
<th scope="col" class="py-2 pr-3">{{ __('reports.reimbursements.what') }}</th>
<th scope="col" class="py-2 pr-3">{{ __('reports.reimbursements.job') }}</th>
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.reimbursements.km') }}</th>
<th scope="col" class="py-2 text-right">{{ __('reports.reimbursements.amount') }}</th>
</tr>
</thead>
<tbody class="divide-y divide-zinc-200 dark:divide-zinc-700">
@foreach ($person->lines as $line)
<tr class="align-top">
<td class="py-2 pr-3 whitespace-nowrap">{{ $this->dateLabel($line->expense->spentOn) }}</td>
<td class="py-2 pr-3">{{ __('expenses.categories.'.$line->expense->category) }}</td>
<td class="py-2 pr-3">{{ $this->what($line->expense) }}</td>
<td class="py-2 pr-3">{{ $line->jobNumber }} {{ $line->jobName }}</td>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->kilometers($line->expense->kilometerTenths) }}</td>
<td class="py-2 text-right whitespace-nowrap">{{ $this->money($line->expense->costCents) }}</td>
</tr>
@endforeach
</tbody>
<tfoot>
<tr class="border-t-2 border-zinc-400 font-semibold dark:border-zinc-500">
<th scope="row" colspan="4" class="py-2 pr-3 text-left">{{ __('reports.reimbursements.person_total', ['name' => $person->name]) }}</th>
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->kilometers($person->kilometerTenths) }}</td>
<td class="py-2 text-right whitespace-nowrap">{{ $this->money($person->costCents) }}</td>
</tr>
</tfoot>
</table>
</div>
</section>
@endforeach
@endif
@endif
</div>
@@ -2,6 +2,7 @@
namespace App\Modules\Reports\Presentation;
use App\Modules\Reports\Application\DTOs\ReimbursementEntry;
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
use App\Modules\Shared\Domain\ValueObjects\Money;
use DateTimeImmutable;
@@ -42,6 +43,28 @@ final class ReportFormat
return number_format($ratio * 100, 1, ',', "\u{00A0}")."\u{00A0}%";
}
/**
* E.g. 125 tenths → "12,5 km".
*/
public static function kilometers(int $tenths): string
{
return number_format($tenths / 10, 1, ',', "\u{00A0}")."\u{00A0}km";
}
public static function csvKilometers(int $tenths): string
{
return number_format($tenths / 10, 1, ',', '');
}
/**
* What an expense was: its description, and for mileage the
* destination and purpose.
*/
public static function expenseText(ReimbursementEntry $expense): string
{
return implode(', ', array_filter([$expense->description, $expense->destination, $expense->purpose], fn (?string $part): bool => $part !== null && $part !== ''));
}
public static function date(string $day): string
{
return (new DateTimeImmutable($day))->format('j.n.Y');
@@ -10,5 +10,6 @@ Route::middleware(['web', 'auth', 'can:reports.view'])
Route::livewire('/hours', 'reports::hours')->name('hours');
Route::livewire('/unbilled', 'reports::unbilled')->name('unbilled');
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');
});