Tuesday, 18 August 2015

Exchange Rate in Ax 2012


Four pieces of information are necessary to perform a currency calculation in Microsoft Dynamics AX 2012: 1) From currency 2) To currency 3)Date 4) Exchange rate type 

=>Get Exchange Rate Currency Factor for two Currencies :
DD\Tables\ExchangeRateCurrencyPair\ExchangeRateDisplayFactor.

=> Calculate the accounting currency amount from a transaction currency
CurrencyExchangeHelper currencyExchangeHelper;
 TransDate transactionDate;
CurrencyCode transactionCurrency = 'CAD';
 AmountCur amountToConvert = 100.50;
boolean shouldRoundResult = true;
 AmountMst result;
currencyExchangeHelper = CurrencyExchangeHelper::newExchangeDate( Ledger::current(),transactionDate);

result = currencyExchangeHelper.calculateTransactionToAccounting( transactionCurrency, amountToConvert, shouldRoundResult);

=> Calculate the transaction currency amount from an accounting currency

CurrencyExchangeHelper currencyExchangeHelper;
 TransDate transactionDate;
CurrencyCode transactionCurrency = 'CAD';
 AmountMst amountToConvert = 100.50;
boolean shouldRoundResult = true;
AmountCur result;

currencyExchangeHelper = CurrencyExchangeHelper::newExchangeDate( Ledger::primaryLedger(CompanyInfo::findDataArea(‘TST’).RecId), transactionDate);


result = currencyExchangeHelper.calculateAccountingToTransaction( transactionCurrency, amountToConvert, shouldRoundResult);

=> Calculate using Exchange rates that have been provided
CurrencyExchangeHelper currencyExchangeHelper;
TransDate transactionDate;
 CurrencyCode transactionCurrency = 'CAD';
AmountMst result;
 currencyExchangeHelper = CurrencyExchangeHelper::newExchangeDate( Ledger::current(), transactionDate);

currencyExchangeHelper.parmExchangeRate1(1.234);
currencyExchangeHelper.parmExchangeRate2(2.54321);


result = currencyExchangeHelper.calculateTransactionToAccounting( transactionCurrency, 543.34, true);

=> Calculate by overriding the default exchange rate type from the ledger

Calculating an exchange rate by overriding the default exchange rate type would be useful when it is necessary to use a different set of exchange rates for a calculation scenario. Examples might include budget processing or consolidations.

CurrencyExchangeHelper currencyExchangeHelper;
TransDate transactionDate;
CurrencyCode transactionCurrency = 'CAD';
AmountMst result;
currencyExchangeHelper = CurrencyExchangeHelper::newExchangeDate( Ledger::current(), transactionDate);
currencyExchangeHelper.parmExchangeRateType( ExchangeRateType::findByName('SpecialRateType').RecId);


result = currencyExchangeHelper.calculateTransactionToAccounting( transactionCurrency, 200.75, true);

=> Calculate outside the context of ledger

Calculating an exchange rate by overriding the default exchange rate type would be useful when it is necessary to use a different set of exchange rates for a calculation scenario. Examples might include budget processing or consolidations.

CurrencyExchangeHelper currencyExchangeHelper;
TransDate transactionDate;
CurrencyCode fromCurrency = 'CAD';
CurrencyCode toCurrency = 'USD';
AmountCur result;
currencyExchangeHelper = CurrencyExchangeHelper::construct(); currencyExchangeHelper.parmExchangeDate(transactionDate); currencyExchangeHelper.parmExchangeRateType( ExchangeRateType::findByName('SpecialRateType').RecId);


result = currencyExchangeHelper.calculateCurrencyToCurrency( fromCurrency, toCurrency, 123.45, true);


Code for Exchange rates in AX 2012 :
currency::find(CurrencyCode).findExchRate(today());
in Dynamic ax 2009 following code is for Dynamic AX 2012

    static ExchRate exchRateFind(CurrencyCode _currencyCode = '')
{
    ExchangeRateType    ExchangeRateType;
   // ExchangeRate        ExchangeRate;
    ExchangeRateCurrencyPair    ExchangeRateCurrencyPair;
    ExchRate    ret;
    ExchangeRateHelper exchangeRateHelper;
    TransDate transactiondate;
    CurrencyExchangeRate exchangeRate1;
    CurrencyExchangeRate exchangeRate;
    ;
    exchangeRateHelper = ExchangeRateHelper::newExchangeDate(Ledger::current(),
_currencyCode, systemDateGet());

    exchangeRate1 = exchangeRateHelper.getExchangeRate1();
    exchangeRate = exchangeRateHelper.displayStoredExchangeRate(exchangeRate1);

//Ledger::find(Ledger::current()).DefaultExchangeRateType
    return exchangeRate;
}
There is a new class by name CurrencyExchangeHelper that has been introduced in AX 2012 to support this.In Microsoft Dynamics AX 2012, the currency and exchange rate framework has been enhanced to share information across multiple legal entities.
This class will help you to do some calculations between currencies. Some important methods to use:
calculateTransactionToAccounting
Example : This method will convert the transaction currency in to accounting currency defined in ledger Table.
static void SR_CEH_Example1(Args _args)
{
    CurrencyExchangeHelper currencyExchangeHelper;
    CurrencyCode transCurrency = ‘EUR’;
    AmountCur amountCur = 500.00;
    AmountMst amountMST;
   
    currencyExchangeHelper = CurrencyExchangeHelper::newExchangeDate(Ledger::current(), systemDateGet());
    amountMST = currencyExchangeHelper.calculateTransactionToAccounting(transCurrency, amountCur ,true);
    info(strFmt(‘%1’,amountMST));
}

Result :
image
calculateAccountingToTransaction
This method calculates the transaction currency amount from an accounting currency given.
static void SR_CEH_Example2(Args _args)
{
    CurrencyExchangeHelper currencyExchangeHelper;
    CurrencyCode transCurrency = ‘EUR’;
    AmountCur amountCur;
    AmountMst amountMST = 500.00;
   
    currencyExchangeHelper = CurrencyExchangeHelper::newExchangeDate(Ledger::primaryLedger(CompanyInfo::findDataArea("DUM").RecId), systemDateGet());
    amountCur = currencyExchangeHelper.calculateAccountingToTransaction(transCurrency, amountMST ,true);
    info(strFmt(‘%1’,amountcur));
}
Result :
image
While searching through, I found that there are  parmExchangeRate1 and parmExchangeRate2  methods that to calculate based on the exchange rates that have been provided. Please refer to the below example which calculates the misc charges [markup amount] based on the exchange rates defined.


Class Name : Markup >> calcMarkupAmount
image

How to Find Exchange rate in AX 2012 :

ExchangeRateHelper    exchangeRatehelper;
TransDate    transdate;
CurrencyCode  transactioncurrency = 'CAD';
CurrencyExchangeRate  excRate1,excRate2;

;
transdate  = mkdate(21,2,2012);

exchangeRatehelper = ExchangeRateHelper::newExchangeDate(Ledger::current(),transactionCurrency,transdate);
excRate1    = exchangeRateHelper.getExchangeRate1();
excRate2    = exchangeRateHelper.getExchangeRate2(0;
INFO(NUM2STR(exchangeRate1,2,2,1,1));

1 comment: