[SAP ABAP] Convert Currency

 

FUNCTION zfm_convert_currency .
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(I_CURR) LIKE  T001-WAERS DEFAULT 'IDR'
*"     VALUE(I_DATE) LIKE  SY-DATUM DEFAULT SY-DATUM
*"     VALUE(I_VALUE) LIKE  EINE-NETPR OPTIONAL
*"  EXPORTING
*"     VALUE(E_EXCH_RATE) LIKE  BAPI1093_0 STRUCTURE  BAPI1093_0
*"     VALUE(E_VALUE) LIKE  EINE-NETPR
*"----------------------------------------------------------------------
  DATA : lv_return LIKE bapiret1,
         lw_exch_rate LIKE bapi1093_0,
         lv_curx   LIKE bapi1090_1,
         lv_retr   LIKE bapiret1,
         lv_xfactor TYPE i.

  CALL FUNCTION 'BAPI_EXCHANGERATE_GETDETAIL'
    EXPORTING
      rate_type  = 'M'
      from_curr  = i_curr
      to_currncy = 'IDR'
      date       = i_date
    IMPORTING
      exch_rate  = lw_exch_rate
      return     = lv_return.


  CALL FUNCTION 'BAPI_CURRENCY_GETDECIMALS'
    EXPORTING
      currency          = 'IDR'
    IMPORTING
      currency_decimals = lv_curx.

  IF sy-subrc = 0.
    CASE lv_curx-curdecimals.
      WHEN 0.
        lv_xfactor = 100.
      WHEN 1.
        lv_xfactor = 10.
      WHEN 2.
        lv_xfactor = 1.
    ENDCASE.
  ELSE.
    lv_xfactor = 1.
  ENDIF.

  e_value =  i_value * ( lw_exch_rate-exch_rate * lw_exch_rate-to_factor ) / lv_xfactor.


ENDFUNCTION.

Comments