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); } }
private void AdjustBudgetItemBalances(EditExpenseTransactionViewModel vm, int?oldBudgetItemId) { ExpenseService.AdjustOldBudgetItemBalance(oldBudgetItemId); if (oldBudgetItemId != vm.SelectedBudgetItemId) { ExpenseService.AdjustNewBudgetItemBalance(vm); } }
public void AdjustNewBudgetItemBalance(EditExpenseTransactionViewModel vm) { var budgetItem = FinancialPlannerRepository.GetBudgetItems().FirstOrDefault(m => m.Id == vm.SelectedBudgetItemId); if (budgetItem != null) { EditBudgetItemBalance(budgetItem); } }
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)); }
private ActionResult FindBudgetItemsAndReturn(EditExpenseTransactionViewModel vm) { ExpenseService.MapEditTransactionViewModel(vm, User.Identity.Name); ModelState.Remove("FindingBudgetItems"); vm.FindingBudgetItems = false; return(PartialView("EditTransaction", vm)); }
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 AdjustAccountAndTransactionBalances(EditExpenseTransactionViewModel vm) { var account = FinancialPlannerRepository.GetAccounts().FirstOrDefault(m => m.Id == vm.SelectedAccountId); if (account != null) { ExpenseService.AdjustAccountBalance(account, vm); ExpenseService.AdjustTransactionBalances(account, 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 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 void AdjustBudgetItemBalance(BudgetItem budgetItem, EditExpenseTransactionViewModel vm) { budgetItem.Balance = vm.IsWithdrawal ? budgetItem.Balance - vm.Amount : budgetItem.Balance + vm.Amount; if (budgetItem.Balance > budgetItem.Amount) { budgetItem.Amount = budgetItem.Balance; } FinancialPlannerRepository.EditBudgetItem(budgetItem); FinancialPlannerRepository.Save(); }
public void AdjustAccountBalance(Account account, EditExpenseTransactionViewModel vm) { if (vm.IsWithdrawal) { account.Amount = account.Amount - vm.Amount; } else { account.Amount = account.Amount + vm.Amount; } FinancialPlannerRepository.EditAccount(account); FinancialPlannerRepository.Save(); }
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 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); }
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 AdjustTransactionBalances(Account account, EditExpenseTransactionViewModel vm) { var transactions = FinancialPlannerRepository.GetTransactions().Where(m => m.AccountId == vm.SelectedAccountId).ToList(); var accountTransactions = transactions .OrderBy(m => m.PaymentDate); var firstTransaction = accountTransactions.FirstOrDefault(); if (firstTransaction != null) { var previousTransaction = new Transaction(); foreach (var tran in accountTransactions) { if (tran.PaymentDate == firstTransaction.PaymentDate) { tran.Balance = tran.IsWithdrawal ? account.InitialAmount - tran.Amount : account.InitialAmount + tran.Amount; } else { tran.Balance = tran.IsWithdrawal ? previousTransaction.Balance - tran.Amount : previousTransaction.Balance + tran.Amount; } //FinancialPlannerRepository.EditTransaction(tran); FinancialPlannerRepository.Save(); previousTransaction = tran; } } }
public void AdjustAccountBalance(Account account, EditExpenseTransactionViewModel vm) { SharedOperationsService.AdjustAccountBalance(account, vm.IsWithdrawal, vm.Amount); }
public void AdjustTransactionBalances(Account account, EditExpenseTransactionViewModel vm) { SharedOperationsService.AdjustTransactionBalances(vm.SelectedAccountId, account); }
public void AdjustBudgetItemBalance(BudgetItem budgetItem, EditExpenseTransactionViewModel vm) { SharedOperationsService.AdjustBudgetItemBalance(budgetItem, vm.IsWithdrawal, vm.Amount); }
public void AdjustNewBudgetItemBalance(EditExpenseTransactionViewModel vm) { SharedOperationsService.AdjustNewBudgetItemBalance(vm.SelectedBudgetItemId); }