Beispiel #1
0
        public async Task <BiMonthlyIncomeReportModel> BiMonthlyIncomePerCategoryReport(DateTime startMonth)
        {
            var userId = await _authenticationService.GetCurrentUserId();

            var nextMonthStart = startMonth.AddMonths(1);
            var nextMonthEnd   = nextMonthStart.AddMonths(1).AddDays(-1);

            using var unitOfWork = _unitOfWork();

            var transactions = await unitOfWork.TransactionRepository.FindAll(
                findQuery : item =>
                item.Date >= startMonth &&
                item.Date <= nextMonthEnd &&
                item.UserId == userId &&
                item.Amount > 0,
                includeProperties : nameof(TransactionEntity.TransactionCategories));

            var allCategories = await unitOfWork.CategoryRepository.FindAll(
                findQuery : item => item.UserId == userId,
                includeProperties : new string[]
            {
                nameof(CategoryEntity.ParentCategory),
                nameof(CategoryEntity.ChildCategories)
            });

            var bimonthlyIncomeReport = new BimonthlyIncomeReportItem
            {
                Incomes     = new List <BimonthlyIncome>(),
                FirstMonth  = startMonth,
                SecondMonth = nextMonthStart
            };

            // loop through all the regular top categories
            foreach (var parentCategory in allCategories.Where(category =>
                                                               category.ParentId == null))
            {
                AddCategoryToBimonthlyIncome(transactions, parentCategory, bimonthlyIncomeReport, startMonth, nextMonthStart);
            }

            var uncategorizedTransactionsThisMonth = transactions.Where(item =>
                                                                        item.TransactionCategories.Count == 0 &&
                                                                        item.Date < nextMonthStart &&
                                                                        item.Date >= startMonth).ToList().ToDto();

            return(new BiMonthlyIncomeReportModel
            {
                BimonthlyIncomeReport = bimonthlyIncomeReport,
                UncategorizedTransactionsThisMonth = uncategorizedTransactionsThisMonth
            });
        }
Beispiel #2
0
        private static void AddCategoryToBimonthlyIncome(IList <TransactionEntity> transactions, CategoryEntity category,
                                                         BimonthlyIncomeReportItem bimonthlyExpenseReport, DateTime firstMonthStart, DateTime secondMonthStart)
        {
            var lastMonthParentTransactions = TransactionsForMonth(transactions, category, firstMonthStart.Year, firstMonthStart.Month);
            var thisMonthParentTransactions = TransactionsForMonth(transactions, category, secondMonthStart.Year, secondMonthStart.Month);

            var bimonthlyParentIncome = new BimonthlyIncome
            {
                Name           = category.Name,
                AmountPrevious = CalculateSumCategoryTransactions(category, lastMonthParentTransactions),
                AmountNow      = CalculateSumCategoryTransactions(category, thisMonthParentTransactions)
            };

            bimonthlyExpenseReport.ThisMonthTotal     += bimonthlyParentIncome.AmountNow;
            bimonthlyExpenseReport.PreviousMonthTotal += bimonthlyParentIncome.AmountPrevious;

            bimonthlyExpenseReport.Incomes.Add(bimonthlyParentIncome);

            if (category.ChildCategories.Any())
            {
                foreach (var childCategory in category.ChildCategories)
                {
                    var lastMonthChildTransactions = TransactionsForMonth(transactions, childCategory, firstMonthStart.Year, firstMonthStart.Month);
                    var thisMonthChildTransactions = TransactionsForMonth(transactions, childCategory, secondMonthStart.Year, secondMonthStart.Month);

                    var bimonthlyChildIncome = new BimonthlyIncome
                    {
                        Name           = childCategory.Name,
                        AmountPrevious = CalculateSumCategoryTransactions(childCategory, lastMonthChildTransactions),
                        AmountNow      = CalculateSumCategoryTransactions(childCategory, thisMonthChildTransactions),
                    };

                    bimonthlyExpenseReport.ThisMonthTotal     += bimonthlyChildIncome.AmountNow;
                    bimonthlyExpenseReport.PreviousMonthTotal += bimonthlyChildIncome.AmountPrevious;

                    bimonthlyParentIncome.AmountNow      += bimonthlyChildIncome.AmountNow;
                    bimonthlyParentIncome.AmountPrevious += bimonthlyChildIncome.AmountPrevious;

                    bimonthlyParentIncome.ChildBimonthlyIncomes.Add(bimonthlyChildIncome);
                }
            }
        }