Esempio n. 1
0
        public async Task <IActionResult> List(int?id)
        {
            var vm   = new BudgetListViewModel();
            var user = await _userManager.GetUserAsync(HttpContext.User);

            var userID = _userManager.GetUserId(User);
            IEnumerable <Budget> budgets = _budgetRepo.GetAll(userID);

            if (id != null)
            {
                vm.Budgets  = budgets;
                vm.Budget   = _budgetRepo.Get((int)id);
                vm.Expenses = _budgetRepo.GetExpenses((int)id);
                return(View(vm));
            }

            var budget = _budgetRepo.GetAll(userID).FirstOrDefault();

            if (budget != null)
            {
                vm.Budget   = budget;
                vm.Expenses = _budgetRepo.GetExpenses(budget.Id);
                vm.Budgets  = budgets;

                return(View(vm));
            }

            return(View(vm));
        }
        public void GetAll_Test()
        {
            ICollection <Budget> budgets = new HashSet <Budget>();

            budgets = budgetRepo.GetAll();

            Assert.IsNotNull(budgets);
            Assert.AreEqual(budgets.Count, budgetRepo.GetAll().Count);
        }
Esempio n. 3
0
        public void No_Budget()
        {
            _repo.GetAll().Returns(new List <Budget>()
            {
            });
            decimal actual = _service.Query(new DateTime(2019, 04, 01), new DateTime(2019, 04, 03));

            TotalBudgetShouldBe(0, actual);
        }
Esempio n. 4
0
        public double TotalAmount(DateTime start, DateTime end)
        {
            var thisMon    = start;
            int thisEndMon = end.Month;

            while (thisMon <= end)
            {
                Budget budget = _budgetRepository.GetAll().FirstOrDefault(p => p.YearMonth == thisMon.ToString("yyyyMM"));
                if (budget != null)
                {
                    double   oneDayAmount      = budget.Amount / DateTime.DaysInMonth(thisMon.Year, thisMon.Month);
                    DateTime thisMonthStartDay = start == thisMon ? start : new DateTime(thisMon.Year, thisMon.Month, 1);

                    DateTime thisMonthEndDay = end.Year == thisMon.Year && end.Month == thisMon.Month ? end : CaculateMonthLastDate(thisMon);

                    int thisMonthTotalDays = thisMonthEndDay.Day - thisMonthStartDay.Day + 1;

                    _totalAmount += oneDayAmount * thisMonthTotalDays;
                }
                else
                {
                    _totalAmount += 0;
                }
                thisMon = thisMon.AddMonths(1);
            }

            return(_totalAmount);
        }
Esempio n. 5
0
        public decimal TotalAmount(DateTime start, DateTime end)
        {
            var budgets = _budgetRepository.GetAll();
            var period  = new Period(start, end);

            return(budgets.Any() ? budgets.Sum(budget => budget.EffectiveAmount(period)) : 0);
        }
Esempio n. 6
0
        private int GetMonthAmount(string yearMonth)
        {
            var totalAmount = _budgetRepository.GetAll();

            var temp = totalAmount.Where(x => x.YearMonth == yearMonth).FirstOrDefault();

            return temp == null  ? 0:temp.Amount;
        }
Esempio n. 7
0
        public void Test_NoBudgetData()
        {
            _budgetRepository.GetAll().Returns(new List <Budget>());
            decimal actual = _budgetService.Query(new DateTime(2019, 7, 27), new DateTime(2019, 7, 31));

            Assert.AreEqual(0, actual);
        }
        public static bool IsBudgetExist(this IBudgetRepository budgetRepository, Guid id)
        {
            var budgetExist = budgetRepository.GetAll().Where(x => x.Id == id).Any();

            if (budgetExist)
            {
                return(true);
            }

            return(false);
        }
        public decimal Query(DateTime start, DateTime end)
        {
            if (start > end)
            {
                return(0);
            }
            var period = new Period(start, end);

            return(_repo.GetAll().Where(b => !period.IsNoOverlapping(b.Period))
                   .Sum(b => period.OverlappingDays(b.Period) * b.DailyAmount));
        }
Esempio n. 10
0
        public double TotalAmount(DateTime start, DateTime end)
        {
            var budgets = _budgetRepository.GetAll();

            if (budgets.Any())
            {
                return((end - start).TotalDays);
            }

            return(0);
        }
Esempio n. 11
0
        public void OneAmount()
        {
            var list = new List <Budget>();

            list.Add(new Budget()
            {
                YearMonth = "201801", Amount = 31
            });
            _repo.GetAll().Returns(list);
            TotalAmountShouldBe(new DateTime(2018, 1, 1), new DateTime(2018, 1, 15), 15);
        }
Esempio n. 12
0
        public decimal CalculateBudget(DateTime start, DateTime end)
        {
            var budgets = _budgetRepository.GetAll();

            var budget = 0M;

            foreach (var budgetModel in budgets)
            {
                if (budgetModel.YearMonth != start.ToString("yyyyMM") &&
                    budgetModel.YearMonth != end.ToString("yyyyMM"))
                {
                    DateTime firstDayOfBudgetMonth = DateTime.ParseExact(budgetModel.YearMonth + "01", "yyyyMMdd", null);
                    if (firstDayOfBudgetMonth > start && firstDayOfBudgetMonth < end)
                    {
                        budget += budgetModel.Budget;
                    }
                }
            }

            if (start.ToString("yyyyMM") != end.ToString("yyyyMM"))
            {
                var totalStartDaysInAMonth = DateTime.DaysInMonth(start.Year, start.Month);
                var startDays         = CalculateDays(start, new DateTime(start.Year, start.Month, totalStartDaysInAMonth));
                var startBudgetModels = budgets.Where(model => { return(model.YearMonth == start.ToString("yyyyMM")); });
                if (startBudgetModels.Any())
                {
                    budget += startBudgetModels.First().Budget / totalStartDaysInAMonth * startDays;
                }

                var endDays = CalculateDays(new DateTime(end.Year, end.Month, 1), end);
                var totalEndDaysInAMonth = DateTime.DaysInMonth(end.Year, end.Month);
                var endBudgetModels      = budgets.Where(model => { return(model.YearMonth == end.ToString("yyyyMM")); });
                if (endBudgetModels.Any())
                {
                    budget += endBudgetModels.First().Budget / totalEndDaysInAMonth * endDays;
                }

                return(budget);
            }

            var days = CalculateDays(start, end);
            var totalDaysInAMonth = DateTime.DaysInMonth(start.Year, start.Month);
            var budgetModels      = budgets.Where(model => { return(model.YearMonth == start.ToString("yyyyMM")); });

            if (!budgetModels.Any())
            {
                return(0);
            }

            return(budgetModels.First().Budget / totalDaysInAMonth * days);
        }
Esempio n. 13
0
        public decimal CalBudgets(DateTime startDate, DateTime endDate)
        {
            if (startDate > endDate)
            {
                throw new ArgumentException();
            }

            var budgets     = _budgetRepository.GetAll();
            var totalAmount = 0m;

            foreach (var budget in budgets)
            {
                totalAmount += BudgetInMonth(startDate, endDate, budget);
            }

            return(totalAmount);
        }
 private void GivenBudgets(params Budget[] budgets)
 {
     _budgetRepository.GetAll().Returns(budgets.ToList());
 }
Esempio n. 15
0
 public List <Budget> GetAll()
 {
     return(_repository.GetAll());
 }
Esempio n. 16
0
        public double TotalAmount(DateTime start, DateTime end)
        {
            var period = new Period(start, end);

            return(_budgetRepository.GetAll().Sum(x => x.DailyAmount() * period.OverlappingDays(x.CreatePeriod())));
        }
Esempio n. 17
0
 public List <Budget> GetBudgets(string userId)
 {
     return(_budgetRepository.GetAll(userId).ToList());
 }
Esempio n. 18
0
        public decimal TotalAmount(DateTime start, DateTime end)
        {
            var period = new Period(start, end);

            return(_budgetRepository.GetAll().Sum(b => b.OverlapAmount(period)));
        }
Esempio n. 19
0
 public decimal TotalAmount(Period period)
 {
     return(_budgetRepository
            .GetAll()
            .Sum(budget => budget.MonthlyAmount(period)));
 }
Esempio n. 20
0
 public void NoBudget()
 {
     _budgetRepository.GetAll().Returns(new List <Budget>());
     TotalAmountShouldBe("20180301", "20180301", 0);
 }
Esempio n. 21
0
 private void GivenBudget(List <Budget> budgets)
 {
     budgetRepository.GetAll().Returns(budgets);
 }
Esempio n. 22
0
 public BudgetService(IBudgetRepository budgetRepository)
 {
     _budgets = budgetRepository.GetAll();
 }
Esempio n. 23
0
        public Dictionary <string, int> SetBudgetToLookUp()
        {
            var budgets = _budgetRepository.GetAll();

            return(budgets.ToDictionary(x => x.YearMonth, x => x.Amount / GetDays(x.YearMonth)));
        }
Esempio n. 24
0
 public IEnumerable <Budget> GetAll()
 {
     return(_budgetRepository.GetAll());
 }