[Y-m-d => "2,5"]]. * The cells send their value with wire:change; the desktop grid and the * phone list both have an input for the same cell. * * @var array> */ #[Locked] public array $cells = []; /** * Tasks added to this week by search, besides the user's own. * * @var list */ #[Locked] public array $extraTaskIds = []; /** * Values that were not saved, shown back in their cell with the error. * * @var array */ #[Locked] public array $rejected = []; public string $selectedDay = ''; public string $taskSearch = ''; public string $statusMessage = ''; public string $errorMessage = ''; public function mount(Clock $clock): void { $this->authorize('time.use'); $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; $this->loadCells(); } #[Computed] public function sheet(): WeekSheet { return app(TimeEntryQueries::class)->week((int) auth()->id(), $this->week, $this->extraTaskIds); } /** * @return list */ #[Computed] public function searchResults(): array { $term = trim($this->taskSearch); if (mb_strlen($term) < 2) { return []; } $shown = array_map(fn ($row): int => $row->task->taskId, $this->sheet->rows); return array_values(array_filter( app(TaskQueries::class)->searchBookableTasks($term, $this->isSubcontractor() ? (int) auth()->id() : null), fn (BookableTask $task): bool => ! in_array($task->taskId, $shown, true), )); } public function saveCell(int $taskId, string $day, string $value): void { $this->authorize('time.use'); $row = collect($this->sheet->rows)->first(fn ($row): bool => $row->task->taskId === $taskId); if ($row === null || ! in_array($day, $this->sheet->days, true)) { return; } $this->authorize('time.book', $row->task->jobId); $this->resetErrorBag("cells.{$taskId}.{$day}"); unset($this->rejected["{$taskId}.{$day}"]); $this->errorMessage = ''; try { app(SetDayHours::class)->execute((int) auth()->id(), $taskId, $day, $value); } catch (DomainException $exception) { $message = __(match (true) { $exception instanceof InvalidWorkedHours => 'time.errors.invalid_hours', $exception instanceof DailyHoursExceeded => 'time.errors.day_full', $exception instanceof TimeLocked => 'time.errors.locked', default => 'time.errors.not_found', }); $this->addError("cells.{$taskId}.{$day}", $message); $this->errorMessage = $row->task->taskTitle.', '.$this->dayLabel($day).': '.$message; $this->rejected["{$taskId}.{$day}"] = $value; return; } unset($this->sheet); $this->loadCells(); $hours = $this->cells[$taskId][$day] ?? ''; $this->statusMessage = $hours === '' ? __('time.removed', ['task' => $row->task->taskTitle, 'day' => $this->dayLabel($day)]) : __('time.saved', ['task' => $row->task->taskTitle, 'day' => $this->dayLabel($day), 'hours' => $hours]); } public function addTask(int $taskId): void { $this->authorize('time.use'); $task = app(TaskQueries::class)->bookableTask($taskId); if ($task === null || $task->isLocked() || Gate::denies('time.book', $task->jobId)) { return; } if (! in_array($taskId, $this->extraTaskIds, true)) { $this->extraTaskIds[] = $taskId; } $this->taskSearch = ''; unset($this->sheet); $this->loadCells(); $this->statusMessage = __('time.add.added', ['task' => $task->label()]); $this->js("(document.getElementById('cell-{$taskId}-{$this->selectedDay}')?.offsetParent ? document.getElementById('cell-{$taskId}-{$this->selectedDay}') : document.getElementById('mcell-{$taskId}-{$this->selectedDay}'))?.focus()"); } public function selectDay(string $day): void { if (in_array($day, $this->sheet->days, true)) { $this->selectedDay = $day; } } public function showWeek(string $which, Clock $clock): void { $this->week = match ($which) { 'previous' => CarbonImmutable::parse($this->week)->subWeek()->toDateString(), 'next' => CarbonImmutable::parse($this->week)->addWeek()->toDateString(), default => $this->mondayOf($clock->today()->format('Y-m-d')), }; unset($this->sheet); $this->selectedDay = $this->week; $this->statusMessage = $this->weekHeading(); $this->errorMessage = ''; $this->resetErrorBag(); $this->rejected = []; $this->loadCells(); } public function weekHeading(): string { $monday = CarbonImmutable::parse($this->week); return __('time.week_heading', [ 'number' => $monday->isoWeek(), 'from' => $monday->format('j.n.'), 'to' => $monday->addDays(6)->format('j.n.Y'), ]); } public function dayLabel(string $day): string { $date = CarbonImmutable::parse($day); return __('time.days')[$date->dayOfWeekIso - 1].' '.$date->format('j.n.'); } public function hours(int $quarters): string { return WorkedHours::formatQuarters($quarters); } public function isSubcontractor(): bool { return auth()->user()?->role === Role::Subcontractor; } public function render(): mixed { return $this->view()->title(__('time.title')); } private function loadCells(): void { $cells = []; foreach ($this->sheet->rows as $row) { foreach ($this->sheet->days as $day) { $quarters = $row->quartersByDay[$day] ?? 0; $cells[$row->task->taskId][$day] = $quarters === 0 ? '' : WorkedHours::formatQuarters($quarters); } } $this->cells = $cells; } private function mondayOf(string $date): string { try { return CarbonImmutable::parse($date)->startOfWeek()->toDateString(); } catch (\Throwable) { return CarbonImmutable::now()->startOfWeek()->toDateString(); } } }; ?> @php($sheet = $this->sheet)

{{ __('time.title') }}

{{ __('time.intro') }}

{{ $this->weekHeading() }}

{{ $statusMessage }}

@if ($sheet->rows === [])

{{ __('time.no_rows') }}

@else {{-- Desktop: the whole week as an input grid (a form, so a native table rather than ). --}} {{-- Phone: one day at a time. --}}
@foreach ($sheet->days as $day) @endforeach
    @foreach ($sheet->rows as $row)
  • {{ $row->task->label() }} {{ $row->task->workTypeName }} @if ($row->task->isLocked())

    @else task->taskId.'.'.$selectedDay) aria-invalid="true" aria-describedby="mcell-{{ $row->task->taskId }}-{{ $selectedDay }}-error" @enderror class="mt-1 w-24 rounded-md border border-zinc-400 bg-white px-2 py-2 text-zinc-900 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-700 aria-invalid:border-red-700 dark:border-zinc-500 dark:bg-zinc-900 dark:text-zinc-100 dark:aria-invalid:border-red-400" /> @error('cells.'.$row->task->taskId.'.'.$selectedDay)

    {{ $message }}

    @enderror @endif
  • @endforeach

{{ __('time.week_total', ['hours' => $this->hours($sheet->totalQuarters)]) }}

@endif

{{ __('time.add.search') }}

@if (mb_strlen(trim($taskSearch)) >= 2)

{{ trans_choice('time.add.results', count($this->searchResults), ['count' => count($this->searchResults)]) }}

    @foreach ($this->searchResults as $task)
  • @endforeach
@endif