Ejemplo n.º 1
0
        private async Task <List <CategoriesAndExpensesDTO> > CreateBarExpensesDataForUser(int walletId, string userId, DateTime[] date)
        {
            var monthStart = date[0];
            var monthEnd   = date[1];
            List <CategoriesAndExpensesDTO> expenses = new List <CategoriesAndExpensesDTO>();

            foreach (var category in _context.WalletsCategories.Where(w => w.WalletId == walletId).AsEnumerable())
            {
                CategoriesAndExpensesDTO categoriesAndExpenses = new CategoriesAndExpensesDTO();
                categoriesAndExpenses.CategoryExpenses = _context.Expenses.Where(e => e.ExpenseCategoryId == category.CategoryId && e.FamilyWalletId == walletId && e.ExpenseUserId == userId && e.CreationDate >= monthStart && e.CreationDate <= monthEnd).Sum(e => e.MoneySpent);
                expenses.Add(categoriesAndExpenses);
            }

            return(expenses);
        }
Ejemplo n.º 2
0
        public async Task <List <CategoriesAndExpensesDTO> > CreateBarExpensesData(int walletId, DateTime d)
        {
            DateTime[] date       = GetDate(d);
            var        monthStart = date[0];
            var        monthEnd   = date[1];
            //TODO: проверить что будет если везде будет 0
            List <CategoriesAndExpensesDTO> expenses = new List <CategoriesAndExpensesDTO>();

            foreach (var category in _context.WalletsCategories.Where(w => w.WalletId == walletId).AsEnumerable())
            {
                CategoriesAndExpensesDTO categoriesAndExpenses = new CategoriesAndExpensesDTO();
                categoriesAndExpenses.CategoryExpenses = await _context.Expenses.Where(e => e.ExpenseCategoryId == category.CategoryId && e.FamilyWalletId == walletId && e.CreationDate >= monthStart && e.CreationDate <= monthEnd).SumAsync(e => e.MoneySpent);

                expenses.Add(categoriesAndExpenses);
            }
            return(expenses);
        }
Ejemplo n.º 3
0
        private async Task <BarComparison> GetCurrentAndPreviousMonthsData(int walletId)
        {
            var today                = DateTime.Today;
            var currentMonth         = new DateTime(today.Year, today.Month, 1);
            var startOfPreviousMonth = currentMonth.AddMonths(-1);
            var endOfPreviousMonth   = currentMonth.AddMilliseconds(-1);
            var endOfCurrentMonth    = currentMonth.AddMonths(1).AddMilliseconds(-1);



            var lastMonthData = await _context.Expenses.Where(e => e.FamilyWalletId == walletId && e.CreationDate >= startOfPreviousMonth && e.CreationDate <= endOfPreviousMonth).ToListAsync();

            var currentMonthData = await _context.Expenses.Where(e => e.FamilyWalletId == walletId && e.CreationDate >= currentMonth && e.CreationDate <= endOfCurrentMonth).ToListAsync();

            List <CategoriesAndExpensesDTO> currentMonthExpenses = new List <CategoriesAndExpensesDTO>();
            List <CategoriesAndExpensesDTO> lastMonthExpenses    = new List <CategoriesAndExpensesDTO>();

            foreach (var category in _context.WalletsCategories.Where(w => w.WalletId == walletId).AsEnumerable())
            {
                CategoriesAndExpensesDTO currentMonthcategoriesAndExpenses = new CategoriesAndExpensesDTO();
                //sum of current month expenses for this wallet and this category
                var currentMonthCategoryExpense = _context.Expenses.Where(e => e.ExpenseCategoryId == category.CategoryId && e.FamilyWalletId == walletId && e.CreationDate >= currentMonth && e.CreationDate <= endOfCurrentMonth).Sum(e => e.MoneySpent);
                currentMonthcategoriesAndExpenses.Id = category.CategoryId;
                currentMonthcategoriesAndExpenses.CategoryExpenses = currentMonthCategoryExpense;

                CategoriesAndExpensesDTO lastMonthcategoriesAndExpenses = new CategoriesAndExpensesDTO();
                //sum of last month expenses for this wallet and this category
                var lastMonthCategoryExpenses = _context.Expenses.Where(e => e.ExpenseCategoryId == category.CategoryId && e.FamilyWalletId == walletId && e.CreationDate >= startOfPreviousMonth && e.CreationDate <= endOfPreviousMonth).Sum(e => e.MoneySpent);
                lastMonthcategoriesAndExpenses.Id = category.CategoryId;
                lastMonthcategoriesAndExpenses.CategoryExpenses = lastMonthCategoryExpenses;

                currentMonthExpenses.Add(currentMonthcategoriesAndExpenses);
                lastMonthExpenses.Add(lastMonthcategoriesAndExpenses);
            }

            BarComparison barComparison = new BarComparison
            {
                CurrentMonthData = currentMonthExpenses,
                LastMonthData    = lastMonthExpenses
            };

            return(barComparison);
        }