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,28 @@
<?php
declare(strict_types=1);
namespace App\Modules\Reports\Application\Contracts;
use App\Modules\Reports\Application\DTOs\ReimbursementEntry;
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
/**
* Expenses by the day spent, from the Expenses module.
*/
interface ReportExpenses
{
/**
* Expenses the employees paid themselves, by user and day.
*
* @return list<ReimbursementEntry>
*/
public function employeePaid(ReportPeriod $period): array;
/**
* Purchase costs (VAT 0) of every expense, by job id.
*
* @return array<int, int>
*/
public function costsByJob(ReportPeriod $period): array;
}
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace App\Modules\Reports\Application\DTOs;
/**
* A person's expenses to reimburse by day, with the kilometres (tenths)
* and costs (cents, VAT 0) summed.
*/
final readonly class PersonReimbursements
{
/**
* @param list<ReimbursementLine> $lines
*/
public function __construct(
public int $userId,
public string $name,
public array $lines,
public int $kilometerTenths,
public int $costCents,
) {}
}
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace App\Modules\Reports\Application\DTOs;
/**
* An expense an employee paid. The category is the Expenses module's key
* (materials, mileage, other); kilometres are in tenths and the cost in
* cents (VAT 0).
*/
final readonly class ReimbursementEntry
{
public function __construct(
public int $userId,
public int $jobId,
public string $category,
public string $spentOn,
public ?string $description,
public ?string $destination,
public ?string $purpose,
public ?int $kilometerTenths,
public int $costCents,
) {}
}
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace App\Modules\Reports\Application\DTOs;
/**
* One expense to reimburse, with its job.
*/
final readonly class ReimbursementLine
{
public function __construct(
public ReimbursementEntry $expense,
public string $jobNumber,
public string $jobName,
) {}
}
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace App\Modules\Reports\Application\DTOs;
/**
* Expense reimbursements of a period by person (by name), with totals.
*/
final readonly class ReimbursementReport
{
/**
* @param list<PersonReimbursements> $people
*/
public function __construct(
public array $people,
public int $kilometerTenths,
public int $costCents,
) {}
}
@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
namespace App\Modules\Reports\Application\UseCases;
use App\Modules\Reports\Application\Contracts\ReportExpenses;
use App\Modules\Reports\Application\Contracts\ReportJobs;
use App\Modules\Reports\Application\Contracts\ReportPeople;
use App\Modules\Reports\Application\DTOs\PersonReimbursements;
use App\Modules\Reports\Application\DTOs\ReimbursementEntry;
use App\Modules\Reports\Application\DTOs\ReimbursementLine;
use App\Modules\Reports\Application\DTOs\ReimbursementReport;
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
/**
* Expense reimbursements (spec 10, 13): the expenses employees paid
* themselves in a period, by person, for payroll. Reimbursing them happens
* outside this system, so nothing is marked paid here.
*/
final readonly class ReimbursementsReport
{
public function __construct(
private ReportExpenses $expenses,
private ReportJobs $jobs,
private ReportPeople $people,
) {}
public function execute(ReportPeriod $period): ReimbursementReport
{
$entries = $this->expenses->employeePaid($period);
$jobs = $this->jobs->infoOf(array_values(array_unique(array_map(fn (ReimbursementEntry $entry): int => $entry->jobId, $entries))));
$names = $this->people->namesByIds(array_values(array_unique(array_map(fn (ReimbursementEntry $entry): int => $entry->userId, $entries))));
$byPerson = [];
foreach ($entries as $entry) {
$job = $jobs[$entry->jobId] ?? null;
$byPerson[$entry->userId][] = new ReimbursementLine($entry, $job->number ?? '', $job->name ?? '');
}
$people = [];
foreach ($byPerson as $userId => $lines) {
$people[] = new PersonReimbursements(
$userId,
$names[$userId] ?? '',
$lines,
array_sum(array_map(fn (ReimbursementLine $line): int => $line->expense->kilometerTenths ?? 0, $lines)),
array_sum(array_map(fn (ReimbursementLine $line): int => $line->expense->costCents, $lines)),
);
}
usort($people, fn (PersonReimbursements $a, PersonReimbursements $b): int => strnatcasecmp($a->name, $b->name));
return new ReimbursementReport(
$people,
array_sum(array_map(fn (PersonReimbursements $person): int => $person->kilometerTenths, $people)),
array_sum(array_map(fn (PersonReimbursements $person): int => $person->costCents, $people)),
);
}
}