Ejemplo n.º 1
0
        private static void DeactivateAllExchangeRates(CrmService CustomService, DateTime relevanceDate)
        {
            List <sl_ExchangeRate> activeRates =
                CustomService.XrmContext
                .sl_ExchangeRateSet
                .Where(x =>
                       x.statecode == sl_ExchangeRateState.Active &&
                       x.sl_RelevanceDate <= relevanceDate)
                .ToList();

            foreach (var rate in activeRates)
            {
                CustomService.DeactivateExchangeRateById(rate.sl_ExchangeRateId.Value);
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            CustomService = new CrmService();

            Console.WriteLine("Connection esteblished...");

            List <TransactionCurrency> currencies =
                CustomService
                .XrmContext
                .TransactionCurrencySet
                .ToList();

            List <ExchangeRateDTO> eRateDtos = DownloadRatesFile();

            DateTime relevanceDate = GetRelevanceDate(eRateDtos.First());

            DeactivateAllExchangeRates(CustomService, relevanceDate);

            decimal usdRate = findUsdToAznRate(eRateDtos);

            foreach (var rate in eRateDtos)
            {
                if (rate.IsoCode.ToUpper() == "USD")
                {
                    rate.IsoCode = "AZN";
                }

                TransactionCurrency currentCurrency =
                    currencies
                    .SingleOrDefault(x =>
                                     x.ISOCurrencyCode.ToUpper() == rate.IsoCode.ToUpper());

                if (currentCurrency == null)
                {
                    continue;
                }

                sl_ExchangeRate newRate = new sl_ExchangeRate();

                if (rate.IsoCode != "AZN")
                {
                    newRate = new sl_ExchangeRate()
                    {
                        sl_RelevanceDate         = relevanceDate,
                        sl_TransactionCurrencyId = currentCurrency.ToEntityReference(),
                        sl_ExchangeRate1         = 1 / decimal.Parse(rate.ExchangeValue.Replace('.', ',')) * usdRate, // eur/azn / usd/azn
                    };
                }
                else
                {
                    newRate = new sl_ExchangeRate()
                    {
                        sl_RelevanceDate         = relevanceDate,
                        sl_TransactionCurrencyId = new EntityReference(TransactionCurrency.EntityLogicalName, currentCurrency.TransactionCurrencyId.Value),
                        sl_ExchangeRate1         = decimal.Parse(rate.ExchangeValue.Replace('.', ','))
                    };
                }

                CustomService.Create(newRate);

                Console.WriteLine("Created exchange rate of {0} = {1}", currentCurrency.ISOCurrencyCode, newRate.sl_ExchangeRate1);
            }

            Console.WriteLine("Update complete!");
        }