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]>
This commit is contained in:
2026-11-24 08:24:34 +02:00
co-authored by Claude Opus 5.5
parent 54d98d9d2f
commit d5cbf9d6eb
43 changed files with 1183 additions and 34 deletions
@@ -5,12 +5,16 @@ use App\Modules\Jobs\Application\DTOs\BookableTask;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use App\Modules\TimeTracking\Application\Contracts\TimeEntryQueries;
use App\Modules\TimeTracking\Application\DTOs\WeekSheet;
use App\Modules\TimeTracking\Application\UseCases\ApproveWeek;
use App\Modules\TimeTracking\Application\UseCases\CancelWeekApproval;
use App\Modules\TimeTracking\Application\UseCases\SetDayHours;
use App\Modules\TimeTracking\Domain\Exceptions\DailyHoursExceeded;
use App\Modules\TimeTracking\Domain\Exceptions\InvalidWorkedHours;
use App\Modules\TimeTracking\Domain\Exceptions\TimeApproved;
use App\Modules\TimeTracking\Domain\Exceptions\TimeLocked;
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\Domain\Enums\Role;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Gate;
@@ -22,7 +26,8 @@ use Livewire\Component;
/**
* The weekly time sheet: tasks as rows, days as columns. A cell saves when
* focus leaves it; an empty cell removes the day's entry. On a phone one
* day is shown at a time.
* day is shown at a time. Approved hours are locked. Management may open
* another user's week (?kayttaja=ID) to read, approve or cancel approval.
*/
new class extends Component
{
@@ -34,6 +39,19 @@ new class extends Component
#[Url(as: 'viikko', except: '')]
public string $week = '';
/**
* Another user's id from the URL, for management; '' is the signed-in user.
*/
#[Locked]
#[Url(as: 'kayttaja', except: '')]
public string $user = '';
#[Locked]
public int $viewedUserId = 0;
#[Locked]
public string $viewedUserName = '';
/**
* Stored hours as shown, by task id and day: [taskId => [Y-m-d => "2,5"]].
* The cells send their value with wire:change; the desktop grid and the
@@ -77,6 +95,20 @@ new class extends Component
{
$this->authorize('time.use');
$this->viewedUserId = (int) auth()->id();
if ($this->user !== '' && (int) $this->user !== $this->viewedUserId) {
$this->authorize('time.approve-others');
$summary = ctype_digit($this->user) ? app(UserAccountQueries::class)->findSummary((int) $this->user) : null;
abort_if($summary === null, 404);
$this->viewedUserId = $summary->id;
$this->viewedUserName = $summary->name;
} else {
$this->user = '';
}
$this->week = $this->mondayOf($this->week !== '' ? $this->week : $clock->today()->format('Y-m-d'));
$today = $clock->today()->format('Y-m-d');
$this->selectedDay = in_array($today, $this->sheet->days, true) ? $today : $this->week;
@@ -86,7 +118,56 @@ new class extends Component
#[Computed]
public function sheet(): WeekSheet
{
return app(TimeEntryQueries::class)->week((int) auth()->id(), $this->week, $this->extraTaskIds);
return app(TimeEntryQueries::class)->week($this->viewedUserId, $this->week, $this->extraTaskIds);
}
/**
* Whether management is looking at someone else's week: nothing can be typed then.
*/
public function isReadOnly(): bool
{
return $this->viewedUserId !== (int) auth()->id();
}
public function canApprove(): bool
{
return Gate::allows('time.approve', $this->viewedUserId);
}
public function approveWeek(ApproveWeek $approveWeek): void
{
$this->authorize('time.approve', $this->viewedUserId);
$count = $approveWeek->execute($this->viewedUserId, $this->week, (int) auth()->id());
unset($this->sheet);
$this->loadCells();
$this->rejected = [];
$this->resetErrorBag();
$this->errorMessage = '';
$this->statusMessage = $count === 0
? __('time.approval.nothing_to_approve')
: __('time.approval.approved', ['week' => $this->weekHeading()]);
}
public function confirmCancelApproval(): void
{
$this->authorize('time.cancel-approval');
$this->js("document.getElementById('cancel-approval-dialog').showModal()");
}
public function cancelApproval(CancelWeekApproval $cancelApproval): void
{
$this->authorize('time.cancel-approval');
$cancelApproval->execute($this->viewedUserId, $this->week);
unset($this->sheet);
$this->loadCells();
$this->statusMessage = __('time.approval.cancelled', ['week' => $this->weekHeading()]);
$this->js("document.getElementById('cancel-approval-dialog').close(); document.getElementById('approval-heading').focus()");
}
/**
@@ -115,7 +196,7 @@ new class extends Component
$row = collect($this->sheet->rows)->first(fn ($row): bool => $row->task->taskId === $taskId);
if ($row === null || ! in_array($day, $this->sheet->days, true)) {
if ($this->isReadOnly() || $row === null || ! in_array($day, $this->sheet->days, true)) {
return;
}
@@ -131,6 +212,7 @@ new class extends Component
$exception instanceof InvalidWorkedHours => 'time.errors.invalid_hours',
$exception instanceof DailyHoursExceeded => 'time.errors.day_full',
$exception instanceof TimeLocked => 'time.errors.locked',
$exception instanceof TimeApproved => 'time.errors.approved',
default => 'time.errors.not_found',
});
$this->addError("cells.{$taskId}.{$day}", $message);
@@ -155,7 +237,7 @@ new class extends Component
$task = app(TaskQueries::class)->bookableTask($taskId);
if ($task === null || $task->isLocked() || Gate::denies('time.book', $task->jobId)) {
if ($this->isReadOnly() || $task === null || $task->isLocked() || Gate::denies('time.book', $task->jobId)) {
return;
}
@@ -223,9 +305,14 @@ new class extends Component
return auth()->user()?->role === Role::Subcontractor;
}
public function heading(): string
{
return $this->isReadOnly() ? __('time.other_title', ['name' => $this->viewedUserName]) : __('time.title');
}
public function render(): mixed
{
return $this->view()->title(__('time.title'));
return $this->view()->title($this->heading());
}
private function loadCells(): void
@@ -257,8 +344,13 @@ new class extends Component
<div class="mx-auto max-w-7xl space-y-6 p-4 sm:p-6">
<div class="space-y-2">
<h1 class="text-2xl font-semibold">{{ __('time.title') }}</h1>
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('time.intro') }}</p>
<h1 class="text-2xl font-semibold break-words">{{ $this->heading() }}</h1>
@if ($this->isReadOnly())
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('time.other_intro') }}</p>
<x-link :href="route('time.approvals')" navigate class="text-sm">{{ __('time.approval.back_to_list') }}</x-link>
@else
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('time.intro') }}</p>
@endif
</div>
<div class="flex flex-wrap items-center justify-between gap-3">
@@ -303,7 +395,13 @@ new class extends Component
</th>
@foreach ($sheet->days as $day)
<td class="px-1 py-2 text-center">
@if ($row->task->isLocked())
@if ($row->isApprovedOn($day))
<span class="inline-flex items-center gap-1">
{{ $this->hours($row->quartersByDay[$day] ?? 0) }}
<x-icon name="check-circle" class="size-4" aria-hidden="true" />
<span class="sr-only">{{ __('time.approval.cell_approved') }}</span>
</span>
@elseif ($row->task->isLocked() || $this->isReadOnly())
{{ isset($row->quartersByDay[$day]) ? $this->hours($row->quartersByDay[$day]) : '' }}
@else
<input
@@ -366,12 +464,19 @@ new class extends Component
<x-link :href="route('time.task', $row->task->taskId)" navigate class="font-medium break-words">{{ $row->task->label() }}</x-link>
<span class="block text-zinc-600 dark:text-zinc-300">{{ $row->task->workTypeName }}</span>
@if ($row->task->isLocked())
@if ($row->isApprovedOn($selectedDay))
<p class="mt-2 inline-flex items-center gap-1">
<x-icon name="lock-closed" class="size-4" aria-hidden="true" /> {{ __('time.locked') }}
@isset($row->quartersByDay[$selectedDay])
– {{ __('time.hours_suffix', ['hours' => $this->hours($row->quartersByDay[$selectedDay])]) }}
@endisset
<x-icon name="check-circle" class="size-4" aria-hidden="true" /> {{ __('time.approval.cell_approved') }}
– {{ __('time.hours_suffix', ['hours' => $this->hours($row->quartersByDay[$selectedDay] ?? 0)]) }}
</p>
@elseif ($row->task->isLocked() || $this->isReadOnly())
<p class="mt-2 inline-flex items-center gap-1">
@if ($row->task->isLocked())
<x-icon name="lock-closed" class="size-4" aria-hidden="true" /> {{ __('time.locked') }}
@else
{{ $this->dayLabel($selectedDay) }}
@endif
– {{ __('time.hours_suffix', ['hours' => $this->hours($row->quartersByDay[$selectedDay] ?? 0)]) }}
</p>
@else
<label for="mcell-{{ $row->task->taskId }}-{{ $selectedDay }}" class="mt-2 block font-medium">
@@ -399,6 +504,50 @@ new class extends Component
<p class="text-sm font-semibold">{{ __('time.week_total', ['hours' => $this->hours($sheet->totalQuarters)]) }}</p>
@endif
<section aria-labelledby="approval-heading" class="space-y-3 rounded-lg border border-zinc-200 p-4 dark:border-zinc-700">
<h2 id="approval-heading" tabindex="-1" class="text-lg font-semibold focus:outline-none">{{ __('time.approval.title') }}</h2>
@if ($sheet->hasUnapprovedHours())
<p class="text-sm">{{ __('time.approval.unapproved', ['hours' => $this->hours($sheet->unapprovedQuarters)]) }}</p>
@elseif ($sheet->totalQuarters === 0)
<p class="text-sm">{{ __('time.approval.no_hours') }}</p>
@endif
@if ($sheet->lastApproval !== null)
<p class="inline-flex items-center gap-1 text-sm">
<x-icon name="check-circle" class="size-4" aria-hidden="true" />
{{ __('time.approval.approved_by', ['date' => \Carbon\CarbonImmutable::parse($sheet->lastApproval->approvedAt)->format('j.n.Y H:i'), 'name' => $sheet->lastApproval->approverName]) }}
</p>
@endif
<div class="flex flex-wrap gap-2">
@if ($sheet->hasUnapprovedHours())
@if ($this->canApprove())
<x-button wire:click="approveWeek" icon="check" :text="__('time.approval.approve_week')" />
@else
<p class="text-sm">{{ __('time.approval.management_approves') }}</p>
@endif
@endif
@if ($sheet->hasApprovedHours())
@can('time.cancel-approval')
<x-button wire:click="confirmCancelApproval" subtle color="black" icon="arrow-uturn-left" :text="__('time.approval.cancel')" />
@endcan
@endif
</div>
</section>
@can('time.cancel-approval')
<x-confirm-dialog id="cancel-approval-dialog" :title="__('time.approval.cancel_title')">
<p>{{ __('time.approval.cancel_question', ['week' => $this->weekHeading()]) }}</p>
<x-slot:actions>
<x-button wire:click="cancelApproval" color="red" icon="arrow-uturn-left" :text="__('time.approval.cancel')" />
</x-slot:actions>
</x-confirm-dialog>
@endcan
@unless ($this->isReadOnly())
<section aria-labelledby="add-task-heading" class="space-y-2">
<h2 id="add-task-heading" class="sr-only">{{ __('time.add.search') }}</h2>
<x-input
@@ -432,4 +581,5 @@ new class extends Component
</ul>
@endif
</section>
@endunless
</div>