Add Reports module with the hours by person report
Management's reports (spec 13) get their own module that reads the other modules through ports. The first report sums hours by person in a period, narrowed by service unit or customer, with breakdowns, a chart and CSV. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Reports\Presentation\Livewire\Concerns;
|
||||
|
||||
use App\Modules\Reports\Domain\Exceptions\InvalidReportPeriod;
|
||||
use App\Modules\Reports\Domain\ValueObjects\ReportPeriod;
|
||||
use App\Modules\Reports\Presentation\ReportFormat;
|
||||
use App\Modules\Users\Application\Contracts\Clock;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Url;
|
||||
|
||||
/**
|
||||
* The period every report page covers, kept in the URL (alku, loppu), its
|
||||
* quick choices and the shared number formats. Without a valid period in
|
||||
* the URL a page starts with the current month.
|
||||
*/
|
||||
trait ReportPage
|
||||
{
|
||||
#[Url(as: 'alku', except: '')]
|
||||
public string $from = '';
|
||||
|
||||
#[Url(as: 'loppu', except: '')]
|
||||
public string $to = '';
|
||||
|
||||
protected function startPeriod(Clock $clock, string $default = 'this_month'): void
|
||||
{
|
||||
$this->authorize('reports.view');
|
||||
|
||||
if ($this->period() === null) {
|
||||
$this->usePeriod($this->preset($default, $clock));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The period chosen, or null while the two days do not form one.
|
||||
*/
|
||||
#[Computed]
|
||||
public function period(): ?ReportPeriod
|
||||
{
|
||||
try {
|
||||
return new ReportPeriod($this->from, $this->to);
|
||||
} catch (InvalidReportPeriod) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function updatedFrom(): void
|
||||
{
|
||||
$this->authorize('reports.view');
|
||||
unset($this->period);
|
||||
}
|
||||
|
||||
public function updatedTo(): void
|
||||
{
|
||||
$this->authorize('reports.view');
|
||||
unset($this->period);
|
||||
}
|
||||
|
||||
public function choosePeriod(string $preset, Clock $clock): void
|
||||
{
|
||||
$this->authorize('reports.view');
|
||||
$this->usePeriod($this->preset($preset, $clock));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string> The quick choices by key.
|
||||
*/
|
||||
public function periodPresets(): array
|
||||
{
|
||||
return [
|
||||
'this_month' => __('reports.period.this_month'),
|
||||
'previous_month' => __('reports.period.previous_month'),
|
||||
'this_year' => __('reports.period.this_year'),
|
||||
'previous_year' => __('reports.period.previous_year'),
|
||||
];
|
||||
}
|
||||
|
||||
public function periodLabel(): string
|
||||
{
|
||||
$period = $this->period();
|
||||
|
||||
return $period === null ? '' : ReportFormat::period($period);
|
||||
}
|
||||
|
||||
public function money(int $cents): string
|
||||
{
|
||||
return ReportFormat::money($cents);
|
||||
}
|
||||
|
||||
public function hours(int $quarters): string
|
||||
{
|
||||
return ReportFormat::hours($quarters);
|
||||
}
|
||||
|
||||
public function dateLabel(string $day): string
|
||||
{
|
||||
return ReportFormat::date($day);
|
||||
}
|
||||
|
||||
/**
|
||||
* The CSV of the report with the filters shown.
|
||||
*
|
||||
* @param array<string, string> $filters
|
||||
*/
|
||||
protected function csvUrl(string $report, array $filters = []): string
|
||||
{
|
||||
return route('reports.csv', array_filter(['report' => $report, 'alku' => $this->from, 'loppu' => $this->to, ...$filters], fn (string $value): bool => $value !== ''));
|
||||
}
|
||||
|
||||
private function usePeriod(ReportPeriod $period): void
|
||||
{
|
||||
$this->from = $period->from;
|
||||
$this->to = $period->to;
|
||||
unset($this->period);
|
||||
}
|
||||
|
||||
private function preset(string $preset, Clock $clock): ReportPeriod
|
||||
{
|
||||
$today = $clock->today();
|
||||
|
||||
return match ($preset) {
|
||||
'previous_month' => ReportPeriod::previousMonth($today),
|
||||
'this_year' => ReportPeriod::year($today),
|
||||
'previous_year' => ReportPeriod::previousYear($today),
|
||||
default => ReportPeriod::month($today),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user