authorize('jobs.view-job', [$jobId]); $details = app(CustomerJobQueries::class)->findDetails($jobId); if ($details === null) { return; } $this->resetErrorBag(); $this->reset('newTag', 'newTaskTitle', 'newTaskPin', 'templateId', 'statusMessage', 'customerSearch'); $this->jobId = $details->id; $this->loadDetails($details); $this->newTaskAssignee = (string) $details->responsible->id; unset($this->job, $this->tasks, $this->changes, $this->contacts, $this->billingUnits, $this->statusOptions); $this->js("document.getElementById('".self::DIALOG."').showModal(); document.getElementById('job-editor-name')?.focus()"); } #[Computed] public function job(): ?CustomerJobDetails { return $this->jobId === null ? null : app(CustomerJobQueries::class)->findDetails($this->jobId); } /** * @return list */ #[Computed] public function tasks(): array { return $this->jobId === null ? [] : app(TaskQueries::class)->tasksOf($this->jobId); } /** * @return list */ #[Computed] public function templates(): array { $job = $this->job; return $job === null ? [] : app(WorkflowTemplateQueries::class)->activeFor(array_map(fn ($unit): int => $unit->id, $job->serviceUnits)); } /** * Pinned state of the job's tasks: TaskSummary does not carry it. * * @return array */ #[Computed] public function pinnedTasks(): array { return $this->jobId === null ? [] : app(TaskQueries::class)->pinnedTaskIds($this->jobId); } public function canEdit(): bool { return Gate::allows('jobs.manage'); } public function loggedHours(): string { $quarters = $this->jobId === null ? 0 : (app(LoggedJobHours::class)->quartersByJobs([$this->jobId])[$this->jobId] ?? 0); return str_replace('.', ',', rtrim(rtrim(number_format($quarters / 4, 2, '.', ''), '0'), '.')); } public function changeStatus(string $value, ChangeJobStatus $changeStatus): void { $this->authorize('jobs.manage'); $status = JobStatus::tryFrom($value); $job = $this->job; if ($status === null || $job === null) { return; } if ($status->value !== $this->storedStatus && ! $this->maySetStatus($status)) { $this->addError('form.status', __('jobs.errors.status_not_allowed')); $this->form->status = (string) $this->storedStatus; return; } try { $changeStatus->execute($job->id, $status, (int) auth()->id()); } catch (CustomerJobNotFound) { return; } $this->resetErrorBag('form.status'); $this->storedStatus = $status->value; $this->form->status = $status->value; unset($this->job, $this->changes, $this->statusOptions); $this->statusMessage = __('jobs.editor.status_changed', ['number' => $job->number, 'status' => __('jobs.statuses.'.$status->value)]); $this->dispatch('job-saved'); } public function unlock(ChangeJobStatus $changeStatus): void { $this->authorize('jobs.unlock'); $this->changeStatus(JobStatus::InProduction->value, $changeStatus); } public function changePriority(string $value, ChangeJobPriority $changePriority): void { $this->authorize('jobs.manage'); $priority = Priority::tryFrom($value); $job = $this->job; if ($priority === null || $job === null) { return; } $changePriority->execute($job->id, $priority, (int) auth()->id()); $this->form->priority = $priority->value; unset($this->job, $this->changes); $this->statusMessage = __('jobs.editor.priority_changed', ['number' => $job->number, 'priority' => __('jobs.priorities.'.$priority->value)]); $this->dispatch('job-saved'); } public function setTaskDone(int $taskId, bool $done, ChangeTaskStatus $changeStatus): void { $this->authorize('jobs.update-task-status', [$this->jobId]); $task = $this->findTask($taskId); if ($task === null) { return; } try { $changeStatus->execute((int) $this->jobId, $taskId, $done ? TaskStatus::Done : TaskStatus::InProgress); } catch (TaskNotFound) { return; } unset($this->tasks); $this->statusMessage = __($done ? 'jobs.board.task_done' : 'jobs.board.task_reopened', ['task' => $task->title]); $this->dispatch('job-saved'); } /** * One field of a task row changed: title, status, assignee or due date. */ public function updateTaskField(int $taskId, string $field, string $value, UpdateTask $updateTask): void { $this->authorize('jobs.manage-tasks', [$this->jobId]); $task = $this->findTask($taskId); if ($task === null || ! in_array($field, ['title', 'status', 'assignee', 'due_on'], true)) { return; } $data = new TaskData( title: $field === 'title' ? $value : $task->title, workTypeId: $task->workType->id, status: $field === 'status' ? $value : $task->status->value, description: $task->description, dueOn: $field === 'due_on' ? ($value === '' ? null : $value) : $task->dueOn, estimatedHours: $task->estimatedHours, assigneeUserId: $field === 'assignee' ? ($value === '' ? null : (int) $value) : $task->assignee?->id, memberUserIds: array_map(fn ($member): int => $member->id, $task->members), plannedFrom: $task->plannedFrom, plannedTo: $task->plannedTo, ); try { $updateTask->execute((int) $this->jobId, $taskId, $data, (int) auth()->id()); } catch (DomainException) { $this->addError('tasks.'.$taskId, __('jobs.board.not_moved')); unset($this->tasks); return; } $this->resetErrorBag('tasks.'.$taskId); unset($this->tasks); $this->statusMessage = __('jobs.editor.task_saved', ['task' => $data->title]); $this->dispatch('job-saved'); } public function pinTask(int $taskId, bool $pinned, PinTaskToBoard $pin): void { $this->authorize('jobs.manage-tasks', [$this->jobId]); $task = $this->findTask($taskId); try { $pin->execute((int) $this->jobId, $taskId, $pinned); } catch (DomainException) { return; } unset($this->pinnedTasks); $this->statusMessage = __($pinned ? 'jobs.board.task_pinned' : 'jobs.board.task_unpinned', ['task' => $task->title ?? '']); $this->dispatch('job-saved'); } public function addTask(AddTask $addTask, PinTaskToBoard $pin): void { $this->authorize('jobs.manage-tasks', [$this->jobId]); $title = trim($this->newTaskTitle); $workTypeId = $this->firstWorkTypeId(); if ($title === '') { $this->addError('newTaskTitle', __('jobs.board.task_title_required')); return; } if ($workTypeId === null) { $this->addError('newTaskTitle', __('jobs.board.no_work_type')); return; } try { $taskId = $addTask->execute((int) $this->jobId, new TaskData( $title, $workTypeId, TaskStatus::Waiting->value, assigneeUserId: $this->newTaskAssignee === '' ? null : (int) $this->newTaskAssignee, ), (int) auth()->id()); if ($this->newTaskPin && $this->job?->status === JobStatus::Ongoing) { $pin->execute((int) $this->jobId, $taskId, true); } } catch (DomainException) { $this->addError('newTaskTitle', __('jobs.board.not_moved')); return; } $this->reset('newTaskTitle', 'newTaskPin'); unset($this->tasks, $this->pinnedTasks); $this->statusMessage = __('jobs.editor.task_added', ['task' => $title]); $this->dispatch('job-saved'); $this->js("document.getElementById('job-editor-new-task')?.focus()"); } public function applyTemplate(AddTasksFromWorkflowTemplate $addFromTemplate): void { $this->authorize('jobs.manage-tasks', [$this->jobId]); if ($this->templateId === '') { return; } try { $result = $addFromTemplate->execute((int) $this->jobId, (int) $this->templateId, (int) auth()->id(), [], true); } catch (DomainException) { $this->addError('templateId', __('jobs.board.not_moved')); return; } $this->templateId = ''; unset($this->tasks); $count = $result->added; $this->statusMessage = trans_choice('jobs.editor.template_added', $count, ['count' => $count]); $this->dispatch('job-saved'); } public function save(UpdateCustomerJob $updateJob): void { $this->authorize('jobs.manage'); $job = $this->job; if ($job === null) { return; } // Status and priority are saved by their own controls in the header. $this->form->status = $job->status->value; $this->form->priority = $job->priority->value; try { $updateJob->execute($job->id, $this->form->toData(), (int) auth()->id()); } catch (CustomerJobNotFound) { return; } catch (DomainException $exception) { $this->form->showDomainError($exception); return; } $this->dispatch('job-saved'); $this->js("document.getElementById('".self::DIALOG."').close()"); $this->statusMessage = __('jobs.editor.saved', ['number' => $job->number]); $this->dispatch('job-editor-saved', message: $this->statusMessage); } public function render(): mixed { return $this->view(); } private function loadDetails(CustomerJobDetails $details): void { $this->storedStatus = $details->status->value; $this->customerName = $details->customer->name; $this->form->reset(); $this->form->fillFromDetails($details); } private function findTask(int $taskId): ?TaskSummary { foreach ($this->tasks as $task) { if ($task->id === $taskId) { return $task; } } return null; } private function firstWorkTypeId(): ?int { $job = $this->job; if ($job === null) { return null; } $unitIds = array_map(fn ($unit): int => $unit->id, $job->serviceUnits); $shared = null; foreach (app(\App\Modules\Catalog\Application\Contracts\CatalogQueries::class)->activeWorkTypesFor($unitIds) 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, $unitIds, true)) { return $first; } $shared ??= $first; } return $shared; } }; ?> @php $job = $this->job; $canEdit = $this->canEdit(); $staff = collect($this->users)->reject(fn ($user) => $user->role === Role::Subcontractor); $headerColor = new HexColor($job?->customer->id !== null ? (app(\App\Modules\Customers\Application\Contracts\CustomerQueries::class)->headerColorsByIds([$job->customer->id])[$job->customer->id] ?? '#23476a') : '#23476a'); $fieldClass = 'w-full rounded-lg border border-neutral-400 bg-white px-2.5 py-1.5 text-xs text-neutral-900 focus-visible:outline-2 focus-visible:outline-primary-600 dark:border-[#6b7079] dark:bg-[#24262a] dark:text-white dark:focus-visible:outline-primary-300'; $labelClass = 'mb-1 block text-[11px] font-semibold text-neutral-800 dark:text-neutral-100'; $boxClass = 'space-y-2.5 rounded-xl border border-neutral-200 bg-neutral-100 p-3.5 dark:border-[#36393f] dark:bg-[#18191b]'; $boxHeading = 'text-[10px] font-extrabold tracking-wider text-neutral-700 uppercase dark:text-neutral-200'; @endphp
{{ $job?->number }}

{{ __('jobs.editor.heading', ['number' => $job?->number]) }}: {{ $job?->customer->name }}

@if ($job !== null && Gate::allows('customers.use')) @endif
@if ($job !== null)
@endif
@if ($job !== null) @error('form.status') @enderror @if (in_array($job->status, [JobStatus::Done, JobStatus::Invoiced, JobStatus::Billed], true))
@can('jobs.unlock') @endcan
@endif

{{ $statusMessage }}

@unless ($canEdit)

{{ __('jobs.editor.read_only') }}

@endunless
{{-- Left: content, tasks and records --}}
@error('form.name')

{{ $message }}

@enderror
@if ($canEdit) @endif
@if ($form->links === [])

{{ __('jobs.links.none') }}

@endif @error('form.links')

{{ $message }}

@enderror
{{-- Tasks --}}

{{ $job->status === JobStatus::Ongoing ? __('jobs.editor.tasks_ongoing') : __('jobs.editor.tasks') }}

{{ $job->status === JobStatus::Ongoing ? __('jobs.editor.tasks_hint_ongoing') : __('jobs.editor.tasks_hint') }}

@if (Gate::allows('jobs.manage-tasks', [$job->id]) && $this->templates !== [] && $job->status->allowsTasks())
@endif
@if ($this->tasks === [])

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

@endif
    @foreach ($this->tasks as $task) @php($canManageTask = Gate::allows('jobs.manage-tasks', [$job->id]))
  • status === TaskStatus::Done) @disabled(! Gate::allows('jobs.update-task-status', [$job->id])) wire:change="setTaskDone({{ $task->id }}, $event.target.checked)" aria-label="{{ __('jobs.editor.task_done', ['task' => $task->title]) }}" class="size-4 rounded border-neutral-500 text-emerald-700 dark:border-neutral-400 dark:bg-[#16171a]" /> @if ($canManageTask) @else {{ $task->title }} @endif
    @if ($canManageTask)
    @if ($job->status === JobStatus::Ongoing) @php($pinned = $this->pinnedTasks[$task->id] ?? false) @endif
    @endif
    @error('tasks.'.$task->id)

    {{ $message }}

    @enderror
  • @endforeach
@if (Gate::allows('jobs.manage-tasks', [$job->id]) && $job->status->allowsTasks())
@error('newTaskTitle')

{{ $message }}

@enderror @if ($job->status === JobStatus::Ongoing) @endif
@endif
{{-- Records: hours, expenses, history --}}
@if ($this->changes === [])

{{ __('jobs.history.empty') }}

@else
    @foreach ($this->changes as $change)
  1. {{ $change->userName }}: @if ($change->field === 'created') {{ __('jobs.history.created', ['user' => $change->userName, 'number' => $change->newValue]) }} @else {{ __('jobs.history.changed', [ 'field' => __('jobs.fields.'.$change->field), 'old' => $this->historyValue($change->field, $change->oldValue), 'new' => $this->historyValue($change->field, $change->newValue), ]) }} @endif {{ CarbonImmutable::parse($change->changedAt)->format('j.n.Y H.i') }}
  2. @endforeach
@endif
{{-- Right: metadata, schedule, team and money --}}
{{ __('jobs.sections.basics') }}

{{ __('jobs.editor.customer_section') }}

{{ __('jobs.editor.team_section') }}

@foreach (['responsible_user_id' => 'responsible_user', 'contact_user_id' => 'contact_user', 'biller_user_id' => 'biller_user'] as $field => $label)
@error('form.'.$field)

{{ $message }}

@enderror
@endforeach

{{ __('jobs.editor.units_section') }}

{{ __('jobs.fields.tags') }}

@if ($form->tags !== [])
    @foreach ($form->tags as $index => $tag)
  • {{ $tag }} @if ($canEdit) @endif
  • @endforeach
@endif @if ($canEdit)
@foreach ($this->tagSuggestions as $suggestion) @endforeach
@error('newTag')

{{ $message }}

@enderror @endif

{{ __('jobs.editor.schedule_section') }}

@foreach (['starts_on', 'due_on', 'planned_from', 'planned_to'] as $field)
@error('form.'.$field)

{{ $message }}

@enderror
@endforeach
@error('form.estimated_hours')

{{ $message }}

@enderror

{{ __('jobs.editor.money_section') }}

@can('money.quotes')
@error('form.quoted_amount')

{{ $message }}

@enderror
@if ($form->payment_basis === 'monthly_fee')
@error('form.monthly_fee')

{{ $message }}

@enderror
@endif
@else

{{ __('jobs.editor.money_hidden') }}

@endcan
@endif {{ __('jobs.editor.footer') }}
@if ($job !== null) {{ __('jobs.editor.open_page') }} @endif @if ($job !== null && $canEdit) @endif