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,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Expenses\Application\Contracts;
|
||||||
|
|
||||||
|
use App\Modules\Expenses\Application\DTOs\ReimbursableExpense;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Expenses for management's reports, by the day spent (Y-m-d, both ends
|
||||||
|
* included).
|
||||||
|
*/
|
||||||
|
interface ExpenseReportQueries
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Expenses the employees paid themselves, to be reimbursed outside
|
||||||
|
* this system, by user and day.
|
||||||
|
*
|
||||||
|
* @return list<ReimbursableExpense>
|
||||||
|
*/
|
||||||
|
public function employeePaidBetween(string $from, string $to): array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Purchase costs (VAT 0) of every expense, by job id.
|
||||||
|
*
|
||||||
|
* @return array<int, int>
|
||||||
|
*/
|
||||||
|
public function costsByJobBetween(string $from, string $to): array;
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Expenses\Application\DTOs;
|
||||||
|
|
||||||
|
use App\Modules\Expenses\Domain\Enums\ExpenseCategory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An expense an employee paid: on which job and day, what it was, the
|
||||||
|
* kilometres in tenths for mileage and the cost in cents (VAT 0).
|
||||||
|
*/
|
||||||
|
final readonly class ReimbursableExpense
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public int $id,
|
||||||
|
public int $userId,
|
||||||
|
public int $jobId,
|
||||||
|
public ExpenseCategory $category,
|
||||||
|
public string $spentOn,
|
||||||
|
public ?string $description,
|
||||||
|
public ?string $destination,
|
||||||
|
public ?string $purpose,
|
||||||
|
public ?int $kilometerTenths,
|
||||||
|
public int $costCents,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
+58
@@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Modules\Expenses\Infrastructure\Persistence\Repositories;
|
||||||
|
|
||||||
|
use App\Modules\Expenses\Application\Contracts\ExpenseReportQueries;
|
||||||
|
use App\Modules\Expenses\Application\DTOs\ReimbursableExpense;
|
||||||
|
use App\Modules\Expenses\Domain\Enums\ExpensePayer;
|
||||||
|
use App\Modules\Expenses\Infrastructure\Persistence\Eloquent\ExpenseModel;
|
||||||
|
use DateTimeImmutable;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
|
||||||
|
class EloquentExpenseReportQueries implements ExpenseReportQueries
|
||||||
|
{
|
||||||
|
public function employeePaidBetween(string $from, string $to): array
|
||||||
|
{
|
||||||
|
return $this->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<ExpenseModel>
|
||||||
|
*/
|
||||||
|
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'));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,9 +6,11 @@ use App\Models\User;
|
|||||||
use App\Modules\Expenses\Application\Contracts\ExpenseJobs;
|
use App\Modules\Expenses\Application\Contracts\ExpenseJobs;
|
||||||
use App\Modules\Expenses\Application\Contracts\ExpensePricing;
|
use App\Modules\Expenses\Application\Contracts\ExpensePricing;
|
||||||
use App\Modules\Expenses\Application\Contracts\ExpenseQueries;
|
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\Domain\Contracts\ExpenseRepository;
|
||||||
use App\Modules\Expenses\Infrastructure\JobsForExpenses;
|
use App\Modules\Expenses\Infrastructure\JobsForExpenses;
|
||||||
use App\Modules\Expenses\Infrastructure\Persistence\Repositories\EloquentExpenseQueries;
|
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\Persistence\Repositories\EloquentExpenseRepository;
|
||||||
use App\Modules\Expenses\Infrastructure\PriceListForExpenses;
|
use App\Modules\Expenses\Infrastructure\PriceListForExpenses;
|
||||||
use App\Modules\Users\Domain\Enums\Role;
|
use App\Modules\Users\Domain\Enums\Role;
|
||||||
@@ -29,6 +31,7 @@ class ExpensesServiceProvider extends ServiceProvider
|
|||||||
public array $bindings = [
|
public array $bindings = [
|
||||||
ExpenseRepository::class => EloquentExpenseRepository::class,
|
ExpenseRepository::class => EloquentExpenseRepository::class,
|
||||||
ExpenseQueries::class => EloquentExpenseQueries::class,
|
ExpenseQueries::class => EloquentExpenseQueries::class,
|
||||||
|
ExpenseReportQueries::class => EloquentExpenseReportQueries::class,
|
||||||
ExpenseJobs::class => JobsForExpenses::class,
|
ExpenseJobs::class => JobsForExpenses::class,
|
||||||
ExpensePricing::class => PriceListForExpenses::class,
|
ExpensePricing::class => PriceListForExpenses::class,
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -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\Models\User;
|
||||||
use App\Modules\Reports\Application\Contracts\ReportCatalog;
|
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\ReportHours;
|
||||||
use App\Modules\Reports\Application\Contracts\ReportJobs;
|
use App\Modules\Reports\Application\Contracts\ReportJobs;
|
||||||
use App\Modules\Reports\Application\Contracts\ReportPeople;
|
use App\Modules\Reports\Application\Contracts\ReportPeople;
|
||||||
use App\Modules\Reports\Application\Contracts\ReportSales;
|
use App\Modules\Reports\Application\Contracts\ReportSales;
|
||||||
use App\Modules\Reports\Infrastructure\Adapters\CatalogForReports;
|
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\HoursForReports;
|
||||||
use App\Modules\Reports\Infrastructure\Adapters\JobsForReports;
|
use App\Modules\Reports\Infrastructure\Adapters\JobsForReports;
|
||||||
use App\Modules\Reports\Infrastructure\Adapters\PeopleForReports;
|
use App\Modules\Reports\Infrastructure\Adapters\PeopleForReports;
|
||||||
@@ -33,6 +35,7 @@ class ReportsServiceProvider extends ServiceProvider
|
|||||||
ReportCatalog::class => CatalogForReports::class,
|
ReportCatalog::class => CatalogForReports::class,
|
||||||
ReportPeople::class => PeopleForReports::class,
|
ReportPeople::class => PeopleForReports::class,
|
||||||
ReportSales::class => SalesForReports::class,
|
ReportSales::class => SalesForReports::class,
|
||||||
|
ReportExpenses::class => ExpensesForReports::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
public function boot(): void
|
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\Domain\Exceptions\InvalidReportPeriod;
|
||||||
use App\Modules\Reports\Presentation\Csv\HoursCsv;
|
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\ReportCsv;
|
||||||
use App\Modules\Reports\Presentation\Csv\SalesCsv;
|
use App\Modules\Reports\Presentation\Csv\SalesCsv;
|
||||||
use App\Modules\Reports\Presentation\Csv\UnbilledCsv;
|
use App\Modules\Reports\Presentation\Csv\UnbilledCsv;
|
||||||
@@ -20,7 +21,7 @@ use Symfony\Component\HttpFoundation\StreamedResponse;
|
|||||||
class DownloadReport
|
class DownloadReport
|
||||||
{
|
{
|
||||||
/** The reports that have a CSV, by their URL key. */
|
/** 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
|
public function __invoke(string $report, Request $request): StreamedResponse
|
||||||
{
|
{
|
||||||
@@ -75,6 +76,7 @@ class DownloadReport
|
|||||||
'hours' => app(HoursCsv::class),
|
'hours' => app(HoursCsv::class),
|
||||||
'unbilled' => app(UnbilledCsv::class),
|
'unbilled' => app(UnbilledCsv::class),
|
||||||
'sales' => app(SalesCsv::class),
|
'sales' => app(SalesCsv::class),
|
||||||
|
'reimbursements' => app(ReimbursementsCsv::class),
|
||||||
default => abort(404),
|
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;
|
namespace App\Modules\Reports\Presentation;
|
||||||
|
|
||||||
|
use App\Modules\Reports\Application\DTOs\ReimbursementEntry;
|
||||||
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
|
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
|
||||||
use App\Modules\Shared\Domain\ValueObjects\Money;
|
use App\Modules\Shared\Domain\ValueObjects\Money;
|
||||||
use DateTimeImmutable;
|
use DateTimeImmutable;
|
||||||
@@ -42,6 +43,28 @@ final class ReportFormat
|
|||||||
return number_format($ratio * 100, 1, ',', "\u{00A0}")."\u{00A0}%";
|
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
|
public static function date(string $day): string
|
||||||
{
|
{
|
||||||
return (new DateTimeImmutable($day))->format('j.n.Y');
|
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('/hours', 'reports::hours')->name('hours');
|
||||||
Route::livewire('/unbilled', 'reports::unbilled')->name('unbilled');
|
Route::livewire('/unbilled', 'reports::unbilled')->name('unbilled');
|
||||||
Route::livewire('/sales', 'reports::sales')->name('sales');
|
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');
|
Route::get('/{report}/csv', DownloadReport::class)->whereIn('report', DownloadReport::REPORTS)->name('csv');
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -100,6 +100,7 @@ final class MainMenu
|
|||||||
['route' => 'reports.hours', 'pattern' => ['reports.hours'], 'label' => 'layout.report_hours', 'ability' => 'reports.view'],
|
['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.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.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' => [
|
'maintenance' => [
|
||||||
|
|||||||
@@ -135,4 +135,31 @@ return [
|
|||||||
'closed' => 'Closed',
|
'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)',
|
||||||
|
],
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -135,4 +135,31 @@ return [
|
|||||||
'closed' => 'Päätetty',
|
'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)',
|
||||||
|
],
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Modules\Expenses\Infrastructure\Persistence\Eloquent\ExpenseModel;
|
||||||
|
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Livewire\Livewire;
|
||||||
|
|
||||||
|
uses(RefreshDatabase::class);
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
$this->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');
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user