", "unit-" or "all". */ #[Url(as: 'taulu', except: 'mine')] public string $board = 'mine'; #[Url(as: 'nakyma', except: 'production')] public string $view = 'production'; #[Url(as: 'kiire', except: false)] public bool $onlyUrgent = false; #[Url(as: 'myohassa', except: false)] public bool $onlyLate = false; public string $statusMessage = ''; /** @var array new task titles typed on continuing job cards, by job id */ public array $newTaskTitles = []; /** @var array "lift into the pipeline at once" on those cards, by job id */ public array $newTaskPins = []; public function mount(): void { $this->authorize('jobs.view'); if (BoardView::tryFrom($this->view) === null) { $this->view = BoardView::Production->value; } } public function isSubcontractor(): bool { return auth()->user()?->role === Role::Subcontractor; } #[Computed] public function preferences(): BoardPreferences { return app(JobBoardQueries::class)->preferencesOf((int) auth()->id()); } public function boardView(): BoardView { return BoardView::tryFrom($this->view) ?? BoardView::Production; } /** * @return list */ #[Computed] public function cards(): array { $userId = (int) auth()->id(); $board = $this->isSubcontractor() ? 'all' : $this->board; $personId = null; $unitId = null; if ($board === 'mine') { $personId = $userId; } elseif (preg_match('/^person-(\d+)$/', $board, $matches) === 1) { $personId = (int) $matches[1]; } elseif (preg_match('/^unit-(\d+)$/', $board, $matches) === 1) { $unitId = (int) $matches[1]; } return app(JobBoardQueries::class)->cards(new BoardFilter( personUserId: $personId, serviceUnitId: $unitId, onlyMemberUserId: $this->isSubcontractor() ? $userId : null, onlyUrgent: $this->onlyUrgent, onlyLate: $this->onlyLate, ), app(Clock::class)->today()->format('Y-m-d')); } /** * @return array> */ #[Computed] public function columns(): array { return app(BoardLayout::class)->arrange($this->cards, $this->boardView(), $this->preferences); } public function shownCardCount(): int { return array_sum(array_map('count', $this->columns)); } public function quoteCount(): int { return count(array_filter($this->cards, fn (BoardCard $card): bool => ! $card->isTaskCard() && in_array($card->jobStatus, [JobStatus::Prospect, JobStatus::Tentative, JobStatus::QuoteRequest], true))); } /** * @return array board value => label */ #[Computed] public function boardOptions(): array { $options = ['mine' => __('jobs.board.board_mine')]; foreach (app(UserAccountQueries::class)->activeUserOptions() as $user) { if ($user->id !== (int) auth()->id() && $user->role !== Role::Subcontractor) { $options['person-'.$user->id] = __('jobs.board.board_person', ['name' => $user->name]); } } foreach (app(CatalogQueries::class)->activeServiceUnits() as $unit) { $options['unit-'.$unit->id] = __('jobs.board.board_unit', ['name' => $unit->name]); } $options['all'] = __('jobs.board.board_all'); return $options; } /** * Colours of a column: the header and its text, and the body and border * tinted with the header colour in light and dark mode. * * @return array{header: string, text: string, light: bool, body: string, bodyDark: string, border: string, borderDark: string} */ public function columnStyle(BoardColumn $column): array { $color = $this->preferences->colorOf($column); return [ 'header' => $color->value, 'text' => $color->textColor(), 'light' => $color->isLight(), 'body' => $color->mixedInto(new HexColor('#f6f7f9'), 0.08)->value, 'bodyDark' => $color->mixedInto(new HexColor('#16160e'), 0.11)->value, 'border' => $color->mixedInto(new HexColor('#d1d5db'), 0.12)->value, 'borderDark' => $color->mixedInto(new HexColor('#2a2b23'), 0.15)->value, ]; } public function openJob(int $jobId): void { $this->authorize('jobs.view-job', [$jobId]); $this->dispatch('open-job-editor', jobId: $jobId); } /** * A card was dropped (wire:sort): into another column, or to another * place in its own column. */ public function moveDropped(string $key, int $position, string $columnValue, MoveBoardCard $move): void { $this->authorize('jobs.manage'); $column = BoardColumn::tryFrom($columnValue); $card = $this->findCard($key); if ($column === null || $card === null || ! in_array($column, $this->boardView()->columns(), true)) { $this->statusMessage = __('jobs.board.not_moved'); $this->refreshBoard(); return; } $label = $card->number.' '.$card->title; $columnName = __('jobs.board.column_names.'.$column->value); if (! $card->isTaskCard()) { $status = MoveBoardCard::statusAfterDrop($card->jobStatus, $column); if ($status->isBillingStatus() && $status !== $card->jobStatus && ! Gate::allows('jobs.set-billing-status', [$card->billerUserId])) { $this->statusMessage = __('jobs.board.billing_not_allowed', ['job' => $label, 'status' => __('jobs.statuses.'.$status->value)]); $this->refreshBoard(); return; } if ($card->jobStatus->unlocksTo($status) && ! Gate::allows('jobs.unlock')) { $this->statusMessage = __('jobs.board.unlock_not_allowed', ['job' => $label, 'status' => __('jobs.statuses.'.$status->value)]); $this->refreshBoard(); return; } } elseif (! Gate::allows('jobs.update-task-status', [$card->jobId])) { abort(403); } $keys = array_values(array_filter( array_map(fn (BoardCard $other): string => $other->key(), $this->columns[$column->value] ?? []), fn (string $other): bool => $other !== $key, )); array_splice($keys, max(0, min($position, count($keys))), 0, [$key]); $fromColumn = app(BoardLayout::class)->columnOf($card, $this->boardView(), $this->preferences); $sortWasByDate = $this->preferences->sortOf($column) === ColumnSort::DueDate; $threshold = $this->preferences->urgentThreshold; try { $move->execute($card->jobId, $card->taskId, $column, $keys, $threshold, (int) auth()->id()); } catch (CardCannotMoveThere) { $this->statusMessage = __('jobs.board.cannot_move', ['column' => $columnName]); $this->refreshBoard(); return; } catch (CustomerJobNotFound|TaskNotFound) { $this->statusMessage = __('jobs.board.not_moved'); $this->refreshBoard(); return; } if ($sortWasByDate) { $this->savePreferences($this->preferences->withColumnSort($column, ColumnSort::Manual)); } $this->refreshBoard(); $this->statusMessage = match (true) { $sortWasByDate => __('jobs.board.switched_to_manual', ['column' => $columnName]), $card->isTaskCard() => __('jobs.board.task_moved', ['task' => $card->title, 'column' => $columnName]), $fromColumn === $column => __('jobs.board.reordered', ['column' => $columnName]), $column === BoardColumn::InProductionUrgent && ! $threshold->includes($card->priority) => __('jobs.board.priority_raised', [ 'job' => $label, 'priority' => __('jobs.priorities.'.$threshold->lowestPriority()->value), ]), $column === BoardColumn::InProduction && $fromColumn === BoardColumn::InProductionUrgent => __('jobs.board.priority_lowered', ['job' => $label]), default => __('jobs.board.moved', ['job' => $label, 'status' => __('jobs.statuses.'.MoveBoardCard::statusAfterDrop($card->jobStatus, $column)->value)]), }; } /** * A task's checkbox on a card: done, or back to in progress. */ public function setTaskDone(int $jobId, int $taskId, bool $done, ChangeTaskStatus $changeStatus): void { $this->authorize('jobs.update-task-status', [$jobId]); $task = $this->findTask($jobId, $taskId); if ($task === null) { $this->statusMessage = __('jobs.board.not_moved'); return; } try { $changeStatus->execute($jobId, $taskId, $done ? TaskStatus::Done : TaskStatus::InProgress); } catch (TaskNotFound) { $this->statusMessage = __('jobs.board.not_moved'); return; } $this->refreshBoard(); $this->statusMessage = __($done ? 'jobs.board.task_done' : 'jobs.board.task_reopened', ['task' => $task->title]); } public function pinTask(int $jobId, int $taskId, bool $pinned, PinTaskToBoard $pin): void { $this->authorize('jobs.manage-tasks', [$jobId]); $task = $this->findTask($jobId, $taskId); try { $pin->execute($jobId, $taskId, $pinned); } catch (CustomerJobNotFound|TaskNotFound|TaskCannotBePinned) { $this->statusMessage = __('jobs.board.not_moved'); return; } $this->refreshBoard(); $this->statusMessage = __($pinned ? 'jobs.board.task_pinned' : 'jobs.board.task_unpinned', ['task' => $task->title ?? '']); } /** * Quick add on a continuing job's card: the title only, for the job's * responsible person, with the first work type of the job's service units. */ public function addTask(int $jobId, AddTask $addTask, PinTaskToBoard $pin): void { $this->authorize('jobs.manage-tasks', [$jobId]); $field = 'newTaskTitles.'.$jobId; $title = trim((string) ($this->newTaskTitles[$jobId] ?? '')); if ($title === '') { $this->addError($field, __('jobs.board.task_title_required')); return; } $details = app(CustomerJobQueries::class)->findDetails($jobId); if ($details === null) { $this->statusMessage = __('jobs.board.not_moved'); return; } $workTypeId = $this->firstWorkTypeId(array_map(fn ($unit): int => $unit->id, $details->serviceUnits)); if ($workTypeId === null) { $this->addError($field, __('jobs.board.no_work_type')); return; } try { $taskId = $addTask->execute($jobId, new TaskData($title, $workTypeId, TaskStatus::Waiting->value, assigneeUserId: $details->responsible->id), (int) auth()->id()); if ($this->newTaskPins[$jobId] ?? false) { $pin->execute($jobId, $taskId, true); } } catch (DomainException) { $this->addError($field, __('jobs.board.not_moved')); return; } unset($this->newTaskTitles[$jobId], $this->newTaskPins[$jobId]); $this->refreshBoard(); $this->statusMessage = __('jobs.board.task_added', ['task' => $title, 'job' => $details->number]); } public function setCustomerColor(int $customerId, ?string $color, ChangeCustomerHeaderColor $changeColor): void { $this->authorize('customers.use'); $card = collect($this->cards)->first(fn (BoardCard $card): bool => $card->customerId === $customerId); try { $changeColor->execute($customerId, $color); } catch (InvalidHexColor|CustomerNotFound) { $this->statusMessage = __('jobs.board.invalid_color'); return; } $this->refreshBoard(); $this->dispatch('customer-color-changed', customerId: $customerId); $this->statusMessage = __('jobs.board.customer_color_saved', ['customer' => $card->customerName ?? '']); } /** * A card's task list opened or closed: remembered for this user only. * Renderless, so opening a card does not redraw the board. */ #[Renderless] public function setCardExpanded(string $key, bool $expanded): void { if ($this->findCard($key) === null) { return; } $this->savePreferences($this->preferences->withCardExpanded($key, $expanded)); } public function setColumnColor(string $columnValue, ?string $color): void { $column = BoardColumn::tryFrom($columnValue); try { $hex = $color === null ? null : new HexColor($color); } catch (InvalidHexColor) { $hex = false; } if ($column === null || $hex === false) { $this->statusMessage = __('jobs.board.invalid_color'); return; } $this->savePreferences($this->preferences->withColumnColor($column, $hex)); $this->statusMessage = __($hex === null ? 'jobs.board.color_reset' : 'jobs.board.color_saved', ['column' => __('jobs.board.column_names.'.$column->value)]); } public function setColumnSort(string $columnValue, string $sortValue): void { $column = BoardColumn::tryFrom($columnValue); $sort = ColumnSort::tryFrom($sortValue); if ($column === null || $sort === null) { return; } $this->savePreferences($this->preferences->withColumnSort($column, $sort)); $this->statusMessage = __('jobs.board.sort_saved', ['column' => __('jobs.board.column_names.'.$column->value), 'sort' => __('jobs.board.sorts.'.$sort->value)]); } public function setUrgentThreshold(string $value): void { $threshold = UrgentThreshold::tryFrom($value); if ($threshold === null) { return; } $this->savePreferences($this->preferences->withUrgentThreshold($threshold)); $this->statusMessage = __('jobs.board.threshold_saved', ['threshold' => __('jobs.board.thresholds.'.$threshold->value)]); } public function setDensity(string $value): void { $density = BoardDensity::tryFrom($value); if ($density === null) { return; } $this->savePreferences($this->preferences->withDensity($density)); $this->statusMessage = __('jobs.board.density_saved', ['density' => __('jobs.board.density_'.$density->value)]); } public function setView(string $value): void { $this->view = (BoardView::tryFrom($value) ?? BoardView::Production)->value; $this->refreshBoard(); } public function updated(string $property): void { if (in_array($property, ['board', 'onlyUrgent', 'onlyLate'], true)) { $this->refreshBoard(); } } #[On('job-saved')] public function refreshBoard(): void { unset($this->cards, $this->columns); } #[On('job-editor-saved')] public function editorSaved(string $message): void { $this->refreshBoard(); $this->statusMessage = $message; } /** * A job created in the quick intake dialog: tentative jobs and quote * requests show up in the sales view. */ #[On('quick-job-created')] public function quickJobCreated(string $status): void { if (in_array($status, [JobStatus::Tentative->value, JobStatus::QuoteRequest->value], true) && $this->boardView() === BoardView::Production) { $this->view = BoardView::Sales->value; } $this->refreshBoard(); } public function render(): mixed { return $this->view()->title(__('jobs.board.title')); } private function savePreferences(BoardPreferences $preferences): void { app(SaveBoardPreferences::class)->execute((int) auth()->id(), $preferences); unset($this->preferences, $this->columns); } /** * The first active work type of the job's own service units (their * services included), else of the shared unit. * * @param list $serviceUnitIds */ private function firstWorkTypeId(array $serviceUnitIds): ?int { $shared = null; foreach (app(CatalogQueries::class)->activeWorkTypesFor($serviceUnitIds) as $node) { $workTypes = [...$node->workTypes, ...array_merge([], ...array_map(fn ($service): array => $service->workTypes, $node->services))]; $first = $workTypes[0]->id ?? null; if ($first === null) { continue; } if (in_array($node->serviceUnit->id, $serviceUnitIds, true)) { return $first; } $shared ??= $first; } return $shared; } private function findCard(string $key): ?BoardCard { foreach ($this->cards as $card) { if ($card->key() === $key) { return $card; } } return null; } private function findTask(int $jobId, int $taskId): ?BoardTask { foreach ($this->cards as $card) { if ($card->jobId !== $jobId || $card->isTaskCard()) { continue; } foreach ($card->tasks as $task) { if ($task->id === $taskId) { return $task; } } } return null; } }; ?> @php $canMove = Gate::allows('jobs.manage'); $canColor = Gate::allows('customers.use'); $compact = $this->preferences->density === BoardDensity::Compact; $boardView = $this->boardView(); @endphp

{{ __('jobs.board.heading') }}

{{-- Toolbar: whose board, pipeline view, quick filters, density, new job --}}
@unless ($this->isSubcontractor())
@endunless
@foreach (BoardView::cases() as $option) @endforeach
{{ trans_choice('jobs.board.showing', $this->shownCardCount(), ['count' => $this->shownCardCount()]) }}
{{ __('jobs.board.show_list') }}
@foreach (BoardDensity::cases() as $option) @endforeach
@can('jobs.manage') @endcan

{{ $statusMessage }}

{{-- Columns: side by side from lg up (horizontal scroll), stacked on phones --}}
@foreach ($this->columns as $columnValue => $cards) @php $column = BoardColumn::from($columnValue); $style = $this->columnStyle($column); $sort = $this->preferences->sortOf($column); $width = $compact ? ($column->isOngoing() ? 'lg:w-[250px]' : 'lg:w-[234px]') : ($column->isOngoing() ? 'lg:w-[268px]' : 'lg:w-[252px]'); $columnName = __('jobs.board.column_names.'.$columnValue); @endphp
$style['light'], 'border-white/20' => ! $style['light']]) >
@if ($column->isUrgentLane()) @endif $style['light'], 'border-white/30 bg-white/20' => ! $style['light']])> {{ trans_choice('jobs.board.card_count', count($cards), ['count' => count($cards)]) }}
    $compact, 'gap-2 p-2' => ! $compact]) > @foreach ($cards as $card)
  • @if ($card->isTaskCard()) @elseif ($card->isOngoing()) @else @endif
  • @endforeach @if ($cards === [])
  • {{ __('jobs.board.empty') }}
  • @endif
@endforeach
@can('jobs.manage') @endcan