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

237 lines
8.7 KiB
PHP

<?php
use App\Modules\Shared\Domain\Exceptions\DomainException;
use App\Modules\Users\Application\Contracts\UserAccountQueries;
use App\Modules\Users\Application\DTOs\UserOption;
use App\Modules\Users\Application\UseCases\CreateUserAccount;
use App\Modules\Users\Application\UseCases\SendPasswordSetupLink;
use App\Modules\Users\Application\UseCases\UpdateUserAccount;
use App\Modules\Users\Domain\Enums\MoneyPermission;
use App\Modules\Users\Domain\Enums\Role;
use App\Modules\Users\Domain\Exceptions\UserAccountNotFound;
use App\Modules\Users\Presentation\Livewire\Forms\UserForm;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* Create page (no route parameter) and edit page (with {user}).
*/
new class extends Component
{
#[Locked]
public ?int $userId = null;
public UserForm $form;
public string $linkStatus = '';
/**
* The latest set-password link, shown to the admin to copy. It lives only
* in this page's state, never in the session or the database.
*/
#[Locked]
public string $setupLink = '';
public function mount(UserAccountQueries $userQueries, ?int $user = null): void
{
$this->authorize('users.manage');
if ($user === null) {
return;
}
$summary = $userQueries->findSummary($user);
abort_if($summary === null, 404);
$this->userId = $summary->id;
$this->form->fillFromSummary($summary);
}
/**
* @return list<UserOption>
*/
#[Computed]
public function supervisorOptions(): array
{
return app(UserAccountQueries::class)->supervisorOptions($this->userId);
}
public function save(CreateUserAccount $createUser, UpdateUserAccount $updateUser): void
{
$this->authorize('users.manage');
$data = $this->form->toData();
$created = null;
try {
if ($this->userId === null) {
$created = $createUser->execute($data);
} else {
$updateUser->execute($this->userId, $data);
session()->flash('status', __('users.updated', ['name' => $data->name]));
}
} catch (UserAccountNotFound) {
abort(404);
} catch (DomainException $exception) {
$this->form->showDomainError($exception);
return;
}
if ($created !== null) {
// Stay on the page so the admin can copy the link; it becomes
// the new user's edit page.
$this->userId = $created->userId;
$this->setupLink = $created->setupLink->url;
$this->linkStatus = __(
$created->setupLink->emailSent ? 'users.created' : 'users.created_link_failed',
['name' => $data->name, 'email' => $data->email],
);
$this->js('history.replaceState(history.state, "", '.json_encode(route('users.edit', $created->userId)).')');
return;
}
$this->redirectRoute('users.index', navigate: true);
}
public function sendPasswordLink(SendPasswordSetupLink $sendLink): void
{
$this->authorize('users.manage');
abort_if($this->userId === null, 404);
try {
$link = $sendLink->execute($this->userId);
} catch (UserAccountNotFound) {
abort(404);
}
$this->setupLink = $link->url;
$this->linkStatus = $link->emailSent
? __('users.link_sent', ['email' => $this->form->email])
: __('users.link_mail_failed');
}
public function render(): mixed
{
return $this->view()->title(__($this->userId === null ? 'users.create_title' : 'users.edit_title'));
}
};
?>
<div class="mx-auto max-w-2xl space-y-6 p-4 sm:p-6">
<h1 class="text-2xl font-semibold">
{{ __($userId === null ? 'users.create_title' : 'users.edit_title') }}
</h1>
<form wire:submit="save" class="space-y-6" novalidate>
<x-input wire:model="form.name" :label="__('users.fields.name')" autocomplete="off" required />
<x-input wire:model="form.email" type="email" :label="__('users.fields.email')" autocomplete="off" required />
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
<x-select.native
wire:model.live="form.role"
:label="__('users.fields.role')"
:aria-label="__('users.fields.role')"
:options="collect(Role::cases())->map(fn (Role $role) => ['label' => __('users.roles.'.$role->value), 'value' => $role->value])->all()"
/>
@if ($form->isIntern())
<x-input
wire:model="form.employment_ends_on"
type="date"
:label="__('users.fields.employment_ends_on')"
:hint="__('users.fields.employment_ends_on_description')"
required
/>
<x-select.native
wire:model="form.supervisor_id"
:label="__('users.fields.supervisor')"
:aria-label="__('users.fields.supervisor')"
required
>
<option value="">{{ __('users.fields.supervisor_placeholder') }}</option>
@foreach ($this->supervisorOptions as $option)
<option wire:key="supervisor-{{ $option->id }}" value="{{ $option->id }}">{{ $option->name }}</option>
@endforeach
</x-select.native>
@endif
@if ($form->hasMoneyPermissions())
<x-checkbox-group
:legend="__('users.money_permissions.legend')"
model="form.money_permissions"
:hint="__('users.money_permissions.hint')"
:columns="false"
:options="collect(MoneyPermission::cases())->map(fn (MoneyPermission $permission) => [
'value' => $permission->value,
'label' => __('users.money_permissions.'.$permission->value),
'note' => __('users.money_permissions.'.$permission->value.'_note'),
])->all()"
/>
@endif
<div>
<x-checkbox wire:model="form.active" :label="__('users.fields.active')" aria-describedby="active-description" />
<p id="active-description" class="mt-1 text-sm text-zinc-600 dark:text-zinc-300">{{ __('users.fields.active_description') }}</p>
</div>
<div class="flex flex-wrap gap-3">
<x-button submit :text="__('users.save')" />
<x-button :href="route('users.index')" wire:navigate subtle color="black" :text="__('users.cancel')" />
</div>
</form>
@if ($userId !== null)
<div class="space-y-3 border-t border-zinc-200 pt-6 dark:border-zinc-700">
<x-button wire:click="sendPasswordLink" subtle color="black" icon="link" :text="__('users.send_link')" />
<div role="status" aria-live="polite">
@if ($linkStatus !== '')
<p class="text-sm">{{ $linkStatus }}</p>
@endif
</div>
@if ($setupLink !== '')
<div
wire:key="setup-link-{{ md5($setupLink) }}"
x-data="{
message: '',
async copyLink() {
try {
await navigator.clipboard.writeText($refs.setupLink.value);
this.message = @js(__('users.setup_link.copied'));
} catch (error) {
$refs.setupLink.focus();
$refs.setupLink.select();
this.message = @js(__('users.setup_link.copy_failed'));
}
},
}"
class="space-y-3"
>
{{-- Without wire:model, TallStackUI ties the label to the input only through an explicit id. --}}
<x-input
id="setup-link"
:label="__('users.setup_link.label')"
:hint="__('users.setup_link.hint', ['count' => intdiv((int) config('auth.passwords.admin_links.expire'), 60)])"
:value="$setupLink"
x-ref="setupLink"
x-on:focus="$el.select()"
readonly
/>
<x-button x-on:click="copyLink()" subtle color="black" icon="clipboard-document" :text="__('users.setup_link.copy')" />
<p role="status" aria-live="polite" class="text-sm" x-text="message"></p>
</div>
@endif
</div>
@endif
</div>