Files
Toiminnanohjaus/app/Modules/Jobs/Presentation/Livewire/Forms/JobForm.php
T
petriandClaude Opus 5.5 c149c56388 Add the quoted amount and completion time to customer jobs
- Jobs get a budget / quoted amount (cents, VAT 0). Only users who may
  see quotes see and edit it on the job form and page, and only they see
  its changes in the history. Others' saves keep the stored amount: the
  server decides whether the amount is set, not the browser.
- Jobs record when they were done: set when a job first reaches a status
  that locks its hours, kept through the billing statuses, cleared when
  management opens the lock. The billing export will use it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 11:46:36 +02:00

253 lines
11 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 App\Modules\Shared\Domain\Exceptions\InvalidMoneyAmount;
use App\Modules\Shared\Domain\ValueObjects\Money;
use Illuminate\Support\Facades\Gate;
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 = '';
/**
* Only for users who may see quoted amounts; filled and sent only for them.
*/
public string $quoted_amount = '';
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->quoted_amount = $job->quotedAmountCents === null || ! Gate::allows('money.quotes') ? '' : Money::fromCents($job->quotedAmountCents)->inputValue();
$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']) !== '')),
quotedAmount: $text($this->quoted_amount),
setsQuotedAmount: Gate::allows('money.quotes'),
);
}
/**
* 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 InvalidMoneyAmount => ['quoted_amount', 'jobs.errors.invalid_amount'],
$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'],
'quoted_amount' => ['nullable', 'string', 'max:20'],
'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'),
'quoted_amount.max' => __('jobs.errors.invalid_amount'),
'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'),
];
}
}