private void EditExpenseBalance(EditBudgetItemTransactionViewModel vm, Transaction budgetItemTransaction)
        {
            var expense = FinancialPlannerRepository.GetExpenses().FirstOrDefault(m => m.Id == vm.SelectedExpenseId);

            if (expense != null)
            {
                BudgetService.AdjustExpenseBalance(expense, budgetItemTransaction);
            }
        }
        public ActionResult AddTransaction(EditExpenseTransactionViewModel vm, bool isDone = false)
        {
            ViewBag.Title = "Add Transaction";

            var transaction = new Transaction();

            if (vm.FindingBudgetItems)
            {
                return FindBudgetItemsAndReturn(vm);
            }

            if (vm.SelectedExpenseId == NotSelected)
            {
                ModelState.AddModelError("SelectedExpenseId", "An expense must be selected");
            }

            if (!ModelState.IsValid)
            {
                ExpenseService.MapEditTransactionViewModel(vm, User.Identity.Name);

                return PartialView("EditTransaction", vm);
            }

            if (vm.ExpenseTransactionId == 0)
            {
                AdjustAccountAndTransactionBalances(vm);

                if (vm.SelectedBudgetItemId != null && vm.SelectedBudgetItemId > 0)
                {
                    AdjustBudgetItemBalance(vm);
                }

                var expense = FinancialPlannerRepository.GetExpenses().FirstOrDefault(m => m.Id == vm.SelectedExpenseId);

                transaction = ExpenseService.AddTransaction(expense, vm);
                ModelState.Remove("ExpenseTransactionId");
                vm.ExpenseTransactionId = transaction.Id;

                if (expense != null)
                {
                    ExpenseService.AdjustExpenseBalance(expense, transaction);
                }

                ExpenseService.MapEditTransactionViewModel(vm, User.Identity.Name);

                ViewBag.Title = "Edit Transaction";

                if (isDone)
                    return RedirectToAction("Index", new { expenseId = transaction.ExpenseId });

                return PartialView("EditTransaction", vm);
            }

            return isDone ? RedirectToAction("Index", new { expenseId = transaction.ExpenseId }) : EditTransaction(vm);
        }
        private void EditBudgetItemBalances(EditBudgetItemTransactionViewModel vm, int? oldBudgetItemId,
            Transaction budgetItemTransaction, decimal oldTransactionAmount, bool wasWithdrawal)
        {
            var oldBudgetItem =
                FinancialPlannerRepository.GetBudgetItems().FirstOrDefault(m => m.Id == oldBudgetItemId);
            var budgetItem =
                FinancialPlannerRepository.GetBudgetItems()
                    .FirstOrDefault(m => m.Id == budgetItemTransaction.BudgetItemId);

            if (oldBudgetItem != null)
            {
                BudgetService.EditBudgetItemBalance(oldBudgetItem);
            }

            if (budgetItem != null)
            {
                BudgetService.EditBudgetItemBalance(budgetItem);
            }
        }