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]>
78 lines
2.2 KiB
PHP
78 lines
2.2 KiB
PHP
<?php
|
||
|
||
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;
|
||
|
||
/**
|
||
* Finnish number, money and date formats of the reports, in both UI
|
||
* languages. Amounts may be negative (credits, losses), which Money does
|
||
* not hold, so the sign is added here.
|
||
*/
|
||
final class ReportFormat
|
||
{
|
||
public static function money(int $cents): string
|
||
{
|
||
return ($cents < 0 ? '-' : '').Money::fromCents(abs($cents))->format();
|
||
}
|
||
|
||
/**
|
||
* E.g. 6 quarters → "1,5".
|
||
*/
|
||
public static function hours(int $quarters): string
|
||
{
|
||
return rtrim(rtrim(number_format($quarters / 4, 2, ',', "\u{00A0}"), '0'), ',');
|
||
}
|
||
|
||
/**
|
||
* Hours for a CSV cell: a decimal comma and no thousands separator.
|
||
*/
|
||
public static function csvHours(int $quarters): string
|
||
{
|
||
return rtrim(rtrim(number_format($quarters / 4, 2, ',', ''), '0'), ',');
|
||
}
|
||
|
||
/**
|
||
* E.g. 0.256 → "25,6 %".
|
||
*/
|
||
public static function percent(float $ratio): string
|
||
{
|
||
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');
|
||
}
|
||
|
||
public static function period(ReportPeriod $period): string
|
||
{
|
||
return self::date($period->from).'–'.self::date($period->to);
|
||
}
|
||
}
|