예제 #1
0
        public Transaction AddBudgetItemTransaction(EditBudgetItemTransactionViewModel vm)
        {
            var budgetItemTransaction = new Transaction
            {
                Name = vm.Name,
                AccountId = vm.SelectedAccountId == -1 ? null : vm.SelectedAccountId,
                ExpenseId = vm.SelectedExpenseId == -1 ? null : vm.SelectedExpenseId,
                Amount = vm.Amount,
                BudgetItemId = vm.SelectedBudgetItemId,
                IsWithdrawal = vm.IsWithdrawal,
                PaymentDate = vm.PaymentDate
            };

            FinancialPlannerRepository.AddTransaction(budgetItemTransaction);
            FinancialPlannerRepository.Save();

            return budgetItemTransaction;
        }
예제 #2
0
 public void EditAccountTransactionBalances(EditBudgetItemTransactionViewModel vm, Account account,
     IList<Transaction> transactions)
 {
     SharedOperationsService.AdjustTransactionBalances(vm.SelectedAccountId, account);
 }
예제 #3
0
 public void EditAccountAmount(EditBudgetItemTransactionViewModel vm, Account account, IList<Transaction> transactions)
 {
     SharedOperationsService.AdjustAccountAmount(account);
 }
예제 #4
0
        public EditBudgetItemTransactionViewModel MapEditBudgetItemTransactionViewModelForChange(EditBudgetItemTransactionViewModel vm, string username)
        {
            var budgetItems = FinancialPlannerRepository.GetBudgetItems().Where(m => m.BudgetId == vm.SelectedBudgetId).ToList();

            var budgetItemTransaction = FinancialPlannerRepository.GetTransactions()
                .FirstOrDefault(m => m.Id == vm.BudgetItemTransactionId);

            if (budgetItemTransaction == null)
                return null;

            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 != null && m.BudgetItems.Any()).ToList();

            var expenses = FinancialPlannerRepository.GetExpenses().Where(m => m.Username == username);

            vm.BudgetItems = SetViewModelsService.SetBudgetItemViewModels(budgetItems);
            vm.Budgets = SetViewModelsService.SetBudgetViewModels(budgets);
            vm.Accounts = SetViewModelsService.SetAccountViewModels(accounts);
            vm.Expenses = SetViewModelsService.SetExpenseViewModels(expenses);

            var selectedBudgetItem = budgetItems.FirstOrDefault();
            vm.SelectedBudgetItemId = selectedBudgetItem != null ? selectedBudgetItem.Id : vm.SelectedBudgetItemId;

            return vm;
        }
예제 #5
0
        public EditBudgetItemTransactionViewModel MapEditBudgetItemTransactionViewModelForEdit(int id, string username)
        {
            var budgetItems = new List<BudgetItem>();

            var budgetItemTransaction = FinancialPlannerRepository.GetTransactions()
                .FirstOrDefault(m => m.Id == id);

            if (budgetItemTransaction == null)
                return null;

            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 != null && m.BudgetItems.Any()).ToList();

            var expenses = FinancialPlannerRepository.GetExpenses().Where(m => m.Username == username);

            var budgetItemForTransaction = FinancialPlannerRepository.GetBudgetItems()
                .FirstOrDefault(m => m.Id == budgetItemTransaction.BudgetItemId);

            if (budgetItemForTransaction != null)
            {
                budgetItems = FinancialPlannerRepository.GetBudgetItems()
                    .Where(m => m.BudgetId == budgetItemForTransaction.BudgetId).ToList();
            }

            var vm = new EditBudgetItemTransactionViewModel
            {
                BudgetItemTransactionId = budgetItemTransaction.Id,
                Name = budgetItemTransaction.Name,
                Amount = budgetItemTransaction.Amount,
                PaymentDate = budgetItemTransaction.PaymentDate < DateTime.Now.AddYears(-50) ?
                                budgetItemTransaction.PaymentDate : DateTime.Now,
                OldAmount = budgetItemTransaction.Amount,
                OldAccountId = budgetItemTransaction.AccountId,
                WasWithdrawal = budgetItemTransaction.IsWithdrawal,
                Accounts = SetViewModelsService.SetAccountViewModels(accounts),
                BudgetItems = SetViewModelsService.SetBudgetItemViewModels(budgetItems),
                Budgets = SetViewModelsService.SetBudgetViewModels(budgets),
                Expenses = SetViewModelsService.SetExpenseViewModels(expenses),
                SelectedBudgetId = budgetItemForTransaction == null ? -1 : budgetItemForTransaction.BudgetId,
                SelectedBudgetItemId = budgetItemForTransaction == null ? -1 : budgetItemForTransaction.Id,
                IsWithdrawal = budgetItemTransaction.IsWithdrawal
            };

            var account = FinancialPlannerRepository.GetAccounts()
                               .FirstOrDefault(m => m.Id == budgetItemTransaction.AccountId);

            if (account != null)
                vm.SelectedAccountId = account.Id;

            var expense = FinancialPlannerRepository.GetExpenses()
                .FirstOrDefault(m => m.Id == budgetItemTransaction.ExpenseId);

            if (expense != null)
                vm.SelectedExpenseId = expense.Id;

            return vm;
        }
예제 #6
0
        public EditBudgetItemTransactionViewModel MapEditBudgetItemTransactionViewModelChangeBudget(string username,
            EditBudgetItemTransactionViewModel vm)
        {
            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 != null && m.BudgetItems.Any()).ToList();
            var budgetItems = FinancialPlannerRepository.GetBudgetItems().Where(m => m.BudgetId == vm.SelectedBudgetId).ToList();
            var expenses = FinancialPlannerRepository.GetExpenses().Where(m => m.Username == username);

            vm.PaymentDate = DateTime.Now;
            vm.BudgetItems = SetViewModelsService.SetBudgetItemViewModels(budgetItems);
            vm.Accounts = SetViewModelsService.SetAccountViewModels(accounts);
            vm.Budgets = SetViewModelsService.SetBudgetViewModels(budgets);
            vm.Expenses = SetViewModelsService.SetExpenseViewModels(expenses);

            var selectedBudgetItem = budgetItems.Any() ? budgetItems.FirstOrDefault() : null;

            if (selectedBudgetItem != null)
                vm.SelectedBudgetItemId = selectedBudgetItem.Id;

            return vm;
        }
예제 #7
0
        public EditBudgetItemTransactionViewModel MapEditBudgetItemTransactionViewModelForAdd(string username, int budgetItemId)
        {
            var accounts = FinancialPlannerRepository.GetAccounts()
                .Where(m => m.UserName == username);

            var selectedBudgetItem =
                FinancialPlannerRepository.GetBudgetItems().FirstOrDefault(m => m.Id == budgetItemId);

            if (selectedBudgetItem == null)
                return null;

            var budgets = FinancialPlannerRepository.GetBudgets().Where(m => m.Username == username).ToList();
            budgets = budgets.Where(m => m.BudgetItems != null && m.BudgetItems.Any()).ToList();
            var budgetItems = FinancialPlannerRepository.GetBudgetItems().Where(m => m.BudgetId == selectedBudgetItem.BudgetId);
            var expenses = FinancialPlannerRepository.GetExpenses().Where(m => m.Username == username);

            var vm = new EditBudgetItemTransactionViewModel
            {
                PaymentDate = DateTime.Now,
                BudgetItems = SetViewModelsService.SetBudgetItemViewModels(budgetItems),
                Accounts = SetViewModelsService.SetAccountViewModels(accounts),
                Budgets = SetViewModelsService.SetBudgetViewModels(budgets),
                Expenses = SetViewModelsService.SetExpenseViewModels(expenses),
                SelectedBudgetId = selectedBudgetItem.BudgetId,
                SelectedBudgetItemId = selectedBudgetItem.Id
            };

            return vm;
        }
예제 #8
0
        public bool EditBudgetItemForAdd(EditBudgetItemTransactionViewModel vm)
        {
            var budgetItem =
               FinancialPlannerRepository.GetBudgetItems().FirstOrDefault(m => m.Id == vm.SelectedBudgetItemId);

            if (budgetItem == null)
                return false;

            budgetItem.Balance = vm.IsWithdrawal
              ? budgetItem.Balance - vm.Amount
              : budgetItem.Balance + vm.Amount;

            FinancialPlannerRepository.Save();

            return true;
        }
예제 #9
0
        public void EditBudgetTransaction(Transaction transaction, EditBudgetItemTransactionViewModel vm)
        {
            transaction.Amount = vm.Amount;
            transaction.AccountId = vm.SelectedAccountId == -1 ? null : vm.SelectedAccountId;
            transaction.ExpenseId = vm.SelectedExpenseId == -1 ? null : vm.SelectedExpenseId;
            transaction.BudgetItemId = vm.SelectedBudgetItemId;
            transaction.IsWithdrawal = vm.IsWithdrawal;
            transaction.PaymentDate = vm.PaymentDate;
            transaction.Name = vm.Name;

            FinancialPlannerRepository.EditTransaction(transaction);
            FinancialPlannerRepository.Save();
        }
        private ActionResult FindBudgetItemsAndReturnEditTransactionView(ref EditBudgetItemTransactionViewModel vm)
        {
            vm = BudgetService.MapEditBudgetItemTransactionViewModelForChange(vm,
                User.Identity.Name);

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

            return PartialView("EditBudgetItemTransaction", vm);
        }
        private ActionResult FindBudgetItemsReturnEditTransactionView(ref EditBudgetItemTransactionViewModel vm, string username)
        {
            vm = BudgetService.MapEditBudgetItemTransactionViewModelChangeBudget(username, vm);
            vm.FindingBudgetItems = false;

            ModelState.Clear();

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

            if (expense != null)
            {
                BudgetService.AdjustExpenseBalance(expense, budgetItemTransaction);
            }
        }
        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);
            }
        }
        private void EditAccountAmountAndTransactionBalances(EditBudgetItemTransactionViewModel vm, Account account)
        {
            var transactions =
                FinancialPlannerRepository.GetTransactions()
                    .Where(m => m.AccountId == vm.SelectedAccountId)
                    .ToList();

            BudgetService.EditAccountAmount(vm, account, transactions);

            BudgetService.EditAccountTransactionBalances(vm, account, transactions);
        }
        public ActionResult EditBudgetItemTransaction(EditBudgetItemTransactionViewModel vm, bool isDone)
        {
            @ViewBag.Title = "Edit Budget Transaction";

            if (vm.FindingBudgetItems)
            {
                return FindBudgetItemsAndReturnEditTransactionView(ref vm);
            }

            var username = User.Identity.Name;

            if (vm.SelectedBudgetItemId == NotSelected)
            {
                ModelState.AddModelError("SelectedBudgetItemId", "Must select a budget item");
            }

            if (!ModelState.IsValid)
            {
                ViewBag.Title = "Edit Budget Transaction";
                vm = BudgetService.MapEditBudgetItemTransactionViewModelChangeBudget(username, vm);
                return PartialView("EditBudgetItemTransaction", vm);
            }

            var budgetItemTransaction =
                FinancialPlannerRepository.GetTransactions().FirstOrDefault(m => m.Id == vm.BudgetItemTransactionId);

            if (budgetItemTransaction == null || !ModelState.IsValid)
                return View("Error");

            var oldBudgetItemId = budgetItemTransaction.BudgetItemId;
            var wasWithdrawal = budgetItemTransaction.IsWithdrawal;
            var oldTransactionAmount = budgetItemTransaction.Amount;
            var oldExpenseId = budgetItemTransaction.ExpenseId;

            BudgetService.EditBudgetTransaction(budgetItemTransaction, vm);

            if (vm.SelectedAccountId != -1)
            {
                var account = FinancialPlannerRepository.GetAccounts()
                   .FirstOrDefault(m => m.Id == vm.SelectedAccountId);

                if (account == null)
                    return View("Error");

                EditAccountAmountAndTransactionBalances(vm, account);
            }

            if (vm.OldAccountId != null && vm.OldAccountId != 0)
            {
                var originalAccount = FinancialPlannerRepository.GetAccounts().FirstOrDefault(m => m.Id == vm.OldAccountId);

                if (originalAccount == null)
                    return View("Error");

                EditAccountAmountAndTransactionBalances(vm, originalAccount);
            }

            EditBudgetItemBalances(vm, oldBudgetItemId, budgetItemTransaction, oldTransactionAmount, wasWithdrawal);

            AdjustExpenseBalances(vm, oldExpenseId);

            vm = BudgetService.MapEditBudgetItemTransactionViewModelChangeBudget(username, vm);

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

            return PartialView("EditBudgetItemTransaction", vm);
        }
        public ActionResult AddBudgetItemTransaction(EditBudgetItemTransactionViewModel vm, bool isDone)
        {
            ViewBag.Title = "Add Budget Item Transaction";
            var username = User.Identity.Name;

            if (vm.FindingBudgetItems)
            {
                return FindBudgetItemsReturnEditTransactionView(ref vm, username);
            }

            if (vm.BudgetItemTransactionId == 0)
            {
                if (vm.SelectedBudgetId == NotSelected)
                {
                    ModelState.AddModelError("SelectedBudgetId", "A budget must be selected");
                }

                if (!ModelState.IsValid)
                {
                    vm = BudgetService.MapEditBudgetItemTransactionViewModelChangeBudget(username, vm);
                    return View("EditBudgetItemTransaction", vm);
                }

                ViewBag.Title = "Edit Budget Item Transaction";

                var budgetItemTransaction = BudgetService.AddBudgetItemTransaction(vm);

                ModelState.Remove("BudgetItemTransactionId");

                vm.BudgetItemTransactionId = budgetItemTransaction.Id;

                var budgetItemSaved = BudgetService.EditBudgetItemForAdd(vm);

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

                if (vm.SelectedAccountId != -1)
                {
                    var account = FinancialPlannerRepository.GetAccounts().FirstOrDefault(m => m.Id == vm.SelectedAccountId);

                    if (account == null)
                        return View("Error");

                    EditAccountAmountAndTransactionBalances(vm, account);
                }

                EditExpenseBalance(vm, budgetItemTransaction);

                vm = BudgetService.MapEditBudgetItemTransactionViewModelChangeBudget(username, vm);

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

                return PartialView("EditBudgetItemTransaction", vm);
            }

            ViewBag.Title = "Edit Budget Transaction";

            return isDone ? RedirectToAction("Index", new { budgetId = vm.SelectedBudgetId, budgetItemId = vm.SelectedBudgetItemId }) : EditBudgetItemTransaction(vm, false);
        }