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:
@@ -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)),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Reports\Infrastructure\Adapters;
|
||||
|
||||
use App\Modules\Expenses\Application\Contracts\ExpenseReportQueries;
|
||||
use App\Modules\Expenses\Application\DTOs\ReimbursableExpense;
|
||||
use App\Modules\Reports\Application\Contracts\ReportExpenses;
|
||||
use App\Modules\Reports\Application\DTOs\ReimbursementEntry;
|
||||
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
|
||||
|
||||
/**
|
||||
* Expenses for reports through the Expenses module.
|
||||
*/
|
||||
class ExpensesForReports implements ReportExpenses
|
||||
{
|
||||
public function __construct(private ExpenseReportQueries $queries) {}
|
||||
|
||||
public function employeePaid(ReportPeriod $period): array
|
||||
{
|
||||
return array_map(fn (ReimbursableExpense $expense): ReimbursementEntry => new ReimbursementEntry(
|
||||
$expense->userId,
|
||||
$expense->jobId,
|
||||
$expense->category->value,
|
||||
$expense->spentOn,
|
||||
$expense->description,
|
||||
$expense->destination,
|
||||
$expense->purpose,
|
||||
$expense->kilometerTenths,
|
||||
$expense->costCents,
|
||||
), $this->queries->employeePaidBetween($period->from, $period->to));
|
||||
}
|
||||
|
||||
public function costsByJob(ReportPeriod $period): array
|
||||
{
|
||||
return $this->queries->costsByJobBetween($period->from, $period->to);
|
||||
}
|
||||
}
|
||||
@@ -4,11 +4,13 @@ namespace App\Modules\Reports\Infrastructure\Providers;
|
||||
|
||||
use App\Models\User;
|
||||
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\ReportPeople;
|
||||
use App\Modules\Reports\Application\Contracts\ReportSales;
|
||||
use App\Modules\Reports\Infrastructure\Adapters\CatalogForReports;
|
||||
use App\Modules\Reports\Infrastructure\Adapters\ExpensesForReports;
|
||||
use App\Modules\Reports\Infrastructure\Adapters\HoursForReports;
|
||||
use App\Modules\Reports\Infrastructure\Adapters\JobsForReports;
|
||||
use App\Modules\Reports\Infrastructure\Adapters\PeopleForReports;
|
||||
@@ -33,6 +35,7 @@ class ReportsServiceProvider extends ServiceProvider
|
||||
ReportCatalog::class => CatalogForReports::class,
|
||||
ReportPeople::class => PeopleForReports::class,
|
||||
ReportSales::class => SalesForReports::class,
|
||||
ReportExpenses::class => ExpensesForReports::class,
|
||||
];
|
||||
|
||||
public function boot(): void
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user