private CurrencyRate FindCurrencyRate(string fromCury, string toCury, string rateType, DateTime effectiveDate) { ConcurrentBag <CurrencyRate> rates = GetCachedCurrencyRates(); CM.CurrencyRate foundRate = FindCurrencyRate(rates, fromCury, toCury, rateType, (DateTime)effectiveDate); if (foundRate == null) { PXGraph graph = new PXGraph(); PXResultset <CurrencyRate> currencyRates = PXSelect <CurrencyRate, Where <CurrencyRate.fromCuryID, Equal <Required <CurrencyRate.fromCuryID> >, And <CurrencyRate.toCuryID, Equal <Required <CurrencyRate.toCuryID> >, And <CurrencyRate.curyRateType, Equal <Required <CurrencyRate.curyRateType> >, And <CurrencyRate.curyEffDate, GreaterEqual <Required <CurrencyRate.curyEffDate> > > > > >, OrderBy <Asc <CM.CurrencyRate.curyEffDate> > > .SelectWindowed(graph, 0, 100, fromCury, toCury, rateType, effectiveDate); if (currencyRates.Count == 0) { currencyRates = PXSelect <CurrencyRate, Where <CurrencyRate.fromCuryID, Equal <Required <CurrencyRate.fromCuryID> >, And <CurrencyRate.toCuryID, Equal <Required <CurrencyRate.toCuryID> >, And <CurrencyRate.curyRateType, Equal <Required <CurrencyRate.curyRateType> >, And <CurrencyRate.curyEffDate, Less <Required <CurrencyRate.curyEffDate> > > > > >, OrderBy <Desc <CurrencyRate.curyEffDate> > > .SelectWindowed(graph, 0, 1, fromCury, toCury, rateType, effectiveDate); } foreach (CurrencyRate rate in currencyRates) { ((IProducerConsumerCollection <CurrencyRate>)rates).TryAdd(rate); } foundRate = FindCurrencyRate(rates, fromCury, toCury, rateType, (DateTime)effectiveDate); } return(foundRate); }
public object CuryConvCury(object fromCury, object toCury, object rateType, object baseval, object effectiveDate) { CM.CurrencyRate foundRate = FindCurrencyRate((string)fromCury, (string)toCury, (string)rateType, (DateTime)effectiveDate); decimal curyval; if (foundRate != null) { decimal rate; try { rate = (decimal)foundRate.CuryRate; } catch (InvalidOperationException) { throw new PXRateNotFoundException(); } if (rate == 0.0m) { rate = 1.0m; } bool mult = foundRate.CuryMultDiv != "D"; curyval = mult ? (decimal)baseval * rate : (decimal)baseval / rate; } else { curyval = baseval == null ? 0m : (decimal)baseval; } return(curyval); }
private CurrencyRate FindCurrencyRate(ConcurrentBag <CurrencyRate> rates, string fromCury, string toCury, string rateType, DateTime effectiveDate) { CM.CurrencyRate foundRate = null; if (rates.Count != 0) { foreach (CurrencyRate rate in rates) { if (rate.CuryRateType == (string)rateType && rate.FromCuryID == (string)fromCury && rate.ToCuryID == (string)toCury) { if (rate.CuryEffDate == (DateTime)effectiveDate) { foundRate = rate; break; } else if (rate.CuryEffDate < (DateTime)effectiveDate && ((foundRate != null && rate.CuryEffDate > foundRate.CuryEffDate) || foundRate == null)) { foundRate = rate; } } } } return(foundRate); }