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);
        }
        public Transaction AddTransaction(Expense expense, EditExpenseTransactionViewModel vm)
        {
            var newTransaction = new Transaction
            {
                Name = vm.Name,
                Amount = vm.Amount,
                IsWithdrawal = vm.IsWithdrawal,
                ExpenseId = vm.SelectedExpenseId != -1 ? vm.SelectedExpenseId : 0,
                PaymentDate = vm.PaymentDate,
                Balance = expense.Amount,
                BudgetItemId = vm.SelectedBudgetItemId
            };

            FinancialPlannerRepository.AddTransaction(newTransaction);
            FinancialPlannerRepository.Save();

            return newTransaction;
        }
 public void AdjustNewBudgetItemBalance(EditExpenseTransactionViewModel vm)
 {
     SharedOperationsService.AdjustNewBudgetItemBalance(vm.SelectedBudgetItemId);
 }
 public void AdjustBudgetItemBalance(BudgetItem budgetItem, EditExpenseTransactionViewModel vm)
 {
     SharedOperationsService.AdjustBudgetItemBalance(budgetItem, vm.IsWithdrawal, vm.Amount);
 }
 public void AdjustAccountBalance(Account account, EditExpenseTransactionViewModel vm)
 {
     SharedOperationsService.AdjustAccountBalance(account, vm.IsWithdrawal, vm.Amount);
 }
        public EditExpenseTransactionViewModel MapEditTransactionViewModelForEdit(int id, int expenseId, string username)
        {
            var transaction = FinancialPlannerRepository.GetTransactions()
              .FirstOrDefault(m => m.Id == id);

            if (transaction == null)
                return null;

            var expenses = FinancialPlannerRepository.GetExpenses().Where(m => m.Username == username);
            var accounts = FinancialPlannerRepository.GetAccounts()
                .Where(m => m.UserName == username);
            var budgets = FinancialPlannerRepository.GetBudgets()
                .Where(m => m.Username == username).ToList();

            var vm = new EditExpenseTransactionViewModel
            {
                Accounts = SetViewModelsService.SetAccountViewModels(accounts),
                ExpenseTransactionId = transaction.Id,
                Amount = transaction.Amount,
                IsWithdrawal = transaction.IsWithdrawal,
                Name = transaction.Name,
                PaymentDate = transaction.PaymentDate,
                SelectedExpenseId = expenseId != 0 ? expenseId : 0,
                OldExpenseId = expenseId != 0 ? expenseId : 0,
                WasWithdrawal = transaction.IsWithdrawal,
                OldAmount = transaction.Amount,
                Budgets = SetViewModelsService.SetBudgetViewModels(budgets),
                Expenses = SetViewModelsService.SetExpenseViewModels(expenses),
                SelectedAccountId = transaction.AccountId,
                SelectedBudgetItemId = transaction.BudgetItemId
            };

            if (budgets.Any())
            {
                var budgetIds = budgets.Select(m => m.Id);
                var budgetItems = FinancialPlannerRepository.GetBudgetItems().Where(m => budgetIds.Contains(m.BudgetId));

                vm.BudgetItems = SetViewModelsService.SetBudgetItemViewModels(budgetItems);
            }

            var budgetItemForExpense = FinancialPlannerRepository.GetBudgetItems().FirstOrDefault(m => m.Id == transaction.BudgetItemId);

            if (budgetItemForExpense != null)
                vm.SelectedBudgetId = budgetItemForExpense.BudgetId;

            return vm;
        }
        public EditExpenseTransactionViewModel MapEditTransactionViewModelForAdd(int id, string username)
        {
            var accounts = FinancialPlannerRepository.GetAccounts()
               .Where(m => m.UserName == username);
            var budgets = FinancialPlannerRepository.GetBudgets()
                .Where(m => m.Username == username).ToList();
            budgets = budgets.Where(m => m.BudgetItems.Any()).ToList();
            var expenses = FinancialPlannerRepository.GetExpenses().Where(m => m.Username == username);

            var vm = new EditExpenseTransactionViewModel
            {
                Accounts = SetViewModelsService.SetAccountViewModels(accounts),
                Budgets = budgets.Any() ? SetViewModelsService.SetBudgetViewModels(budgets) : null,
                SelectedAccountId = id != 0 ? id : 0,
                PaymentDate = DateTime.Now,
                NewTransaction = true,
                Expenses = SetViewModelsService.SetExpenseViewModels(expenses)
            };

            return vm;
        }
 public void MapEditTransactionViewModel(EditExpenseTransactionViewModel vm, string username)
 {
     var expenses = FinancialPlannerRepository.GetExpenses().Where(m => m.Username == username);
     var accounts = FinancialPlannerRepository.GetAccounts().Where(m => m.UserName == username);
     var budgets = FinancialPlannerRepository.GetBudgets().Where(m => m.Username == username);
     var budgetItems =
         FinancialPlannerRepository.GetBudgetItems().Where(m => m.BudgetId == vm.SelectedBudgetId);
     vm.BudgetItems = SetViewModelsService.SetBudgetItemViewModels(budgetItems);
     vm.Accounts = SetViewModelsService.SetAccountViewModels(accounts);
     vm.Budgets = SetViewModelsService.SetBudgetViewModels(budgets);
     vm.Expenses = SetViewModelsService.SetExpenseViewModels(expenses);
 }
        public Transaction EditTransaction(EditExpenseTransactionViewModel vm)
        {
            var transaction = FinancialPlannerRepository.GetTransactions().FirstOrDefault(m => m.Id == vm.ExpenseTransactionId);

            if (transaction != null)
            {
                transaction.Amount = vm.Amount;
                transaction.Name = vm.Name;
                transaction.PaymentDate = vm.PaymentDate;
                transaction.IsWithdrawal = vm.IsWithdrawal;
                transaction.BudgetItemId = vm.SelectedBudgetItemId == -1 ? null : vm.SelectedBudgetItemId;
                transaction.AccountId = vm.SelectedAccountId == -1 ? null : vm.SelectedAccountId;
                transaction.ExpenseId = vm.SelectedExpenseId;

                FinancialPlannerRepository.EditTransaction(transaction);
                FinancialPlannerRepository.Save();
            }

            return transaction;
        }
Exemplo n.º 10
0
 public void AdjustTransactionBalances(Account account, EditExpenseTransactionViewModel vm)
 {
     SharedOperationsService.AdjustTransactionBalances(vm.SelectedAccountId, account);
 }
        private ActionResult FindBudgetItemsAndReturn(EditExpenseTransactionViewModel vm)
        {
            ExpenseService.MapEditTransactionViewModel(vm, User.Identity.Name);

            ModelState.Remove("FindingBudgetItems");
            vm.FindingBudgetItems = false;

            return PartialView("EditTransaction", vm);
        }
        private void AdjustBudgetItemBalances(EditExpenseTransactionViewModel vm, int? oldBudgetItemId)
        {
            ExpenseService.AdjustOldBudgetItemBalance(oldBudgetItemId);

            if (oldBudgetItemId != vm.SelectedBudgetItemId)
            {
                ExpenseService.AdjustNewBudgetItemBalance(vm);
            }
        }
        private void AdjustBudgetItemBalance(EditExpenseTransactionViewModel vm)
        {
            var budgetItem =
                FinancialPlannerRepository.GetBudgetItems().FirstOrDefault(m => m.Id == vm.SelectedBudgetItemId);

            if (budgetItem != null)
                ExpenseService.AdjustBudgetItemBalance(budgetItem, vm);
        }
        private void AdjustAccountAndTransactionBalances(EditExpenseTransactionViewModel vm)
        {
            var account = FinancialPlannerRepository.GetAccounts().FirstOrDefault(m => m.Id == vm.SelectedAccountId);

            if (account != null)
            {
                ExpenseService.AdjustAccountBalance(account, vm);
                ExpenseService.AdjustTransactionBalances(account, vm);
            }
        }
        private void AdjustAccountAmountsAndBalances(EditExpenseTransactionViewModel vm, int? oldAccountId)
        {
            var account = FinancialPlannerRepository.GetAccounts().FirstOrDefault(m => m.Id == vm.SelectedAccountId);
            var originalAccount = FinancialPlannerRepository.GetAccounts().FirstOrDefault(m => m.Id == oldAccountId);

            if (account != null)
            {
                ExpenseService.AdjustTransactionBalances(account);
                ExpenseService.AdjustAccountAmount(account);
            }

            if (originalAccount != null && account != null && (account.Amount != originalAccount.Amount))
            {
                var oldAccount = FinancialPlannerRepository.GetAccounts().FirstOrDefault(m => m.Id == vm.OldExpenseId);

                if (oldAccount != null && account.Id != oldAccount.Id)
                {
                    ExpenseService.AdjustTransactionBalances(oldAccount);
                }
            }

            if (originalAccount != null && (originalAccount.Id != vm.SelectedAccountId && originalAccount.Id != 0))
            {
                ExpenseService.AdjustAccountAmount(originalAccount);
            }
        }
        public ActionResult EditTransaction(EditExpenseTransactionViewModel vm, bool isDone = false)
        {
            @ViewBag.Title = "Edit 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);
            }

            var transaction = FinancialPlannerRepository.GetTransactions().FirstOrDefault(m => m.Id == vm.ExpenseTransactionId);

            if (transaction == null)
                return PartialView("Error");

            ExpenseService.EditTransaction(vm);

            var oldBudgetItemId = transaction.BudgetItemId;
            var oldExpenseId = transaction.ExpenseId;
            var oldAccountId = transaction.AccountId;

            transaction.AccountId = vm.SelectedAccountId;

            AdjustBudgetItemBalances(vm, oldBudgetItemId);
            AdjustAccountAmountsAndBalances(vm, oldAccountId);

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

            if (expense == null)
                return PartialView("Error");

            AdjustExpenseBalances(expense, oldExpenseId);

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

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

            return PartialView("EditTransaction", vm);
        }