예제 #1
0
파일: mainForm.cs 프로젝트: krom13/pretest
        public mainForm()
        {
            InitializeComponent();

            di   = new DailyInfo();
            curs = di.GetCursOnDateXML(DateTime.Today);

            foreach (XmlNode node in curs.ChildNodes)
            {
                CursListBox.Items.Add(node["VchCode"].InnerText + "\t" + node["Vname"].InnerText.Trim());
            }

            CursListBox.SelectedItem = defaultSelectedCur;
        }
예제 #2
0
        public decimal getRate(DateTime date, currency currency)
        {
            if (currency == currency.RUR)
            {
                return(1);
            }
            DailyInfo cbrClient = new DailyInfo();

            cbrClient.Proxy = new WebProxy("http://msk01-wsncls02.uralsibins.ru:8080")
            {
                Credentials = new NetworkCredential(@"uralsibins\svcTinkoff", "USER4tinkoff"),
            };
            XmlNode       resultXml  = cbrClient.GetCursOnDateXML(date);
            XmlSerializer serializer = new XmlSerializer(typeof(ValuteData));
            ValuteData    result     = (ValuteData)serializer.Deserialize(new StringReader(resultXml.OuterXml));

            return(result.ValuteCursOnDate.Single(A => A.VchCode, currency.ToString()).Vcurs);
        }
예제 #3
0
        /// <summary>
        /// Executes the workflow activity.
        /// </summary>
        /// <param name="executionContext">The execution context.</param>
        protected override void Execute(CodeActivityContext executionContext)
        {
            // Create the tracing service
            ITracingService tracingService = executionContext.GetExtension <ITracingService>();

            if (tracingService == null)
            {
                throw new InvalidPluginExecutionException("Failed to retrieve tracing service.");
            }

            tracingService.Trace("Entered UpdateFXs.Execute(), Activity Instance Id: {0}, Workflow Instance Id: {1}",
                                 executionContext.ActivityInstanceId,
                                 executionContext.WorkflowInstanceId);

            // Create the context
            IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>();

            if (context == null)
            {
                throw new InvalidPluginExecutionException("Failed to retrieve workflow context.");
            }

            tracingService.Trace("UpdateFXs.Execute(), Correlation Id: {0}, Initiating User: {1}",
                                 context.CorrelationId,
                                 context.InitiatingUserId);

            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>();
            IOrganizationService        service        = serviceFactory.CreateOrganizationService(context.UserId);

            string serviceBaseCurrency = "RUB";

            using (var ctx = new OrganizationServiceContext(service))
            {
                try
                {
                    // Get exchange rates

                    DailyInfo client = new DailyInfo();
                    tracingService.Trace("Getting Most recent date available");
                    DateTime lastestDate = client.GetLatestDateTime();
                    tracingService.Trace("Getting FX Rates");
                    var exchangeRates = client.GetCursOnDateXML(lastestDate);



                    string baseCurrencyISO = string.Empty;
                    Guid   empty           = Guid.Empty;

                    var baseCurrency = (from c in ctx.CreateQuery <TransactionCurrency>()
                                        join o in ctx.CreateQuery <Organization>()
                                        on c.TransactionCurrencyId equals o.BaseCurrencyId.Id
                                        select new TransactionCurrency
                    {
                        TransactionCurrencyId = c.TransactionCurrencyId,
                        ISOCurrencyCode = c.ISOCurrencyCode,
                    }).FirstOrDefault();



                    if (baseCurrency != null)
                    {
                        baseCurrencyISO = baseCurrency.ISOCurrencyCode.ToUpper();

                        // Get the rate from service base rate to crm base currency
                        var serviceRateToCrmBase = GetExchangeRate(baseCurrencyISO, exchangeRates);
                        if (serviceRateToCrmBase == null)
                        {
                            throw new Exception(String.Format("Cannot find rate for base rate '{0}'", baseCurrencyISO));
                        }

                        // Get the currencies that are not the base currency
                        // Only update active currencies
                        var currencies = (from c in ctx.CreateQuery <TransactionCurrency>()
                                          where c.TransactionCurrencyId != baseCurrency.TransactionCurrencyId &&
                                          c.StateCode == TransactionCurrencyState.Active
                                          select new TransactionCurrency
                        {
                            TransactionCurrencyId = c.TransactionCurrencyId,
                            ISOCurrencyCode = c.ISOCurrencyCode,
                        });



                        foreach (TransactionCurrency currency in currencies)
                        {
                            string  isoCode    = currency.ISOCurrencyCode.ToUpper();
                            decimal?latestRate = null;

                            // Get rate from service base currency to this currency
                            decimal?serviceRate = 1;
                            if (isoCode != serviceBaseCurrency)
                            {
                                serviceRate = GetExchangeRate(isoCode, exchangeRates);
                            }


                            if (serviceRate != null)
                            {
                                // Get the rate from crm base rate
                                latestRate = serviceRateToCrmBase / serviceRate;
                            }
                            else
                            {
                                // The webserviceX currency service is no longer working - investigating alternatives
                                // latestRate = GetStandardRate(baseCurrencyISO, isoCode);
                            }


                            if (latestRate != null)
                            {
                                // We have a new rate so update it (even if it is the same!)
                                TransactionCurrency update = new TransactionCurrency();
                                update.TransactionCurrencyId = currency.TransactionCurrencyId;
                                update.ExchangeRate          = latestRate;
                                service.Update(update);
                            }
                        }
                    }
                }
                catch (FaultException <OrganizationServiceFault> e)
                {
                    tracingService.Trace("Exception: {0}", e.ToString());

                    // Handle the exception.
                    throw;
                }
                catch (Exception ex)
                {
                    tracingService.Trace("Exception (will retry): {0}", ex.ToString());
                    // All exceptions must be retried since this workflow must call it's self to run on a daily basis
                    OrganizationServiceFault fault = new OrganizationServiceFault();
                    fault.ErrorCode = -2147204346; // This will cause the Async Server to retry up to 10 times before failing
                    fault.Message   = ex.ToString();
                    var networkException = new FaultException <OrganizationServiceFault>(fault);
                    throw networkException;
                }
            }
            tracingService.Trace("Exiting UpdateFXs.Execute(), Correlation Id: {0}", context.CorrelationId);
        }