Files
Toiminnanohjaus/app/Modules/TimeTracking/Presentation/Livewire/⚡approvals.blade.php
T
petriandClaude Opus 5.5 d5cbf9d6eb Add approval of hours and lock opening for management
- Time entries store who approved them and when. Approved hours are
  locked: cells, task page edits and removals are refused.
- "Hyväksy viikko" in the weekly view approves the user's own week;
  subcontractors' hours are approved by management.
- Management gets an approval page (unapproved weeks by person, recently
  approved weeks) and can open anyone's week read-only to approve it or
  cancel its approval, which only management may do.
- Only management may move a done job back to an earlier status, which
  opens the lock on its hours and expenses; the history marks it.
- The Clock contract gets now() for approval times.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 08:24:34 +02:00

230 lines
8.9 KiB
PHP

<?php
use App\Modules\TimeTracking\Application\Contracts\TimeEntryQueries;
use App\Modules\TimeTracking\Application\DTOs\ApprovalWeek;
use App\Modules\TimeTracking\Application\UseCases\ApproveWeek;
use App\Modules\TimeTracking\Domain\ValueObjects\WorkedHours;
use App\Modules\Users\Application\Contracts\Clock;
use App\Modules\Users\Application\Contracts\UserAccountQueries;
use App\Modules\Users\Application\DTOs\UserOption;
use Carbon\CarbonImmutable;
use Livewire\Attributes\Computed;
use Livewire\Component;
/**
* Management's view of hours to approve: unapproved hours by person and
* week, and recently approved weeks. Each week opens in the weekly view.
*/
new class extends Component
{
public string $personFilter = '';
public string $statusMessage = '';
public function mount(): void
{
$this->authorize('time.approve-others');
}
/**
* @return list<ApprovalWeek>
*/
#[Computed]
public function pending(): array
{
return app(TimeEntryQueries::class)->pendingWeeks($this->personFilter === '' ? null : (int) $this->personFilter);
}
/**
* @return list<ApprovalWeek>
*/
#[Computed]
public function approved(): array
{
$since = CarbonImmutable::instance(app(Clock::class)->today())->startOfWeek()->subWeeks(7)->toDateString();
$weeks = app(TimeEntryQueries::class)->approvedWeeks($since);
return $this->personFilter === ''
? $weeks
: array_values(array_filter($weeks, fn (ApprovalWeek $week): bool => $week->userId === (int) $this->personFilter));
}
/**
* @return list<UserOption>
*/
#[Computed]
public function people(): array
{
return app(UserAccountQueries::class)->activeUserOptions();
}
public function approve(int $userId, string $monday, ApproveWeek $approveWeek): void
{
$this->authorize('time.approve-others');
$week = collect($this->pending)->first(fn (ApprovalWeek $week): bool => $week->userId === $userId && $week->monday === $monday);
if ($week === null) {
return;
}
$approveWeek->execute($userId, $monday, (int) auth()->id());
unset($this->pending, $this->approved);
$this->statusMessage = __('time.approval.approved_for', ['name' => $week->userName, 'week' => $this->weekLabel($monday)]);
$this->js("document.getElementById('pending-heading')?.focus()");
}
/**
* @return list<array{index: string, label: string, sortable: bool, align?: string}>
*/
public function pendingHeaders(): array
{
return [
['index' => 'person', 'label' => __('time.approvals.person'), 'sortable' => false],
['index' => 'week', 'label' => __('time.approvals.week'), 'sortable' => false],
['index' => 'hours', 'label' => __('time.approvals.hours'), 'sortable' => false, 'align' => 'right'],
['index' => 'actions', 'label' => __('time.approvals.actions'), 'sortable' => false],
];
}
/**
* @return list<array{index: string, label: string, sortable: bool, align?: string}>
*/
public function approvedHeaders(): array
{
return [
['index' => 'person', 'label' => __('time.approvals.person'), 'sortable' => false],
['index' => 'week', 'label' => __('time.approvals.week'), 'sortable' => false],
['index' => 'hours', 'label' => __('time.approvals.hours'), 'sortable' => false, 'align' => 'right'],
['index' => 'approver', 'label' => __('time.approvals.approver'), 'sortable' => false],
['index' => 'actions', 'label' => __('time.approvals.actions'), 'sortable' => false],
];
}
public function weekLabel(string $monday): string
{
$start = CarbonImmutable::parse($monday);
return __('time.week_heading', ['number' => $start->isoWeek(), 'from' => $start->format('j.n.'), 'to' => $start->addDays(6)->format('j.n.Y')]);
}
public function hours(int $quarters): string
{
return WorkedHours::formatQuarters($quarters);
}
public function weekUrl(ApprovalWeek $week): string
{
return route('time.week', ['kayttaja' => $week->userId, 'viikko' => $week->monday]);
}
public function render(): mixed
{
return $this->view()->title(__('time.approvals.title'));
}
};
?>
<div class="mx-auto max-w-5xl space-y-6 p-4 sm:p-6">
<div class="space-y-2">
<h1 class="text-2xl font-semibold">{{ __('time.approvals.title') }}</h1>
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('time.approvals.intro') }}</p>
</div>
<div class="max-w-sm">
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
<x-select.native wire:model.live="personFilter" :label="__('time.approvals.person')" :aria-label="__('time.approvals.person')">
<option value="">{{ __('time.approvals.all_people') }}</option>
@foreach ($this->people as $person)
<option wire:key="person-{{ $person->id }}" value="{{ $person->id }}">{{ $person->name }}</option>
@endforeach
</x-select.native>
</div>
<p role="status" aria-live="polite" class="min-h-5 text-sm">{{ $statusMessage }}</p>
<section aria-labelledby="pending-heading" class="space-y-3">
<h2 id="pending-heading" tabindex="-1" class="text-xl font-semibold focus:outline-none">{{ __('time.approvals.pending') }}</h2>
@if ($this->pending === [])
<p class="text-sm">{{ __('time.approvals.none_pending') }}</p>
@else
<x-table :headers="$this->pendingHeaders()" :rows="$this->pending">
@interact('column_person', $week)
<span class="font-medium">{{ $week->userName }}</span>
@endinteract
@interact('column_week', $week)
{{ $this->weekLabel($week->monday) }}
@endinteract
@interact('column_hours', $week)
{{ $this->hours($week->quarters) }}
@endinteract
@interact('column_actions', $week)
<div class="flex flex-wrap gap-2">
<x-button
wire:click="approve({{ $week->userId }}, '{{ $week->monday }}')"
sm
icon="check"
:text="__('time.approval.approve_week')"
:aria-label="__('time.approval.approve_week_of', ['name' => $week->userName, 'week' => $this->weekLabel($week->monday)])"
/>
<x-button
:href="$this->weekUrl($week)"
wire:navigate
subtle
color="black"
sm
:text="__('time.approvals.open_week')"
:aria-label="__('time.approvals.open_week_of', ['name' => $week->userName, 'week' => $this->weekLabel($week->monday)])"
/>
</div>
@endinteract
</x-table>
@endif
</section>
<section aria-labelledby="approved-heading" class="space-y-3">
<h2 id="approved-heading" class="text-xl font-semibold">{{ __('time.approvals.approved') }}</h2>
@if ($this->approved === [])
<p class="text-sm">{{ __('time.approvals.none_approved') }}</p>
@else
<x-table :headers="$this->approvedHeaders()" :rows="$this->approved">
@interact('column_person', $week)
<span class="font-medium">{{ $week->userName }}</span>
@endinteract
@interact('column_week', $week)
{{ $this->weekLabel($week->monday) }}
@endinteract
@interact('column_hours', $week)
{{ $this->hours($week->quarters) }}
@endinteract
@interact('column_approver', $week)
@if ($week->approval !== null)
{{ __('time.approvals.approver_value', ['name' => $week->approval->approverName, 'date' => \Carbon\CarbonImmutable::parse($week->approval->approvedAt)->format('j.n.Y H:i')]) }}
@endif
@endinteract
@interact('column_actions', $week)
<x-button
:href="$this->weekUrl($week)"
wire:navigate
subtle
color="black"
sm
:text="__('time.approvals.open_week')"
:aria-label="__('time.approvals.open_week_of', ['name' => $week->userName, 'week' => $this->weekLabel($week->monday)])"
/>
@endinteract
</x-table>
@endif
</section>
</div>