AccountingNext/Schaad.Accounting.UI/Components/Pages/TextCleanup.razor
Claudio Schaad c9a25481f2 Add "Texte bereinigen" page for cleaning booking texts (#8)
## Summary
A new page **Stammdaten → Texte bereinigen** (`/text-cleanup?acc=<number>`) to view and clean the booking texts of one account, e.g. "Ferien", in the year selected in the header.

Scope, as agreed: one account, the selected year, **only the `Text` field**, only existing bookings. Booking rules and the booking-text list are not touched.

### Page
- **Account dropdown**, filter, and a table of all bookings of the account (both sides, so refunds are negative): date, amount, counter account, the **editable text**, and the bank text as a read-only hint.
- **Group panel:** the texts grouped by first word, exactly as the Analyse page groups them, with count and total. It updates live while editing, and clicking a group filters the table.
- **Tools, acting on the rows currently shown** (the filter limits their reach):
  - **Search and replace:** plain text, or regex with `$1` groups. For example `^(.+) Luzern$` → `Luzern $1` turns "Migros Luzern" into "Luzern Migros".
  - **Leerzeichen bereinigen:** trims and collapses repeated spaces.
  - **Several rows at once:** checkboxes plus "Text setzen" give selected rows one text.
- **Changes:** changed rows are highlighted, with the old text crossed out underneath. Nothing is written until **Speichern (n)**; **Verwerfen** discards.
- **Unsaved changes:** leaving the page asks first (same `unsaved-guard.js` as the JSON editor).
- **Entry points:** the menu, a new button per account on **Konten**, and a "Texte <Jahr> bereinigen" link on the **Analyse** page at account level.

### Saving
`RawDataRepository.SaveTransactionTextsAsync` changes only the `Text` of the given bookings (by ID):
- refused if the file changed since loading (SHA-256), if a text is empty, or if a booking no longer exists;
- the previous version is backed up to `backup/{mandator}/{year}/json-{timestamp}/`, then saved atomically through `BaseRepository`, so cached data is refreshed;
- the file is read once, so the backup is exactly the version that was checked.

`TextReplacement` (Services) holds the search and replace logic:
- plain search is literal, including a `$` in the replacement;
- regex has a 500 ms timeout against runaway patterns;
- invalid patterns give a German error message.

### Layout fix (app-wide)
The content area (`.body-content`) scrolls its own content and was only as tall as the page, so on short pages open dropdowns were cut off after a line or two. It now fills at least the window between header and footer (`app.css`). On short pages the footer therefore sits at the bottom of the window. The account list is also limited to 360 px and scrolls.

## Test plan
- [x] `dotnet build`: no warnings
- [x] `dotnet test`: 111/111 pass, including 13 new tests:
  - 5 `RawDataRepositoryTestShould`: load with hash, only Text changes plus backup, empty text refused, unknown booking refused, changed file refused
  - 8 `TextReplacementTestShould`: case handling, literal special characters, regex groups, no match, invalid regex, empty search, tidy
- [x] Headless Chrome on a copy of the real data, Ferien account (21 bookings in 2026):
  - "Mailand" → "Milano" via replace, 1 direct edit, and 3 rows set via "Text setzen"; the group panel showed "Milano" before saving
  - after saving, exactly those 7 texts changed on disk, no other field of any booking, and a backup was written
- [x] Opening the page without an account: hint shown, no render error. An early `return` inside an element first caused "A frame of type 'Element' was left unclosed"; fixed and checked.
- [x] Account dropdown on the empty page opens fully (360 px, scrollable) within the content area
- [x] Links from Konten and Analyse open the page with the account preselected

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Reviewed-on: #8
Co-authored-by: Claudio Schaad <c.schaad@pog.ch>
Co-committed-by: Claudio Schaad <c.schaad@pog.ch>
2026-10-22 18:32:23 +00:00

148 lines
7.4 KiB
Text

@page "/text-cleanup"
@rendermode InteractiveServer
<PageTitle>Texte bereinigen</PageTitle>
@if (snapshot is null)
{
<Loading/>
return;
}
<div class="cleanup">
<div class="toolbar">
<FluentSelect TOption="Account" Label="Konto" Width="280px" Height="360px"
Items="@accounts"
OptionText="@AccountLabel"
OptionValue="@(a => a.Number.ToString())"
Placeholder="Konto wählen…"
SelectedOption="@account"
SelectedOptionChanged="@OnAccountChanged"/>
<FluentSearch @bind-Value="filter" Immediate="true" Placeholder="Filter…" Style="width: 260px;"/>
<span class="year">Jahr @Year</span>
<span class="spacer"></span>
<FluentButton Appearance="Appearance.Accent" IconStart="@(new Icons.Regular.Size16.Save())"
Disabled="@(Dirty == false || saving)" Loading="@saving" OnClick="SaveAsync">
Speichern@(Dirty ? $" ({edits.Count})" : "")
</FluentButton>
<FluentButton Appearance="Appearance.Neutral" Disabled="@(Dirty == false)" OnClick="DiscardAsync">Verwerfen</FluentButton>
</div>
@* No early return here: it would leave this <div> unclosed (invalid render tree). *@
@if (account is null)
{
<FluentMessageBar Intent="MessageIntent.Info" AllowDismiss="false">Bitte ein Konto wählen.</FluentMessageBar>
}
else
{
<div class="tools">
<FluentTextField @bind-Value="find" Placeholder="Suchen" Style="width: 220px;" aria-label="Suchen"/>
<FluentTextField @bind-Value="replacement" Placeholder="Ersetzen durch" Style="width: 220px;" aria-label="Ersetzen durch"/>
<FluentCheckbox @bind-Value="useRegex" Label="Regex"/>
<FluentCheckbox @bind-Value="matchCase" Label="Gross/klein"/>
<FluentButton Appearance="Appearance.Neutral" IconStart="@(new Icons.Regular.Size16.ArrowSwap())"
OnClick="ReplaceAsync" title="In den angezeigten Texten ersetzen">Ersetzen</FluentButton>
<FluentButton Appearance="Appearance.Lightweight" OnClick="TidyAsync"
title="Leerzeichen am Anfang/Ende entfernen, doppelte zusammenfassen">Leerzeichen bereinigen</FluentButton>
</div>
@if (selected.Count > 0)
{
<div class="tools bulk">
<span>@selected.Count ausgewählt:</span>
<FluentTextField @bind-Value="bulkText" Placeholder="Neuer Text" Style="width: 260px;" aria-label="Neuer Text für die ausgewählten Buchungen"/>
<FluentButton Appearance="Appearance.Neutral" OnClick="SetSelectedTextAsync">Text setzen</FluentButton>
<FluentButton Appearance="Appearance.Lightweight" OnClick="@(() => selected.Clear())">Auswahl aufheben</FluentButton>
</div>
}
@if (toolMessage is not null)
{
<div class="tool-message @(toolMessageIsError ? "error" : "")">@toolMessage</div>
}
@if (saveErrors.Count > 0)
{
<FluentMessageBar Intent="MessageIntent.Error" AllowDismiss="false" Title="Nicht gespeichert">
@foreach (var error in saveErrors)
{
<div>@error</div>
}
</FluentMessageBar>
}
<div class="main">
<FluentCard class="groups">
<div class="card-title">Gruppen (wie in der Analyse)</div>
<table>
<tbody>
@foreach (var group in Groups)
{
<tr @key="group.Name">
<td class="name">
<button type="button" class="link" title="Nach dieser Gruppe filtern" @onclick="@(() => FilterByGroup(group.Name))">@(string.IsNullOrEmpty(group.Name) ? "(ohne Text)" : group.Name)</button>
</td>
<td class="count">@group.Count</td>
<td class="amount">@Amount(group.Total)</td>
</tr>
}
</tbody>
</table>
</FluentCard>
<FluentCard class="rows">
<div class="card-title">
@AccountLabel(account): @VisibleRows.Count() von @rows.Count Buchungen@(Dirty ? $", {edits.Count} geändert" : "")
</div>
<div class="table-scroll">
<table class="bookings">
<thead>
<tr>
<th class="check">
<input type="checkbox" aria-label="Alle angezeigten auswählen" checked="@AllVisibleSelected"
@onchange="@(e => ToggleAllVisible((bool)e.Value!))"/>
</th>
<th>Datum</th>
<th class="amount">Betrag</th>
<th>Gegenkonto</th>
<th class="text">Text</th>
<th>Banktext</th>
</tr>
</thead>
<tbody>
@foreach (var row in VisibleRows)
{
var t = row.Transaction;
<tr @key="t.Id" class="@(IsChanged(t) ? "changed" : "")">
<td class="check">
<input type="checkbox" aria-label="Auswählen" checked="@selected.Contains(t.Id)"
@onchange="@(e => ToggleSelected(t.Id, (bool)e.Value!))"/>
</td>
<td class="date">@t.ValueDate.ToString("dd.MM.yyyy")</td>
<td class="amount">@Amount(row.Amount)</td>
<td class="counter" title="@row.CounterAccount">@row.CounterAccount</td>
<td class="text">
<input type="text" value="@TextOf(t)" aria-label="Text"
@onchange="@(e => SetTextAsync(t, e.Value?.ToString() ?? ""))"/>
@if (IsChanged(t))
{
<div class="original" title="Bisheriger Text">@t.Text</div>
}
</td>
<td class="bank" title="@t.BankTransactionText">@t.BankTransactionText</td>
</tr>
}
</tbody>
</table>
</div>
</FluentCard>
</div>
<div class="hint">
Ersetzen und „Leerzeichen bereinigen“ wirken auf die angezeigten Buchungen; mit dem Filter lässt sich das eingrenzen.
Regex: z.B. Suchen <code>^(.+) Luzern$</code>, Ersetzen <code>Luzern $1</code> macht aus „Migros Luzern“ „Luzern Migros“.
Gespeichert wird nur der Text; die bisherige Datei wird vorher unter backup/… gesichert.
</div>
}
</div>