@if (IsAssets)
diff --git a/Schaad.Accounting.UI/Components/Pages/Analytics/Analytics.razor.css b/Schaad.Accounting.UI/Components/Pages/Analytics/Analytics.razor.css
index db84fda..ddb6022 100644
--- a/Schaad.Accounting.UI/Components/Pages/Analytics/Analytics.razor.css
+++ b/Schaad.Accounting.UI/Components/Pages/Analytics/Analytics.razor.css
@@ -312,3 +312,14 @@
background: #ffffff;
}
}
+
+.crumbs {
+ display: flex;
+ flex-wrap: wrap;
+ align-items: center;
+ gap: 6px 16px;
+}
+
+.cleanup-link {
+ font-size: 13px;
+}
diff --git a/Schaad.Accounting.UI/Components/Pages/TextCleanup.razor b/Schaad.Accounting.UI/Components/Pages/TextCleanup.razor
new file mode 100644
index 0000000..7d9ae97
--- /dev/null
+++ b/Schaad.Accounting.UI/Components/Pages/TextCleanup.razor
@@ -0,0 +1,148 @@
+@page "/text-cleanup"
+@rendermode InteractiveServer
+
+
+
+
+
+ Jahr @Year
+
+
+ Speichern@(Dirty ? $" ({edits.Count})" : "")
+
+ Verwerfen
+
+
+ @* No early return here: it would leave this
unclosed (invalid render tree). *@
+ @if (account is null)
+ {
+
Bitte ein Konto wählen.
+ }
+ else
+ {
+
+
+
+
+
+
+ Ersetzen
+ Leerzeichen bereinigen
+
+
+ @if (selected.Count > 0)
+ {
+
+ @selected.Count ausgewählt:
+
+ Text setzen
+ Auswahl aufheben
+
+ }
+
+ @if (toolMessage is not null)
+ {
+
@toolMessage
+ }
+
+ @if (saveErrors.Count > 0)
+ {
+
+ @foreach (var error in saveErrors)
+ {
+ @error
+ }
+
+ }
+
+
+
+ Gruppen (wie in der Analyse)
+
+
+ @foreach (var group in Groups)
+ {
+
+
+ FilterByGroup(group.Name))">@(string.IsNullOrEmpty(group.Name) ? "(ohne Text)" : group.Name)
+
+ @group.Count
+ @Amount(group.Total)
+
+ }
+
+
+
+
+
+
+ @AccountLabel(account): @VisibleRows.Count() von @rows.Count Buchungen@(Dirty ? $", {edits.Count} geändert" : "")
+
+
+
+
+
+
+ Ersetzen und „Leerzeichen bereinigen“ wirken auf die angezeigten Buchungen; mit dem Filter lässt sich das eingrenzen.
+ Regex: z.B. Suchen ^(.+) Luzern$, Ersetzen Luzern $1 macht aus „Migros Luzern“ „Luzern Migros“.
+ Gespeichert wird nur der Text; die bisherige Datei wird vorher unter backup/… gesichert.
+
+ }
+
diff --git a/Schaad.Accounting.UI/Components/Pages/TextCleanup.razor.cs b/Schaad.Accounting.UI/Components/Pages/TextCleanup.razor.cs
new file mode 100644
index 0000000..4f55763
--- /dev/null
+++ b/Schaad.Accounting.UI/Components/Pages/TextCleanup.razor.cs
@@ -0,0 +1,341 @@
+using System.Globalization;
+using System.Text.RegularExpressions;
+using Microsoft.AspNetCore.Components;
+using Microsoft.FluentUI.AspNetCore.Components;
+using Microsoft.JSInterop;
+using Schaad.Accounting.Datasets;
+using Schaad.Accounting.Interfaces;
+using Schaad.Accounting.Models;
+using Schaad.Accounting.Services;
+
+namespace Schaad.Accounting.UI.Components.Pages;
+
+///
+/// Shows the booking texts of one account in the selected year and lets the user clean them:
+/// edit single texts, search and replace (plain or regex) over the listed rows, tidy spaces, or
+/// give several rows the same text. Only the Text field changes; saving goes through
+/// (change check, backup).
+///
+public partial class TextCleanup : ComponentBase, IAsyncDisposable
+{
+ private static readonly CultureInfo SwissNumbers = CreateSwissNumbers();
+
+ [Inject]
+ private IRawDataRepository rawDataRepository { get; set; } = null!;
+
+ [Inject]
+ private IAccountRepository accountRepository { get; set; } = null!;
+
+ [Inject]
+ private ISettingsService settingsService { get; set; } = null!;
+
+ [Inject]
+ private NavigationManager navigation { get; set; } = null!;
+
+ [Inject]
+ private IDialogService dialogService { get; set; } = null!;
+
+ [Inject]
+ private IToastService toastService { get; set; } = null!;
+
+ [Inject]
+ private IJSRuntime js { get; set; } = null!;
+
+ [SupplyParameterFromQuery(Name = "acc")]
+ public int? AccountNumber { get; set; }
+
+ private record Row(Transaction Transaction, decimal Amount, string CounterAccount);
+
+ private record Group(string Name, int Count, decimal Total);
+
+ private IReadOnlyList
accounts = [];
+ private Dictionary accountsById = [];
+ private Account? account;
+ private TransactionSnapshot? snapshot;
+ private List rows = [];
+
+ // Pending edits: transaction id → new text (only entries that differ from the stored text).
+ private readonly Dictionary edits = [];
+ private readonly HashSet selected = [];
+
+ private string filter = "";
+ private string find = "";
+ private string replacement = "";
+ private bool useRegex;
+ private bool matchCase;
+ private string bulkText = "";
+ private string? toolMessage;
+ private bool toolMessageIsError;
+ private IReadOnlyList saveErrors = [];
+ private bool saving;
+ private IJSObjectReference? unsavedGuard;
+
+ protected override async Task OnParametersSetAsync()
+ {
+ if (accounts.Count == 0)
+ {
+ accounts = (await accountRepository.GetAccountListAsync()).OrderBy(a => a.Number).ToList();
+ accountsById = accounts.Where(a => a.Id != null).ToDictionary(a => a.Id);
+ }
+
+ var requested = accounts.FirstOrDefault(a => a.Number == AccountNumber);
+ if (requested?.Number == account?.Number && snapshot is not null)
+ {
+ return;
+ }
+ account = requested;
+ await LoadAsync();
+ }
+
+ protected override async Task OnAfterRenderAsync(bool firstRender)
+ {
+ if (firstRender)
+ {
+ unsavedGuard = await js.InvokeAsync("import", "./js/unsaved-guard.js");
+ await UpdateGuardAsync();
+ }
+ }
+
+ private async Task LoadAsync()
+ {
+ snapshot = await rawDataRepository.LoadTransactionsAsync();
+ rows = account is null
+ ? []
+ : snapshot.Transactions
+ .Where(t => t.TargetAccountId == account.Id || t.OriginAccountId == account.Id)
+ .OrderBy(t => t.ValueDate)
+ .Select(t =>
+ {
+ var isTarget = t.TargetAccountId == account.Id;
+ var counterId = isTarget ? t.OriginAccountId : t.TargetAccountId;
+ var counter = accountsById.TryGetValue(counterId ?? "", out var c) ? $"{c.Number} {c.Name}" : counterId ?? "";
+ // From the account's point of view: bookings to it count positive.
+ return new Row(t, isTarget ? t.Value : -t.Value, counter);
+ })
+ .ToList();
+ edits.Clear();
+ selected.Clear();
+ saveErrors = [];
+ toolMessage = null;
+ await UpdateGuardAsync();
+ }
+
+ private bool Dirty => edits.Count > 0;
+
+ private string TextOf(Transaction transaction) => edits.GetValueOrDefault(transaction.Id) ?? transaction.Text ?? "";
+
+ private bool IsChanged(Transaction transaction) => edits.ContainsKey(transaction.Id);
+
+ private async Task SetTextAsync(Transaction transaction, string text)
+ {
+ if (text == (transaction.Text ?? ""))
+ {
+ edits.Remove(transaction.Id);
+ }
+ else
+ {
+ edits[transaction.Id] = text;
+ }
+ await UpdateGuardAsync();
+ }
+
+ private IEnumerable VisibleRows => string.IsNullOrWhiteSpace(filter)
+ ? rows
+ : rows.Where(r => Contains(TextOf(r.Transaction), filter)
+ || Contains(r.Transaction.Text, filter)
+ || Contains(r.Transaction.BankTransactionText, filter)
+ || Contains(r.CounterAccount, filter));
+
+ private static bool Contains(string? text, string part) => text?.Contains(part, StringComparison.OrdinalIgnoreCase) == true;
+
+ // Same grouping as the Analyse page: by the first word of the text.
+ private IReadOnlyList Groups => rows
+ .GroupBy(r => TransactionDataset.GetTextGroup(TextOf(r.Transaction)) ?? "")
+ .Select(g => new Group(g.Key, g.Count(), g.Sum(r => r.Amount)))
+ .OrderByDescending(g => Math.Abs(g.Total))
+ .ToList();
+
+ private void FilterByGroup(string group) => filter = group;
+
+ private async Task ReplaceAsync()
+ {
+ var replacer = TextReplacement.Create(find, replacement, useRegex, matchCase, out var error);
+ if (replacer is null)
+ {
+ ShowToolMessage(error!, isError: true);
+ return;
+ }
+ try
+ {
+ var changed = await ChangeVisibleAsync(replacer.Apply);
+ ShowToolMessage(changed == 0 ? "Kein Text gefunden." : $"{changed} Texte geändert (noch nicht gespeichert).", isError: false);
+ }
+ catch (RegexMatchTimeoutException)
+ {
+ ShowToolMessage("Der reguläre Ausdruck braucht zu lange; bitte vereinfachen.", isError: true);
+ }
+ }
+
+ private async Task TidyAsync()
+ {
+ var changed = await ChangeVisibleAsync(TextReplacement.Tidy);
+ ShowToolMessage(changed == 0 ? "Keine überflüssigen Leerzeichen gefunden." : $"{changed} Texte bereinigt (noch nicht gespeichert).", isError: false);
+ }
+
+ private async Task ChangeVisibleAsync(Func change)
+ {
+ var changed = 0;
+ foreach (var row in VisibleRows.ToList())
+ {
+ var current = TextOf(row.Transaction);
+ var updated = change(current);
+ if (updated != current)
+ {
+ await SetTextAsync(row.Transaction, updated);
+ changed++;
+ }
+ }
+ return changed;
+ }
+
+ private void ToggleSelected(string id, bool on)
+ {
+ if (on)
+ {
+ selected.Add(id);
+ }
+ else
+ {
+ selected.Remove(id);
+ }
+ }
+
+ private bool AllVisibleSelected => VisibleRows.Any() && VisibleRows.All(r => selected.Contains(r.Transaction.Id));
+
+ private void ToggleAllVisible(bool on)
+ {
+ foreach (var row in VisibleRows)
+ {
+ ToggleSelected(row.Transaction.Id, on);
+ }
+ }
+
+ private async Task SetSelectedTextAsync()
+ {
+ if (string.IsNullOrWhiteSpace(bulkText))
+ {
+ ShowToolMessage("Bitte einen Text eingeben.", isError: true);
+ return;
+ }
+ foreach (var row in rows.Where(r => selected.Contains(r.Transaction.Id)))
+ {
+ await SetTextAsync(row.Transaction, bulkText.Trim());
+ }
+ ShowToolMessage($"{selected.Count} Texte gesetzt (noch nicht gespeichert).", isError: false);
+ selected.Clear();
+ }
+
+ private void ShowToolMessage(string message, bool isError)
+ {
+ toolMessage = message;
+ toolMessageIsError = isError;
+ }
+
+ private async Task SaveAsync()
+ {
+ if (snapshot is null || Dirty == false)
+ {
+ return;
+ }
+ saving = true;
+ try
+ {
+ var count = edits.Count;
+ var result = await rawDataRepository.SaveTransactionTextsAsync(new Dictionary(edits), snapshot.Hash);
+ saveErrors = result.Errors;
+ if (result.Saved)
+ {
+ await LoadAsync();
+ toastService.ShowToast(ToastIntent.Success, $"{count} Texte gespeichert. Vorherige Version: {result.BackupPath}", 6000);
+ }
+ }
+ finally
+ {
+ saving = false;
+ }
+ }
+
+ private async Task DiscardAsync()
+ {
+ if (Dirty && await ConfirmDiscardAsync() == false)
+ {
+ return;
+ }
+ await LoadAsync();
+ }
+
+ private async Task OnAccountChanged(Account? chosen)
+ {
+ if (chosen is null || chosen.Number == account?.Number)
+ {
+ return;
+ }
+ if (Dirty)
+ {
+ if (await ConfirmDiscardAsync() == false)
+ {
+ StateHasChanged();
+ return;
+ }
+ edits.Clear();
+ await UpdateGuardAsync();
+ }
+ filter = "";
+ navigation.NavigateTo(navigation.GetUriWithQueryParameter("acc", chosen.Number));
+ }
+
+ private async Task ConfirmDiscardAsync()
+ {
+ var dialog = await dialogService.ShowConfirmationAsync(
+ $"{edits.Count} geänderte Texte sind noch nicht gespeichert. Verwerfen?", "Verwerfen", "Abbrechen", "Ungespeicherte Änderungen");
+ return (await dialog.Result).Cancelled == false;
+ }
+
+ // Asks before leaving the page while there are unsaved edits (see unsaved-guard.js).
+ private async Task UpdateGuardAsync()
+ {
+ if (unsavedGuard is not null)
+ {
+ await unsavedGuard.InvokeVoidAsync("set", Dirty ? $"{edits.Count} geänderte Texte sind noch nicht gespeichert. Seite trotzdem verlassen?" : null);
+ }
+ }
+
+ public async ValueTask DisposeAsync()
+ {
+ try
+ {
+ if (unsavedGuard is not null)
+ {
+ await unsavedGuard.InvokeVoidAsync("set", (object?)null);
+ await unsavedGuard.DisposeAsync();
+ }
+ }
+ catch (JSDisconnectedException)
+ {
+ // Circuit gone; the page and its guard are gone with it.
+ }
+ }
+
+ private static string AccountLabel(Account a) => $"{a.Number} {a.Name}";
+
+ private static string Amount(decimal value) => value.ToString("#,0.00", SwissNumbers);
+
+ private int Year => settingsService.GetYear();
+
+ private static CultureInfo CreateSwissNumbers()
+ {
+ var culture = new CultureInfo("de-CH");
+ culture.NumberFormat.NumberGroupSeparator = "'";
+ return culture;
+ }
+}
diff --git a/Schaad.Accounting.UI/Components/Pages/TextCleanup.razor.css b/Schaad.Accounting.UI/Components/Pages/TextCleanup.razor.css
new file mode 100644
index 0000000..5446824
--- /dev/null
+++ b/Schaad.Accounting.UI/Components/Pages/TextCleanup.razor.css
@@ -0,0 +1,174 @@
+.cleanup {
+ display: flex;
+ flex-direction: column;
+ gap: 10px;
+}
+
+.toolbar,
+.tools {
+ display: flex;
+ flex-wrap: wrap;
+ align-items: flex-end;
+ gap: 8px;
+}
+
+.tools {
+ align-items: center;
+}
+
+.tools.bulk {
+ padding: 6px 10px;
+ background: #ebf3fc;
+ border-radius: 4px;
+}
+
+.year {
+ color: #52514e;
+ padding-bottom: 6px;
+}
+
+.spacer {
+ flex: 1;
+}
+
+.tool-message {
+ color: #107c10;
+ font-size: 13px;
+}
+
+.tool-message.error {
+ color: #c50f1f;
+}
+
+.main {
+ display: grid;
+ grid-template-columns: 260px minmax(0, 1fr);
+ gap: 12px;
+ align-items: start;
+}
+
+@media (max-width: 900px) {
+ .main {
+ grid-template-columns: minmax(0, 1fr);
+ }
+}
+
+::deep .groups,
+::deep .rows {
+ padding: 12px;
+ min-width: 0;
+}
+
+.card-title {
+ font-weight: 600;
+ margin-bottom: 8px;
+}
+
+.groups table {
+ width: 100%;
+ border-collapse: collapse;
+ font-size: 13px;
+}
+
+.groups td {
+ padding: 3px 2px;
+ border-bottom: 1px solid #f0efea;
+}
+
+.groups .name {
+ overflow-wrap: anywhere;
+}
+
+.groups .count,
+.amount {
+ text-align: right;
+ white-space: nowrap;
+ font-variant-numeric: tabular-nums;
+}
+
+.link {
+ border: none;
+ background: none;
+ padding: 0;
+ font: inherit;
+ color: #0f6cbd;
+ text-align: left;
+ cursor: pointer;
+}
+
+.link:hover,
+.link:focus-visible {
+ text-decoration: underline;
+}
+
+.table-scroll {
+ overflow-x: auto;
+}
+
+.bookings {
+ width: 100%;
+ border-collapse: collapse;
+ font-size: 13px;
+}
+
+.bookings th {
+ text-align: left;
+ color: #52514e;
+ font-weight: 600;
+ font-size: 12px;
+ padding: 4px 6px;
+ border-bottom: 1px solid #e1e0d9;
+}
+
+.bookings td {
+ padding: 3px 6px;
+ border-bottom: 1px solid #f0efea;
+ vertical-align: top;
+}
+
+.bookings .check {
+ width: 24px;
+}
+
+.bookings .date {
+ white-space: nowrap;
+}
+
+.bookings .counter,
+.bookings .bank {
+ max-width: 220px;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ color: #52514e;
+}
+
+.bookings .text {
+ min-width: 240px;
+}
+
+.bookings .text input {
+ width: 100%;
+ box-sizing: border-box;
+ font: inherit;
+ padding: 3px 6px;
+ border: 1px solid #d1d1d1;
+ border-radius: 3px;
+}
+
+.bookings tr.changed .text input {
+ border-color: #0f6cbd;
+ background: #ebf3fc;
+}
+
+.original {
+ color: #898781;
+ font-size: 11px;
+ text-decoration: line-through;
+ padding: 1px 2px;
+}
+
+.hint {
+ color: #52514e;
+ font-size: 12px;
+}
diff --git a/Schaad.Accounting.UI/wwwroot/app.css b/Schaad.Accounting.UI/wwwroot/app.css
index 1d2bc9a..a5c5ed9 100644
--- a/Schaad.Accounting.UI/wwwroot/app.css
+++ b/Schaad.Accounting.UI/wwwroot/app.css
@@ -20,6 +20,13 @@ a, .btn-link {
padding-top: 1.1rem;
}
+/* The content area scrolls its own content (overflow: auto) and was only as tall as the page.
+ On short pages that cut off open dropdowns after a line or two; filling the window below the
+ header (50px) and above the footer (46px) leaves them room. */
+.body-content {
+ min-height: calc(100vh - 96px);
+}
+
h1:focus {
outline: none;
}