/// <summary> /// get daily exchange rate for the given currencies and date; /// TODO: might even collect the latest exchange rate from the web /// </summary> /// <param name="ACurrencyFrom"></param> /// <param name="ACurrencyTo"></param> /// <param name="ADateEffective"></param> /// <returns>Zero if no exchange rate found</returns> public static decimal GetDailyExchangeRate(string ACurrencyFrom, string ACurrencyTo, DateTime ADateEffective) { if (ACurrencyFrom == ACurrencyTo) { return(1.0M); } bool NewTransaction; TDBTransaction Transaction = DBAccess.GDBAccessObj.GetNewOrExistingTransaction (IsolationLevel.ReadCommitted, TEnforceIsolationLevel.eilMinimum, out NewTransaction); ADailyExchangeRateRow fittingRate = null; // TODO: get the most recent exchange rate for the given date and currencies bool oppositeRate = false; ADailyExchangeRateTable rates = ADailyExchangeRateAccess.LoadByPrimaryKey(ACurrencyFrom, ACurrencyTo, ADateEffective, 0, Transaction); if (rates.Count == 0) { // try other way round rates = ADailyExchangeRateAccess.LoadByPrimaryKey(ACurrencyTo, ACurrencyFrom, ADateEffective, 0, Transaction); oppositeRate = true; } if (rates.Count == 1) { fittingRate = rates[0]; } else if (rates.Count == 0) { // TODO: collect exchange rate from the web; save to db // see tracker http://sourceforge.net/apps/mantisbt/openpetraorg/view.php?id=87 // Or look for most recent exchange rate??? ADailyExchangeRateTable tempTable = new ADailyExchangeRateTable(); ADailyExchangeRateRow templateRow = tempTable.NewRowTyped(false); templateRow.FromCurrencyCode = ACurrencyFrom; templateRow.ToCurrencyCode = ACurrencyTo; oppositeRate = false; rates = ADailyExchangeRateAccess.LoadUsingTemplate(templateRow, Transaction); if (rates.Count == 0) { templateRow.FromCurrencyCode = ACurrencyTo; templateRow.ToCurrencyCode = ACurrencyFrom; oppositeRate = true; rates = ADailyExchangeRateAccess.LoadUsingTemplate(templateRow, Transaction); } if (rates.Count > 0) { // sort rates by date, look for rate just before the date we are looking for rates.DefaultView.Sort = ADailyExchangeRateTable.GetDateEffectiveFromDBName(); rates.DefaultView.RowFilter = ADailyExchangeRateTable.GetDateEffectiveFromDBName() + "<= #" + ADateEffective.ToString("yyyy-MM-dd") + "#"; if (rates.DefaultView.Count > 0) { fittingRate = (ADailyExchangeRateRow)rates.DefaultView[rates.DefaultView.Count - 1].Row; } } } if (NewTransaction) { DBAccess.GDBAccessObj.RollbackTransaction(); } if (fittingRate != null) { if (oppositeRate) { return(1.0M / fittingRate.RateOfExchange); } return(fittingRate.RateOfExchange); } TLogging.Log("Cannot find daily exchange rate for " + ACurrencyFrom + " " + ACurrencyTo); //return 1.0M; //Instead, cause a validation error to force the user to select an exchange rate return(0M); }