Currencies plugin

  • Tags : functionality, domainclass
  • Latest : 1.3.2
  • Last Updated: 15 December 2010
  • Grails version : *
  • Authors : null
2 votes
Dependency :
compile ":currencies:1.3.2"

Summary

Description

Description

The currencies plugin provides a Money domain class, which is currency-aware, along with an ExchangeRate domain class which can be used to store historical exchange rates.  It's useful on applications where you're expected to support multiple currencies, along with a history of exchange rates.

Source is available on GitHub.

Examples

To install, execute:

grails install-plugin http://www.arquetipos.co.cr/blog/files/grails-currencies-0.3.zip
Usage is extremly simple, and exemplified on the included integration tests.  To create a list of exchange rates:
def dollar = Currency.getInstance('USD')
def yen = Currency.getInstance('JPY')
def euro = Currency.getInstance('EUR')
def gbp = Currency.getInstance('GBP')

new ExchangeRate(baseCurrency:euro, toCurrency:dollar, rate:1.46122, date:new Date('2007/12/02')).save() new ExchangeRate(baseCurrency:yen, toCurrency:dollar, rate:0.008981, date:new Date('2007/12/02')).save() new ExchangeRate(baseCurrency:dollar, toCurrency:yen, rate:111.336, date:new Date('2007/12/02')).save() new ExchangeRate(baseCurrency:gbp, toCurrency:dollar, rate:2.02369, date:new Date('2007/12/02')).save() new ExchangeRate(baseCurrency:euro, toCurrency:dollar, rate:1.33159, date:new Date('2006/12/02')).save()

You then create Money items in this way:

Money dollars = new Money(amount:9.50, currency:'USD')
assert dollars
assertEquals dollars.currency.currencyCode, 'USD'
assertEquals dollars.currency.symbol, '$'
You can also obtain an instance of a Money class from a string containing the amount and currency code:
def money = Money.getInstance('250.0 EUR')
assertEquals money.amount, 250f
assertEquals money.currency.currencyCode, 'EUR'

money = Money.getInstance('15.50 USD') assertEquals money.amount, 15.5f assertEquals money.currency.currencyCode, 'USD'


To convert from a currency to another, you call the domain class' convertTo function:

def dollar = Currency.getInstance('USD')
def euro = Currency.getInstance('EUR')

assert dollar assert euro

Money euros = new Money(amount:10, currency:euro)

Money toDollars = euros.convertTo(dollar) assertEquals toDollars.amount, 14.6122f assertEquals toDollars, new Money(amount:14.6122, currency:dollar)

toDollars = euros.convertTo(dollar, new Date('2007/01/01')) assertEquals toDollars.amount, 13.315901f

Finally, to add money amounts, simply use the overriden Plus operator.  The Money class will look for the most recent exchange rate for both currencies, and convert it to the currency of the first Money object.
def dollars = new Money(amount:1, currency:'USD')
def pounds = new Money(amount:1, currency:'GBP')
def sum = dollars + pounds
assert sum
assertEquals sum.amount, 3.02369f
assertEquals sum.currency.currencyCode, 'USD'
Attempting to convert between two amounts at a point for which there isn't a valid exchange rate will raise an exception:
def dollar = Currency.getInstance('USD')
def euro = Currency.getInstance('EUR')
Money euros = new Money(amount:10, currency:euro)

shouldFail(Exception) { Money toDollars = euros.convertTo(dollar, newDate('2005/01/01')) }

Recommended usage

We use the Money class embedded on objects, for instance:

class CustomerTransaction {
    Date date = new Date()
    Money amount

static embedded = ['money'] }

 Next steps

  • Caching exchange rates
  • Controllers and views for exchange rates