private decimal CalculateAmountDestination(ValCursValute sourceAccountCurrencyInfo, ValCursValute destAccountCurrencyInfo, decimal amount) { var sourceAccRate = 1M;// Для случая с дефолтным счетом(рублевым) var sourceNominal = 1; if (sourceAccountCurrencyInfo != null) { sourceAccRate = Convert.ToDecimal(sourceAccountCurrencyInfo.Value); sourceNominal = Convert.ToInt32(sourceAccountCurrencyInfo.Nominal); } var destAccRate = 1M;// Для случая с дефолтным счетом(рублевым) var destNominal = 1; if (destAccountCurrencyInfo != null) { destAccRate = Convert.ToDecimal(destAccountCurrencyInfo.Value); destNominal = Convert.ToInt32(destAccountCurrencyInfo.Nominal); } return(amount * sourceAccRate / sourceNominal / destAccRate * destNominal); }
public async Task TransferMoney(Guid accountIdSource, Guid accountIdDest, decimal amount) { //INFO: валюта amount = валюте sourceAccount if (amount <= 0) { throw new Exception("Сумма перевода должна быть больше нуля"); } var accountSource = await _accountRepository.GetAccount(accountIdSource); if (accountSource == null) { throw new Exception("Счет для списания денежных средств не существует"); } if (accountSource.Balance < amount) { throw new Exception("Недостаточно денежных средств для перевода"); } var accountDestination = await _accountRepository.GetAccount(accountIdDest); if (accountDestination == null) { throw new Exception("Счет назначения перевода ДС не существует"); } if (accountSource.PurseId != accountDestination.PurseId) { throw new Exception("Переводы валюты разрешены в рамках одного кошелька"); } var currenciesData = await _currencyApi.GetApi(); if (currenciesData == null) { throw new Exception("Не найдены курсы валют"); } ValCursValute sourceAccountCurrencyInfo = null; if (!(accountSource.Currency.CurrencyId == (await _currencyRepository.GetDefaultCurrency()).CurrencyId)) { sourceAccountCurrencyInfo = currenciesData.Valute .FirstOrDefault(s => s.CharCode == accountSource.Currency.Name); if (sourceAccountCurrencyInfo == null) { throw new Exception("Не найден курс валют для исходного счета"); } } ValCursValute destAccountCurrencyInfo = null; if (!(accountDestination.Currency.CurrencyId == (await _currencyRepository.GetDefaultCurrency()).CurrencyId)) { destAccountCurrencyInfo = currenciesData.Valute .FirstOrDefault(s => s.CharCode == accountDestination.Currency.Name); if (destAccountCurrencyInfo == null) { throw new Exception("Не найден курс валют счета назначения"); } } await _accountRepository.WithDrawMoney(accountSource.AccountId, amount); var amountDest = CalculateAmountDestination(sourceAccountCurrencyInfo, destAccountCurrencyInfo, amount); await _accountRepository.AddBalance(accountDestination.AccountId, amountDest); }