public ActionResult AddBudgetItem(EditBudgetItemViewModel vm, bool isDone)
        {
            ViewBag.Title = "Add Budget Item";

            var budgets = FinancialPlannerRepository.GetBudgets().Where(m => m.Username == User.Identity.Name);
            vm.Budgets = SetViewModelsService.SetBudgetViewModels(budgets);

            if (vm.SelectedBudgetId == NotSelected)
                ModelState.AddModelError("SelectedBudgetId", "Budget must be selected");

            if (!ModelState.IsValid)
                return PartialView("EditBudgetItem", vm);

            ViewBag.Title = "Edit Budget Item";

            var budgetItem = new BudgetItem();
            if (vm.BudgetItemId == 0)
            {
                budgetItem = BudgetService.AddBudgetItem(vm);
                ModelState.Remove("BudgetItemId");
                vm.BudgetItemId = budgetItem.Id;

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

                return PartialView("EditBudgetItem", vm);
            }

            return isDone ? RedirectToAction("Index", new {budgetId = budgetItem.BudgetId, budgetItemId = budgetItem.Id}) : EditBudgetItem(vm, false);
        }
        public BudgetItem AddBudgetItem(EditBudgetItemViewModel vm)
        {
            var budgetItem = new BudgetItem
            {
                Name = vm.Name,
                Amount = vm.Amount,
                BudgetId = vm.SelectedBudgetId,
                Balance = vm.Amount
            };

            FinancialPlannerRepository.AddBudgetItem(budgetItem);
            FinancialPlannerRepository.Save();

            return budgetItem;
        }
        public EditBudgetItemViewModel MapEditBudgetItemViewModelForAdd(string username)
        {
            var budgets = FinancialPlannerRepository.GetBudgets()
               .Where(m => m.Username == username);

            var vm = new EditBudgetItemViewModel
            {
                Budgets = SetViewModelsService.SetBudgetViewModels(budgets)
            };

            return vm;
        }
        public EditBudgetItemViewModel MapEditBudgetItemViewModel(int id, string username)
        {
            var budgetItem = FinancialPlannerRepository.GetBudgetItems()
               .FirstOrDefault(m => m.Id == id);
            var budgets = FinancialPlannerRepository.GetBudgets()
                .Where(m => m.Username == username);

            if (budgetItem == null)
                return null;

            var vm = new EditBudgetItemViewModel
            {
                BudgetItemId = budgetItem.Id,
                Name = budgetItem.Name,
                Amount = budgetItem.Amount,
                SelectedBudgetId = budgetItem.BudgetId,
                Budgets = budgets.Select(m => new BudgetViewModel
                {
                    Id = m.Id,
                    Name = m.Name,
                    StartDate = m.StartDate,
                    EndDate = m.EndDate
                }).ToList()
            };

            return vm;
        }
        public bool EditBudgetItem(EditBudgetItemViewModel vm)
        {
            var budgetItem = FinancialPlannerRepository.GetBudgetItems().FirstOrDefault(m => m.Id == vm.BudgetItemId);

            if (budgetItem == null)
                return false;

            var oldBudgetBalance = budgetItem.Balance;
            var oldBudgetAmount = budgetItem.Amount;

            budgetItem.Name = vm.Name;
            budgetItem.Amount = vm.Amount;
            budgetItem.BudgetId = vm.SelectedBudgetId;
            budgetItem.Balance = oldBudgetBalance + (vm.Amount - oldBudgetAmount);

            FinancialPlannerRepository.Save();

            return true;
        }
        public ActionResult EditBudgetItem(EditBudgetItemViewModel vm, bool isDone)
        {
            ViewBag.Title = "Edit Budget Item";

            var budgets = FinancialPlannerRepository.GetBudgets().Where(m => m.Username == User.Identity.Name);
            vm.Budgets = SetViewModelsService.SetBudgetViewModels(budgets);

            if (vm.SelectedBudgetId == NotSelected)
            {
                ModelState.AddModelError("SelectedBudgetId", "Budget must be selected");
                return PartialView("EditBudgetItem", vm);
            }

            var saved = BudgetService.EditBudgetItem(vm);

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

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

            return PartialView("EditBudgetItem", vm);
        }