Handling of Foreign Currencies: Difference between revisions

From OpenPetra Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
Actually I've put put the requirements based on "[[Foreign Currency Transactions]]" and "[[Testcase requirements for the CommonAccountingTool]]" into a new object GetCurrencyInfo (in File GL.CommonTools.cs). The object is tested in Test.GL.CommonTools.cs and the informative test is the routine Test_05_GetCurrencyInfo_2.  
Actually I've put put the requirements based on "[[Foreign Currency Transactions]]" and "[[Testcase requirements for the CommonAccountingTool]]" into a new object GetCurrencyInfo (in File GL.CommonTools.cs). The object is tested in Test.GL.CommonTools.cs and the informative test is the routine Test_05_GetCurrencyInfo_2.  


<pre>GetCurrencyInfo getCurrencyInfo = new GetCurrencyInfo("EUR", "JPY");</pre>
<pre>TCurrencyInfo tCurrencyInfo = new TCurrencyInfo("EUR", "JPY");</pre>


The constructor of GetCurrencyInfo 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.
The constructor of GetCurrencyInfo 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.
Line 7: Line 7:
Then you can use RoundBaseCurrencyValue and - if a pointer is set - RoundForeignCurrencyValue to round a currency value to the correct number of digits.  
Then you can use RoundBaseCurrencyValue and - if a pointer is set - RoundForeignCurrencyValue to round a currency value to the correct number of digits.  


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


Above this you can use ToBaseValue and or ToForeignValue to calculate the foreign currency calculations.  
Above this you can use ToBaseValue and or ToForeignValue to calculate the foreign currency calculations.  
Line 20: Line 20:
<pre>decimal exchangeRate = 1/119.7295m;
<pre>decimal exchangeRate = 1/119.7295m;
      
      
Assert.AreEqual(0.84m, getCurrencyInfo.ToBaseValue(100.00m, exchangeRate),  
Assert.AreEqual(0.84m, tCurrencyInfo.ToBaseValue(100.00m, exchangeRate),  
     "Conversion from 100 YEN to 0.83 EUR");
     "Conversion from 100 YEN to 0.83 EUR");
Assert.AreEqual(11973, getCurrencyInfo.ToForeignValue(100.00m, exchangeRate),  
Assert.AreEqual(11973, tCurrencyInfo.ToForeignValue(100.00m, exchangeRate),  
     "Conversion from 100 EUR to 11983 YEN");
     "Conversion from 100 EUR to 11983 YEN");
Assert.AreEqual(120, getCurrencyInfo.ToForeignValue(1.00m, exchangeRate),  
Assert.AreEqual(120, tCurrencyInfo.ToForeignValue(1.00m, exchangeRate),  
     "Conversion from 1 EUR to 120 YEN");
     "Conversion from 1 EUR to 120 YEN");
        
        
Line 30: Line 30:
exchangeRate = 1/0.8801m;
exchangeRate = 1/0.8801m;
   
   
Assert.AreEqual(113.62m, getCurrencyInfo.ToBaseValue(100.00m, exchangeRate),  
Assert.AreEqual(113.62m, tCurrencyInfo.ToBaseValue(100.00m, exchangeRate),  
     "Conversion from 100 GBP to 113.62 EUR");
     "Conversion from 100 GBP to 113.62 EUR");
Assert.AreEqual(88.01m, getCurrencyInfo.ToForeignValue(100.00m, exchangeRate),  
Assert.AreEqual(88.01m, tCurrencyInfo.ToForeignValue(100.00m, exchangeRate),  
     "Conversion from 100 EUR to 88.01 GBP");</pre>
     "Conversion from 100 EUR to 88.01 GBP");</pre>



Revision as of 04:57, 21 April 2011

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

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

The constructor of GetCurrencyInfo 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.83 EUR");
Assert.AreEqual(11973, tCurrencyInfo.ToForeignValue(100.00m, exchangeRate), 
     "Conversion from 100 EUR to 11983 YEN");
Assert.AreEqual(120, tCurrencyInfo.ToForeignValue(1.00m, exchangeRate), 
     "Conversion from 1 EUR to 120 YEN");
        	
getCurrencyInfo.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