Quick sales (pikamyynti) of active consumer products at the counter, in a new Sales module: - Product buttons add one to the sale's list per press; quantities can be lowered, raised or removed. Payment by cash or card, optional customer. - Product durations are booked as the seller's hours on the yearly "Pikamyynnit" job (internal customer, not billed) through Jobs' ProvideQuickSaleTask and TimeTracking's AddTaskHours. The sale stands even when the hours cannot be booked, and the seller is told. - Sales are never deleted: a credit records the lines negated on today's cash day, once per sale, by the seller or management. - Closing a cash day stores its balance by payment method and VAT rate and stops sales on it; only management reopens a day. - A printable A5 PDF receipt (barryvdh/laravel-dompdf) with the company's details, business ID, VAT number, lines and VAT breakdown. Company details are kept in the settings and edited by the admin only on the new Yritystiedot page. BusinessId moves from Customers to Shared so both use the same check. Co-Authored-By: Claude Opus 5.5 <[email protected]>
112 lines
4.5 KiB
PHP
112 lines
4.5 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\InternalCustomerMustBeCompany;
|
|
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 App\Modules\Shared\Domain\Exceptions\InvalidBusinessId;
|
|
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'],
|
|
InternalCustomerMustBeCompany::class => ['type', 'customers.errors.internal_must_be_company'],
|
|
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'),
|
|
];
|
|
}
|
|
}
|