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]>
This commit is contained in:
2026-11-24 19:41:57 +02:00
co-authored by Claude Sonnet 5.5
parent 48030e7689
commit 7d7b72989d
25 changed files with 195 additions and 23 deletions
@@ -14,6 +14,7 @@ final readonly class ExportSummary
{
public function __construct(
public int $id,
public ?int $number,
public ExportStatus $status,
public DateTimeImmutable $createdAt,
public string $createdByName,
@@ -21,6 +21,7 @@ final readonly class ExportView
*/
public function __construct(
public int $id,
public ?int $number,
public ExportStatus $status,
public string $period,
public DateTimeImmutable $createdAt,
@@ -70,6 +70,7 @@ final class ExportViews
return new ExportView(
(int) $export->id(),
$export->number(),
$export->status(),
$export->period(),
$export->createdAt(),
@@ -23,7 +23,8 @@ use App\Modules\Users\Application\Contracts\Clock;
* Creates the billing material from its draft (spec 12), all or nothing:
* checks every job again, marks the approved hours and billable expenses
* billed, forms the invoice lines from what was marked with the current
* prices, and moves the jobs to "Lasku luotu". Monthly fee jobs keep
* prices, gives the material the next running number (reserved only now, so
* discarded drafts leave no gaps), and moves the jobs to "Lasku luotu". Monthly fee jobs keep
* running, so their status stays.
*/
final class FinalizeBillingExport
@@ -81,7 +82,7 @@ final class FinalizeBillingExport
}
}
$export->finalize($linesByJob, $actorUserId, $this->clock->now());
$export->finalize($linesByJob, $this->exports->nextNumber(), $actorUserId, $this->clock->now());
$this->exports->save($export);
return count($export->invoices());
@@ -26,6 +26,11 @@ interface BillingExportRepository
public function delete(BillingExport $export): void;
/**
* The next free number of a created material (1, 2, ...), without gaps.
*/
public function nextNumber(): int;
/**
* The jobs among the given ones whose fixed price is already on a created material.
*
@@ -30,6 +30,7 @@ final class BillingExport
*/
private function __construct(
private ?int $id,
private ?int $number,
private ExportStatus $status,
private string $period,
private int $createdBy,
@@ -44,15 +45,15 @@ final class BillingExport
*/
public static function startDraft(int $createdBy, DateTimeImmutable $now): self
{
return new self(null, ExportStatus::Draft, $now->format('Y-m'), $createdBy, $now, null, null, []);
return new self(null, null, ExportStatus::Draft, $now->format('Y-m'), $createdBy, $now, null, null, []);
}
/**
* @param list<BillingInvoice> $invoices
*/
public static function reconstitute(int $id, ExportStatus $status, string $period, int $createdBy, DateTimeImmutable $createdAt, ?int $finalizedBy, ?DateTimeImmutable $finalizedAt, array $invoices): self
public static function reconstitute(int $id, ?int $number, ExportStatus $status, string $period, int $createdBy, DateTimeImmutable $createdAt, ?int $finalizedBy, ?DateTimeImmutable $finalizedAt, array $invoices): self
{
return new self($id, $status, $period, $createdBy, $createdAt, $finalizedBy, $finalizedAt, $invoices);
return new self($id, $number, $status, $period, $createdBy, $createdAt, $finalizedBy, $finalizedAt, $invoices);
}
/**
@@ -170,14 +171,14 @@ final class BillingExport
}
/**
* Forms the invoice lines and locks the material.
* Forms the invoice lines, gives the material its number and locks it.
*
* @param array<int, list<InvoiceLine>> $linesByJob Keyed by job id.
*
* @throws ExportNotDraft
* @throws ExportEmpty
*/
public function finalize(array $linesByJob, int $finalizedBy, DateTimeImmutable $now): void
public function finalize(array $linesByJob, int $number, int $finalizedBy, DateTimeImmutable $now): void
{
$this->ensureDraft();
@@ -193,6 +194,7 @@ final class BillingExport
$invoice->setLines($lines);
}
$this->number = $number;
$this->status = ExportStatus::Created;
$this->finalizedBy = $finalizedBy;
$this->finalizedAt = $now;
@@ -240,6 +242,14 @@ final class BillingExport
return $this->id;
}
/**
* The running number, reserved when the material is created; a draft has none.
*/
public function number(): ?int
{
return $this->number;
}
public function status(): ExportStatus
{
return $this->status;
@@ -13,6 +13,7 @@ use Illuminate\Support\Carbon;
* Persistence model, used only inside the Billing Infrastructure layer.
*
* @property int $id
* @property int|null $number
* @property ExportStatus $status
* @property string|null $period Y-m
* @property int $created_by
@@ -21,7 +22,7 @@ use Illuminate\Support\Carbon;
* @property Carbon $created_at
*/
#[Table('billing_exports')]
#[Fillable(['status', 'period', 'created_by', 'finalized_by', 'finalized_at'])]
#[Fillable(['number', 'status', 'period', 'created_by', 'finalized_by', 'finalized_at'])]
class BillingExportModel extends Model
{
/**
@@ -39,6 +40,7 @@ class BillingExportModel extends Model
{
return [
'status' => ExportStatus::class,
'number' => 'integer',
'created_by' => 'integer',
'finalized_by' => 'integer',
'finalized_at' => 'datetime',
@@ -45,6 +45,7 @@ class EloquentBillingExportRepository implements BillingExportRepository
$model = $export->id() === null ? new BillingExportModel : BillingExportModel::query()->findOrFail($export->id());
$model->fill([
'number' => $export->number(),
'status' => $export->status(),
'period' => $export->period(),
'created_by' => $export->createdBy(),
@@ -116,6 +117,11 @@ class EloquentBillingExportRepository implements BillingExportRepository
BillingExportModel::query()->whereKey($export->id())->delete();
}
public function nextNumber(): int
{
return (int) BillingExportModel::query()->max('number') + 1;
}
public function fixedPriceBilledJobIds(array $jobIds): array
{
if ($jobIds === []) {
@@ -175,6 +181,7 @@ class EloquentBillingExportRepository implements BillingExportRepository
return BillingExport::reconstitute(
$model->id,
$model->number,
$model->status,
$model->period ?? $model->created_at->format('Y-m'),
$model->created_by,
@@ -43,6 +43,7 @@ class EloquentBillingQueries implements BillingQueries
return $models->map(fn (BillingExportModel $model): ExportSummary => new ExportSummary(
$model->id,
$model->number,
$model->status,
DateTimeImmutable::createFromInterface($model->created_at),
$names[$model->created_by] ?? '',
@@ -4,6 +4,7 @@ namespace App\Modules\Billing\Presentation\Http\Controllers;
use App\Modules\Billing\Application\DTOs\ExportRow;
use App\Modules\Billing\Application\Services\ExportRows;
use App\Modules\Billing\Application\Services\ExportViews;
use App\Modules\Billing\Domain\Enums\LineKind;
use App\Modules\Billing\Domain\Exceptions\BillingExportNotFound;
use App\Modules\Billing\Domain\Exceptions\ExportNotCreated;
@@ -19,7 +20,7 @@ use Symfony\Component\HttpFoundation\StreamedResponse;
*/
class DownloadBillingCsv
{
public function __invoke(int $export, ExportRows $exportRows): StreamedResponse
public function __invoke(int $export, ExportRows $exportRows, ExportViews $exportViews): StreamedResponse
{
Gate::authorize('billing.manage');
@@ -29,6 +30,8 @@ class DownloadBillingCsv
abort(404);
}
$number = $exportViews->of($export)->number ?? abort(404);
return response()->streamDownload(function () use ($rows): void {
$out = fopen('php://output', 'w');
@@ -44,7 +47,7 @@ class DownloadBillingCsv
}
fclose($out);
}, __('billing.csv.file', ['number' => $export]), [
}, __('billing.csv.file', ['number' => $number]), [
'Content-Type' => 'text/csv; charset=UTF-8',
'Cache-Control' => 'no-store, private',
]);
@@ -210,9 +210,19 @@ new class extends Component
];
}
/**
* A draft has no number yet: it is reserved when the material is created.
*/
public function heading(): string
{
$number = $this->export->number;
return $number === null ? __('billing.export.draft_title') : __('billing.export.title', ['number' => $number]);
}
public function render(): mixed
{
return $this->view()->title(__('billing.export.title', ['number' => $this->exportId]));
return $this->view()->title($this->heading());
}
/**
@@ -308,7 +318,7 @@ new class extends Component
<div class="space-y-2">
<p><x-link :href="route('billing.index')" navigate>{{ __('billing.export.back') }}</x-link></p>
<h1 class="text-2xl font-semibold">{{ __('billing.export.title', ['number' => $export->id]) }}</h1>
<h1 class="text-2xl font-semibold">{{ $this->heading() }}</h1>
<p class="flex items-center gap-2 text-sm">
@if ($export->isDraft())
<x-icon name="pencil-square" class="size-5 shrink-0" aria-hidden="true" />
@@ -106,7 +106,7 @@ new class extends Component
@else
<x-table :headers="$this->headers()" :rows="$this->exports">
@interact('column_number', $export)
<x-link :href="route('billing.export', $export->id)" navigate>{{ __('billing.index.export_label', ['number' => $export->id]) }}</x-link>
<x-link :href="route('billing.export', $export->id)" navigate>{{ $export->number === null ? __('billing.index.draft_label') : __('billing.index.export_label', ['number' => $export->number]) }}</x-link>
@endinteract
@interact('column_status', $export)
@@ -142,7 +142,7 @@ new class extends Component
@interact('column_file', $export)
@if ($export->status->value === 'created')
<x-link :href="route('billing.csv', $export->id)" icon="arrow-down-tray" :aria-label="__('billing.csv.download_named', ['number' => $export->id])">{{ __('billing.csv.download_short') }}</x-link>
<x-link :href="route('billing.csv', $export->id)" icon="arrow-down-tray" :aria-label="__('billing.csv.download_named', ['number' => $export->number])">{{ __('billing.csv.download_short') }}</x-link>
@endif
@endinteract
</x-table>
@@ -16,6 +16,7 @@ final readonly class CompanyDetails
public ?string $address = null,
public ?string $phone = null,
public ?string $email = null,
public ?string $menuName = null,
) {}
/**
@@ -25,6 +25,7 @@ final class CompanyDetailsStore
'address' => 'shared.company_address',
'phone' => 'shared.company_phone',
'email' => 'shared.company_email',
'menu_name' => 'shared.company_menu_name',
];
public function __construct(private Settings $settings) {}
@@ -47,6 +48,7 @@ final class CompanyDetailsStore
$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']),
);
}
@@ -72,6 +74,15 @@ final class CompanyDetailsStore
$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
@@ -16,6 +16,8 @@ new class extends Component
{
public string $name = '';
public string $menu_name = '';
public string $business_id = '';
public string $address = '';
@@ -35,6 +37,7 @@ new class extends Component
if ($details !== null) {
$this->name = $details->name;
$this->menu_name = $details->menuName ?? '';
$this->business_id = $details->businessId;
$this->address = $details->address ?? '';
$this->phone = $details->phone ?? '';
@@ -49,6 +52,7 @@ new class extends Component
$this->validate([
'name' => ['required', 'string', 'max:255'],
'menu_name' => ['nullable', 'string', 'max:100'],
'business_id' => ['required', 'string', 'max:20'],
'address' => ['nullable', 'string', 'max:500'],
'phone' => ['nullable', 'string', 'max:50'],
@@ -56,6 +60,7 @@ new class extends Component
], [
'name.required' => __('company.errors.name'),
'name.max' => __('company.errors.name'),
'menu_name.max' => __('company.errors.menu_name'),
'business_id.required' => __('company.errors.business_id'),
'business_id.max' => __('company.errors.business_id'),
'address.max' => __('company.errors.address'),
@@ -64,7 +69,7 @@ new class extends Component
]);
try {
$store->save(new CompanyDetails($this->name, $this->business_id, $this->address, $this->phone, $this->email));
$store->save(new CompanyDetails($this->name, $this->business_id, $this->address, $this->phone, $this->email, $this->menu_name));
} catch (CompanyNameRequired) {
$this->addError('name', __('company.errors.name'));
@@ -100,6 +105,7 @@ new class extends Component
<form wire:submit="save" class="space-y-6" novalidate>
<x-input wire:model="name" :label="__('company.fields.name')" autocomplete="organization" required />
<x-input wire:model="menu_name" :label="__('company.fields.menu_name')" :hint="__('company.fields.menu_name_hint')" autocomplete="off" />
<x-input wire:model="business_id" :label="__('company.fields.business_id')" :hint="__('company.fields.business_id_hint')" autocomplete="off" required />
<x-textarea wire:model="address" :label="__('company.fields.address')" :hint="__('company.fields.address_hint')" rows="3" autocomplete="street-address" />
<x-input wire:model="phone" type="tel" :label="__('company.fields.phone')" autocomplete="tel" />