Files
Toiminnanohjaus/app/Modules/Users/Application/UseCases/UpdateUserAccount.php
T
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

69 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Users\Application\UseCases;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Shared\Domain\ValueObjects\EmailAddress;
use App\Modules\Users\Application\DTOs\UserAccountData;
use App\Modules\Users\Domain\Contracts\UserAccountRepository;
use App\Modules\Users\Domain\Exceptions\EmailAlreadyInUse;
use App\Modules\Users\Domain\Exceptions\InvalidSupervisor;
use App\Modules\Users\Domain\Exceptions\LastAdminCannotBeRemoved;
use App\Modules\Users\Domain\Exceptions\SupervisorHasInterns;
use App\Modules\Users\Domain\Exceptions\UserAccountNotFound;
use App\Modules\Users\Domain\Services\SupervisorRule;
use App\Modules\Users\Domain\ValueObjects\PersonName;
final class UpdateUserAccount
{
public function __construct(
private UserAccountRepository $users,
private SupervisorRule $supervisorRule,
private TransactionManager $transactions,
) {}
/**
* @throws UserAccountNotFound
* @throws EmailAlreadyInUse
* @throws InvalidSupervisor
* @throws LastAdminCannotBeRemoved
* @throws SupervisorHasInterns
*/
public function execute(int $userId, UserAccountData $data): void
{
$name = new PersonName($data->name);
$email = new EmailAddress($data->email);
$endsOn = EmploymentEndDate::parse($data->employmentEndsOn);
$this->transactions->run(function () use ($userId, $data, $name, $email, $endsOn): void {
$user = $this->users->findById($userId) ?? throw new UserAccountNotFound($userId);
$wasActiveAdmin = $user->isActiveAdmin();
$couldSupervise = $user->canSuperviseInterns();
if ($this->users->isEmailTaken($email, $userId)) {
throw new EmailAlreadyInUse;
}
$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;
}
if ($couldSupervise && ! $user->canSuperviseInterns()) {
$internCount = $this->users->countActiveInternsSupervisedBy($userId);
if ($internCount > 0) {
throw new SupervisorHasInterns($internCount);
}
}
$this->supervisorRule->ensureValidFor($user);
$this->users->save($user);
});
}
}