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 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); } /** * The CSV of the report with the filters shown. * * @param array $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), }; } }