authorize('jobs.view'); } public function isSubcontractor(): bool { return auth()->user()?->role === Role::Subcontractor; } public function startDate(): CarbonImmutable { $start = $this->from === '' ? null : CarbonImmutable::createFromFormat('!Y-m-d', $this->from); return $start instanceof CarbonImmutable && $start->format('Y-m-d') === $this->from ? $start : CarbonImmutable::today(); } /** * @return list */ #[Computed] public function days(): array { $start = $this->startDate(); return array_map(fn (int $offset): string => $start->addDays($offset)->format('Y-m-d'), range(0, self::DAYS - 1)); } /** * Holidays in use on the shown days, date => name. * * @return array */ #[Computed] public function holidays(): array { $names = []; foreach (app(HolidayCalendar::class)->between($this->days[0], $this->days[self::DAYS - 1]) as $holiday) { if ($holiday->active) { $names[$holiday->date] = $holiday->key === null ? (string) $holiday->name : __('holidays.names.'.$holiday->key); } } return $names; } /** * Rows: the signed-in user first, then the others by name, then tasks * without an assignee. A subcontractor sees only their own row. * * @return list */ #[Computed] public function people(): array { $userId = (int) auth()->id(); $own = ['id' => $userId, 'name' => (string) auth()->user()?->name]; if ($this->isSubcontractor() || $this->mine) { return [$own]; } $others = []; foreach (app(UserAccountQueries::class)->activeUserOptions() as $user) { if ($user->id !== $userId) { $others[] = ['id' => $user->id, 'name' => $user->name]; } } return [$own, ...$others, ['id' => null, 'name' => __('jobs.calendar.no_assignee')]]; } /** * @return list */ #[Computed] public function rows(): array { $queries = app(TaskCalendarQueries::class); $first = $this->days[0]; $last = $this->days[self::DAYS - 1]; $userIds = array_values(array_filter(array_column($this->people, 'id'))); return TaskCalendarLayout::build( $this->days, $this->people, $queries->scheduledTasks($first, $last, $this->serviceUnitId(), $this->onlyAssigneeUserId()), $queries->openMinutesByUser($userIds, $first, $last), ); } /** * @return list */ #[Computed] public function unscheduled(): array { $tasks = app(TaskCalendarQueries::class)->unscheduledOpenTasks($this->serviceUnitId(), $this->onlyAssigneeUserId()); if ($this->mine) { $userId = (int) auth()->id(); $tasks = array_values(array_filter($tasks, fn (CalendarTask $task): bool => $task->assignmentOf($userId) !== null)); } return $tasks; } /** * Names of the people on the rows, user id => name, for assignee lists. * * @return array */ #[Computed] public function userNames(): array { $names = []; foreach (app(UserAccountQueries::class)->activeUserOptions() as $user) { $names[$user->id] = $user->name; } return $names; } #[Computed] public function capacityMinutes(): int { return app(TaskPlanner::class)->capacityMinutes(); } public function canManage(): bool { return Gate::allows('jobs.manage-tasks', 0); } public function showPrevious(): void { $this->moveRange(-self::DAYS); } public function showNext(): void { $this->moveRange(self::DAYS); } public function showToday(): void { $this->from = ''; $this->refresh(); $this->statusMessage = $this->showingMessage(); } /** * A bar was dropped (drag and drop). Without an end, the task gets as * many working days as its estimate needs. */ public function reschedule(int $taskId, int $jobId, ?int $fromUserId, ?int $toUserId, string $plannedFrom, ?string $plannedTo, ScheduleTask $scheduleTask): void { $this->authorize('jobs.manage-tasks', $jobId); $this->schedule($scheduleTask, $taskId, $jobId, $fromUserId, $toUserId, $plannedFrom, $plannedTo); } /** * Opens the dialog of a bar or an unscheduled task (keyboard alternative to dragging). */ public function openDialog(int $taskId, ?int $userId): void { $task = $this->findTask($taskId); if ($task === null) { $this->statusMessage = __('jobs.calendar.not_changed'); return; } $this->authorize('jobs.update-task-status', $task->jobId); $this->resetErrorBag(); $this->dialogTaskId = $task->taskId; $this->dialogJobId = $task->jobId; $this->dialogUserId = $userId; $this->dialogTitle = __('jobs.calendar.dialog_task', ['title' => $task->title, 'job' => $task->jobNumber]); $this->dialogStatus = $task->status->value; $this->plannedFrom = $task->plannedFrom ?? CarbonImmutable::today()->format('Y-m-d'); $this->plannedTo = $task->plannedTo ?? ''; $this->assignee = $userId === null ? '' : (string) $userId; $this->status = $task->status->value; $this->js("document.getElementById('task-schedule-dialog').showModal()"); } public function saveDialog(ScheduleTask $scheduleTask, ChangeTaskStatus $changeTaskStatus): void { if ($this->dialogTaskId === null || $this->dialogJobId === null) { return; } $this->authorize('jobs.update-task-status', $this->dialogJobId); $taskId = $this->dialogTaskId; $jobId = $this->dialogJobId; $task = $this->findTask($taskId); $toUserId = $this->dialogUserId; $messages = []; if (Gate::allows('jobs.manage-tasks', $jobId)) { $this->validate([ 'plannedFrom' => ['required', 'date_format:Y-m-d'], 'plannedTo' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:plannedFrom'], 'assignee' => ['nullable', 'integer'], ], [ 'plannedFrom.*' => __('jobs.calendar.errors.from'), 'plannedTo.*' => __('jobs.calendar.errors.to'), ]); $toUserId = $this->assignee === '' ? null : (int) $this->assignee; if (! $this->schedule($scheduleTask, $taskId, $jobId, $this->dialogUserId, $toUserId, $this->plannedFrom, $this->plannedTo === '' ? null : $this->plannedTo)) { return; } $messages[] = $this->statusMessage; } $status = TaskStatus::tryFrom($this->status); if ($status !== null && $status->value !== $this->dialogStatus) { try { $changeTaskStatus->execute($jobId, $taskId, $status); } catch (TaskNotFound) { $this->statusMessage = __('jobs.calendar.not_changed'); return; } $this->refresh(); $messages[] = __('jobs.calendar.status_changed', ['title' => $task->title ?? '', 'status' => __('jobs.tasks.statuses.'.$status->value)]); } $this->statusMessage = implode(' ', $messages); $this->closeDialogAndFocus($taskId, $toUserId); } public function clearSchedule(ClearTaskSchedule $clearTaskSchedule): void { if ($this->dialogTaskId === null || $this->dialogJobId === null) { return; } $this->authorize('jobs.manage-tasks', $this->dialogJobId); $task = $this->findTask($this->dialogTaskId); try { $clearTaskSchedule->execute($this->dialogJobId, $this->dialogTaskId); } catch (TaskNotFound) { $this->statusMessage = __('jobs.calendar.not_changed'); return; } $this->refresh(); $this->statusMessage = __('jobs.calendar.cleared', ['title' => $task->title ?? '']); $taskId = $this->dialogTaskId; $this->js("document.getElementById('task-schedule-dialog').close(); document.getElementById('unscheduled-{$taskId}')?.focus()"); } public function updatedServiceUnit(): void { $this->refresh(); } public function updatedMine(): void { $this->refresh(); } public function dayLabel(string $date): string { $day = CarbonImmutable::createFromFormat('!Y-m-d', $date); return $day instanceof CarbonImmutable ? __('holidays.weekdays.'.$day->dayOfWeekIso).' '.$day->format('j.n.') : $date; } public function isDayOff(string $date): bool { $day = CarbonImmutable::createFromFormat('!Y-m-d', $date); return isset($this->holidays[$date]) || ($day instanceof CarbonImmutable && $day->dayOfWeekIso >= 6); } public function hours(int $minutes): string { if ($minutes === 0) { return '0'; } return rtrim(rtrim(number_format($minutes / 60, 2, ',', ''), '0'), ','); } public function shortDate(string $date): string { $day = CarbonImmutable::createFromFormat('!Y-m-d', $date); return $day instanceof CarbonImmutable ? $day->format('j.n.Y') : $date; } public function barLabel(CalendarBar $bar): string { return __('jobs.calendar.bar_label', [ 'title' => $bar->task->title, 'job' => $bar->task->jobNumber.' '.$bar->task->jobName, 'status' => __('jobs.tasks.statuses.'.$bar->task->status->value), 'from' => $this->shortDate($bar->from), 'to' => $this->shortDate($bar->to), 'hours' => $bar->minutes > 0 ? __('jobs.calendar.bar_hours', ['hours' => $this->hours($bar->minutes)]) : '', ]).($bar->late ? ', '.__('jobs.late') : ''); } /** * The person an unscheduled task is moved from when it is dropped on a * row: its assignee, who the person on the row replaces. */ public function onlyAssignee(CalendarTask $task): ?int { return $task->assignment?->userId; } public function render(): mixed { return $this->view()->title(__('jobs.calendar.title')); } private function schedule(ScheduleTask $scheduleTask, int $taskId, int $jobId, ?int $fromUserId, ?int $toUserId, string $plannedFrom, ?string $plannedTo): bool { $task = $this->findTask($taskId); try { $result = $scheduleTask->execute($jobId, $taskId, $fromUserId, $toUserId, $plannedFrom, $plannedTo); } catch (CustomerJobNotFound|TaskNotFound|InvalidArgumentException) { $this->statusMessage = __('jobs.calendar.not_changed'); return false; } catch (PlanEndsBeforeStart) { $this->addError('plannedTo', __('jobs.calendar.errors.to')); $this->statusMessage = __('jobs.calendar.errors.to'); return false; } catch (InvalidTaskReference) { $this->statusMessage = __('jobs.calendar.inactive_person'); return false; } $this->refresh(); $this->statusMessage = __($result->extended ? 'jobs.calendar.moved_extended' : 'jobs.calendar.moved', [ 'title' => $task->title ?? '', 'person' => $toUserId === null ? __('jobs.calendar.no_assignee') : ($this->userNames[$toUserId] ?? ''), 'from' => $this->shortDate($result->plannedFrom), 'to' => $this->shortDate($result->plannedTo), ]); return true; } private function closeDialogAndFocus(int $taskId, ?int $userId): void { $key = $taskId.'-'.($userId ?? 'none'); $this->js("document.getElementById('task-schedule-dialog').close();" ."const bar = document.getElementById('bar-{$key}');" ."(bar && bar.offsetParent !== null ? bar : document.getElementById('agenda-bar-{$key}'))?.focus();"); } private function findTask(int $taskId): ?CalendarTask { foreach ($this->rows as $row) { foreach ($row->bars as $bar) { if ($bar->task->taskId === $taskId) { return $bar->task; } } } foreach ($this->unscheduled as $task) { if ($task->taskId === $taskId) { return $task; } } return null; } private function moveRange(int $days): void { $this->from = $this->startDate()->addDays($days)->format('Y-m-d'); $this->refresh(); $this->statusMessage = $this->showingMessage(); } private function showingMessage(): string { return __('jobs.calendar.showing', ['from' => $this->shortDate($this->days[0]), 'to' => $this->shortDate($this->days[self::DAYS - 1])]); } private function refresh(): void { unset($this->days, $this->holidays, $this->people, $this->rows, $this->unscheduled); } private function serviceUnitId(): ?int { return $this->serviceUnit === '' ? null : (int) $this->serviceUnit; } private function onlyAssigneeUserId(): ?int { return $this->isSubcontractor() ? (int) auth()->id() : null; } }; ?> @php $canManage = $this->canManage(); $days = $this->days; $capacity = $this->hours($this->capacityMinutes); $columns = 'grid-template-columns: 11rem repeat('.count($days).', minmax(0, 1fr));'; @endphp

{{ __('jobs.calendar.title') }}

{{ __('jobs.calendar.intro') }}

{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}} @foreach (app(CatalogQueries::class)->activeServiceUnits() as $unit) @endforeach
@if ($this->isSubcontractor())

{{ __('jobs.calendar.subcontractor_note') }}

@else
@endif

{{ $statusMessage }}

{{ __('jobs.calendar.range', ['from' => $this->shortDate($days[0]), 'to' => $this->shortDate($days[count($days) - 1])]) }}

{{-- Desktop: the Gantt grid. Dragging is a pointer shortcut; every bar is a button that opens the dialog. --}} {{-- Phones and narrow windows: the same tasks as a list per person. --}}
@foreach ($this->rows as $row) @php($rowKey = $row->userId ?? 'none')

{{ $row->name }}

@if ($row->bars === [])

{{ __('jobs.calendar.no_tasks') }}

@else
    @foreach ($row->bars as $bar)
  • @endforeach
@endif
@endforeach

{{ __('jobs.calendar.unscheduled') }}

@if ($this->unscheduled === [])

{{ __('jobs.calendar.unscheduled_none') }}

@else

{{ __('jobs.calendar.unscheduled_intro') }}

    @foreach ($this->unscheduled as $task)
  • $canManage, ]) > {{ $task->title }} {{ $task->jobNumber }} {{ $task->jobName }} {{ $task->assignment === null ? __('jobs.calendar.no_assignee') : ($this->userNames[$task->assignment->userId] ?? '') }} @if ($task->estimatedHours !== null) · {{ str_replace('.', ',', $task->estimatedHours) }} h @endif @if ($task->dueOn !== null) {{ __('jobs.tasks.fields.due_on') }}: @endif
  • @endforeach
@endif

{{ $dialogTitle }}

@if ($dialogJobId !== null && Gate::allows('jobs.manage-tasks', $dialogJobId)) @foreach ($this->userNames as $userId => $name) @endforeach @endif @foreach (TaskStatus::cases() as $option) @endforeach @if ($dialogJobId !== null)

{{ __('jobs.calendar.open_job') }} @if (Gate::allows('jobs.manage-tasks', $dialogJobId) && $dialogTaskId !== null) {{ __('jobs.calendar.edit_task') }} @endif

@endif @if ($dialogJobId !== null && Gate::allows('jobs.manage-tasks', $dialogJobId)) @endif