$users * @property-read list $contacts * @property-read list $billingUnits * @property-read list $statusOptions */ trait EditsJobDetails { /** * @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()"); } protected function maySetStatus(JobStatus $status): bool { $stored = $this->storedStatus === null ? null : JobStatus::tryFrom($this->storedStatus); if ($stored !== null && $stored->unlocksTo($status) && ! Gate::allows('jobs.unlock')) { return false; } 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 = ''; } }