Customer page and contacts/billing units (phase 1, item 2): - New customer page (customers.show) with details, contacts and billing units; add and edit on their own pages, delete after confirmation. Creating or editing a customer now leads to this page. - Contacts: name, title, phone, email, note. Billing units: name, billing address, e-invoice address (12-35 letters/digits), operator (required with an e-invoice address) and reference. - Use cases check that the row belongs to the customer, so ids in the URL can't reach another customer's rows. - EmailAddress moves from Users to Shared so both modules can use it. - <x-confirm-dialog> (native <dialog>) replaces <x-modal>, whose title, close button and focus handling are not accessible; solid red buttons use red-700 via published NormalButtonColors for 4.5:1 contrast. Personal identity code (henkilötunnus) for consumers who are invoiced: - Validated (date, century sign, check character), stored encrypted with an HMAC blind index for duplicate checks; never in CustomerSummary, search, logs or Livewire state. - Admins and management see it in full and may remove it; others enter it and see only the masked form. The form never shows the stored code. Spec, architecture doc and CLAUDE.md updated. Co-Authored-By: Claude Opus 5.5 <[email protected]>
110 lines
4.4 KiB
PHP
110 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Customers\Presentation\Livewire\Forms;
|
|
|
|
use App\Modules\Customers\Application\DTOs\CustomerDetailsData;
|
|
use App\Modules\Customers\Application\DTOs\CustomerSummary;
|
|
use App\Modules\Customers\Domain\Enums\CustomerType;
|
|
use App\Modules\Customers\Domain\Exceptions\BusinessIdAlreadyInUse;
|
|
use App\Modules\Customers\Domain\Exceptions\CompanyCannotHavePersonalIdentityCode;
|
|
use App\Modules\Customers\Domain\Exceptions\ConsumerCannotHaveBusinessId;
|
|
use App\Modules\Customers\Domain\Exceptions\InvalidBusinessId;
|
|
use App\Modules\Customers\Domain\Exceptions\InvalidCustomerName;
|
|
use App\Modules\Customers\Domain\Exceptions\InvalidPersonalIdentityCode;
|
|
use App\Modules\Customers\Domain\Exceptions\PersonalIdentityCodeAlreadyInUse;
|
|
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
|
use Illuminate\Validation\Rule;
|
|
use Livewire\Form;
|
|
|
|
/**
|
|
* Customer details form shared by the create and edit pages.
|
|
*
|
|
* Validates only the shape of the input. Business rules (business ID
|
|
* checksum, consumer without business ID, uniqueness) stay in the Domain,
|
|
* and their exceptions are shown as field errors by showDomainError().
|
|
*/
|
|
class CustomerForm extends Form
|
|
{
|
|
public string $name = '';
|
|
|
|
public string $type = CustomerType::Company->value;
|
|
|
|
public string $business_id = '';
|
|
|
|
/**
|
|
* A new personal identity code. Never filled from the stored code: empty
|
|
* keeps the stored one.
|
|
*/
|
|
public string $personal_identity_code = '';
|
|
|
|
public bool $remove_personal_identity_code = false;
|
|
|
|
public function fillFromSummary(CustomerSummary $customer): void
|
|
{
|
|
$this->name = $customer->name;
|
|
$this->type = $customer->type->value;
|
|
$this->business_id = $customer->businessId ?? '';
|
|
}
|
|
|
|
public function toData(): CustomerDetailsData
|
|
{
|
|
$this->validate();
|
|
|
|
return CustomerDetailsData::fromArray([
|
|
'name' => $this->name,
|
|
'type' => $this->type,
|
|
'business_id' => $this->type === CustomerType::Consumer->value ? null : $this->business_id,
|
|
'personal_identity_code' => $this->type === CustomerType::Consumer->value ? $this->personal_identity_code : null,
|
|
'remove_personal_identity_code' => $this->remove_personal_identity_code,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Shows a broken business rule as an error on the field it concerns.
|
|
*/
|
|
public function showDomainError(DomainException $exception): void
|
|
{
|
|
[$field, $key] = match ($exception::class) {
|
|
InvalidCustomerName::class => ['name', 'customers.errors.invalid_name'],
|
|
InvalidBusinessId::class => ['business_id', 'customers.errors.invalid_business_id'],
|
|
ConsumerCannotHaveBusinessId::class => ['business_id', 'customers.errors.consumer_business_id'],
|
|
BusinessIdAlreadyInUse::class => ['business_id', 'customers.errors.business_id_taken'],
|
|
InvalidPersonalIdentityCode::class => ['personal_identity_code', 'customers.errors.invalid_personal_identity_code'],
|
|
CompanyCannotHavePersonalIdentityCode::class => ['personal_identity_code', 'customers.errors.company_personal_identity_code'],
|
|
PersonalIdentityCodeAlreadyInUse::class => ['personal_identity_code', 'customers.errors.personal_identity_code_taken'],
|
|
default => throw $exception,
|
|
};
|
|
|
|
$this->addError($field, __($key));
|
|
}
|
|
|
|
/**
|
|
* @return array<string, array<int, mixed>>
|
|
*/
|
|
protected function rules(): array
|
|
{
|
|
return [
|
|
'name' => ['required', 'string', 'max:255'],
|
|
'type' => ['required', Rule::enum(CustomerType::class)],
|
|
'business_id' => ['nullable', 'string', 'max:20'],
|
|
'personal_identity_code' => ['nullable', 'string', 'max:20'],
|
|
'remove_personal_identity_code' => ['boolean'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
protected function messages(): array
|
|
{
|
|
return [
|
|
'name.required' => __('customers.errors.name_required'),
|
|
'name.max' => __('customers.errors.invalid_name'),
|
|
'type.required' => __('customers.errors.type_required'),
|
|
'type.enum' => __('customers.errors.type_required'),
|
|
'business_id.max' => __('customers.errors.invalid_business_id'),
|
|
'personal_identity_code.max' => __('customers.errors.invalid_personal_identity_code'),
|
|
];
|
|
}
|
|
}
|