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); } }
protected virtual IEnumerable currencyRateList() { foreach (PXResult <CurrencyList, CurrencyRateType> res in PXSelectJoin <CurrencyList, CrossJoin <CurrencyRateType>, Where <CurrencyRateType.refreshOnline, Equal <boolTrue>, And <CurrencyList.isActive, Equal <boolTrue>, And <CurrencyList.curyID, NotEqual <Current <RefreshFilter.curyID> >, And <Where <CurrencyRateType.curyRateTypeID, Equal <Current <RefreshFilter.curyRateTypeID> >, Or <Current <RefreshFilter.curyRateTypeID>, IsNull> > > > > > > .Select(this)) { CurrencyList curr = res; CurrencyRateType rateType = res; RefreshRate rate = new RefreshRate(); rate.FromCuryID = curr.CuryID; rate.CuryRateType = rateType.CuryRateTypeID; rate.OnlineRateAdjustment = rateType.OnlineRateAdjustment; CurrencyRateList.Cache.SetStatus(rate, PXEntryStatus.Held); yield return(rate); } }
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); } }