// GET: OwnershipLists/Create
        public async Task <IActionResult> Create()
        {
            ViewData["CurrencyId"]        = new SelectList(await _currencyService.GetAll(), "Id", "Name");
            ViewData["InterestPaymentId"] = new SelectList(await _interestPaymentService.GetAll(), "Id", "Name");
            ViewData["DepositTermId"]     = new SelectList(await _depositTerm.GetAllWithTerm(), "Id", "Name");


            return(View());
        }
        public async Task<IActionResult> OpenAsync(int id)
        {
            var user = await _userManager.GetUserAsync(User);
            var depositTypes = await _depositService.GetAllTypes();
            var depositType = depositTypes.FirstOrDefault(x => x.Id == id);
            ViewData["DepositTypeId"] = depositType;
            ViewData["DepositTermId"] = new SelectList(await _depositTermService.GetAllWithTerm(), "Id", "Name");
            ViewData["CurrencyId"] = new SelectList(await _currencyService.GetAll(), "Id", "Name");
            ViewData["AccountToTransferId"] = new SelectList(await _bankAccountService.GetUserAccounts(user.Id), "Id", "Name");
            ViewData["AccountForInterestId"] = new SelectList(await _bankAccountService.GetUserAccounts(user.Id), "Id", "Name");
            ViewData["InterestRate"] = new Dictionary<int, string>(depositTypes.Select(x => new KeyValuePair<int, string>(x.Id, $"{x.AnnualRate}%")));

            return View();
        }
        public async Task <bool> OpenDepositAsync(Deposit deposit, int id, Account account)
        {
            await _unitOfWork.BeginTransactionAsync();

            var accountDeposit = await _accountRepository.GetByIdAsync(deposit.AccountForInterestId);

            if (deposit.DepositAmount > account.Balance)
            {
                throw new NotEnoughMoneyException();
            }
            try
            {
                account.Balance        -= deposit.DepositAmount;
                accountDeposit.Balance += deposit.DepositAmount;

                deposit.Status = DepositConstants.Active;
                deposit.UserId = id;
                StringBuilder stringBuilder = new StringBuilder();

                var terms = await _depositTermService.GetAllWithTerm();

                var stringMonths = new string(terms
                                              .FirstOrDefault(x => x.Id == deposit.DepositTermId)
                                              .Name.ToCharArray()
                                              .Where(x => char.IsDigit(x))
                                              .Select(x => x).ToArray());

                int.TryParse(stringMonths, out int month);

                deposit.TermOfDeposit = DateTime.Now.AddMonths(month);

                var cards = await _cardService.GetCardsAsync();

                var senderCardId = cards.FirstOrDefault(x => account.Id == x.AccountId).Id;

                var accounts = await _accountRepository.GetAllAsync();

                var services = await _serviceRepository.GetAllAsync();


                var bankAccountId = accounts.FirstOrDefault(x => x.Name == DepositConstants.BankAccount).Id;
                var bankServiceId = services.FirstOrDefault(x => x.Name == DepositConstants.BankDepositService).Id;
                var bankCardId    = cards.FirstOrDefault(x => x.Name == DepositConstants.BankCard).Id;

                var userToBankTransaction = new Transactions
                {
                    PersonalAccountId = "",
                    SenderCardId      = senderCardId,
                    ReceiverAccountId = bankAccountId,
                    SenderAccountId   = account.Id,
                    ServiceId         = bankServiceId,
                    Description       = $"Moved money from sorce acc to bank acc",
                    Amount            = deposit.DepositAmount,
                    Date      = DateTime.Now,
                    IsDeleted = false,
                };

                var bankToDepositTransaction = new Transactions
                {
                    PersonalAccountId = "",
                    SenderCardId      = bankCardId,
                    ReceiverAccountId = deposit.AccountForInterestId,
                    SenderAccountId   = bankAccountId,
                    ServiceId         = bankServiceId,
                    Description       = $"Moved money from bank acc to deposit acc",
                    Amount            = deposit.DepositAmount,
                    Date      = DateTime.Now,
                    IsDeleted = false,
                };

                await _transactionsService.InsertAsync(userToBankTransaction);

                await _transactionsService.InsertAsync(bankToDepositTransaction);
                await Add(deposit);

                await _unitOfWork.CommitAsync();
            }
            catch (Exception)
            {
                await _unitOfWork.RollbackAsync();

                return(false);
            }

            return(true);
        }