Files
Toiminnanohjaus/app/Modules/Reports/Presentation/Livewire/Concerns/ReportPage.php
T
petriandClaude Sonnet 5.5 568871c9dc Add the quick sales report
Quick sales of a period by cash day, product, seller and VAT rate, with
credits, open cash days flagged, a chart by day or month and CSV. VAT is
taken out of each day's total of a rate, as on the cash day balance.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-25 08:44:20 +02:00

116 lines
3.2 KiB
PHP

<?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 formats. Without a valid period in
* the URL a page starts with the current month.
*/
trait ReportPage
{
use FormatsReports;
#[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);
}
/**
* The CSV of the report with the filters shown.
*
* @param array<string, string> $filters
*/
public 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),
};
}
}