示例#1
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);
            }
        }
        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);
            }
        }