public async Task <AmountsViewModel> Fill(Db db, Client client) { var overviewCalculator = new OverviewCalculator(db, client, new SumCalculator(db, client)); var overview = await overviewCalculator.Calculate(); AmountPerMonth = new Dictionary <string, IDictionary <Month, decimal> >(); foreach (var monthPair in overview) { foreach (var categoryPair in monthPair.Value) { if (!AmountPerMonth.ContainsKey(categoryPair.Key)) { AmountPerMonth[categoryPair.Key] = new Dictionary <Month, decimal>(); } if (!AmountPerMonth[categoryPair.Key].ContainsKey(monthPair.Key)) { AmountPerMonth[categoryPair.Key][monthPair.Key] = 0; } AmountPerMonth[categoryPair.Key][monthPair.Key] += categoryPair.Value; } } foreach (var category in AmountPerMonth.Keys.ToList()) { AmountPerMonth[category] = AmountPerMonth[category].OrderBy(x => x.Key.Date).ToDictionary(x => x.Key, x => x.Value); } AmountPerMonth = AmountPerMonth.OrderBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value); return(this); }
public async Task <IndexViewModel> Calculate(Client client, Db db) { var overviewCalculator = new OverviewCalculator(db, client, new SumCalculator(db, client)); var allCategories = (await overviewCalculator.GetCategories()).ToList(); Overview = new OverviewContainer { CategoryHeaders = allCategories }; var overview = await overviewCalculator.Calculate(); foreach (var monthPair in overview) { var monthContainer = new MonthContainer { Year = monthPair.Key.Date.Year, Month = monthPair.Key.Date.Month, Name = monthPair.Key.ToString(), Income = monthPair.Value.Where(x => x.Key == Constants.IncomeCategory).Sum(x => x.Value), Spent = monthPair.Value.Where(x => x.Key != Constants.IncomeCategory).Sum(x => x.Value) }; foreach (var category in allCategories) { monthContainer[category] = monthPair.Value.ContainsKey(category) ? monthPair.Value[category] : 0; } if (monthContainer.Income > 0 || monthContainer.Spent < 0) { Overview.Months.Insert(0, monthContainer); } } return(this); }