*/ public array $service_unit_ids = []; public string $starts_on = ''; public string $due_on = ''; public string $planned_from = ''; public string $planned_to = ''; public string $kind = JobKind::OneOff->value; public string $status = JobStatus::Tentative->value; public string $priority = Priority::Normal->value; public string $payment_basis = ''; public string $estimated_hours = ''; public string $customer_reference = ''; public string $instructions = ''; public string $responsible_user_id = ''; public string $contact_user_id = ''; public string $customer_contact_id = ''; public string $biller_user_id = ''; public string $billing_unit_id = ''; /** @var list */ public array $member_user_ids = []; /** @var list */ public array $tags = []; /** @var list */ public array $links = []; public function fillFromDetails(CustomerJobDetails $job): void { $this->name = $job->name; $this->description = $job->description ?? ''; $this->customer_id = (string) $job->customer->id; $this->service_unit_ids = array_map(fn (NamedId $unit): string => (string) $unit->id, $job->serviceUnits); $this->starts_on = $job->startsOn ?? ''; $this->due_on = $job->dueOn ?? ''; $this->planned_from = $job->plannedFrom ?? ''; $this->planned_to = $job->plannedTo ?? ''; $this->kind = $job->kind->value; $this->status = $job->status->value; $this->priority = $job->priority->value; $this->payment_basis = $job->paymentBasis->value ?? ''; $this->estimated_hours = $job->estimatedHours ?? ''; $this->customer_reference = $job->customerReference ?? ''; $this->instructions = $job->instructions ?? ''; $this->responsible_user_id = (string) $job->responsible->id; $this->contact_user_id = (string) ($job->contactUser->id ?? ''); $this->customer_contact_id = (string) ($job->customerContact->id ?? ''); $this->biller_user_id = (string) ($job->biller->id ?? ''); $this->billing_unit_id = (string) ($job->billingUnit->id ?? ''); $this->member_user_ids = array_map(fn (NamedId $member): string => (string) $member->id, $job->members); $this->tags = $job->tags; $this->links = $job->links; } public function toData(): CustomerJobData { $this->validate(); $id = fn (string $value): ?int => $value === '' ? null : (int) $value; $text = fn (string $value): ?string => trim($value) === '' ? null : $value; return new CustomerJobData( name: $this->name, customerId: (int) $this->customer_id, serviceUnitIds: array_map('intval', $this->service_unit_ids), responsibleUserId: (int) $this->responsible_user_id, kind: $this->kind, status: $this->status, priority: $this->priority, description: $text($this->description), startsOn: $text($this->starts_on), dueOn: $text($this->due_on), plannedFrom: $text($this->planned_from), plannedTo: $text($this->planned_to), paymentBasis: $text($this->payment_basis), estimatedHours: $text($this->estimated_hours), customerReference: $text($this->customer_reference), instructions: $text($this->instructions), contactUserId: $id($this->contact_user_id), customerContactId: $id($this->customer_contact_id), billerUserId: $id($this->biller_user_id), billingUnitId: $id($this->billing_unit_id), memberUserIds: array_map('intval', $this->member_user_ids), tags: $this->tags, links: array_values(array_filter($this->links, fn (array $link): bool => trim($link['title']) !== '' || trim($link['url']) !== '')), ); } /** * Shows a broken business rule as an error on the field it concerns. */ public function showDomainError(DomainException $exception): void { [$field, $key] = match (true) { $exception instanceof InvalidJobName => ['name', 'jobs.errors.invalid_name'], $exception instanceof ServiceUnitRequired => ['service_unit_ids', 'jobs.errors.service_units_required'], $exception instanceof DueBeforeStart => ['due_on', 'jobs.errors.due_before_start'], $exception instanceof PlanEndsBeforeStart => ['planned_to', 'jobs.errors.plan_ends_before_start'], $exception instanceof InvalidEstimatedHours => ['estimated_hours', 'jobs.errors.invalid_hours'], $exception instanceof InvalidTag => ['tags', 'jobs.errors.invalid_tag'], $exception instanceof InvalidJobLink => ['links', 'jobs.errors.invalid_link'], $exception instanceof InvalidJobReference => [self::referenceField($exception->field), 'jobs.errors.invalid_reference'], default => throw $exception, }; $this->addError($field, __($key)); } private static function referenceField(string $field): string { return match ($field) { 'customer' => 'customer_id', 'service_units' => 'service_unit_ids', 'members' => 'member_user_ids', default => $field.'_id', }; } /** * @return array> */ protected function rules(): array { return [ 'name' => ['required', 'string', 'max:255'], 'description' => ['nullable', 'string', 'max:10000'], 'customer_id' => ['required', 'integer'], 'service_unit_ids' => ['required', 'array', 'min:1'], 'service_unit_ids.*' => ['integer'], 'starts_on' => ['nullable', 'date_format:Y-m-d'], 'due_on' => ['nullable', 'date_format:Y-m-d'], 'planned_from' => ['nullable', 'date_format:Y-m-d'], 'planned_to' => ['nullable', 'date_format:Y-m-d'], 'kind' => ['required', Rule::enum(JobKind::class)], 'status' => ['required', Rule::enum(JobStatus::class)], 'priority' => ['required', Rule::enum(Priority::class)], 'payment_basis' => ['nullable', Rule::enum(PaymentBasis::class)], 'estimated_hours' => ['nullable', 'numeric', 'min:0', 'max:99999'], 'customer_reference' => ['nullable', 'string', 'max:255'], 'instructions' => ['nullable', 'string', 'max:10000'], 'responsible_user_id' => ['required', 'integer'], 'contact_user_id' => ['nullable', 'integer'], 'customer_contact_id' => ['nullable', 'integer'], 'biller_user_id' => ['nullable', 'integer'], 'billing_unit_id' => ['nullable', 'integer'], 'member_user_ids' => ['array'], 'member_user_ids.*' => ['integer'], 'tags' => ['array', 'max:30'], 'tags.*' => ['string', 'max:50'], 'links' => ['array', 'max:30'], 'links.*.title' => ['nullable', 'required_with:links.*.url', 'string', 'max:255'], 'links.*.url' => ['nullable', 'required_with:links.*.title', 'url:http,https', 'max:2000'], ]; } /** * @return array */ protected function messages(): array { return [ 'name.required' => __('jobs.errors.name_required'), 'name.max' => __('jobs.errors.invalid_name'), 'description.max' => __('jobs.errors.too_long', ['max' => 10000]), 'customer_id.required' => __('jobs.errors.customer_required'), 'customer_id.integer' => __('jobs.errors.customer_required'), 'service_unit_ids.required' => __('jobs.errors.service_units_required'), 'service_unit_ids.min' => __('jobs.errors.service_units_required'), '*.date_format' => __('jobs.errors.invalid_date'), 'starts_on.date_format' => __('jobs.errors.invalid_date'), 'due_on.date_format' => __('jobs.errors.invalid_date'), 'planned_from.date_format' => __('jobs.errors.invalid_date'), 'planned_to.date_format' => __('jobs.errors.invalid_date'), 'estimated_hours.numeric' => __('jobs.errors.invalid_hours'), 'estimated_hours.min' => __('jobs.errors.invalid_hours'), 'estimated_hours.max' => __('jobs.errors.invalid_hours'), 'customer_reference.max' => __('jobs.errors.too_long', ['max' => 255]), 'instructions.max' => __('jobs.errors.too_long', ['max' => 10000]), 'responsible_user_id.required' => __('jobs.errors.responsible_required'), 'tags.*.max' => __('jobs.errors.invalid_tag'), 'links.*.title.required_with' => __('jobs.errors.invalid_link'), 'links.*.url.required_with' => __('jobs.errors.invalid_link'), 'links.*.url.url' => __('jobs.errors.invalid_link'), ]; } }