public IActionResult Create(CreateMonthlyBudgetViewModel model)
        {
            if (!ModelState.IsValid)
            {
                model.AccountName = AccountName(accountID: model.AccountID);
                model.Categories  = Categories(accountID: model.AccountID);

                return(View(model));
            }

            var monthlyBudgetID = Db.InsertOrUpdate(new MonthlyBudget(
                                                        accountID: model.AccountID,
                                                        startDate: model.StartDate.WithZeroedTime(),
                                                        endDate: model.EndDate.SetTime(23, 59, 59)
                                                        ));

            var categories = model.Categories.Where(c => c.Amount != 0).Select(c => new Category_MonthlyBudget(
                                                                                   monthlyBudgetId: monthlyBudgetID,
                                                                                   categoryId: c.CategoryID,
                                                                                   amount: c.Amount < 0 ? c.Amount : -c.Amount
                                                                                   ));

            foreach (var category in categories)
            {
                Db.Execute((conn, tran) => conn.Insert(category, tran));
            }

            UnitOfWork.CommitChanges();

            return(RedirectToAction(nameof(Index), new { id = model.AccountID }));
        }
        public IActionResult Copy(int id)
        {
            var dto = Db.Get <MonthlyBudget>(id);

            // Start the day after the old budget finished
            var startDate = dto.EndDate.AddDays(1);

            // End on the last day of the same month
            var endDate = startDate.LastDayOfMonth();

            // If the budget started just before the end of a month,
            // skip the end date to the end of the *next* month
            if ((endDate - startDate).Days < 28)
            {
                endDate = endDate.LastDayOfNextMonth();
            }

            var model = new CreateMonthlyBudgetViewModel {
                AccountID   = dto.AccountID,
                AccountName = AccountName(dto.AccountID),
                StartDate   = startDate,
                EndDate     = endDate,
                Categories  = Categories(accountID: dto.AccountID, monthlyBudgetID: dto.ID)
            };

            return(View(nameof(Create), model));
        }
        public IActionResult Create(int id)
        {
            var model = new CreateMonthlyBudgetViewModel {
                AccountID   = id,
                AccountName = AccountName(accountID: id),
                StartDate   = DateTime.Now.FirstDayOfMonth(),
                EndDate     = DateTime.Now.LastDayOfMonth(),
                Categories  = Categories(accountID: id)
            };

            return(View(model));
        }