public ActionResult AddBudget(EditBudgetViewModel vm, bool isDone = false)
        {
            ViewBag.Title = "Add Budget";

            ModelState.Remove("Id");
            if (!ModelState.IsValid)
            {
                return ReturnViewWithDateError(vm);
            }

            ViewBag.Title = "Edit Budget";

            return vm.Id == 0 ? AddBudgetReturnEditBudgetView(vm, isDone) : EditBudget(vm, isDone);
        }
        public Budget AddBudget(EditBudgetViewModel vm, string username)
        {
            var budget = new Budget
            {
                Name = vm.Name,
                StartDate = vm.StartDate,
                EndDate = vm.EndDate,
                Username = username
            };

            FinancialPlannerRepository.AddBudget(budget);
            FinancialPlannerRepository.Save();

            return budget;
        }
        public EditBudgetViewModel MapEditBudgetViewModel(int id)
        {
            var budget = FinancialPlannerRepository.GetBudgets()
               .FirstOrDefault(m => m.Id == id);

            if (budget == null)
                return null;

            var vm = new EditBudgetViewModel
            {
                Id = budget.Id,
                Name = budget.Name,
                StartDate = budget.StartDate,
                EndDate = budget.EndDate
            };

            return vm;
        }
        public bool EditBudget(EditBudgetViewModel vm)
        {
            var budget = FinancialPlannerRepository.GetBudgets().FirstOrDefault(m => m.Id == vm.Id);

            if (budget == null)
                return false;

            budget.Name = vm.Name;
            budget.StartDate = vm.StartDate;
            budget.EndDate = vm.EndDate;

            FinancialPlannerRepository.Save();

            return true;
        }
        private ActionResult ReturnViewWithDateError(EditBudgetViewModel vm)
        {
            if (vm.EndDate < vm.StartDate)
            {
                ModelState.AddModelError("StartEnd", "End Date must be later than Start Date");
            }

            return PartialView("EditBudget", vm);
        }
        private ActionResult AddBudgetReturnEditBudgetView(EditBudgetViewModel vm, bool isDone = false)
        {
            var username = User.Identity.Name;
            var budget = BudgetService.AddBudget(vm, username);

            ModelState.Clear();
            vm.Id = budget.Id;

            if (isDone)
                return RedirectToAction("Index", new {budgetId = budget.Id});

            return PartialView("EditBudget", vm);
        }
        public ActionResult EditBudget(EditBudgetViewModel vm, bool isDone = false)
        {
            ViewBag.Title = "Edit Budget";

            if (!ModelState.IsValid)
                return ReturnViewWithDateError(vm);

            var saved = BudgetService.EditBudget(vm);

            if (!saved)
                return PartialView("Error");

            if (isDone)
                return RedirectToAction("Index", new {budgetId = vm.Id});

            return PartialView("EditBudget", vm);
        }