value; public string $priority = Priority::Normal->value; public string $customer_contact_id = ''; /** The intake has a new contact person of the customer to add. */ public bool $adding_contact = false; public string $new_contact_name = ''; public string $new_contact_phone = ''; /** * The statuses a job can start in at intake. * * @return list */ public static function startStatuses(): array { return [JobStatus::Prospect, JobStatus::Tentative, JobStatus::QuoteRequest, JobStatus::Order, JobStatus::InProduction]; } public function toData(): CustomerJobData { $this->validate(); return new CustomerJobData( name: $this->name, customerId: (int) $this->customer_id, serviceUnitIds: [(int) $this->service_unit_id], responsibleUserId: (int) $this->responsible_user_id, kind: JobKind::OneOff->value, status: $this->status, priority: $this->priority, description: trim($this->description) === '' ? null : $this->description, customerContactId: $this->customer_contact_id === '' ? null : (int) $this->customer_contact_id, ); } /** * Empties the form for the next job; responsible person and status stay. */ public function clearForNextJob(): void { $this->reset('customer_id', 'name', 'description', 'service_unit_id', 'priority', 'customer_contact_id', 'adding_contact', 'new_contact_name', 'new_contact_phone'); $this->resetErrorBag(); } public function showDomainError(DomainException $exception): void { [$field, $key] = match (true) { $exception instanceof InvalidJobName => ['name', 'jobs.errors.invalid_name'], $exception instanceof ServiceUnitRequired => ['service_unit_id', 'jobs.errors.service_units_required'], $exception instanceof InvalidJobReference => [match ($exception->field) { 'customer' => 'customer_id', 'service_units' => 'service_unit_id', default => 'responsible_user_id', }, 'jobs.errors.invalid_reference'], default => throw $exception, }; $this->addError($field, __($key)); } /** * @return array> */ protected function rules(): array { return [ 'customer_id' => ['required', 'integer'], 'name' => ['required', 'string', 'max:255'], 'description' => ['nullable', 'string', 'max:2000'], 'service_unit_id' => ['required', 'integer'], 'responsible_user_id' => ['required', 'integer'], 'status' => ['required', Rule::in(array_map(fn (JobStatus $status): string => $status->value, self::startStatuses()))], 'priority' => ['required', Rule::enum(Priority::class)], 'customer_contact_id' => ['nullable', 'integer'], 'new_contact_name' => [$this->adding_contact ? 'required' : 'nullable', 'string', 'max:255'], 'new_contact_phone' => ['nullable', 'string', 'max:50'], ]; } /** * @return array */ protected function messages(): array { return [ 'customer_id.required' => __('jobs.errors.customer_required'), 'name.required' => __('jobs.errors.name_required'), 'name.max' => __('jobs.errors.invalid_name'), 'description.max' => __('jobs.errors.too_long', ['max' => 2000]), 'service_unit_id.required' => __('jobs.quick.service_unit_required'), 'responsible_user_id.required' => __('jobs.errors.responsible_required'), 'status.in' => __('jobs.errors.status_not_allowed'), 'priority.*' => __('jobs.errors.status_not_allowed'), 'new_contact_name.required' => __('jobs.quick.contact_name_required'), 'new_contact_name.max' => __('jobs.errors.too_long', ['max' => 255]), 'new_contact_phone.max' => __('jobs.errors.too_long', ['max' => 50]), ]; } }