refactor(app): implement year filter for account entries and update related services
This commit is contained in:
@@ -27,19 +27,25 @@ public class PdfStatementService : IPdfStatementService
|
||||
_settingsService = settingsService;
|
||||
}
|
||||
|
||||
public async Task<byte[]> GenerateStatementAsync(int accountId, bool currentYearOnly)
|
||||
public Task<byte[]> GenerateStatementAsync(int accountId, bool currentYearOnly)
|
||||
{
|
||||
var year = currentYearOnly ? DateTime.Now.Year : (int?)null;
|
||||
return GenerateStatementAsync(accountId, year);
|
||||
}
|
||||
|
||||
public async Task<byte[]> GenerateStatementAsync(int accountId, int? year)
|
||||
{
|
||||
await using var db = await _dbFactory.CreateDbContextAsync();
|
||||
|
||||
var account = await db.Accounts.FindAsync(accountId)
|
||||
?? throw new InvalidOperationException($"Konto {accountId} nicht gefunden.");
|
||||
|
||||
var entries = await _entryService.GetEntriesAsync(accountId, currentYearOnly);
|
||||
var balance = await _balanceQueryService.GetAccountBalanceAsync(accountId);
|
||||
var entries = await _entryService.GetEntriesAsync(accountId, year);
|
||||
var balance = await _balanceQueryService.GetAccountBalanceAsync(accountId, year);
|
||||
var clubName = await _settingsService.GetClubNameAsync() ?? "Mein Verein";
|
||||
|
||||
var title = currentYearOnly
|
||||
? $"{account.Name} – Auszug {DateTime.Now.Year}"
|
||||
var title = year.HasValue
|
||||
? $"{account.Name} – Auszug {year.Value}"
|
||||
: $"{account.Name} – Gesamtauszug";
|
||||
|
||||
var document = Document.Create(container =>
|
||||
@@ -61,12 +67,6 @@ public class PdfStatementService : IPdfStatementService
|
||||
|
||||
page.Content().PaddingTop(15).Column(col =>
|
||||
{
|
||||
col.Item().PaddingBottom(10).Row(row =>
|
||||
{
|
||||
row.RelativeItem().Text("Übertrag:").SemiBold();
|
||||
row.AutoItem().Text(FormatCurrency(balance.CarryoverBalance)).SemiBold();
|
||||
});
|
||||
|
||||
col.Item().Table(table =>
|
||||
{
|
||||
table.ColumnsDefinition(columns =>
|
||||
@@ -114,9 +114,16 @@ public class PdfStatementService : IPdfStatementService
|
||||
col.Item().PaddingTop(20).LineHorizontal(1).LineColor(Colors.Grey.Lighten2);
|
||||
col.Item().PaddingTop(10).Column(summaryCol =>
|
||||
{
|
||||
SummaryRow(summaryCol, "Übertrag:", balance.CarryoverBalance);
|
||||
SummaryRow(summaryCol, "Einnahmen gesamt:", balance.TotalIncome);
|
||||
SummaryRow(summaryCol, "Ausgaben gesamt:", -balance.TotalExpense);
|
||||
var incomeValue = year.HasValue ? balance.CurrentYearIncome : balance.TotalIncome;
|
||||
var expenseValue = year.HasValue ? balance.CurrentYearExpense : balance.TotalExpense;
|
||||
|
||||
if (year.HasValue)
|
||||
{
|
||||
SummaryRow(summaryCol, $"Übertrag von {year.Value - 1}:", balance.CarryoverBalance);
|
||||
}
|
||||
|
||||
SummaryRow(summaryCol, year.HasValue ? "Einnahmen:" : "Einnahmen gesamt:", incomeValue);
|
||||
SummaryRow(summaryCol, year.HasValue ? "Ausgaben:" : "Ausgaben gesamt:", -expenseValue);
|
||||
summaryCol.Item().PaddingTop(5).Row(row =>
|
||||
{
|
||||
row.RelativeItem().Text("Saldo:").Bold().FontSize(12);
|
||||
@@ -175,6 +182,11 @@ public class PdfStatementService : IPdfStatementService
|
||||
g => g.Key,
|
||||
g => g.Sum(e => e.Type == EntryType.Income ? e.Amount : -e.Amount));
|
||||
|
||||
var balanceByAccountId = accounts
|
||||
.ToDictionary(
|
||||
a => a.Id,
|
||||
a => a.CarryoverBalance + movementByAccountId.GetValueOrDefault(a.Id));
|
||||
|
||||
var totalClubBalance = accounts.Sum(a => a.CarryoverBalance + movementByAccountId.GetValueOrDefault(a.Id));
|
||||
|
||||
var processedTransferIds = new HashSet<int>();
|
||||
@@ -291,6 +303,16 @@ public class PdfStatementService : IPdfStatementService
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add one summary line with the final balance for each account column.
|
||||
FooterCell(table, string.Empty);
|
||||
FooterCell(table, string.Empty);
|
||||
FooterCell(table, "Kontostand", alignRight: true);
|
||||
|
||||
foreach (var account in accounts)
|
||||
{
|
||||
FooterAmountCell(table, balanceByAccountId.GetValueOrDefault(account.Id));
|
||||
}
|
||||
});
|
||||
|
||||
content.Item().PaddingTop(8).AlignRight().Text(text =>
|
||||
@@ -352,6 +374,31 @@ public class PdfStatementService : IPdfStatementService
|
||||
.FontColor(amount >= 0 ? Colors.Green.Darken1 : Colors.Red.Darken1);
|
||||
}
|
||||
|
||||
private static void FooterCell(TableDescriptor table, string text, bool alignRight = false)
|
||||
{
|
||||
var cell = table.Cell()
|
||||
.BorderTop(1)
|
||||
.BorderColor(Colors.Grey.Darken1)
|
||||
.PaddingTop(5)
|
||||
.PaddingBottom(2);
|
||||
|
||||
var content = alignRight ? cell.AlignRight() : cell;
|
||||
content.Text(text).SemiBold();
|
||||
}
|
||||
|
||||
private static void FooterAmountCell(TableDescriptor table, decimal amount)
|
||||
{
|
||||
table.Cell()
|
||||
.BorderTop(1)
|
||||
.BorderColor(Colors.Grey.Darken1)
|
||||
.PaddingTop(5)
|
||||
.PaddingBottom(2)
|
||||
.AlignRight()
|
||||
.Text(FormatCurrency(amount))
|
||||
.SemiBold()
|
||||
.FontColor(amount >= 0 ? Colors.Green.Darken1 : Colors.Red.Darken1);
|
||||
}
|
||||
|
||||
private static int ParseDisplayIdYear(string displayId)
|
||||
{
|
||||
var parts = displayId.Split('-');
|
||||
|
||||
Reference in New Issue
Block a user