public ActionResult GetTransferDialog(int fromAccountId)
        {
            var account  = _accountRepository.GetAccountById(fromAccountId);
            var userName = User.Claims.First(x => x.Type == ClaimsIdentity.DefaultNameClaimType).Value;

            var viewModel = new TransferDialogDto
            {
                FromAccountId   = fromAccountId,
                FromAccountType = account.AccountType.Name,
                BankId          = 1,
                UserName        = userName
            };

            return(PartialView("TransferDialog", viewModel));
        }
        public ActionResult CreateTransferTransaction(TransferDialogDto transfer)
        {
            _transactionService.TransferBetweenAccounts(transfer);

            return(RedirectToAction("Index"));
        }
示例#3
0
        /// <summary>
        /// Создание транзакции на перевод между счетами
        /// </summary>
        /// <param name="transfer"></param>
        /// <returns></returns>
        public void TransferBetweenAccounts(TransferDialogDto transfer)
        {
            if (transfer == null)
            {
                throw new ArgumentNullException(nameof(transfer));
            }

            //отправитель
            var senderAccount = _accountRepository.GetAccountById(transfer.FromAccountId);

            if (transfer.ToAccountId == null)
            {
                throw new InvalidOperationException("Не был указан счет получателя");
            }

            //получатель
            var recipientAccount = _accountRepository.GetAccountById((int)transfer.ToAccountId);

            if (recipientAccount == null)
            {
                throw new InvalidOperationException($"Не найден счет получателя {nameof(transfer.ToAccountId)}");
            }

            decimal transferCommision;

            //вычисляем коммисию перевода
            if (senderAccount.AccountTypeId != null && recipientAccount.AccountTypeId != null)
            {
                transferCommision = GetTransferCommision(transfer.Amount,
                                                         (int)senderAccount.AccountTypeId,
                                                         (int)recipientAccount.AccountTypeId);
            }
            else
            {
                throw new InvalidOperationException("Не указан тип счета");
            }

            decimal bankCommision;

            //вычисляем коммисию банка
            if (senderAccount.BankId != null && recipientAccount.BankId != null)
            {
                bankCommision = GetBankCommision(transfer.Amount,
                                                 (int)senderAccount.BankId,
                                                 (int)recipientAccount.BankId);
            }
            else
            {
                throw new InvalidOperationException("Не указан банк");
            }

            //проверяем баланс отправителя
            decimal senderNewDeposit = senderAccount.Deposit - (transfer.Amount + transferCommision + bankCommision);

            if (senderNewDeposit < 0)
            {
                throw new InvalidOperationException($"На счете отправителя {transfer.FromAccountId} не достаточно средств");
            }

            //создание транзакции перевода
            Transaction newtransaction = new Transaction()
            {
                Amount             = transfer.Amount,
                TransferCommission = transferCommision,
                BankCommission     = bankCommision,
                RecipientAccountId = recipientAccount.Id,
                SenderAccountId    = senderAccount.Id,
                Date = DateTime.Now
            };

            //фиксируем изменения
            senderAccount.Deposit    = senderNewDeposit;
            recipientAccount.Deposit = recipientAccount.Deposit + transfer.Amount;

            _transactionRepository.AddTransaction(newtransaction, senderAccount, recipientAccount);
        }