refactor(app): implement year filter for account entries and update related services

This commit is contained in:
2026-04-03 14:11:42 +02:00
parent 08185f88cd
commit 4636acf7b0
11 changed files with 196 additions and 42 deletions
@@ -12,7 +12,7 @@ public class BalanceQueryService : IBalanceQueryService
public BalanceQueryService(IDbContextFactory<FinanceDbContext> dbFactory) => _dbFactory = dbFactory;
public async Task<AccountBalanceDto> GetAccountBalanceAsync(int accountId)
public async Task<AccountBalanceDto> GetAccountBalanceAsync(int accountId, int? year = null)
{
await using var db = await _dbFactory.CreateDbContextAsync();
@@ -21,21 +21,33 @@ public class BalanceQueryService : IBalanceQueryService
.FirstOrDefaultAsync(a => a.Id == accountId)
?? throw new InvalidOperationException($"Konto {accountId} nicht gefunden.");
var currentYear = DateTime.Now.Year;
var selectedYear = year ?? DateTime.Now.Year;
var activeEntries = account.Entries.Where(e => !e.IsDeleted).ToList();
var totalIncome = activeEntries.Where(e => e.Type == EntryType.Income).Sum(e => e.Amount);
var totalExpense = activeEntries.Where(e => e.Type == EntryType.Expense).Sum(e => e.Amount);
var currentYearIncome = activeEntries.Where(e => e.Type == EntryType.Income && e.Date.Year == currentYear).Sum(e => e.Amount);
var currentYearExpense = activeEntries.Where(e => e.Type == EntryType.Expense && e.Date.Year == currentYear).Sum(e => e.Amount);
var totalBalance = account.CarryoverBalance + totalIncome - totalExpense;
var selectedYearIncome = activeEntries
.Where(e => e.Type == EntryType.Income && e.Date.Year == selectedYear)
.Sum(e => e.Amount);
var selectedYearExpense = activeEntries
.Where(e => e.Type == EntryType.Expense && e.Date.Year == selectedYear)
.Sum(e => e.Amount);
var selectedYearCarryover = account.CarryoverBalance + activeEntries
.Where(e => e.Date.Year < selectedYear)
.Sum(e => e.Type == EntryType.Income ? e.Amount : -e.Amount);
var totalBalance = year.HasValue
? selectedYearCarryover + selectedYearIncome - selectedYearExpense
: account.CarryoverBalance + totalIncome - totalExpense;
return new AccountBalanceDto(
account.CarryoverBalance,
year.HasValue ? selectedYearCarryover : account.CarryoverBalance,
totalIncome,
totalExpense,
currentYearIncome,
currentYearExpense,
selectedYearIncome,
selectedYearExpense,
totalBalance);
}
}