diff --git a/app/Modules/Expenses/Application/Contracts/ExpenseReportQueries.php b/app/Modules/Expenses/Application/Contracts/ExpenseReportQueries.php new file mode 100644 index 0000000..a2613e1 --- /dev/null +++ b/app/Modules/Expenses/Application/Contracts/ExpenseReportQueries.php @@ -0,0 +1,29 @@ + + */ + public function employeePaidBetween(string $from, string $to): array; + + /** + * Purchase costs (VAT 0) of every expense, by job id. + * + * @return array + */ + public function costsByJobBetween(string $from, string $to): array; +} diff --git a/app/Modules/Expenses/Application/DTOs/ReimbursableExpense.php b/app/Modules/Expenses/Application/DTOs/ReimbursableExpense.php new file mode 100644 index 0000000..22098cf --- /dev/null +++ b/app/Modules/Expenses/Application/DTOs/ReimbursableExpense.php @@ -0,0 +1,27 @@ +between($from, $to) + ->where('paid_by', ExpensePayer::Employee) + ->orderBy('user_id') + ->orderBy('spent_on') + ->orderBy('id') + ->get() + ->map(fn (ExpenseModel $expense): ReimbursableExpense => new ReimbursableExpense( + $expense->id, + $expense->user_id, + $expense->customer_job_id, + $expense->category, + $expense->spent_on->format('Y-m-d'), + $expense->description, + $expense->destination, + $expense->purpose, + $expense->kilometers === null ? null : (int) round((float) $expense->kilometers * 10), + $expense->cost_cents, + )) + ->all(); + } + + public function costsByJobBetween(string $from, string $to): array + { + return $this->between($from, $to) + ->groupBy('customer_job_id') + ->selectRaw('customer_job_id, SUM(cost_cents) as cost_cents') + ->pluck('cost_cents', 'customer_job_id') + ->mapWithKeys(fn ($cents, $jobId): array => [(int) $jobId => (int) $cents]) + ->all(); + } + + /** + * Days may be stored with a time, so a period ends before the next day. + * + * @return Builder + */ + private function between(string $from, string $to): Builder + { + return ExpenseModel::query() + ->where('spent_on', '>=', $from) + ->where('spent_on', '<', (new DateTimeImmutable($to))->modify('+1 day')->format('Y-m-d')); + } +} diff --git a/app/Modules/Expenses/Infrastructure/Providers/ExpensesServiceProvider.php b/app/Modules/Expenses/Infrastructure/Providers/ExpensesServiceProvider.php index 8b34c3f..cfd1b46 100644 --- a/app/Modules/Expenses/Infrastructure/Providers/ExpensesServiceProvider.php +++ b/app/Modules/Expenses/Infrastructure/Providers/ExpensesServiceProvider.php @@ -6,9 +6,11 @@ use App\Models\User; use App\Modules\Expenses\Application\Contracts\ExpenseJobs; use App\Modules\Expenses\Application\Contracts\ExpensePricing; use App\Modules\Expenses\Application\Contracts\ExpenseQueries; +use App\Modules\Expenses\Application\Contracts\ExpenseReportQueries; use App\Modules\Expenses\Domain\Contracts\ExpenseRepository; use App\Modules\Expenses\Infrastructure\JobsForExpenses; use App\Modules\Expenses\Infrastructure\Persistence\Repositories\EloquentExpenseQueries; +use App\Modules\Expenses\Infrastructure\Persistence\Repositories\EloquentExpenseReportQueries; use App\Modules\Expenses\Infrastructure\Persistence\Repositories\EloquentExpenseRepository; use App\Modules\Expenses\Infrastructure\PriceListForExpenses; use App\Modules\Users\Domain\Enums\Role; @@ -29,6 +31,7 @@ class ExpensesServiceProvider extends ServiceProvider public array $bindings = [ ExpenseRepository::class => EloquentExpenseRepository::class, ExpenseQueries::class => EloquentExpenseQueries::class, + ExpenseReportQueries::class => EloquentExpenseReportQueries::class, ExpenseJobs::class => JobsForExpenses::class, ExpensePricing::class => PriceListForExpenses::class, ]; diff --git a/app/Modules/Reports/Application/Contracts/ReportExpenses.php b/app/Modules/Reports/Application/Contracts/ReportExpenses.php new file mode 100644 index 0000000..a424c55 --- /dev/null +++ b/app/Modules/Reports/Application/Contracts/ReportExpenses.php @@ -0,0 +1,28 @@ + + */ + public function employeePaid(ReportPeriod $period): array; + + /** + * Purchase costs (VAT 0) of every expense, by job id. + * + * @return array + */ + public function costsByJob(ReportPeriod $period): array; +} diff --git a/app/Modules/Reports/Application/DTOs/PersonReimbursements.php b/app/Modules/Reports/Application/DTOs/PersonReimbursements.php new file mode 100644 index 0000000..f5955d6 --- /dev/null +++ b/app/Modules/Reports/Application/DTOs/PersonReimbursements.php @@ -0,0 +1,23 @@ + $lines + */ + public function __construct( + public int $userId, + public string $name, + public array $lines, + public int $kilometerTenths, + public int $costCents, + ) {} +} diff --git a/app/Modules/Reports/Application/DTOs/ReimbursementEntry.php b/app/Modules/Reports/Application/DTOs/ReimbursementEntry.php new file mode 100644 index 0000000..807bfda --- /dev/null +++ b/app/Modules/Reports/Application/DTOs/ReimbursementEntry.php @@ -0,0 +1,25 @@ + $people + */ + public function __construct( + public array $people, + public int $kilometerTenths, + public int $costCents, + ) {} +} diff --git a/app/Modules/Reports/Application/UseCases/ReimbursementsReport.php b/app/Modules/Reports/Application/UseCases/ReimbursementsReport.php new file mode 100644 index 0000000..e18db48 --- /dev/null +++ b/app/Modules/Reports/Application/UseCases/ReimbursementsReport.php @@ -0,0 +1,60 @@ +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)), + ); + } +} diff --git a/app/Modules/Reports/Infrastructure/Adapters/ExpensesForReports.php b/app/Modules/Reports/Infrastructure/Adapters/ExpensesForReports.php new file mode 100644 index 0000000..e78cefd --- /dev/null +++ b/app/Modules/Reports/Infrastructure/Adapters/ExpensesForReports.php @@ -0,0 +1,37 @@ + 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); + } +} diff --git a/app/Modules/Reports/Infrastructure/Providers/ReportsServiceProvider.php b/app/Modules/Reports/Infrastructure/Providers/ReportsServiceProvider.php index d523a06..7796957 100644 --- a/app/Modules/Reports/Infrastructure/Providers/ReportsServiceProvider.php +++ b/app/Modules/Reports/Infrastructure/Providers/ReportsServiceProvider.php @@ -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 diff --git a/app/Modules/Reports/Presentation/Csv/ReimbursementsCsv.php b/app/Modules/Reports/Presentation/Csv/ReimbursementsCsv.php new file mode 100644 index 0000000..cbea596 --- /dev/null +++ b/app/Modules/Reports/Presentation/Csv/ReimbursementsCsv.php @@ -0,0 +1,46 @@ +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, + ]; + } + } + } +} diff --git a/app/Modules/Reports/Presentation/Http/Controllers/DownloadReport.php b/app/Modules/Reports/Presentation/Http/Controllers/DownloadReport.php index 187775e..c267a76 100644 --- a/app/Modules/Reports/Presentation/Http/Controllers/DownloadReport.php +++ b/app/Modules/Reports/Presentation/Http/Controllers/DownloadReport.php @@ -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), }; } diff --git a/app/Modules/Reports/Presentation/Livewire/⚡reimbursements.blade.php b/app/Modules/Reports/Presentation/Livewire/⚡reimbursements.blade.php new file mode 100644 index 0000000..cde7343 --- /dev/null +++ b/app/Modules/Reports/Presentation/Livewire/⚡reimbursements.blade.php @@ -0,0 +1,125 @@ +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')); + } +}; +?> + +
+
+

{{ __('reports.reimbursements.title') }}

+

{{ __('reports.reimbursements.intro') }}

+
+ + + + @php($report = $this->report) + + @if ($report !== null) +
+

{{ $this->periodLabel() }}

+
+ + +
+
+ + @if ($report->people === []) +

{{ __('reports.reimbursements.none') }}

+ @else +

+ {{ __('reports.reimbursements.grand_total', ['amount' => $this->money($report->costCents), 'km' => $this->kilometers($report->kilometerTenths)]) }} +

+ + @foreach ($report->people as $person) +
+

{{ $person->name }}

+ +
+ + + + + + + + + + + + + + @foreach ($person->lines as $line) + + + + + + + + + @endforeach + + + + + + + + +
{{ __('reports.reimbursements.table', ['name' => $person->name, 'period' => $this->periodLabel()]) }}
{{ __('reports.reimbursements.day') }}{{ __('reports.reimbursements.category') }}{{ __('reports.reimbursements.what') }}{{ __('reports.reimbursements.job') }}{{ __('reports.reimbursements.km') }}{{ __('reports.reimbursements.amount') }}
{{ $this->dateLabel($line->expense->spentOn) }}{{ __('expenses.categories.'.$line->expense->category) }}{{ $this->what($line->expense) }}{{ $line->jobNumber }} {{ $line->jobName }}{{ $this->kilometers($line->expense->kilometerTenths) }}{{ $this->money($line->expense->costCents) }}
{{ __('reports.reimbursements.person_total', ['name' => $person->name]) }}{{ $this->kilometers($person->kilometerTenths) }}{{ $this->money($person->costCents) }}
+
+
+ @endforeach + @endif + @endif +
diff --git a/app/Modules/Reports/Presentation/ReportFormat.php b/app/Modules/Reports/Presentation/ReportFormat.php index 101bc62..83afbae 100644 --- a/app/Modules/Reports/Presentation/ReportFormat.php +++ b/app/Modules/Reports/Presentation/ReportFormat.php @@ -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'); diff --git a/app/Modules/Reports/Presentation/Routes/web.php b/app/Modules/Reports/Presentation/Routes/web.php index df62328..0f54493 100644 --- a/app/Modules/Reports/Presentation/Routes/web.php +++ b/app/Modules/Reports/Presentation/Routes/web.php @@ -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'); }); diff --git a/app/View/MainMenu.php b/app/View/MainMenu.php index 34b265f..cd0eb62 100644 --- a/app/View/MainMenu.php +++ b/app/View/MainMenu.php @@ -100,6 +100,7 @@ final class MainMenu ['route' => 'reports.hours', 'pattern' => ['reports.hours'], 'label' => 'layout.report_hours', 'ability' => 'reports.view'], ['route' => 'reports.unbilled', 'pattern' => ['reports.unbilled'], 'label' => 'layout.report_unbilled', 'ability' => 'reports.view'], ['route' => 'reports.sales', 'pattern' => ['reports.sales'], 'label' => 'layout.report_sales', 'ability' => 'reports.view'], + ['route' => 'reports.reimbursements', 'pattern' => ['reports.reimbursements'], 'label' => 'layout.report_reimbursements', 'ability' => 'reports.view'], ], ], 'maintenance' => [ diff --git a/lang/en/reports.php b/lang/en/reports.php index 3931436..45116f4 100644 --- a/lang/en/reports.php +++ b/lang/en/reports.php @@ -135,4 +135,31 @@ return [ 'closed' => 'Closed', ], ], + + 'reimbursements' => [ + 'title' => 'Expense reimbursements', + 'file' => 'expense-reimbursements', + 'intro' => 'Expenses the employees paid themselves, by person, for payroll. Amounts are without VAT (VAT 0). The reimbursements are paid outside this system.', + 'print' => 'Print', + 'none' => 'No expenses were paid by employees in the period.', + 'grand_total' => 'To reimburse in total :amount, kilometres :km', + 'table' => 'Expense reimbursements: :name :period', + 'day' => 'Day', + 'category' => 'Category', + 'what' => 'Description', + 'job' => 'Customer job', + 'km' => 'Kilometres', + 'amount' => 'Amount (VAT 0)', + 'person_total' => 'Total: :name', + 'csv' => [ + 'person' => 'Person', + 'day' => 'Day', + 'category' => 'Category', + 'what' => 'Description', + 'job_number' => 'Job number', + 'job' => 'Customer job', + 'km' => 'Kilometres', + 'amount' => 'Amount VAT 0 (cents)', + ], + ], ]; diff --git a/lang/fi/reports.php b/lang/fi/reports.php index 84cca95..d34d034 100644 --- a/lang/fi/reports.php +++ b/lang/fi/reports.php @@ -135,4 +135,31 @@ return [ 'closed' => 'Päätetty', ], ], + + 'reimbursements' => [ + 'title' => 'Kulukorvaukset', + 'file' => 'kulukorvaukset', + 'intro' => 'Kulut, jotka työntekijät ovat maksaneet itse, henkilöittäin palkanlaskentaa varten. Summat ovat verottomia (ALV 0). Korvausten maksu hoidetaan tämän järjestelmän ulkopuolella.', + 'print' => 'Tulosta', + 'none' => 'Aikavälillä ei ole työntekijöiden itse maksamia kuluja.', + 'grand_total' => 'Korvattavaa yhteensä :amount, kilometrejä :km', + 'table' => 'Kulukorvaukset: :name :period', + 'day' => 'Päivä', + 'category' => 'Laji', + 'what' => 'Kuvaus', + 'job' => 'Asiakastyö', + 'km' => 'Kilometrit', + 'amount' => 'Summa (ALV 0)', + 'person_total' => 'Yhteensä: :name', + 'csv' => [ + 'person' => 'Henkilö', + 'day' => 'Päivä', + 'category' => 'Laji', + 'what' => 'Kuvaus', + 'job_number' => 'Työn numero', + 'job' => 'Asiakastyö', + 'km' => 'Kilometrit', + 'amount' => 'Summa ALV 0 (snt)', + ], + ], ]; diff --git a/tests/Feature/Modules/Reports/ReimbursementsReportTest.php b/tests/Feature/Modules/Reports/ReimbursementsReportTest.php new file mode 100644 index 0000000..8ec4f9f --- /dev/null +++ b/tests/Feature/Modules/Reports/ReimbursementsReportTest.php @@ -0,0 +1,56 @@ +actingAs(User::factory()->management()->create()); + $this->aino = User::factory()->employee()->create(['name' => 'Aino']); + $this->bertta = User::factory()->employee()->create(['name' => 'Bertta']); + $this->job = CustomerJobModel::factory()->create(['name' => 'Messut']); +}); + +it('lists the expenses employees paid by person with totals', function () { + ExpenseModel::factory()->paidByEmployee()->mileage(12.5, 59)->create(['user_id' => $this->bertta->id, 'customer_job_id' => $this->job->id, 'spent_on' => '2026-10-31']); + ExpenseModel::factory()->paidByEmployee()->create(['user_id' => $this->aino->id, 'customer_job_id' => $this->job->id, 'spent_on' => '2026-10-02', 'description' => 'Teippi', 'cost_cents' => 1250]); + ExpenseModel::factory()->paidByEmployee()->create(['user_id' => $this->aino->id, 'customer_job_id' => $this->job->id, 'spent_on' => '2026-10-05', 'cost_cents' => 750]); + ExpenseModel::factory()->create(['user_id' => $this->aino->id, 'customer_job_id' => $this->job->id, 'spent_on' => '2026-10-05', 'cost_cents' => 99900]); + ExpenseModel::factory()->paidByEmployee()->create(['user_id' => $this->aino->id, 'customer_job_id' => $this->job->id, 'spent_on' => '2026-11-01', 'cost_cents' => 99900]); + + $report = Livewire::withQueryParams(['alku' => '2026-10-01', 'loppu' => '2026-10-31']) + ->test('reports::reimbursements') + ->assertOk() + ->assertSee('Teippi') + ->assertSee('Tampere, Asiakastapaaminen') + ->assertSee('12,5') + ->instance() + ->report(); + + expect(array_map(fn ($person) => $person->name, $report->people))->toBe(['Aino', 'Bertta']) + ->and($report->people[0]->costCents)->toBe(2000) + ->and($report->people[0]->lines[0]->jobName)->toBe('Messut') + ->and($report->people[1]->kilometerTenths)->toBe(125) + ->and($report->people[1]->costCents)->toBe(738) + ->and($report->costCents)->toBe(2738); + + $response = $this->get(route('reports.csv', ['report' => 'reimbursements', 'alku' => '2026-10-01', 'loppu' => '2026-10-31'])) + ->assertOk() + ->assertDownload('kulukorvaukset-2026-10-01-2026-10-31.csv'); + + expect($response->streamedContent()) + ->toContain('Aino;2026-10-02;"Tuotteet, tarvikkeet ja materiaalit";Teippi;'.$this->job->number.';Messut;;1250') + ->toContain('Bertta;2026-10-31;Matkakilometrit;"Tampere, Asiakastapaaminen";'.$this->job->number.';Messut;12,5;738'); +}); + +it('starts with the previous month', function () { + $this->travelTo('2026-11-25 12:00'); + + Livewire::test('reports::reimbursements') + ->assertSet('from', '2026-10-01') + ->assertSet('to', '2026-10-31'); +});