Пример #1
0
        public decimal TryConvertToPrimaryCurrency(string sourceCurrencySymbol, decimal sourcePrice, Action <ICurrencyConversion> conversionCallback)
        {
            using (var tx = m_database.OpenTransaction())
            {
                var sourceCurrency = m_currencyRepository.GetCurrency(sourceCurrencySymbol);
                if (sourceCurrency == null)
                {
                    throw new InvalidOperationException($"Neznamy symbol meny '{sourceCurrencySymbol}'");
                }

                if (sourceCurrency.IsProjectMainCurrency)
                {
                    tx.Commit();
                    return(sourcePrice);
                }

                var targetCurrency = m_currencyRepository.GetAllCurrencies()
                                     .FirstOrDefault(c => c.IsProjectMainCurrency);
                if (targetCurrency == null)
                {
                    throw new InvalidOperationException("Není nastavena domácí měna projektu");
                }

                var rate = m_currencyRepository.GetCurrencyRate(sourceCurrency, targetCurrency);
                if (rate == null)
                {
                    throw new InvalidOperationException($"Není dostupná konverze {sourceCurrency.Symbol} -> {targetCurrency.Symbol}");
                }

                var conversion = m_currencyRepository.CreateCurrencyConversion(rate, sourcePrice);
                if (conversion == null)
                {
                    throw new InvalidOperationException("Konverze měny selhala");
                }

                conversionCallback(conversion);

                tx.Commit();

                return(conversion.TargetValue);
            }
        }