[SAP ABAP] - Convert Tax Currency

FUNCTION yofm_convert_tax_currency.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(P_FROM) LIKE  BAPI1093_1-FROM_CURR OPTIONAL
*"     VALUE(P_TO) LIKE  BAPI1093_1-TO_CURRNCY OPTIONAL
*"     VALUE(P_DAT) LIKE  BAPI1093_2-TRANS_DATE DEFAULT SY-DATUM
*"     VALUE(P_LOCAL) LIKE  BSEG-DMBTR OPTIONAL
*"  EXPORTING
*"     VALUE(P_FOREIGN) LIKE  BSEG-DMBTR
*"     VALUE(P_RATE) TYPE  AU_AMOUNT
*"----------------------------------------------------------------------


  DATA :  vcurx   LIKE bapi1090_1,
            vrate   LIKE bapi1093_0,
            vretr   LIKE bapiret1,
            xfactor TYPE i.

  CALL FUNCTION 'BAPI_EXCHANGERATE_GETDETAIL'
    EXPORTING
      rate_type  'M'
      from_curr  p_from
      to_currncy p_to
      date       p_dat
    IMPORTING
      exch_rate  vrate
      return     vretr.

  CALL FUNCTION 'BAPI_CURRENCY_GETDECIMALS'
    EXPORTING
      currency          p_to
    IMPORTING
      currency_decimals vcurx.

  IF sy-subrc 0.
    CASE vcurx-curdecimals.
      WHEN 0.
        xfactor 100.
      WHEN 1.
        xfactor 10.
      WHEN 2.
        xfactor 1.
    ENDCASE.
  ELSE.
    xfactor 1.
  ENDIF.

  p_rate vrate-exch_rate * vrate-to_factor / vrate-from_factor.
  TRY.
      p_foreign =  p_local * p_rate.
    CATCH cx_sy_arithmetic_overflow.
  ENDTRY.


ENDFUNCTION.

Comments