Handling of Foreign Currencies

From OpenPetra Wiki
Jump to navigation Jump to search

Actually I've put put the requirements based on "Foreign Currency Transactions" and "Testcase requirements for the CommonAccountingTool" into a new object TCurrencyInfo (in File GL.CommonTools.cs). The object is tested in Test.GL.CommonTools.cs and the informative test is the routine Test_05_TCurrencyInfo_2.

TCurrencyInfo tCurrencyInfo = new TCurrencyInfo("EUR", "JPY");

The constructor of TCurrencyInfo contains a loadAll-database request for the table a_currency. In this table you can set two row pointer one for the base currency and one for the foreign currency. The base pointer is set once by the constructor and cannot be changed later.

Then you can use RoundBaseCurrencyValue and - if a pointer is set - RoundForeignCurrencyValue to round a currency value to the correct number of digits.

Assert.AreEqual(1.23m, tCurrencyInfo.RoundBaseCurrencyValue(1.23456m), "Round to 2 digits");
Assert.AreEqual(1.0m, tCurrencyInfo.RoundForeignCurrencyValue(1.23456m), "Round to 0 digits");

Above this you can use ToBaseValue and or ToForeignValue to calculate the foreign currency calculations.

The example begins with a

decimal exchangeRate = 1/119.7295m;

where 119.7295 is the todays value elsewhere from the internet and in "Testcase requirements for the CommonAccountingTool" I've explained why we have to use the reziprokal value here.

decimal exchangeRate = 1/119.7295m;
       	
Assert.AreEqual(0.84m, tCurrencyInfo.ToBaseValue(100.00m, exchangeRate), 
     "Conversion from 100 YEN to 0.84 EUR");
Assert.AreEqual(11973, tCurrencyInfo.ToForeignValue(100.00m, exchangeRate), 
     "Conversion from 100 EUR to 11973 YEN");
Assert.AreEqual(120, tCurrencyInfo.ToForeignValue(1.00m, exchangeRate), 
     "Conversion from 1 EUR to 120 YEN");
        	
tCurrencyInfo.ForeignCurrencyCode = "GBP"; // Change foreign Currency to Pound ...
exchangeRate = 1/0.8801m;
 
Assert.AreEqual(113.62m, tCurrencyInfo.ToBaseValue(100.00m, exchangeRate), 
     "Conversion from 100 GBP to 113.62 EUR");
Assert.AreEqual(88.01m, tCurrencyInfo.ToForeignValue(100.00m, exchangeRate), 
     "Conversion from 100 EUR to 88.01 GBP");

So you have one object

  1. to handle all available currency infos
  2. and of course the base currency infos too
  3. can easily be switched to an other currency
  4. handles the base-foreign calculation correctly in the sense of om