Currency Conversion

  • One easy join.

  • Prerequisite: Dataload DS ccy -> CP ccy.

Code

Groovy
def qapi = api.queryApi()

def t1 = qapi.tables().productExtensionRows("Costs")
def t2 = qapi.tables().companyParameterRows("Currencies")
def today = new Date()
def currency = "USD"

return qapi.source(t1, [t1.sku(), t1.Cost, t1.Currency])
        .leftOuterJoin(t2,
        { cols -> [t2.ExchangeRate, cols.Cost.multiply(t2.ExchangeRate).as("CostUSD")] },
        { cols -> qapi.exprs().and(t2.CcyFrom.equal(cols.Currency),
        t2.CcyTo.equal(currency),
        t2.CcyValidFrom.lessOrEqual(today),
        t2.CcyValidTo.greaterOrEqual(today),) })
        .take(10)
        .stream { it.collect { it } }

Result

sku

Cost

Currency

ExchangeRate

CostUSD

10000001

69

GBP

1.26

86.94

10000001

64

CHF

1.12

71.68

10000001

73

GBP

1.26

91.98

10000001

87

CHF

1.12

97.44

10000001

37

DKK

0.14

5.18

10000001

3

USD



10000002

4

USD



10000002

53

USD



10000002

28

EUR

1.05

29.4

10000002

85

USD



See Also