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]>
This commit is contained in:
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Users\Application\DTOs;
|
||||
|
||||
use App\Modules\Users\Domain\Enums\MoneyPermission;
|
||||
use App\Modules\Users\Domain\Enums\Role;
|
||||
|
||||
/**
|
||||
@@ -13,6 +14,7 @@ final readonly class UserAccountData
|
||||
{
|
||||
/**
|
||||
* @param string|null $employmentEndsOn Date as Y-m-d.
|
||||
* @param list<MoneyPermission> $moneyPermissions Only for employees and interns.
|
||||
*/
|
||||
public function __construct(
|
||||
public string $name,
|
||||
@@ -21,10 +23,11 @@ final readonly class UserAccountData
|
||||
public bool $active = true,
|
||||
public ?string $employmentEndsOn = null,
|
||||
public ?int $supervisorId = null,
|
||||
public array $moneyPermissions = [],
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param array{name: string, email: string, role: string|Role, active?: bool, employment_ends_on?: string|null, supervisor_id?: int|string|null} $data
|
||||
* @param array{name: string, email: string, role: string|Role, active?: bool, employment_ends_on?: string|null, supervisor_id?: int|string|null, money_permissions?: list<string>} $data
|
||||
*/
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
@@ -38,6 +41,7 @@ final readonly class UserAccountData
|
||||
active: $data['active'] ?? true,
|
||||
employmentEndsOn: $endsOn === '' ? null : $endsOn,
|
||||
supervisorId: $supervisorId === null || $supervisorId === '' ? null : (int) $supervisorId,
|
||||
moneyPermissions: MoneyPermission::listFrom($data['money_permissions'] ?? []),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Users\Application\DTOs;
|
||||
|
||||
use App\Modules\Users\Domain\Enums\MoneyPermission;
|
||||
use App\Modules\Users\Domain\Enums\Role;
|
||||
|
||||
/**
|
||||
@@ -13,6 +14,7 @@ final readonly class UserSummary
|
||||
{
|
||||
/**
|
||||
* @param string|null $employmentEndsOn Date as Y-m-d.
|
||||
* @param list<MoneyPermission> $moneyPermissions
|
||||
*/
|
||||
public function __construct(
|
||||
public int $id,
|
||||
@@ -23,5 +25,6 @@ final readonly class UserSummary
|
||||
public ?string $employmentEndsOn,
|
||||
public ?int $supervisorId,
|
||||
public ?string $supervisorName,
|
||||
public array $moneyPermissions = [],
|
||||
) {}
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@ final class CreateUserAccount
|
||||
$data->role,
|
||||
EmploymentEndDate::parse($data->employmentEndsOn),
|
||||
$data->supervisorId,
|
||||
$data->moneyPermissions,
|
||||
);
|
||||
|
||||
$userId = $this->transactions->run(function () use ($user): int {
|
||||
|
||||
@@ -46,7 +46,7 @@ final class UpdateUserAccount
|
||||
throw new EmailAlreadyInUse;
|
||||
}
|
||||
|
||||
$user->changeDetails($name, $email, $data->role, $data->active, $endsOn, $data->supervisorId);
|
||||
$user->changeDetails($name, $email, $data->role, $data->active, $endsOn, $data->supervisorId, $data->moneyPermissions);
|
||||
|
||||
if ($wasActiveAdmin && ! $user->isActiveAdmin() && $this->users->countActiveAdmins($userId) === 0) {
|
||||
throw new LastAdminCannotBeRemoved;
|
||||
|
||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace App\Modules\Users\Domain\Entities;
|
||||
|
||||
use App\Modules\Shared\Domain\ValueObjects\EmailAddress;
|
||||
use App\Modules\Users\Domain\Enums\MoneyPermission;
|
||||
use App\Modules\Users\Domain\Enums\Role;
|
||||
use App\Modules\Users\Domain\Exceptions\InternRequiresEndDateAndSupervisor;
|
||||
use App\Modules\Users\Domain\Exceptions\InvalidSupervisor;
|
||||
@@ -16,9 +17,14 @@ use DateTimeImmutable;
|
||||
*
|
||||
* Interns have an employment end date (their last day with access) and a
|
||||
* supervisor. Other roles have neither: they are cleared on a role change.
|
||||
* Employees and interns may have personal money permissions; other roles
|
||||
* see money by role, so theirs are cleared.
|
||||
*/
|
||||
final class UserAccount
|
||||
{
|
||||
/**
|
||||
* @param list<MoneyPermission> $moneyPermissions
|
||||
*/
|
||||
private function __construct(
|
||||
private ?int $id,
|
||||
private PersonName $name,
|
||||
@@ -27,10 +33,13 @@ final class UserAccount
|
||||
private bool $active,
|
||||
private ?DateTimeImmutable $employmentEndsOn,
|
||||
private ?int $supervisorId,
|
||||
private array $moneyPermissions = [],
|
||||
) {}
|
||||
|
||||
/**
|
||||
* A new, active user that is not stored yet, so it has no id.
|
||||
*
|
||||
* @param list<MoneyPermission> $moneyPermissions
|
||||
*/
|
||||
public static function register(
|
||||
PersonName $name,
|
||||
@@ -38,8 +47,9 @@ final class UserAccount
|
||||
Role $role,
|
||||
?DateTimeImmutable $employmentEndsOn = null,
|
||||
?int $supervisorId = null,
|
||||
array $moneyPermissions = [],
|
||||
): self {
|
||||
$user = new self(null, $name, $email, $role, true, $employmentEndsOn, $supervisorId);
|
||||
$user = new self(null, $name, $email, $role, true, $employmentEndsOn, $supervisorId, MoneyPermission::listFrom($moneyPermissions));
|
||||
$user->applyRoleRules();
|
||||
|
||||
return $user;
|
||||
@@ -52,6 +62,8 @@ final class UserAccount
|
||||
* intern whose supervisor was deleted, say) must still load, so the user
|
||||
* can be signed in or out and an admin can correct it. The rules apply
|
||||
* again on the next changeDetails().
|
||||
*
|
||||
* @param list<MoneyPermission> $moneyPermissions
|
||||
*/
|
||||
public static function reconstitute(
|
||||
int $id,
|
||||
@@ -61,10 +73,14 @@ final class UserAccount
|
||||
bool $active,
|
||||
?DateTimeImmutable $employmentEndsOn,
|
||||
?int $supervisorId,
|
||||
array $moneyPermissions = [],
|
||||
): self {
|
||||
return new self($id, $name, $email, $role, $active, $employmentEndsOn, $supervisorId);
|
||||
return new self($id, $name, $email, $role, $active, $employmentEndsOn, $supervisorId, MoneyPermission::listFrom($moneyPermissions));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<MoneyPermission> $moneyPermissions
|
||||
*/
|
||||
public function changeDetails(
|
||||
PersonName $name,
|
||||
EmailAddress $email,
|
||||
@@ -72,6 +88,7 @@ final class UserAccount
|
||||
bool $active,
|
||||
?DateTimeImmutable $employmentEndsOn,
|
||||
?int $supervisorId,
|
||||
array $moneyPermissions = [],
|
||||
): void {
|
||||
$changed = clone $this;
|
||||
$changed->name = $name;
|
||||
@@ -80,6 +97,7 @@ final class UserAccount
|
||||
$changed->active = $active;
|
||||
$changed->employmentEndsOn = $employmentEndsOn;
|
||||
$changed->supervisorId = $supervisorId;
|
||||
$changed->moneyPermissions = MoneyPermission::listFrom($moneyPermissions);
|
||||
$changed->applyRoleRules();
|
||||
|
||||
foreach (get_object_vars($changed) as $property => $value) {
|
||||
@@ -150,8 +168,20 @@ final class UserAccount
|
||||
return $this->supervisorId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<MoneyPermission>
|
||||
*/
|
||||
public function moneyPermissions(): array
|
||||
{
|
||||
return $this->moneyPermissions;
|
||||
}
|
||||
|
||||
private function applyRoleRules(): void
|
||||
{
|
||||
if (! $this->role->hasPersonalMoneyPermissions()) {
|
||||
$this->moneyPermissions = [];
|
||||
}
|
||||
|
||||
if ($this->role !== Role::Intern) {
|
||||
$this->employmentEndsOn = null;
|
||||
$this->supervisorId = null;
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Users\Domain\Enums;
|
||||
|
||||
/**
|
||||
* Personal permissions of employees and interns to see money amounts.
|
||||
* Admins and management see all amounts by role; subcontractors none.
|
||||
* Other modules check these through the `money.<value>` abilities.
|
||||
*/
|
||||
enum MoneyPermission: string
|
||||
{
|
||||
/** Hourly rates and other than consumer product prices. */
|
||||
case Prices = 'prices';
|
||||
|
||||
/** Amounts of expenses. */
|
||||
case Expenses = 'expenses';
|
||||
|
||||
/** Budgets and quoted amounts. */
|
||||
case Quotes = 'quotes';
|
||||
|
||||
/** Billing material and billed amounts. */
|
||||
case Billing = 'billing';
|
||||
|
||||
/**
|
||||
* @param list<self|string> $values
|
||||
* @return list<self>
|
||||
*/
|
||||
public static function listFrom(array $values): array
|
||||
{
|
||||
$permissions = array_map(fn (self|string $value): self => $value instanceof self ? $value : self::from($value), $values);
|
||||
|
||||
return array_values(array_filter(self::cases(), fn (self $case): bool => in_array($case, $permissions, true)));
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,22 @@ enum Role: string
|
||||
return $this === self::Admin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the role sees every money amount without personal permissions.
|
||||
*/
|
||||
public function seesAllMoney(): bool
|
||||
{
|
||||
return $this === self::Admin || $this === self::Management;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether personal money permissions apply to the role.
|
||||
*/
|
||||
public function hasPersonalMoneyPermissions(): bool
|
||||
{
|
||||
return $this === self::Employee || $this === self::Intern;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a user with this role can be an intern's supervisor.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Users\Infrastructure\Persistence\Eloquent;
|
||||
|
||||
use App\Modules\Users\Domain\Enums\MoneyPermission;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Persistence model, used only inside the Users Infrastructure layer.
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $user_id
|
||||
* @property bool $can_see_prices
|
||||
* @property bool $can_see_expenses
|
||||
* @property bool $can_see_quotes
|
||||
* @property bool $can_see_billing
|
||||
*/
|
||||
#[Table('user_money_permissions')]
|
||||
#[Fillable(['user_id', 'can_see_prices', 'can_see_expenses', 'can_see_quotes', 'can_see_billing'])]
|
||||
class UserMoneyPermissionsModel extends Model
|
||||
{
|
||||
public static function column(MoneyPermission $permission): string
|
||||
{
|
||||
return 'can_see_'.$permission->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<MoneyPermission> $permissions
|
||||
* @return array<string, bool>
|
||||
*/
|
||||
public static function columnsFor(array $permissions): array
|
||||
{
|
||||
$columns = [];
|
||||
|
||||
foreach (MoneyPermission::cases() as $permission) {
|
||||
$columns[self::column($permission)] = in_array($permission, $permissions, true);
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
public function allows(MoneyPermission $permission): bool
|
||||
{
|
||||
return (bool) $this->getAttribute(self::column($permission));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<MoneyPermission>
|
||||
*/
|
||||
public function permissions(): array
|
||||
{
|
||||
return array_values(array_filter(MoneyPermission::cases(), fn (MoneyPermission $permission): bool => $this->allows($permission)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'user_id' => 'integer',
|
||||
'can_see_prices' => 'boolean',
|
||||
'can_see_expenses' => 'boolean',
|
||||
'can_see_quotes' => 'boolean',
|
||||
'can_see_billing' => 'boolean',
|
||||
];
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -14,7 +14,7 @@ class EloquentUserAccountQueries implements UserAccountQueries
|
||||
{
|
||||
public function findSummary(int $id): ?UserSummary
|
||||
{
|
||||
$model = User::query()->with('supervisor:id,name')->find($id);
|
||||
$model = User::query()->with(['supervisor:id,name', 'moneyPermissions'])->find($id);
|
||||
|
||||
return $model === null ? null : $this->toSummary($model);
|
||||
}
|
||||
@@ -77,6 +77,7 @@ class EloquentUserAccountQueries implements UserAccountQueries
|
||||
employmentEndsOn: $model->employment_ends_on?->toDateString(),
|
||||
supervisorId: $model->supervisor_id,
|
||||
supervisorName: $model->supervisor?->name,
|
||||
moneyPermissions: $model->moneyPermissions?->permissions() ?? [],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+8
@@ -8,6 +8,7 @@ use App\Modules\Users\Domain\Contracts\UserAccountRepository;
|
||||
use App\Modules\Users\Domain\Entities\UserAccount;
|
||||
use App\Modules\Users\Domain\Enums\Role;
|
||||
use App\Modules\Users\Domain\ValueObjects\PersonName;
|
||||
use App\Modules\Users\Infrastructure\Persistence\Eloquent\UserMoneyPermissionsModel;
|
||||
use DateTimeImmutable;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
@@ -46,6 +47,12 @@ class EloquentUserAccountRepository implements UserAccountRepository
|
||||
'supervisor_id' => $user->supervisorId(),
|
||||
])->save();
|
||||
|
||||
UserMoneyPermissionsModel::query()->updateOrCreate(
|
||||
['user_id' => $model->id],
|
||||
UserMoneyPermissionsModel::columnsFor($user->moneyPermissions()),
|
||||
);
|
||||
$model->unsetRelation('moneyPermissions');
|
||||
|
||||
return $this->toEntity($model->refresh());
|
||||
}
|
||||
|
||||
@@ -90,6 +97,7 @@ class EloquentUserAccountRepository implements UserAccountRepository
|
||||
$model->is_active,
|
||||
$model->employment_ends_on === null ? null : DateTimeImmutable::createFromInterface($model->employment_ends_on),
|
||||
$model->supervisor_id,
|
||||
$model->moneyPermissions?->permissions() ?? [],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ use App\Modules\Users\Application\Contracts\Clock;
|
||||
use App\Modules\Users\Application\Contracts\PasswordLinkSender;
|
||||
use App\Modules\Users\Application\Contracts\UserAccountQueries;
|
||||
use App\Modules\Users\Domain\Contracts\UserAccountRepository;
|
||||
use App\Modules\Users\Domain\Enums\MoneyPermission;
|
||||
use App\Modules\Users\Infrastructure\Auth\LaravelPasswordLinkSender;
|
||||
use App\Modules\Users\Infrastructure\Auth\PasswordLinkMail;
|
||||
use App\Modules\Users\Infrastructure\Persistence\Repositories\EloquentUserAccountQueries;
|
||||
@@ -65,6 +66,12 @@ class UsersServiceProvider extends ServiceProvider
|
||||
|
||||
Gate::define('users.manage', fn (User $user): bool => $user->role->canManageUsers());
|
||||
|
||||
// money.prices, money.expenses, money.quotes, money.billing: whether the
|
||||
// user may see that kind of money amounts (specification 2.3).
|
||||
foreach (MoneyPermission::cases() as $permission) {
|
||||
Gate::define('money.'.$permission->value, fn (User $user): bool => $user->canSeeMoney($permission));
|
||||
}
|
||||
|
||||
Password::defaults(fn () => Password::min(12)->mixedCase()->numbers()->symbols());
|
||||
|
||||
ResetPassword::toMailUsing(fn (CanResetPassword $notifiable, string $token): MailMessage => PasswordLinkMail::build(
|
||||
|
||||
@@ -6,6 +6,7 @@ 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;
|
||||
@@ -13,6 +14,7 @@ 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;
|
||||
|
||||
@@ -35,6 +37,13 @@ class UserForm extends Form
|
||||
|
||||
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;
|
||||
@@ -43,6 +52,7 @@ class UserForm extends Form
|
||||
$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
|
||||
@@ -50,6 +60,14 @@ class UserForm extends Form
|
||||
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();
|
||||
@@ -61,6 +79,7 @@ class UserForm extends Form
|
||||
'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 : [],
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -95,6 +114,15 @@ class UserForm extends Form
|
||||
'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;
|
||||
}
|
||||
}
|
||||
}],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ 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;
|
||||
@@ -161,6 +162,20 @@ new class extends Component
|
||||
</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>
|
||||
|
||||
Reference in New Issue
Block a user