예제 #1
0
        public async Task <IList <IPosting> > CollectPostingsAsync(IContainer container, bool isIntegrationTest)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            var allPostings = new List <IPosting>();

            var sourceFolder = await GetSourceFolderAsync(container, isIntegrationTest);

            if (sourceFolder == null)
            {
                return(allPostings);
            }

            var files  = Directory.GetFiles(sourceFolder.FullName, "*.txt").ToList();
            var reader = container.Resolve <ISourceFileReader>();

            foreach (var file in files)
            {
                await DataPresenter.WriteLineAsync($"File: {file}");

                var errorsAndInfos = new ErrorsAndInfos();
                var postings       = reader.ReadPostings(file, errorsAndInfos);
                if (errorsAndInfos.AnyErrors())
                {
                    await DataPresenter.WriteErrorsAsync(errorsAndInfos);

                    return(allPostings);
                }

                await DataPresenter.WriteLineAsync($"{postings.Count} posting/-s found");

                allPostings.AddRange(postings);
            }

            if (!allPostings.Any())
            {
                return(allPostings);
            }

            var maxDate = allPostings.Max(p => p.Date);
            var minDate = new DateTime(maxDate.Year - 1, 1, 1);
            await DataPresenter.WriteLineAsync($"{allPostings.Count(p => p.Date < minDate)} posting/-s removed");

            allPostings.RemoveAll(p => p.Date < minDate);

            return(allPostings);
        }
예제 #2
0
        public async Task CollectAndShowAsync(IContainer container, bool isIntegrationTest)
        {
            var errorsAndInfos   = new ErrorsAndInfos();
            var secretRepository = container.Resolve <ISecretRepository>();

            CalculationLogger.ClearLogs();

            var allPostings = await PostingCollector.CollectPostingsAsync(container, isIntegrationTest);

            var postingClassificationsSecret = await secretRepository.GetAsync(new PostingClassificationsSecret(), errorsAndInfos);

            if (errorsAndInfos.AnyErrors())
            {
                await DataPresenter.WriteErrorsAsync(errorsAndInfos);

                return;
            }

            var postingClassifications = postingClassificationsSecret.Cast <IPostingClassification>().ToList();

            await SummaryCalculator.CalculateAndShowSummaryAsync(allPostings, postingClassifications);

            await AverageCalculator.CalculateAndShowAverageAsync(allPostings, postingClassifications);

            await MonthlyDeltaCalculator.CalculateAndShowMonthlyDeltaAsync(allPostings, postingClassifications);

            await ClassifiedPostingsCalculator.CalculateAndShowClassifiedPostingsAsync(allPostings, postingClassifications, DateTime.Now.AddYears(-1), 70);

            CalculationLogger.Flush();
        }
예제 #3
0
        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);
            }
        }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }