public async Task CalculateAndShowSummaryAsync(IList <IPosting> allPostings, IList <IPostingClassification> postingClassifications) { var errorsAndInfos = new ErrorsAndInfos(); var fairPostings = allPostings.Where(p => postingClassifications.FirstOrDefault(c => PostingClassificationMatcher.DoesPostingMatchClassification(p, c))?.Unfair != true).ToList(); var pureDebitCreditAggregation = PostingAggregator.AggregatePostings(fairPostings, new List <IPostingClassification> { new PostingClassification { Credit = false, Clue = "", Classification = "Debit" }, new PostingClassification { Credit = true, Clue = "", Classification = "Credit" } }, errorsAndInfos); if (errorsAndInfos.AnyErrors()) { await DataPresenter.WriteErrorsAsync(errorsAndInfos); return; } var overallSumList = pureDebitCreditAggregation.Select( result => new TypeItemSum { Type = result.Key.Sign, Item = result.Key.Classification, Sum = result.Value } ).Cast <ICollectionViewSourceEntity>().ToList(); await DataPresenter.Handlers.OverallSumsHandler.CollectionChangedAsync(overallSumList); errorsAndInfos = new ErrorsAndInfos(); var detailedAggregation = PostingAggregator.AggregatePostings(allPostings, postingClassifications, errorsAndInfos).OrderBy(result => result.Key.CombinedClassification).ToList(); if (errorsAndInfos.AnyErrors()) { await DataPresenter.WriteErrorsAsync(errorsAndInfos); return; } if (detailedAggregation.Any()) { var classificationSumList = detailedAggregation.Select( result => new TypeItemSum { Type = result.Key.Sign, Item = result.Key.Classification, Sum = result.Value } ).Cast <ICollectionViewSourceEntity>().ToList(); await DataPresenter.Handlers.ClassificationSumsHandler.CollectionChangedAsync(classificationSumList); } }
public async Task CalculateAndShowAverageAsync(IList <IPosting> allPostings, IList <IPostingClassification> postingClassifications) { var errorsAndInfos = new ErrorsAndInfos(); var detailedAggregation = PostingAggregator.AggregatePostings(allPostings, postingClassifications, errorsAndInfos).OrderBy(result => result.Key.CombinedClassification).ToList(); if (errorsAndInfos.AnyErrors()) { await DataPresenter.WriteErrorsAsync(errorsAndInfos); return; } var thisYear = allPostings.Max(p => p.Date.Year); var lastYearsPostings = allPostings.Where(p => p.Date.Year < thisYear).ToList(); var lastYearsDetailedAggregation = PostingAggregator.AggregatePostings(lastYearsPostings, postingClassifications, errorsAndInfos).ToList(); if (errorsAndInfos.AnyErrors()) { await DataPresenter.WriteErrorsAsync(errorsAndInfos); return; } var thisYearsPostings = allPostings.Where(p => p.Date.Year == thisYear).ToList(); var thisYearsDetailedAggregation = PostingAggregator.AggregatePostings(thisYearsPostings, postingClassifications, errorsAndInfos).ToList(); if (errorsAndInfos.AnyErrors()) { await DataPresenter.WriteErrorsAsync(errorsAndInfos); return; } var numberOfDistinctMonths = allPostings.Select(p => p.Date.Month * 100 + p.Date.Year).Distinct().Count(); var numberOfDistinctMonthsLastYear = lastYearsPostings.Any() ? lastYearsPostings.Select(p => p.Date.Month * 100 + p.Date.Year).Distinct().Count() : 1; var numberOfDistinctMonthsThisYear = thisYearsPostings.Any() ? thisYearsPostings.Select(p => p.Date.Month * 100 + p.Date.Year).Distinct().Count() : 1; var classificationAverageList = detailedAggregation.Select( result => new TypeItemSum { Type = result.Key.Sign, Item = result.Key.Classification, Sum = result.Value / numberOfDistinctMonths, SumThisYear = GetOtherSum(result.Key, thisYearsDetailedAggregation) / numberOfDistinctMonthsThisYear, SumLastYear = GetOtherSum(result.Key, lastYearsDetailedAggregation) / numberOfDistinctMonthsLastYear } ).Cast <ICollectionViewSourceEntity>().ToList(); await DataPresenter.Handlers.ClassificationAveragesHandler.CollectionChangedAsync(classificationAverageList); }
public async Task CalculateAndShowMonthlyDeltaAsync(IList <IPosting> allPostings, IList <IPostingClassification> postingClassifications) { var fairPostings = allPostings.Where(p => postingClassifications.FirstOrDefault(c => PostingClassificationMatcher.DoesPostingMatchClassification(p, c))?.Unfair != true).ToList(); var minYear = fairPostings.Min(p => p.Date.Year); var years = Enumerable.Range(minYear, DateTime.Today.Year - minYear + 1); var monthsClassifications = new List <IPostingClassification>(); foreach (var year in years) { for (var month = 1; month <= 12; month++) { var classification = postingClassifications.FirstOrDefault(c => c.Month == month && c.Year == year); if (classification != null) { monthsClassifications.Add(classification); } else { monthsClassifications.Add(new PostingClassification { Month = month, Year = year, Classification = $"Δ {year}-{month:00}" }); } } } var errorsAndInfos = new ErrorsAndInfos(); var monthlyDeltas = PostingAggregator.AggregatePostings(fairPostings, monthsClassifications, errorsAndInfos).OrderByDescending(result => result.Key.CombinedClassification).ToList(); if (errorsAndInfos.AnyErrors()) { await DataPresenter.WriteErrorsAsync(errorsAndInfos); return; } var monthlyDeltasList = monthlyDeltas.Select( result => new TypeMonthDelta { Type = "Δ", Month = result.Key.Classification.Replace("Δ", "").Trim(), Delta = result.Value } ).Cast <ICollectionViewSourceEntity>().ToList(); await DataPresenter.Handlers.MonthlyDeltasHandler.CollectionChangedAsync(monthlyDeltasList); }