Files
petriandClaude Opus 5.5 bdd8d446d8 Add money permissions, a read-only price list and VAT rates
- Admins give employees and interns personal money permissions (prices,
  expenses, quotes, billing) on the user form. Other modules check them
  through the money.* abilities; admins and management see all amounts,
  subcontractors none.
- Employees and interns get a read-only price list: consumer products
  with VAT always, hourly rates, other products and the mileage rate only
  with the price permission. Subcontractors cannot open it.
- VAT rates are maintained on the price list page, with one default rate
  that always stays in use. Products get a VAT rate; changing a rate's
  percentage applies to every product using it.
- Consumer product prices are entered and stored with VAT, so cash prices
  stay round; the price without VAT is calculated. The product form shows
  the other price as you type.
- Shared VatPercentage value object and Money::addVat()/removeVat(),
  rounding half up to the cent in integer arithmetic.

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

149 lines
5.6 KiB
PHP

<?php
namespace App\Modules\Users\Presentation\Livewire\Forms;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use App\Modules\Shared\Domain\Exceptions\InvalidEmailAddress;
use App\Modules\Users\Application\DTOs\UserAccountData;
use App\Modules\Users\Application\DTOs\UserSummary;
use App\Modules\Users\Domain\Enums\MoneyPermission;
use App\Modules\Users\Domain\Enums\Role;
use App\Modules\Users\Domain\Exceptions\EmailAlreadyInUse;
use App\Modules\Users\Domain\Exceptions\InternRequiresEndDateAndSupervisor;
use App\Modules\Users\Domain\Exceptions\InvalidPersonName;
use App\Modules\Users\Domain\Exceptions\InvalidSupervisor;
use App\Modules\Users\Domain\Exceptions\LastAdminCannotBeRemoved;
use App\Modules\Users\Domain\Exceptions\SupervisorHasInterns;
use Closure;
use Illuminate\Validation\Rule;
use Livewire\Form;
/**
* User details form shared by the create and edit pages. Validates the
* shape of the input; business rules come from the Domain and are shown as
* field errors by showDomainError().
*/
class UserForm extends Form
{
public string $name = '';
public string $email = '';
public string $role = Role::Employee->value;
public bool $active = true;
public string $employment_ends_on = '';
public string $supervisor_id = '';
/**
* Values of MoneyPermission; only for employees and interns.
*
* @var list<string>
*/
public array $money_permissions = [];
public function fillFromSummary(UserSummary $user): void
{
$this->name = $user->name;
$this->email = $user->email;
$this->role = $user->role->value;
$this->active = $user->active;
$this->employment_ends_on = $user->employmentEndsOn ?? '';
$this->supervisor_id = $user->supervisorId === null ? '' : (string) $user->supervisorId;
$this->money_permissions = array_map(fn (MoneyPermission $permission): string => $permission->value, $user->moneyPermissions);
}
public function isIntern(): bool
{
return $this->role === Role::Intern->value;
}
/**
* Whether the chosen role gets personal money permissions.
*/
public function hasMoneyPermissions(): bool
{
return Role::tryFrom($this->role)?->hasPersonalMoneyPermissions() ?? false;
}
public function toData(): UserAccountData
{
$this->validate();
return UserAccountData::fromArray([
'name' => $this->name,
'email' => $this->email,
'role' => $this->role,
'active' => $this->active,
'employment_ends_on' => $this->isIntern() ? $this->employment_ends_on : null,
'supervisor_id' => $this->isIntern() ? $this->supervisor_id : null,
'money_permissions' => $this->hasMoneyPermissions() ? $this->money_permissions : [],
]);
}
/**
* Shows a broken business rule as an error on the field it concerns.
*/
public function showDomainError(DomainException $exception): void
{
[$field, $key] = match ($exception::class) {
InvalidPersonName::class => ['name', 'users.errors.invalid_name'],
InvalidEmailAddress::class => ['email', 'users.errors.invalid_email'],
EmailAlreadyInUse::class => ['email', 'users.errors.email_taken'],
InternRequiresEndDateAndSupervisor::class => ['supervisor_id', 'users.errors.supervisor_required'],
InvalidSupervisor::class => ['supervisor_id', 'users.errors.invalid_supervisor'],
LastAdminCannotBeRemoved::class => ['role', 'users.errors.last_admin'],
SupervisorHasInterns::class => ['role', 'users.errors.supervisor_has_interns'],
default => throw $exception,
};
$this->addError($field, __($key));
}
/**
* @return array<string, array<int, mixed>>
*/
protected function rules(): array
{
return [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email:filter', 'max:255'],
'role' => ['required', Rule::enum(Role::class)],
'active' => ['boolean'],
'employment_ends_on' => [Rule::requiredIf($this->isIntern()), 'nullable', 'date_format:Y-m-d'],
'supervisor_id' => [Rule::requiredIf($this->isIntern()), 'nullable', 'integer'],
'money_permissions' => ['array', function (string $attribute, mixed $value, Closure $fail): void {
foreach ((array) $value as $permission) {
if (! is_string($permission) || MoneyPermission::tryFrom($permission) === null) {
$fail(__('users.errors.invalid_money_permission'));
return;
}
}
}],
];
}
/**
* @return array<string, string>
*/
protected function messages(): array
{
return [
'name.required' => __('users.errors.name_required'),
'name.max' => __('users.errors.invalid_name'),
'email.required' => __('users.errors.email_required'),
'email.email' => __('users.errors.invalid_email'),
'email.max' => __('users.errors.invalid_email'),
'role.required' => __('users.errors.role_required'),
'role.enum' => __('users.errors.role_required'),
'employment_ends_on.required' => __('users.errors.end_date_required'),
'employment_ends_on.date_format' => __('users.errors.end_date_invalid'),
'supervisor_id.required' => __('users.errors.supervisor_required'),
'supervisor_id.integer' => __('users.errors.invalid_supervisor'),
];
}
}