Files
Toiminnanohjaus/app/Modules/Shared/Application/UseCases/CompanyDetailsStore.php
petriandClaude Sonnet 5.5 7d7b72989d Add an editable menu name and reserve billing material numbers on creation
- Company details get an optional "Valikon nimi", shown at the left of the
  main menu; it falls back to the application name when empty.
- A billing material gets its running number (billing_exports.number) only
  when it is created, so discarded drafts leave no gaps. Drafts have no
  number; existing created materials are numbered by the migration, and the
  CSV file name uses the number.
- TaskAssigneeMigrationTest rolls back from its own migration onward instead
  of assuming it is the last one.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-24 19:41:57 +02:00

95 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Shared\Application\UseCases;
use App\Modules\Shared\Application\Contracts\Settings;
use App\Modules\Shared\Application\DTOs\CompanyDetails;
use App\Modules\Shared\Domain\Exceptions\CompanyNameRequired;
use App\Modules\Shared\Domain\Exceptions\InvalidBusinessId;
use App\Modules\Shared\Domain\Exceptions\InvalidEmailAddress;
use App\Modules\Shared\Domain\ValueObjects\BusinessId;
use App\Modules\Shared\Domain\ValueObjects\EmailAddress;
/**
* Reads and saves the company's details (Asetukset → Yritystiedot), kept
* in the settings under `shared.company_*`. Who may save is checked by
* the caller (admin).
*/
final class CompanyDetailsStore
{
private const array KEYS = [
'name' => 'shared.company_name',
'business_id' => 'shared.company_business_id',
'address' => 'shared.company_address',
'phone' => 'shared.company_phone',
'email' => 'shared.company_email',
'menu_name' => 'shared.company_menu_name',
];
public function __construct(private Settings $settings) {}
/**
* Null until the details have been saved.
*/
public function get(): ?CompanyDetails
{
$name = $this->settings->get(self::KEYS['name']);
$businessId = $this->settings->get(self::KEYS['business_id']);
if ($name === null || $businessId === null) {
return null;
}
return new CompanyDetails(
$name,
$businessId,
$this->settings->get(self::KEYS['address']),
$this->settings->get(self::KEYS['phone']),
$this->settings->get(self::KEYS['email']),
$this->settings->get(self::KEYS['menu_name']),
);
}
/**
* @throws CompanyNameRequired
* @throws InvalidBusinessId
* @throws InvalidEmailAddress
*/
public function save(CompanyDetails $details): void
{
$name = trim($details->name);
if ($name === '' || mb_strlen($name) > 255) {
throw new CompanyNameRequired;
}
$businessId = new BusinessId($details->businessId);
$email = self::text($details->email);
$email = $email === null ? null : (new EmailAddress($email))->value;
$this->settings->put(self::KEYS['name'], $name);
$this->settings->put(self::KEYS['business_id'], $businessId->value);
$this->settings->put(self::KEYS['address'], self::text($details->address));
$this->settings->put(self::KEYS['phone'], self::text($details->phone));
$this->settings->put(self::KEYS['email'], $email);
$this->settings->put(self::KEYS['menu_name'], self::text($details->menuName));
}
/**
* The name shown at the left of the main menu: the saved one, or the application name.
*/
public function menuName(): string
{
return $this->settings->get(self::KEYS['menu_name']) ?? (string) config('app.name');
}
private static function text(?string $value): ?string
{
$value = $value === null ? '' : trim($value);
return $value === '' ? null : $value;
}
}