Example #1
0
        protected virtual void RefreshFilter_RowSelected(PXCache sender, PXRowSelectedEventArgs e)
        {
            RefreshFilter filter = (RefreshFilter)e.Row;

            if (filter != null)
            {
                CurrencyRateList.SetProcessDelegate(list => RefreshRates(filter, list));
            }
        }
        protected virtual void RefreshFilter_RowSelected(PXCache sender, PXRowSelectedEventArgs e)
        {
            RefreshFilter filter = (RefreshFilter)e.Row;

            if (filter != null)
            {
                string apiKey = GetApiKey();
                CurrencyRateList.SetProcessDelegate(
                    delegate(List <RefreshRate> list)
                {
                    RefreshRates(filter, list, apiKey);
                }
                    );
            }
        }
Example #3
0
        public virtual void RefreshRates(RefreshFilter filter, List <RefreshRate> list)
        {
            DateTime date = GetUtcSyncDate(filter.CuryEffDate.Value);
            Dictionary <string, decimal> rates = GetRatesFromService(filter, list, date);

            CuryRateMaint graph = PXGraph.CreateInstance <CuryRateMaint>();

            graph.Filter.Current.ToCurrency = filter.CuryID;
            graph.Filter.Current.EffDate    = date;

            bool hasError = false;

            for (int i = 0; i < list.Count; i++)
            {
                RefreshRate rr = list[i];

                if (rates.ContainsKey(rr.FromCuryID))
                {
                    CurrencyRate curyRate = (CurrencyRate)graph.CuryRateRecordsEntry.Insert();
                    curyRate.FromCuryID   = rr.FromCuryID;
                    curyRate.ToCuryID     = filter.CuryID;
                    curyRate.CuryRateType = rr.CuryRateType;
                    curyRate.CuryRate     = rates[rr.FromCuryID] * (1 + rr.OnlineRateAdjustment.GetValueOrDefault(0) / 100);
                    curyRate.CuryMultDiv  = CuryMultDivType.Div;
                    rr.CuryRate           = curyRate.CuryRate;
                    graph.CuryRateRecordsEntry.Update(curyRate);

                    PXProcessing <RefreshRate> .SetInfo(i, ActionsMessages.RecordProcessed);
                }
                else
                {
                    PXProcessing <RefreshRate> .SetError(i, PXMessages.LocalizeFormatNoPrefixNLA(Messages.NoOnlyRatesFoundForCurrency, rr.FromCuryID));

                    hasError = true;
                }
            }

            graph.Actions.PressSave();

            if (hasError)
            {
                throw new PXOperationCompletedException(Messages.CurrencyRateFailedToRefresh);
            }
        }
Example #4
0
        /// <summary>
        /// Receive Currency Rates from external service
        /// </summary>
        /// <param name="filter">RefreshCurrency Rates Parameters (to get ToCurrency)</param>
        /// <param name="list">Rates to update (For overrides only: to switch services for different currencies etc.)</param>
        /// <param name="date">Date to pass to external service</param>
        /// <returns>Rate value for each currency returned by service</returns>
        public virtual Dictionary <string, decimal> GetRatesFromService(RefreshFilter filter, List <RefreshRate> list, DateTime date)
        {
            string ratesRequestURL = String.Format(
                "http://openexchangerates.org/api/time-series.json?app_id={0}&base={1}&start={2:yyyy-MM-dd}&end={2:yyyy-MM-dd}",
                GetApiKey(),
                filter.CuryID,
                date);

            PXTrace.WriteInformation("Refresh rates URL: " + ratesRequestURL);

            var client   = new WebClient();
            var response = client.DownloadString(new Uri(ratesRequestURL));

            JObject json = (JObject)JsonConvert.DeserializeObject(response);

            if (json == null)
            {
                throw new PXException(Messages.CurrencyRateJsonError, response);
            }
            JToken rates = json.SelectToken(String.Format("rates.{0:yyyy-MM-dd}", date), true);

            return(rates.Children().Cast <JProperty>().ToDictionary(p => p.Name, p => p.Value.Value <decimal>()));
        }
        public static void RefreshRates(RefreshFilter filter, List <RefreshRate> list, string apiKey)
        {
            bool hasError = false;

            var date = GetUtcSyncDate(filter.CuryEffDate.Value);

            string ratesRequestURL = String.Format(
                "http://openexchangerates.org/api/time-series.json?app_id={0}&base={1}&start={2:yyyy-MM-dd}&end={2:yyyy-MM-dd}",
                apiKey,
                filter.CuryID,
                date);

            PXTrace.WriteInformation("Refresh rates URL: " + ratesRequestURL);

            var client   = new WebClient();
            var response = client.DownloadString(new Uri(ratesRequestURL));

            JObject json = (JObject)JsonConvert.DeserializeObject(response);

            if (json == null)
            {
                throw new PXException(Messages.CurrencyRateJsonError, response);
            }
            JToken rates = json.SelectToken(String.Format("rates.{0:yyyy-MM-dd}", date), true);

            CuryRateMaint graph = PXGraph.CreateInstance <CuryRateMaint>();

            graph.Filter.Current.ToCurrency = filter.CuryID;
            graph.Filter.Current.EffDate    = date;

            for (int i = 0; i < list.Count; i++)
            {
                RefreshRate rr = list[i];

                var rate = rates.Children().Cast <JProperty>().Where(p => p.Name == rr.FromCuryID).FirstOrDefault();
                if (rate == null)
                {
                    PXProcessing <RefreshRate> .SetError(i, PXMessages.LocalizeFormatNoPrefixNLA(Messages.NoOnlyRatesFoundForCurrency, rr.FromCuryID));

                    hasError = true;
                }
                else
                {
                    CurrencyRate curyRate = (CurrencyRate)graph.CuryRateRecordsEntry.Insert();
                    curyRate.FromCuryID   = rr.FromCuryID;
                    curyRate.ToCuryID     = filter.CuryID;
                    curyRate.CuryRateType = rr.CuryRateType;
                    curyRate.CuryRate     = rate.Value.Value <decimal>() * (1 + rr.OnlineRateAdjustment.GetValueOrDefault(0) / 100);
                    curyRate.CuryMultDiv  = "D";
                    rr.CuryRate           = curyRate.CuryRate;
                    graph.CuryRateRecordsEntry.Update(curyRate);

                    PXProcessing <RefreshRate> .SetInfo(i, ActionsMessages.RecordProcessed);
                }
            }

            graph.Actions.PressSave();

            if (hasError)
            {
                throw new PXOperationCompletedException(Messages.CurrencyRateFailedToRefresh);
            }
        }