public ActionResult AddExpense(EditExpenseViewModel vm, bool isDone = false)
        {
            ViewBag.Title = "Edit Expense";

            if (vm.ExpenseId == 0)
            {
                ModelState.Remove("ExpenseId");

                if (!ModelState.IsValid)
                {
                    ViewBag.Title = "Add Expense";
                    return PartialView("EditExpense");
                }

                var expense = ExpenseService.AddExpense(vm, User.Identity.Name);
                vm.ExpenseId = expense.Id;

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

                return PartialView("EditExpense", vm);
            }

            return isDone ? RedirectToAction("Index", new { expenseId = vm.ExpenseId }) : EditExpense(vm);
        }
Ejemplo n.º 2
0
        public Expense AddExpense(EditExpenseViewModel vm, string username)
        {
            var expense = new Expense
            {
                Name = vm.Name,
                Amount = vm.Amount,
                Balance = vm.Amount,
                InterestRate = vm.InterestRate,
                Username = username
            };

            FinancialPlannerRepository.AddExpense(expense);
            FinancialPlannerRepository.Save();

            return expense;
        }
Ejemplo n.º 3
0
        public EditExpenseViewModel MapEditExpenseViewModel(int id)
        {
            var expense = FinancialPlannerRepository.GetExpenses().FirstOrDefault(m => m.Id == id);

            if (expense == null)
                return null;

            var vm = new EditExpenseViewModel
            {
                ExpenseId = expense.Id,
                Name = expense.Name,
                Amount = expense.Amount,
                InterestRate = expense.InterestRate
            };

            return vm;
        }
Ejemplo n.º 4
0
        public bool EditExpense(EditExpenseViewModel vm)
        {
            var expense = FinancialPlannerRepository.GetExpenses().FirstOrDefault(m => m.Id == vm.ExpenseId);

            if (expense == null)
                return false;

            var transactions = FinancialPlannerRepository.GetTransactions().Where(m => m.ExpenseId == expense.Id).ToList();

            var withdrawalAmount = transactions.Where(m => m.IsWithdrawal).Sum(m => m.Amount);
            var depositAmount = transactions.Where(m => !m.IsWithdrawal).Sum(m => m.Amount);

            expense.Name = vm.Name;
            expense.Amount = vm.Amount;
            expense.InterestRate = vm.InterestRate;
            expense.Balance = vm.Amount + (depositAmount - withdrawalAmount);

            FinancialPlannerRepository.EditExpense(expense);
            FinancialPlannerRepository.Save();

            return true;
        }
        public ActionResult EditExpense(EditExpenseViewModel vm, bool isDone = false)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.Title = "Edit Expense";
                return View(vm);
            }

            var saved = ExpenseService.EditExpense(vm);

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

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

            return PartialView("EditExpense", vm);
        }