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),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Reports\Application\DTOs\HoursReport;
|
||||
use App\Modules\Reports\Application\UseCases\HoursByPerson;
|
||||
use App\Modules\Reports\Presentation\Livewire\Concerns\ReportPage;
|
||||
use App\Modules\Users\Application\Contracts\Clock;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Url;
|
||||
use Livewire\Component;
|
||||
|
||||
/**
|
||||
* Hours by person (spec 13): everyone's hours in a period on customers'
|
||||
* and internal jobs, waiting for approval and billed, with each person's
|
||||
* hours by work type and by job. Narrowed by the jobs' service unit or
|
||||
* customer.
|
||||
*/
|
||||
new class extends Component
|
||||
{
|
||||
use ReportPage;
|
||||
|
||||
#[Url(as: 'yksikko', except: '')]
|
||||
public string $serviceUnit = '';
|
||||
|
||||
#[Url(as: 'asiakas', except: '')]
|
||||
public string $customer = '';
|
||||
|
||||
public function mount(Clock $clock): void
|
||||
{
|
||||
$this->startPeriod($clock);
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function report(): ?HoursReport
|
||||
{
|
||||
$period = $this->period();
|
||||
|
||||
if ($period === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return app(HoursByPerson::class)->execute(
|
||||
$period,
|
||||
ctype_digit($this->serviceUnit) ? (int) $this->serviceUnit : null,
|
||||
ctype_digit($this->customer) ? (int) $this->customer : null,
|
||||
);
|
||||
}
|
||||
|
||||
public function updated(): void
|
||||
{
|
||||
$this->authorize('reports.view');
|
||||
unset($this->report);
|
||||
}
|
||||
|
||||
public function csv(): string
|
||||
{
|
||||
return $this->csvUrl('hours', ['yksikko' => $this->serviceUnit, 'asiakas' => $this->customer]);
|
||||
}
|
||||
|
||||
public function chartSummary(HoursReport $report): string
|
||||
{
|
||||
return implode(', ', array_map(
|
||||
fn ($person): string => $person->name.' '.$this->hours($person->quarters).' h',
|
||||
array_slice($report->people, 0, 15),
|
||||
));
|
||||
}
|
||||
|
||||
public function render(): mixed
|
||||
{
|
||||
return $this->view()->title(__('reports.hours.title'));
|
||||
}
|
||||
};
|
||||
?>
|
||||
|
||||
<div class="mx-auto max-w-6xl space-y-6 p-4 sm:p-6">
|
||||
<h1 class="text-2xl font-semibold">{{ __('reports.hours.title') }}</h1>
|
||||
|
||||
<x-reports.period-filter :presets="$this->periodPresets()" :valid="$this->period !== null" />
|
||||
|
||||
@php($report = $this->report)
|
||||
|
||||
@if ($report !== null)
|
||||
<div class="grid max-w-2xl gap-4 sm:grid-cols-2">
|
||||
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
|
||||
<x-select.native wire:model.live="serviceUnit" :label="__('reports.filters.service_unit')" :aria-label="__('reports.filters.service_unit')">
|
||||
<option value="">{{ __('reports.filters.all_service_units') }}</option>
|
||||
@foreach ($report->serviceUnits as $id => $name)
|
||||
<option value="{{ $id }}">{{ $name }}</option>
|
||||
@endforeach
|
||||
</x-select.native>
|
||||
|
||||
<x-select.native wire:model.live="customer" :label="__('reports.filters.customer')" :aria-label="__('reports.filters.customer')">
|
||||
<option value="">{{ __('reports.filters.all_customers') }}</option>
|
||||
@foreach ($report->customers as $id => $name)
|
||||
<option value="{{ $id }}">{{ $name }}</option>
|
||||
@endforeach
|
||||
</x-select.native>
|
||||
</div>
|
||||
|
||||
<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>
|
||||
<x-button :href="$this->csv()" subtle color="black" icon="arrow-down-tray" :text="__('reports.download_csv')" />
|
||||
</div>
|
||||
|
||||
@if ($report->people === [])
|
||||
<p>{{ __('reports.hours.none') }}</p>
|
||||
@else
|
||||
<div class="overflow-x-auto">
|
||||
<table class="w-full text-sm">
|
||||
<caption class="sr-only">{{ __('reports.hours.table', ['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.hours.person') }}</th>
|
||||
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.hours.total') }}</th>
|
||||
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.hours.customer') }}</th>
|
||||
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.hours.internal') }}</th>
|
||||
<th scope="col" class="py-2 pr-3 text-right">{{ __('reports.hours.unapproved') }}</th>
|
||||
<th scope="col" class="py-2 text-right">{{ __('reports.hours.billed') }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="divide-y divide-zinc-200 dark:divide-zinc-700">
|
||||
@foreach ($report->people as $person)
|
||||
<tr wire:key="person-{{ $person->userId }}">
|
||||
<th scope="row" class="py-2 pr-3 text-left font-normal">{{ $person->name }}</th>
|
||||
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($person->quarters) }}</td>
|
||||
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($person->customerQuarters) }}</td>
|
||||
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($person->internalQuarters) }}</td>
|
||||
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($person->unapprovedQuarters) }}</td>
|
||||
<td class="py-2 text-right whitespace-nowrap">{{ $this->hours($person->billedQuarters) }}</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</tbody>
|
||||
<tfoot>
|
||||
<tr class="border-t-2 border-zinc-400 font-semibold dark:border-zinc-500">
|
||||
<th scope="row" class="py-2 pr-3 text-left">{{ __('reports.total') }}</th>
|
||||
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($report->total->quarters) }}</td>
|
||||
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($report->total->customerQuarters) }}</td>
|
||||
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($report->total->internalQuarters) }}</td>
|
||||
<td class="py-2 pr-3 text-right whitespace-nowrap">{{ $this->hours($report->total->unapprovedQuarters) }}</td>
|
||||
<td class="py-2 text-right whitespace-nowrap">{{ $this->hours($report->total->billedQuarters) }}</td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<x-reports.chart
|
||||
:title="__('reports.hours.chart')"
|
||||
:summary="$this->chartSummary($report)"
|
||||
:labels="array_map(fn ($person) => $person->name, $report->people)"
|
||||
:values="array_map(fn ($person) => $person->quarters / 4, $report->people)"
|
||||
:formatter="fn (float $value) => number_format($value, 0, ',', ' ').' h'"
|
||||
/>
|
||||
|
||||
<section aria-labelledby="breakdown-heading" class="space-y-3">
|
||||
<h2 id="breakdown-heading" class="text-xl font-semibold">{{ __('reports.hours.breakdown') }}</h2>
|
||||
|
||||
@foreach ($report->people as $person)
|
||||
<details wire:key="details-{{ $person->userId }}" class="rounded-lg border border-zinc-300 p-3 dark:border-zinc-600">
|
||||
<summary class="cursor-pointer font-medium focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary-600">
|
||||
{{ __('reports.hours.person_summary', ['name' => $person->name, 'hours' => $this->hours($person->quarters)]) }}
|
||||
</summary>
|
||||
|
||||
<div class="mt-3 grid gap-6 md:grid-cols-2">
|
||||
<table class="w-full text-sm">
|
||||
<caption class="mb-1 text-left font-semibold">{{ __('reports.hours.by_work_type') }}</caption>
|
||||
<thead>
|
||||
<tr class="text-left">
|
||||
<th scope="col" class="py-1 pr-3">{{ __('reports.hours.work_type') }}</th>
|
||||
<th scope="col" class="py-1 text-right">{{ __('reports.hours.hours') }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="divide-y divide-zinc-200 dark:divide-zinc-700">
|
||||
@foreach ($person->byWorkType as $row)
|
||||
<tr>
|
||||
<th scope="row" class="py-1 pr-3 text-left font-normal">{{ $row->label }}</th>
|
||||
<td class="py-1 text-right whitespace-nowrap">{{ $this->hours($row->quarters) }}</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<table class="w-full text-sm">
|
||||
<caption class="mb-1 text-left font-semibold">{{ __('reports.hours.by_job') }}</caption>
|
||||
<thead>
|
||||
<tr class="text-left">
|
||||
<th scope="col" class="py-1 pr-3">{{ __('reports.hours.job') }}</th>
|
||||
<th scope="col" class="py-1 text-right">{{ __('reports.hours.hours') }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="divide-y divide-zinc-200 dark:divide-zinc-700">
|
||||
@foreach ($person->byJob as $row)
|
||||
<tr>
|
||||
<th scope="row" class="py-1 pr-3 text-left font-normal">
|
||||
<x-link :href="route('jobs.show', $row->jobId)" navigate>{{ $row->label }}</x-link>
|
||||
</th>
|
||||
<td class="py-1 text-right whitespace-nowrap">{{ $this->hours($row->quarters) }}</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</details>
|
||||
@endforeach
|
||||
</section>
|
||||
@endif
|
||||
@endif
|
||||
</div>
|
||||
Reference in New Issue
Block a user