コード例 #1
0
        private List <RecordModel> GetRecordsToCheck(ReportSettingModel reportSettings)
        {
            var recordService = new RecordService(App.Db);

            // get all records for user
            // Note: use period (DateFrom - DateUntil) from ReportSetting because IN TESTS it should correspond to PeriodFilterType
            var allRecords = recordService.GetListForUser(user.Id);
            var records    = reportSettings.PeriodFilterType == PeriodFilterType.All
                ? allRecords.Where(_ => _.RecordType == reportSettings.DataType).ToList()
                : allRecords.Where(_ =>
                                   _.Date >= reportSettings.DateFrom.Value && _.Date <= reportSettings.DateUntil.Value &&
                                   _.RecordType == reportSettings.DataType).ToList();

            // get records that should be included into report
            List <RecordModel> recordsToCheck = null;

            if (reportSettings.AllCategories)
            {
                // take into account IncludeRecordsWithoutCategory
                recordsToCheck = reportSettings.IncludeRecordsWithoutCategory
                    ? records
                    : records.Where(_ => _.CategoryId.HasValue).ToList();
            }
            else
            {
                recordsToCheck = reportSettings.IncludeRecordsWithoutCategory
                    ? records.Where(_ => !_.CategoryId.HasValue || reportSettings.CategoryIds.Contains(_.CategoryId.Value)).ToList()
                    : records.Where(_ => _.CategoryId.HasValue && reportSettings.CategoryIds.Contains(_.CategoryId.Value)).ToList();
            }

            return(recordsToCheck);
        }
コード例 #2
0
        /// <summary>
        /// It builds and checks report for all combinations of RecordType, PeriodFilterType, IncludeRecordsWithoutCategory
        /// </summary>
        private void BuildAndCheckReport(
            ReportSettingModel reportSettings,
            Action <Record> createRecordOneTypeOverrides    = null,
            Action <Record> createRecordSecondTypeOverrides = null)
        {
            var peridFilterTypes = Enum.GetValues(typeof(PeriodFilterType));
            var recordTypes      = Enum.GetValues(typeof(RecordType));

            foreach (bool includeEmptyCategoryRecords in new[] { true, false })
            {
                foreach (RecordType recordType in recordTypes)
                {
                    foreach (PeriodFilterType periodFilterType in peridFilterTypes)
                    {
                        // get correspond period
                        var period = GetPeriod(periodFilterType);
                        // create records for report of one type
                        App.Factory.CreateRecords(user.Id, period.Item1, period.Item2, record =>
                        {
                            record.RecordType = recordType;
                            createRecordOneTypeOverrides?.Invoke(record);
                        });
                        // create records for report of second type
                        App.Factory.CreateRecords(user.Id, period.Item1, period.Item2, record =>
                        {
                            record.RecordType = recordType;
                            createRecordSecondTypeOverrides?.Invoke(record);
                        });
                        // create records without category for report
                        App.Factory.CreateRecords(user.Id, period.Item1, period.Item2, record =>
                        {
                            record.RecordType = recordType;
                            record.CategoryId = null;
                        });
                        // update settings
                        reportSettings.IncludeRecordsWithoutCategory = includeEmptyCategoryRecords;
                        reportSettings.DataType         = recordType;
                        reportSettings.DateFrom         = period.Item1;
                        reportSettings.DateUntil        = period.Item2;
                        reportSettings.PeriodFilterType = periodFilterType;

                        CheckReportFetch(reportSettings);

                        Debug.WriteLine(string.Format("Success. EmptyCategoryRecords: {0}, RecordType: {1}, PeriodFilterType: {2}",
                                                      includeEmptyCategoryRecords, recordType.ToString(), periodFilterType.ToString()));
                    }
                }
            }
        }
コード例 #3
0
        public ChartData Build(List <ReportUnit> reportUnits, ReportSettingModel settings)
        {
            this.settings = settings;
            var result = new ChartData();

            // save global mapper
            if (settings.IsPieChartSelected)
            {
                Charting.For <ReportUnit>(reportUnitMapperPie);
            }
            if (settings.IsBarChartColumnsSelected)
            {
                Charting.For <ReportUnit>(reportUnitMapperColumn);
            }
            if (settings.IsBarChartRowsSelected)
            {
                Charting.For <ReportUnit>(reportUnitMapperRow);
            }

            for (int i = 0; i < reportUnits.Count; i++)
            {
                //build collection
                if (settings.IsBarChartSelected && settings.BarChartSection == BarChartSection.Period)
                {
                    if (result.SeriesCollection.Count == 0)
                    {
                        result.SeriesCollection.AddRange(BuildSeries(reportUnits[i], i, reportUnits.Count));
                    }

                    result.SeriesCollection[0].Values[i] = reportUnits[i];
                }
                else
                {
                    result.SeriesCollection.AddRange(BuildSeries(reportUnits[i], i, reportUnits.Count));
                }

                // build collection
                //result.SeriesCollection.AddRange(BuildSeries(reportUnits[i], settings, i, reportUnits.Count));
                // populate caption list
                result.Titles.Add(reportUnits[i].Caption);
            }

            // build colors collection
            var totalUnitsCount = reportUnits.Count + DetailsCount(reportUnits);

            result.ColorsCollection.AddRange(ColorGenerator.GenerateColors(totalUnitsCount));

            return(result);
        }
コード例 #4
0
        private void CheckReportFetch(ReportSettingModel reportSettings)
        {
            // necessary services
            var recordService               = new RecordService(App.Db);
            var categoryService             = new CategoryService(App.Db);
            var currencyService             = new CurrencyService(App.Db);
            var currencyExchangeRateService = new CurrencyExchangeRateService(App.Db);

            // necessary values
            var mainCurrency = currencyService.GetMain(user.Id);

            mainCurrency.Should().NotBeNull();
            var currencyExchangeRates = currencyExchangeRateService.GetList(user.Id, mainCurrency.Id);

            // initialize report instance
            var reportBuilder = new ReportBuilder(user.Id, recordService, categoryService, currencyService, currencyExchangeRateService);

            // build report
            var result = reportBuilder.Build(reportSettings);

            // get all correspond records
            var records      = GetRecordsToCheck(reportSettings);
            var sumByRecords = records.Sum(_ => _.Value);

            // 1. check result value
            result.Sum(_ => _.Value).ShouldBeEquivalentTo(GetSumValue(records, currencyExchangeRates, mainCurrency));

            // 2. check records without category
            if (reportSettings.IncludeRecordsWithoutCategory)
            {
                if (records.Any(_ => !_.CategoryId.HasValue))
                {
                    var reportUnit = result.FirstOrDefault(_ => _.Caption == null);
                    reportUnit.Should().NotBeNull();
                    reportUnit.Value.ShouldBeEquivalentTo(GetSumValue(records, currencyExchangeRates, mainCurrency, (int?)null));
                }
            }
            else
            {
                result.FirstOrDefault(_ => _.Caption == null).Should().BeNull();
            }

            // load categories
            var categories = reportSettings.AllCategories
                ? categoryService.GetListForUser(user.Id)
                : categoryService.Get(reportSettings.CategoryIds);

            // 3. check categories are shown when all levels are included
            if (reportSettings.CategoryLevel == -1)
            {
                categories.ForEach(category =>
                {
                    if (records.Any(_ => _.CategoryId == category.Id))
                    {
                        // take into account fact that categories can have equal values
                        var reportUnits = result.Where(_ => _.Caption == category.Name).ToList();
                        reportUnits.Count.Should().BeGreaterThan(0);
                        var value = GetSumValue(records, currencyExchangeRates, mainCurrency, category.Id);
                        reportUnits.Any(_ => _.Value == value).ShouldBeEquivalentTo(true);
                    }
                });
            }

            // 4. check categories are shown when special category level is selected
            if (reportSettings.CategoryLevel != -1)
            {
                var categoryLevelMapping = categoryService.GetCategoryLevelMapping(user.Id);
                var categoryMapping      = categoryService.GetCategoryMapping(user.Id, reportSettings.CategoryLevel);

                categories.Where(_ => categoryLevelMapping[_.Id] == reportSettings.CategoryLevel).ToList().ForEach(category =>
                {
                    // get all child categories
                    var childCatIds = categoryMapping.Where(_ => _.Value == category.Id).Select(_ => _.Key);
                    // get child categories from selected by user
                    var childCategoryIds = categories.Where(_ => childCatIds.Contains(_.Id)).Select(_ => _.Id).ToList();

                    if (records.Any(_ => _.CategoryId.HasValue && childCategoryIds.Contains(_.CategoryId.Value)))
                    {
                        // take into account fact that categories can have equal values
                        var reportUnits = result.Where(_ => _.Caption == category.Name).ToList();
                        reportUnits.Count.Should().BeGreaterThan(0);
                        var value = GetSumValue(records, currencyExchangeRates, mainCurrency, childCategoryIds);
                        reportUnits.Any(_ => _.Value == value).ShouldBeEquivalentTo(true);
                    }
                });
            }
        }