public async Task <IActionResult> PutSavingsPlan(long id, SavingsPlan savingsPlan)
        {
            if (id != savingsPlan.Id)
            {
                return(BadRequest());
            }

            _context.Entry(savingsPlan).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SavingsPlanExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <ActionResult <SavingsPlan> > PostSavingsPlan(SavingsPlan savingsPlan)
        {
            _context.SavingsPlans.Add(savingsPlan);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetSavingsPlan", new { id = savingsPlan.Id }, savingsPlan));
        }
        /// <summary>
        /// Calculates the savings plan for the given <paramref name="tag"/>.
        /// </summary>
        /// <param name="tag">The tag.</param>
        public ISavingsPlan CalculateSavingsPlan(string tag)
        {
            var savingsPlan = new SavingsPlan {
                Tag = tag, Date = DateTime.Now
            };

            var yearQuery = new TransactionYearsQuery()
                            .Register(new TransactionStartDateFilter(DateTime.MinValue))
                            .Register(new TransactionEndDateFilter(_dateCalculationService.GetEndOfToDay()))
                            .Register(new TransactionTagFilter(tag))
                            .Register(new TransactionDividendFilter(true));

            var years = _queryDispatcher.Execute(yearQuery).ToList();

            foreach (var year in years)
            {
                var period = new SavingsPlanPeriod()
                {
                    Year = year.ToString()
                };

                //Time period
                var start       = _dateCalculationService.GetStartAndEndDateOfYear(new DateTime(year, 1, 1), out DateTime end);
                var lastYearEnd = _dateCalculationService.GetEndDateOfYear(new DateTime(years[0] - 1, 1, 1));

                //Only this year
                var actualPeriodResult = CalculateCurrentPeriod(start, end, tag);

                period.PerformanceActualPeriodPercentage = actualPeriodResult.PerformanceActualPeriodPercentage;
                period.SumDividends = actualPeriodResult.SumDividends;

                //Accumulated till this end of year
                var periodResult = CalculateOverallPeriod(lastYearEnd, end, tag);

                period.SumOrderCostsPercentage = periodResult.SumOrderCostsPercentage;
                period.SumOrderCosts           = periodResult.SumOrderCosts;
                period.SumInpayment            = periodResult.SumInpayment;
                period.SumCapital = periodResult.SumCapital;
                period.PerformanceOverallPeriodPercentage = periodResult.PerformanceOverallPeriodPercentage;
                period.SumOverallDividends           = periodResult.SumOverallDividends;
                period.SumOverallDividendsPercentage = decimal.Round((periodResult.SumOverallDividends / periodResult.SumInpayment) * 100, 2);
                period.IsCurrentYear = DateTime.Now.Year == year;
                period.IsForecast    = false;

                savingsPlan.Periods.Add(period);
            }

            //Forecast
            if (savingsPlan.Periods.Any())
            {
                var startYear             = years.Max() + 1;
                var amountOfForecastYears = years.Min() + 35 - startYear;
                foreach (ISavingsPlanPeriod forecast in GetTagPeriodForecast(amountOfForecastYears, startYear, savingsPlan.Periods))
                {
                    savingsPlan.Periods.Add(forecast);
                }
            }

            return(savingsPlan);
        }