Files
Toiminnanohjaus/app/Modules/Jobs/Presentation/Livewire/Forms/JobForm.php
T
petriandClaude Opus 5.5 70e107f6e2 Add customer jobs with numbering, members, tags, links and history
Phase 1, item 4. New Jobs module:
- Customer jobs with the spec's fields except money (phase 2): yearly
  running numbers (2026-0001) from a locked counter row, service units,
  schedule, kind, status, priority, payment basis, estimated hours,
  PO reference, instructions, members (responsible, contact person,
  customer contact, biller, billing unit, other members), tags shared
  across jobs, and links.
- Every change is recorded field by field and shown on the job page with
  names and Finnish dates.
- References to other modules are checked through their query
  contracts: contact and billing unit must be the customer's own;
  service units and users must be in use unless already on the job.
- Job list with search, filters and "my jobs" on by default; job form
  with an accessible customer search and quick add; job page.
- Access: subcontractors see only jobs they are members of, read only;
  billing statuses for management and the job's biller; archiving for
  management.

Related changes:
- Customers: one internal customer (created by a migration) for internal
  work; contacts and billing units used by a job cannot be removed.
- Users and Catalog: batch name lookups and active user options with
  roles for other modules.
- <x-checkbox-group> component; spec and CLAUDE.md updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-23 18:57:18 +02:00

239 lines
10 KiB
PHP

<?php
namespace App\Modules\Jobs\Presentation\Livewire\Forms;
use App\Modules\Jobs\Application\DTOs\CustomerJobData;
use App\Modules\Jobs\Application\DTOs\CustomerJobDetails;
use App\Modules\Jobs\Application\DTOs\NamedId;
use App\Modules\Jobs\Domain\Enums\JobKind;
use App\Modules\Jobs\Domain\Enums\JobStatus;
use App\Modules\Jobs\Domain\Enums\PaymentBasis;
use App\Modules\Jobs\Domain\Enums\Priority;
use App\Modules\Jobs\Domain\Exceptions\DueBeforeStart;
use App\Modules\Jobs\Domain\Exceptions\InvalidEstimatedHours;
use App\Modules\Jobs\Domain\Exceptions\InvalidJobLink;
use App\Modules\Jobs\Domain\Exceptions\InvalidJobName;
use App\Modules\Jobs\Domain\Exceptions\InvalidJobReference;
use App\Modules\Jobs\Domain\Exceptions\InvalidTag;
use App\Modules\Jobs\Domain\Exceptions\PlanEndsBeforeStart;
use App\Modules\Jobs\Domain\Exceptions\ServiceUnitRequired;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use Illuminate\Validation\Rule;
use Livewire\Form;
/**
* Customer job form shared by the create and edit pages. Validates the
* shape of the input; business rules stay in the Domain.
*/
class JobForm extends Form
{
public string $name = '';
public string $description = '';
public string $customer_id = '';
/** @var list<string> */
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<string> */
public array $member_user_ids = [];
/** @var list<string> */
public array $tags = [];
/** @var list<array{title: string, url: string}> */
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<string, array<int, mixed>>
*/
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<string, string>
*/
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'),
];
}
}