Ejemplo n.º 1
0
        public void HaveBudget_20180201_20180415()
        {
            var range = new InputRange("2018-02-01", "2018-04-15");

            _repository.GetBudgets().Returns(new List <Budget>
            {
                new Budget
                {
                    YearMonth = "201802",
                    Amount    = 28
                },
                new Budget
                {
                    YearMonth = "201803",
                    Amount    = 3100
                }, new Budget
                {
                    YearMonth = "201804",
                    Amount    = 30000
                }
            });
            var actual = _calculator.GetValidBudgetBy(range);

            Assert.AreEqual(18128, actual);
        }
Ejemplo n.º 2
0
        public void InvalidRange()
        {
            var range = new InputRange("2018-02-15", "2018-2-10");

            _repository.GetBudgets().Returns(new List <Budget>
            {
                new Budget
                {
                    YearMonth = "201802",
                    Amount    = 28
                },
            });
            var actual = _calculator.GetValidBudgetBy(range);
        }
Ejemplo n.º 3
0
        public void HaveBudget()
        {
            var range = new InputRange("2018-01-01", "2018-01-31");

            _repository.GetBudgets().Returns(new List <Budget>
            {
                new Budget
                {
                    YearMonth = "201801",
                    Amount    = 31000
                }
            });
            var actual = _calculator.GetValidBudgetBy(range);

            Assert.AreEqual(31000, actual);
        }
Ejemplo n.º 4
0
        public void HaveBudget_20180210_20180215()
        {
            var range = new InputRange("2018-02-10", "2018-2-15");

            _repository.GetBudgets().Returns(new List <Budget>
            {
                new Budget
                {
                    YearMonth = "201802",
                    Amount    = 28
                },
            });
            var actual = _calculator.GetValidBudgetBy(range);

            Assert.AreEqual(6, actual);
        }
Ejemplo n.º 5
0
        public decimal GetValidBudgetBy(InputRange inputRange)
        {
            if (!inputRange.IsValid())
            {
                throw new ArgumentException("Not a valid From/To date time");
            }
            var budget = _repo.GetBudgets();

            var months = inputRange.GetRangeMonths();

            var result = 0;

            foreach (var month in months)
            {
                var totaldays   = DateTime.DaysInMonth(month.Value.Year, month.Value.Month);
                var totalBudget = budget.Where(x => x.YearMonth == month.Key).Select(d => d.Amount).FirstOrDefault();

                if (string.Compare(month.Key, inputRange.From.ToString("yyyyMM")) == 0)
                {
                    if (months.Count == 1)
                    {
                        result = (inputRange.GetDayofTo() - inputRange.GetDayofFrom() + 1) * totalBudget / totaldays;
                    }
                    else
                    {
                        result += (totaldays - inputRange.GetDayofFrom() + 1) * totalBudget / totaldays;
                    }
                    continue;
                }

                if (string.Compare(month.Key, inputRange.To.ToString("yyyyMM")) == 0)
                {
                    result += totalBudget / totaldays * inputRange.GetDayofTo();
                    continue;
                }
                result += budget.Where(x => x.YearMonth == month.Key).Select(d => d.Amount).FirstOrDefault();
            }
            return(result);
        }