Exchange Rates Handler

BeliasBelias Member Posts: 2,998
edited 2009-09-22 in NAV Tips & Tricks
Hi everyone, I recently have had to deal with exchange rates, and a thing I found really boring is to manage all the cases below:
if i start from LCY amount and i put it on FCY amount then exchrateLCYtoFCY
if i start from FCY amount and i put it on LCY amount then exchrateFCYtoLCY
and so on....
the function i post here, receive the six parameters, and depending on the values of CDFromCurrency and CDToCurrency decides what is the function to run (and if it must be run). At the end, it rounds the value to the correct amount.

Parameters:
Var Name DataType Subtype Length
No vDECAmount Decimal
No vCDFromCurrency Code 10
No vCDToCurrency Code 10
No vGMADate Date
No vDECCurrFactor Decimal
No vTFUnitAmount Boolean

Locals:
Name DataType Subtype Length
lTBCurrency Record Currency
IF vDECAmount <> 0 THEN BEGIN
  IF vCDFromCurrency <> vCDToCurrency THEN BEGIN
    IF vCDToCurrency = '' THEN BEGIN
      IF (vDECCurrFactor =  0) OR (vDECCurrFactor =  1) THEN     //Correction For Currency factor
        vDECCurrFactor := TBCurrExhRate.ExchangeRate(vGMADate,vCDFromCurrency);
      vDECAmount := TBCurrExhRate.ExchangeAmtFCYToLCY(vGMADate,vCDFromCurrency,vDECAmount,vDECCurrFactor);
    END ELSE BEGIN
      IF vCDFromCurrency = '' THEN BEGIN
        IF (vDECCurrFactor =  0) OR (vDECCurrFactor =  1) THEN  //Correction For Currency factor
          vDECCurrFactor := TBCurrExhRate.ExchangeRate(vGMADate,vCDToCurrency);
        vDECAmount := TBCurrExhRate.ExchangeAmtLCYToFCY(vGMADate,vCDToCurrency,vDECAmount,vDECCurrFactor);
      END ELSE BEGIN
        vDECAmount := TBCurrExhRate.ExchangeAmtFCYToFCY(vGMADate,vCDFromCurrency,vCDToCurrency,vDECAmount);
      END;
    END;
  END;

  IF vTFUnitAmount THEN BEGIN
    IF vCDToCurrency = '' THEN BEGIN
      TBGLSetup.GET;
      vDECAmount := ROUND(vDECAmount,TBGLSetup."Unit-Amount Rounding Precision");
    END ELSE BEGIN
      lTBCurrency.GET(vCDToCurrency);
      vDECAmount := ROUND(vDECAmount,lTBCurrency."Unit-Amount Rounding Precision");
    END;
  END ELSE BEGIN
    IF vCDToCurrency = '' THEN BEGIN
      TBGLSetup.GET;
      vDECAmount := ROUND(vDECAmount,TBGLSetup."Amount Rounding Precision");
    END ELSE BEGIN
      lTBCurrency.GET(vCDToCurrency);
      vDECAmount := ROUND(vDECAmount,lTBCurrency."Amount Rounding Precision");
    END;
  END;
END;
EXIT(vDECAmount);
-Mirko-
"Never memorize what you can easily find in a book".....Or Mibuso
My Blog

Comments

  • ara3nara3n Member Posts: 9,256
    Neat.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • BeliasBelias Member Posts: 2,998
    I automated the "choice" of the standard functions of nav
    P.S.: there's a little bug if the user is on an invoice and changes the currency factor: i already corrected it and i'll post it as soon as possible...i'm a little busy now, sorry
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • ara3nara3n Member Posts: 9,256
    yes, I saw it in the function as I was looking at it.
    As for the bug. I suggest to send it to MS.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • BeliasBelias Member Posts: 2,998
    ara3n wrote:
    yes, I saw it in the function as I was looking at it.
    As for the bug. I suggest to send it to MS.
    No, it's a bug of mine :)
    i didn't thought that the user can change the currency factor, and this function does not receive a currency factor as parameter, therefore the currency factor is always recalculated, and there can be some inconsistencies.
    I'm now editing my post in order to correct the function :wink:
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • BeliasBelias Member Posts: 2,998
    Neat.
    I've been noticed by e-mail of your previous post, this is why i told that i'm using the standard functions, afterall :mrgreen:
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • krikikriki Member, Moderator Posts: 9,112
    Belias wrote:
    Neat.
    I've been noticed by e-mail of your previous post, this is why i told that i'm using the standard functions, afterall :mrgreen:
    Send it to MS anyway, it would be a nice standard function!
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • BeliasBelias Member Posts: 2,998
    #-o another little bug (before the go live...phew)
    here's the old line
    IF vDECCurrFactor =  0 THEN
    
    here's the new line
    IF (vDECCurrFactor =  0) OR (vDECCurrFactor =  1) THEN
    
    i corrected the code above, too.
    this is an important correction in my project, because i am adding lines to an invoice, taking values from other documents: if the invoice is in LCY and someone does a
    salesinvhead."currency factor" := currencyexchrates.exchangerate(salesinvhead."posting date","salesinvhead"."currency code");
    
    the resulting factor is 1.
    if my "other document" is in USD, it must be converted, before adding it to the sales invoice line

    @kriki: i posted this to MS connect...were you meaning to post this in that place when you said "send it to MS"?
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • krikikriki Member, Moderator Posts: 9,112
    Belias wrote:
    @kriki: i posted this to MS connect...were you meaning to post this in that place when you said "send it to MS"?
    Yes.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • BeliasBelias Member Posts: 2,998
    They said that they'll take into consideration my function for the next release!
    Should i hope for it or MS said this to everyone? :mrgreen:
    I'd be really proud if will become a standard functionality 8)

    P.S.: i forgot a TBGLSetup.GET; in the function :oops: -Just added-
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
Sign In or Register to comment.