authorize('jobs.manage'); if ($job === null) { $this->form->responsible_user_id = (string) auth()->id(); return; } $details = $jobQueries->findDetails($job); abort_if($details === null, 404); $this->jobId = $details->id; $this->jobNumber = $details->number; $this->customerName = $details->customer->name; $this->storedStatus = $details->status->value; $this->form->fillFromDetails($details); } /** * @return list */ #[Computed] public function serviceUnitOptions(): array { $catalog = app(CatalogQueries::class); $options = []; foreach ($catalog->activeServiceUnits() as $unit) { $options[$unit->id] = ['value' => $unit->id, 'label' => $unit->name]; } // Units already on the job stay visible after being taken out of use. foreach ($this->form->service_unit_ids as $id) { if (! isset($options[(int) $id]) && ($unit = $catalog->findServiceUnit((int) $id)) !== null) { $options[$unit->id] = ['value' => $unit->id, 'label' => $unit->name, 'note' => __('catalog.inactive')]; } } return array_values($options); } /** * @return list */ #[Computed] public function users(): array { return app(UserAccountQueries::class)->activeUserOptions(); } /** * @return list */ #[Computed] public function contacts(): array { return $this->form->customer_id === '' ? [] : app(CustomerQueries::class)->contactsOf((int) $this->form->customer_id); } /** * @return list */ #[Computed] public function billingUnits(): array { return $this->form->customer_id === '' ? [] : app(CustomerQueries::class)->billingUnitsOf((int) $this->form->customer_id); } /** * Statuses this user may choose: billing statuses for management and the * job's biller, archived for management, and always the stored one. * * @return list */ #[Computed] public function statusOptions(): array { return array_values(array_filter( JobStatus::cases(), fn (JobStatus $status): bool => $status->value === $this->storedStatus || $this->maySetStatus($status), )); } /** * @return list */ #[Computed] public function tagSuggestions(): array { return array_values(array_diff(app(CustomerJobQueries::class)->tagSuggestions($this->newTag), $this->form->tags)); } public function addTag(): void { $tag = trim(preg_replace('/\s+/u', ' ', $this->newTag) ?? ''); if ($tag === '' || mb_strlen($tag) > 50) { $this->addError('newTag', __('jobs.errors.invalid_tag')); return; } $taken = array_map('mb_strtolower', $this->form->tags); if (! in_array(mb_strtolower($tag), $taken, true)) { $this->form->tags[] = $tag; } $this->newTag = ''; $this->resetErrorBag('newTag'); $this->statusMessage = __('jobs.tags.added', ['tag' => $tag]); } public function removeTag(int $index): void { $tag = $this->form->tags[$index] ?? null; if ($tag === null) { return; } unset($this->form->tags[$index]); $this->form->tags = array_values($this->form->tags); $this->statusMessage = __('jobs.tags.removed', ['tag' => $tag]); $this->js("document.getElementById('new-tag')?.focus()"); } public function addLink(): void { $this->form->links[] = ['title' => '', 'url' => '']; $index = count($this->form->links) - 1; $this->js("document.getElementById('link-title-{$index}')?.focus()"); } public function removeLink(int $index): void { unset($this->form->links[$index]); $this->form->links = array_values($this->form->links); $this->js("document.getElementById('add-link')?.focus()"); } public function save(CreateCustomerJob $createJob, UpdateCustomerJob $updateJob, CustomerJobQueries $jobQueries): void { $this->authorize('jobs.manage'); $status = JobStatus::tryFrom($this->form->status); if ($status !== null && $status->value !== $this->storedStatus && ! $this->maySetStatus($status)) { $this->addError('form.status', __('jobs.errors.status_not_allowed')); return; } $data = $this->form->toData(); try { if ($this->jobId === null) { $jobId = $createJob->execute($data, (int) auth()->id()); $number = $jobQueries->findDetails($jobId)->number ?? ''; session()->flash('status', __('jobs.created', ['number' => $number, 'name' => trim($data->name)])); } else { $updateJob->execute($this->jobId, $data, (int) auth()->id()); $jobId = $this->jobId; session()->flash('status', __('jobs.updated', ['number' => $this->jobNumber])); } } catch (CustomerJobNotFound) { abort(404); } catch (DomainException $exception) { $this->form->showDomainError($exception); return; } $this->redirectRoute('jobs.show', $jobId, navigate: true); } public function heading(): string { return $this->jobId === null ? __('jobs.create_title') : __('jobs.edit_title').' '.$this->jobNumber; } public function render(): mixed { return $this->view()->title($this->heading()); } private function maySetStatus(JobStatus $status): bool { if ($status === JobStatus::Archived) { return Gate::allows('jobs.archive'); } if ($status->isBillingStatus()) { $details = $this->jobId === null ? null : app(CustomerJobQueries::class)->findDetails($this->jobId); return Gate::allows('jobs.set-billing-status', [$details?->biller?->id]); } return true; } /** * A contact or billing unit of another customer no longer fits. */ protected function customerChosen(?int $customerId): void { $this->form->customer_contact_id = ''; $this->form->billing_unit_id = ''; } }; ?> @php $userOptions = collect($this->users)->map(fn ($user) => [ 'value' => $user->id, 'label' => $user->name, 'note' => $user->role === Role::Subcontractor ? __('users.roles.subcontractor') : null, ])->all(); $staffOptions = collect($this->users)->reject(fn ($user) => $user->role === Role::Subcontractor); @endphp

{{ $this->heading() }}

{{ $statusMessage }}

{{ __('jobs.sections.basics') }}
{{ __('jobs.sections.schedule') }}
{{ __('jobs.sections.classification') }}
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}} @foreach ($this->statusOptions as $status) @endforeach @foreach (Priority::cases() as $priority) @endforeach @foreach (JobKind::cases() as $kind) @endforeach @foreach (PaymentBasis::cases() as $basis) @endforeach
{{ __('jobs.sections.members') }}
@foreach ($staffOptions as $user) @endforeach @foreach ($staffOptions as $user) @endforeach @foreach ($staffOptions as $user) @endforeach @foreach ($this->contacts as $contact) @endforeach @foreach ($this->billingUnits as $billingUnit) @endforeach
{{ __('jobs.sections.extra') }} {{-- Tags: free text with suggestions from earlier tags, added one at a time. --}}
@foreach ($this->tagSuggestions as $suggestion) @endforeach

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

@if ($form->tags === [])

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

@else
    @foreach ($form->tags as $index => $tag)
  • {{ $tag }}
  • @endforeach
@endif @error('form.tags')

{{ $message }}

@enderror
{{-- Links: title and address per row. --}}

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

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

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

@endif @foreach ($form->links as $index => $link)
{{ __('jobs.links.link_number', ['number' => $index + 1]) }}
@endforeach @error('form.links')

{{ $message }}

@enderror