Ejemplo n.º 1
0
        public double TotalAmount(DateTime start, DateTime end)
        {
            var budgetList = budgetRepo.GetAll();

            if (!IsValidateDateRange(start, end))
            {
                return(0);
            }
            if (IsSameMonth(start, end))
            {
                var targetBudgets = GetBudget(start, budgetList);

                if (targetBudgets == null)
                {
                    return(0);
                }

                var unitOfDay         = targetBudgets.Amount / GetDayInTargetMonth(start);
                var daysOfTargetMonth = GetDifferentDays(start, end);

                return(CalculateAmount(unitOfDay, daysOfTargetMonth));
            }

            var totalAmount = 0;

            totalAmount += GeFirstAndLastTotalAmounts(start, end, budgetList);

            totalAmount += GetMiddleTotalAmounts(start, end, budgetList);

            return(totalAmount);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 回傳時間區間內的預算加總。
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public double TotalAmount(DateTime start, DateTime end)
        {
            double amount = 0;

            // 取得所有月預算。
            var budgets = _budgetRepo.GetAll();

            if (budgets.Count == 0)
            {
                return(amount);
            }


            foreach (var budget in budgets)
            {
                var budgetMonthFirstDate = GetBudgetMonthFirstDate(budget.YearMonth);
                var budgetMonthEndDate   = budgetMonthFirstDate.AddMonths(1).AddDays(-1);

                // 針對有落在查詢區間的預算進行加總
                if (IsBudgetMonthIntersectWithQueryInterval(start, end, budgetMonthFirstDate, budgetMonthEndDate))
                {
                    var budgetPerDay = budget.Amount / budgetMonthEndDate.Day;
                    var days         = GetIntersectiveDaysOfBudgetAndQuery(start, end, budgetMonthFirstDate, budgetMonthEndDate);
                    amount += days * budgetPerDay;
                }
            }

            return(amount);
        }
Ejemplo n.º 3
0
        private void SetUp(List <Budget> budget)
        {
            IBudgetRepo budgetRepo = Substitute.For <IBudgetRepo>();

            budgetRepo.GetAll().Returns(budget);
            _budgetService = new BudgetService(budgetRepo);
        }
Ejemplo n.º 4
0
        public double Query(DateTime startDate, DateTime endDate)
        {
            if (IsInvalidDateRange(startDate, endDate))
            {
                return(0);
            }

            Dictionary <string, int> days = Help.GetDaysInMonth(startDate, endDate);

            var budgets = _budgetRepo.GetAll() ?? new List <Budget>();

            var budgetModels = budgets.Select(x => new BudgetTemp()
            {
                BudgetDate = DateTime.ParseExact(x.YearMonth, "yyyyMM", null),
                Amount     = x.Amount,
            });
            double sum = 0;

            foreach (var kv in days)
            {
                var b          = budgetModels.FirstOrDefault(x => x.BudgetDate.ToString("yyyyMM") == kv.Key);
                var dailyAmout = b?.DailyAmount ?? 0;
                sum += (kv.Value) * (dailyAmout);
            }

            return(sum);
        }
Ejemplo n.º 5
0
        public decimal TotalAmount(DateTime start, DateTime end)
        {
            var period = new Period(start, end);

            return(_budgetRepo.GetAll()
                   .Sum(b => b.EffectiveAmount(period)));
        }
        public decimal Query(DateTime start, DateTime end)
        {
            if (start > end)
            {
                return(0);
            }

            var budgets = _repo.GetAll();

            if (start.ToString("yyyyMM") == end.ToString("yyyyMM"))
            // if (start.Month == end.Month)
            {
                var interval     = (end - start).Days + 1;
                var startAmount1 = GetMonthAmount(start, budgets);
                return(interval * startAmount1 / DateTime.DaysInMonth(end.Year, end.Month));
            }

            if (Math.Abs(start.Month - end.Month) >= 2)
            {
                decimal toalInterval = 0;
                var     tempDate     = start.AddMonths(1);
                while (tempDate > start && tempDate < end)
                {
                    toalInterval += GetMonthAmount(tempDate, budgets);
                    tempDate      = tempDate.AddMonths(1);
                }

                return(StartAmount(start, budgets) + toalInterval + EndAmount(end, budgets));
            }
            else
            {
                return(StartAmount(start, budgets) + EndAmount(end, budgets));
            }
        }
Ejemplo n.º 7
0
        public double TotalAmount(DateTime start, DateTime end)
        {
            if (!IsValidDateRange(start, end))
            {
                return(0);
            }

            var budgets = _repo.GetAll();

            if (IsSameMonth(start, end))
            {
                var budget = budgets.SingleOrDefault(x => x.YearMonth.Equals(start.ToString("yyyyMM")));
                if (budget == null)
                {
                    return(0);
                }

                var budgetPerDay = DailyAmountOfBudget(start, budget);
                var intervalDays = DaysInterval(start, end);

                return(budgetPerDay * intervalDays);
            }
            else
            {
                DateTime currentMonth = new DateTime(start.Year, start.Month, 1);
                double   totalAmount  = 0;
                do
                {
                    var budgetByMonth =
                        budgets.SingleOrDefault(x => x.YearMonth.Equals(currentMonth.ToString("yyyyMM")));
                    if (budgetByMonth != null)
                    {
                        int dailyAmount  = 0;
                        int intervalDays = 0;
                        if (IsFirstMonth(start, currentMonth))
                        {
                            dailyAmount  = AmountPerDayInMonth(budgetByMonth, start);
                            intervalDays = (DateTime.DaysInMonth(start.Year, start.Month) - start.Day + 1);
                        }
                        else if (IsLastMonth(end, currentMonth))
                        {
                            dailyAmount  = AmountPerDayInMonth(budgetByMonth, end);
                            intervalDays = end.Day;
                        }
                        else
                        {
                            dailyAmount  = AmountPerDayInMonth(budgetByMonth, currentMonth);
                            intervalDays = DateTime.DaysInMonth(currentMonth.Year, currentMonth.Month);
                        }

                        totalAmount += dailyAmount * intervalDays;
                    }

                    currentMonth = currentMonth.AddMonths(1);
                } while (currentMonth <= end);

                return(totalAmount);
            }
        }
Ejemplo n.º 8
0
 public double TotalAmount(DateTime start, DateTime end)
 {
     if (_budgetRepo.GetAll().Any())
     {
         return(Days(start, end));
     }
     return(0);
 }
Ejemplo n.º 9
0
 public void period_inside_budget_month()
 {
     _budgetRepo.GetAll().Returns(new List <Budget>()
     {
         new Budget {
             YearMonth = "201803", Amount = 31
         }
     });
     AmountShouldBe(new DateTime(2018, 03, 01), new DateTime(2018, 03, 01), 1);
 }
Ejemplo n.º 10
0
        public double TotalAmount(DateTime start, DateTime end)
        {
            var listOfBudgets = _budgetRepo.GetAll();
            var listOfDays    = GetDaysOfYearMonthes(start, end);

            //return listOfDays.Sum(d => CalculateAmount(listOfBudgets, d));

            return(listOfBudgets.Sum(b => CalculateAmountByBudgets(listOfDays, b)));
        }
Ejemplo n.º 11
0
        public void SingleWholeMonth_HasBudget_()
        {
            _stubBudgetRepo.GetAll()
            .Returns(new List <Budget>
            {
                new Budget {
                    YearMonth = "201907", Amount = 3100
                }
            });

            BudgetShouldBe(new DateTime(2019, 7, 1), new DateTime(2019, 7, 31), 3100);
        }
 public double Query(DateTime startDateTime, DateTime endDateTime)
 {
     if (startDateTime > endDateTime)
     {
         return(0);
     }
     else
     {
         var allBudget = _budgetRepo.GetAll();
         return(CalculateBudgetAmountTotal(startDateTime, endDateTime, allBudget));
     }
 }
Ejemplo n.º 13
0
        public double TotalAmount(DateTime start, DateTime end)
        {
            if (!IsValidDateRange(start, end))
            {
                return(0);
            }

            var budgets = _repo.GetAll();

            if (IsSameMonth(start, end))
            {
                var budget = budgets.SingleOrDefault(x => x.YearMonth.Equals(start.ToString("yyyyMM")));
                if (budget == null)
                {
                    return(0);
                }

                var budgetPerDay = budget.Amount / DateTime.DaysInMonth(start.Year, start.Month);
                return(budgetPerDay * DaysInterval(start, end));
            }
            else
            {
                DateTime tempDate   = new DateTime(start.Year, start.Month, 1);
                double   aggrAmount = 0;
                do
                {
                    var budgetByMonth =
                        budgets.SingleOrDefault(x => x.YearMonth.Equals(tempDate.ToString("yyyyMM")));
                    if (budgetByMonth != null)
                    {
                        if (tempDate.ToString("yyyyMM") == start.ToString("yyyyMM"))
                        {
                            aggrAmount += AmountPerDayInMonth(budgetByMonth, start) *
                                          (DateTime.DaysInMonth(start.Year, start.Month) - start.Day + 1);
                        }
                        else if (tempDate.ToString("yyyyMM") == end.ToString("yyyyMM"))
                        {
                            aggrAmount += AmountPerDayInMonth(budgetByMonth, end) * end.Day;
                        }
                        else
                        {
                            aggrAmount += budgetByMonth.Amount;
                        }
                    }

                    tempDate = tempDate.AddMonths(1);
                } while (tempDate <= end);

                return(aggrAmount);
            }
        }
Ejemplo n.º 14
0
        public void QueryTest_SingleDate()
        {
            _stubBudgetRepo.GetAll().Returns(new List <Budget> {
                new Budget {
                    YearMonth = "201901", Amount = 31
                }
            });

            QueryShouldbe(new DateTime(2019, 01, 01), new DateTime(2019, 01, 01), 1);
        }
Ejemplo n.º 15
0
        public void Query_Cross_Year_with_diff_month()
        {
            IBudgetRepo repo = Substitute.For <IBudgetRepo>();

            repo.GetAll()
            .Returns(new List <Budget>
            {
                // new Budget {YearMonth = "202102", Amount = 28},
                new Budget {
                    YearMonth = "202104", Amount = 3000
                }
            });
            var cal   = new BudgetCalculator(repo);
            var query = cal.Query(new DateTime(2020, 4, 27), new DateTime(2021, 4, 2));

            Assert.AreEqual(200, query);
        }
Ejemplo n.º 16
0
        public void Query_Illegal_DateTime()
        {
            var         budget = new Budget();
            IBudgetRepo repo   = Substitute.For <IBudgetRepo>();

            repo.GetAll().Returns(new List <Budget>
            {
                new Budget {
                    YearMonth = "202103", Amount = 310
                }
            });

            var cal   = new BudgetCalculator(repo);
            var query = cal.Query(new DateTime(2021, 3, 1), new DateTime(2021, 2, 1));

            Assert.AreEqual(0, query);
        }
Ejemplo n.º 17
0
        public double Query(DateTime startDate, DateTime endDate)
        {
            if (startDate.CompareTo(endDate) > 0)
            {
                return(0);
            }

            var budgets = _budgetRepo.GetAll();

            var start_yearMonth = startDate.ToString("yyyyMM");
            var end_yearMonth   = endDate.ToString("yyyyMM");
            var startBudget     = budgets.FirstOrDefault(x => x.YearMonth == start_yearMonth);

            if (start_yearMonth == end_yearMonth)
            {
                if (startBudget != null)
                {
                    return(GetSingleBudget(startDate, startBudget) * ((endDate - startDate).Days + 1));
                }
            }
            else
            {
                var sum = 0d;
                if (startBudget != null)
                {
                    sum += GetSingleBudget(startDate, startBudget) * (DateTime.DaysInMonth(startDate.Year, startDate.Month) - startDate.Day + 1);
                }

                foreach (var budget in budgets)
                {
                    var yearMonth = int.Parse(budget.YearMonth);
                    if (int.Parse(start_yearMonth) < yearMonth && yearMonth < int.Parse(end_yearMonth))
                    {
                        sum += budget.Amount;
                    }
                }
                var endBudget = budgets.FirstOrDefault(x => x.YearMonth == end_yearMonth);
                if (endBudget != null)
                {
                    sum += GetSingleBudget(endDate, endBudget) * endDate.Day;
                }
                return(sum);
            }

            return(0);
        }
Ejemplo n.º 18
0
        public void start_day_less_than_end_day()
        {
            IBudgetRepo repo = Substitute.For <IBudgetRepo>();

            repo.GetAll()
            .Returns(new List <Budget>
            {
                new Budget {
                    YearMonth = "202103", Amount = 31
                },
                new Budget {
                    YearMonth = "202104", Amount = 3000
                }
            });
            var cal   = new BudgetCalculator(repo);
            var query = cal.Query(new DateTime(2021, 3, 27), new DateTime(2021, 4, 29));

            Assert.AreEqual(5 + 2900, query);
        }
Ejemplo n.º 19
0
        public double TotalAmount(DateTime start, DateTime end)
        {
            var budgets = _budgetRepo.GetAll();

            if (budgets.Count > 0)
            {
                Dictionary <string, int> monthDaysMap = new Dictionary <string, int>();


                while (start <= end)
                {
                    string key = start.ToString("yyyyMM");
                    if (monthDaysMap.ContainsKey(key))
                    {
                        monthDaysMap[key]++;
                    }
                    else
                    {
                        monthDaysMap.Add(key, 1);
                    }

                    start = start.AddDays(1);
                }

                Dictionary <string, int> monthPerDayBudget = budgets.ToDictionary(x => x.YearMonth,
                                                                                  x => x.Amount / _GetDaysInTheMonth(x.YearMonth));


                double result = 0;

                foreach (string monthKey in monthDaysMap.Keys)
                {
                    if (monthPerDayBudget.ContainsKey(monthKey))
                    {
                        result += monthDaysMap[monthKey] * monthPerDayBudget[monthKey];
                    }
                }

                return(result);
            }

            return(0);
        }
Ejemplo n.º 20
0
        public void Query_Cross_Year()
        {
            var         budget = new Budget();
            IBudgetRepo repo   = Substitute.For <IBudgetRepo>();

            repo.GetAll().Returns(new List <Budget>
            {
                new Budget {
                    YearMonth = "202102", Amount = 28
                },
                // new Budget {YearMonth = "202103", Amount = 310},
                new Budget {
                    YearMonth = "202204", Amount = 3000
                }
            });
            var cal   = new BudgetCalculator(repo);
            var query = cal.Query(new DateTime(2021, 2, 27), new DateTime(2022, 4, 1));

            Assert.AreEqual(102, query);
        }
        public decimal TotalAmount(DateTime start, DateTime end)
        {
            if (start > end)
            {
                throw new ArgumentException();
            }

            var budgetList = _repo.GetAll();

            if (budgetList.Count == 0)
            {
                return(0);
            }

            if (start.ToString("yyyyMM") == end.ToString("yyyyMM"))
            {
                return(CalculatePeriodAmountInMonth(budgetList, start, end));
            }
            else
            {
                var firstMonthEndDay  = new DateTime(start.Year, start.Month, DateTime.DaysInMonth(start.Year, start.Month));
                var lastMonthStartDay = new DateTime(end.Year, end.Month, 1);

                var startAmount = CalculatePeriodAmountInMonth(budgetList, start, firstMonthEndDay);
                var lastAmount  = CalculatePeriodAmountInMonth(budgetList, lastMonthStartDay, end);

                var middleAmount = 0;
                var middleStart  = new DateTime(start.Year, start.Month, 1).AddMonths(1);
                var middleEnd    = new DateTime(end.Year, end.Month, DateTime.DaysInMonth(end.Year, end.Month)).AddMonths(-1);
                while (middleStart < middleEnd)
                {
                    middleAmount += budgetList.Count(a => a.YearMonth == middleStart.ToString("yyyyMM")) > 0 ? budgetList.Single(a => a.YearMonth == middleStart.ToString("yyyyMM")).Amount : 0;
                    middleStart   = middleStart.AddMonths(1);
                }

                return(startAmount + middleAmount + lastAmount);
            }
        }
Ejemplo n.º 22
0
        public void SingleDate()
        {
            //Arrange
            var budget = new List <Budget>()
            {
                new Budget()
                {
                    Amount    = 3100,
                    YearMonth = "201901"
                },
                new Budget()
                {
                    Amount    = 2800,
                    YearMonth = "201902"
                },
            };

            IBudgetRepo budgetRepo = Substitute.For <IBudgetRepo>();

            budgetRepo.GetAll().Returns(budget);
            var budgetService = new BudgetService(budgetRepo);

            Assert.AreEqual(100, budgetService.Query(new DateTime(2019, 1, 1), new DateTime(2019, 1, 1)));
        }
Ejemplo n.º 23
0
        public decimal Query(DateTime start, DateTime end)
        {
            if (start > end || !_repo.GetAll().Any())
            {
                return(0);
            }

            if (IsSameDay(start, end))
            {
                return((decimal)_repo.GetAll().FirstOrDefault(x => x.YearMonth == start.ToString("yyyyMM"))?.Amount /
                       GetDays(start));
            }

            if (IsSameMonth(start, end))
            {
                return((decimal)_repo.GetAll().FirstOrDefault(x => x.YearMonth == start.ToString("yyyyMM"))?.Amount /
                       GetDays(start) * ((end - start).Days + 1));
            }


            return(GetStartMonthBudget(start) +
                   GetSecondMonthBudget(end) +
                   GetEntireMonthBudget(start, end));
        }
 public decimal TotalAmount(Period period)
 {
     return(_budgetRepo.GetAll().Sum(b => b.EffectiveAmount(period)));
 }
Ejemplo n.º 25
0
 private void BudgetSetup(IEnumerable <Budget> list)
 {
     _budgetRepo.GetAll().Returns(list);
 }
Ejemplo n.º 26
0
 private void GivenBudgets(List <Budget> budgets)
 {
     _budgetRepo.GetAll().Returns(budgets);
 }
Ejemplo n.º 27
0
 private void GivenBudget(List <Budget> budget)
 {
     _budgetRepo.GetAll().ReturnsForAnyArgs(budget);
 }
Ejemplo n.º 28
0
 public decimal TotalAmount(string start, string end)
 {
     return(_budgetRepo.GetAll()
            .Sum(b => b.GetOverlappingAmount(new Period(start, end))));
 }
Ejemplo n.º 29
0
 private void GivenBudgets(params Budget[] budgets)
 {
     stubRepo.GetAll().Returns(budgets.ToList());
 }
Ejemplo n.º 30
0
        public double TotalAccoount(DateTime StartDate, DateTime EndDate)
        {
            if (StartDate > EndDate)
            {
                return(0);
            }
            var Budget = budgetRepo.GetAll();

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

            if (EndDate.Year - StartDate.Year == 0 &&
                EndDate.Month - StartDate.Month == 0)
            {
                double dayOMonth = DateTime.DaysInMonth(StartDate.Year, StartDate.Month);
                double dayDiff   = (EndDate - StartDate).Days + 1;

                var ammount = budgetRepo.GetAll()
                              .FirstOrDefault(x => x.YearMonth == StartDate.ToString("yyyyMM")).Amount;

                return(ammount * dayDiff / dayOMonth);
            }
            else
            {
                List <Budget> suBudgets = new List <Budget>();
                foreach (var obj in Budget)
                {
                    DateTime time = new DateTime(GetYear(obj.YearMonth),
                                                 GetMonth(obj.YearMonth), 1);
                    if (time.Year == StartDate.Year &&
                        time.Month == StartDate.Month)
                    {
                        suBudgets.Add(obj);
                        continue;
                    }
                    else if (time.Year == EndDate.Year &&
                             time.Month == EndDate.Month)
                    {
                        suBudgets.Add(obj);
                        continue;
                    }
                    if (time >= StartDate && time <= EndDate)
                    {
                        suBudgets.Add(obj);
                    }
                }

                double result = 0;
                foreach (var item in suBudgets)
                {
                    if (GetYear(item.YearMonth) == StartDate.Year &&
                        GetMonth(item.YearMonth) == StartDate.Month)
                    {
                        int dayOfMonth = DateTime.DaysInMonth(StartDate.Year, StartDate.Month);
                        result += item.Amount * (dayOfMonth - StartDate.Day + 1) / dayOfMonth;
                        continue;
                    }
                    else if (GetYear(item.YearMonth) == EndDate.Year &&
                             GetMonth(item.YearMonth) == EndDate.Month)
                    {
                        int dayOfMonth = DateTime.DaysInMonth(EndDate.Year, EndDate.Month);
                        result += item.Amount * EndDate.Day / dayOfMonth;
                        continue;
                    }
                    else
                    {
                        result += item.Amount;
                    }
                }

                return(result);
            }
        }