public ActionResult Index(int?id)
        {
            if (id == null)
            {
                return(RedirectToAction("Index", "Errors", new { errorMessage = "Account not found" }));
            }
            Account account = db.Accounts.Find(id);

            if (account == null)
            {
                return(RedirectToAction("Index", "Errors", new { errorMessage = "Account not found" }));
            }
            //check if the user is authorized to view this account
            var helper = new AccountUserHelper();
            var user   = User.Identity.GetUserId();

            if (helper.CanUserAccessAccount(user, (int)id) == false)
            {
                return(RedirectToAction("Index", "Errors", new { errorMessage = "Not authorized" }));
            }

            var createTransactionModel = new TransactionCreateViewModel();

            createTransactionModel.AccountId = account.Id;
            var categories = db.Categories.ToList();

            createTransactionModel.CategoryList = new SelectList(categories, "Id", "Name");
            var householdId    = User.Identity.GetHouseholdId();
            var householdUsers = db.Users.Where(x => x.HouseholdId == (int)householdId).ToList();

            createTransactionModel.HouseholdUsersList = new SelectList(householdUsers, "Id", "UserName");
            //pass a model to create a new transaction through the ViewBag
            ViewBag.CreateModel = createTransactionModel;

            //get all the transactions for this account
            var transactions = db.Transactions.Where(x => x.AccountId == id).ToList();
            //transform the transactions so we can show them in the index page
            var transactionsToShow = new List <TransactionsIndexViewModel>();

            foreach (var t in transactions)
            {
                var transToShow = new TransactionsIndexViewModel(t);
                transactionsToShow.Add(transToShow);
            }

            //pass the account Id to the model
            ViewBag.AccountId = account.Id;
            //get the account name and put it in the ViewBag
            ViewBag.AccountName = account.Name;
            //update the account balance
            account.UpdateAccountBalance();
            //update the reconciled balance
            account.UpdateReconciledAccountBalance();
            //pass the balances in the ViewBag
            ViewBag.Balance         = account.Balance;
            ViewBag.Reconciled      = account.ReconciledBalance;
            ViewBag.StartingBalance = account.StartingBalance;

            return(View(transactionsToShow));
        }
Esempio n. 2
0
        public async Task <IActionResult> Index(TransactionsIndexViewModel viewModel = null)
        {
            if (viewModel is null)
            {
                viewModel = new TransactionsIndexViewModel();
            }

            viewModel.TransactionsFilter.CategoriesList = new SelectList(
                await _categoriesDataService.RetrieveCategoriesAsync().ConfigureAwait(false),
                nameof(Category.Id),
                nameof(Category.Name));

            viewModel.TransactionsFilter.IsCreditList = new SelectList(
                new[]
            {
                new { Value = true.ToString(CultureInfo.InvariantCulture), Text = "Yes" },
                new { Value = false.ToString(CultureInfo.InvariantCulture), Text = "No" }
            },
                nameof(SelectListItem.Value),
                nameof(SelectListItem.Text));

            viewModel.TransactionsFilter.PageSizesList = new SelectList(
                new[] { 10, 25, 50, 100 }.Select(x => new
            {
                Value = x.ToString(CultureInfo.InvariantCulture),
                Text  = x.ToString(CultureInfo.InvariantCulture)
            }),
                nameof(SelectListItem.Value),
                nameof(SelectListItem.Text));

            var query = _transactionsDataService.BuildTransactionsQuery();

            query = viewModel.TransactionsFilter.ApplyFilteringSortingAndPaging(query);

            var transactions = await _transactionsDataService.ExecuteTransactionsQueryAsync(query).ConfigureAwait(false);

            foreach (var transaction in transactions)
            {
                viewModel.TransactionsList.Add(transaction);
            }

            return(View(viewModel));
        }
Esempio n. 3
0
        // GET: Transactions
        public IActionResult Index(int?id)
        {
            var applicationDbContext = _context.Transaction.Include(t => t.SubTransactionCategory)
                                       .Include(t => t.FinancialAccount)
                                       .Include(t => t.TargetFinancialAccount);

            TransactionsIndexViewModel transactionIndexViewModel = new TransactionsIndexViewModel();

            IQueryable <Transaction> transactionIndexList;

            if (id != null)
            {
                transactionIndexList = from transaction in applicationDbContext
                                       where transaction.FinancialAccountID == id
                                       orderby transaction.Time descending
                                       select transaction;
            }
            else
            {
                transactionIndexList = from transaction in applicationDbContext
                                       orderby transaction.Time descending
                                       select transaction;
            }

            // 取得不重複的交易帳戶名稱
            var acIDAuqry = (from tran in transactionIndexList
                             select tran.FinancialAccount.Name)
                            .Distinct();

            transactionIndexViewModel.FinacialAccountNameList = acIDAuqry.ToList();

            ViewBag.viewModel = transactionIndexViewModel;

            ViewBag.viewModelCreate = GetTransactionsCreateViewModel();

            return(View(transactionIndexList.ToList()));
        }