SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Currency | Валюта



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Function Modules | Функциональные модули
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Sat Sep 08, 2007 7:15 am    Post subject: Currency | Валюта Reply with quote

FWOS_CURRENCY_DECIMALS_READ
All the currency amounts are stored in SAP tables as CURR(n,2) (the same as DEC(n,2)). So before any arithmetic operation value should be adjusted using the real decimals number for the given currency (stored in TCURX).
Conversion Rates by type and date are stored in TCURR (+factors). Standard type is M. Date is stored in inverted format (the most recent date has the numerically smallest value). ABAP code to convert dates:
convert date p_date into inverted-date w_date.
convert inverted-date w_date into date p_date.
the only difference between CONVERT_TO_LOCAL_CURRENCY and CONVERT_TO_FOREIGN_CURRENCY seems to be the following:
Foreign currency is TCURR-FCURR (From Currency)
Local Currency is TCURR-TCURR (To Currency)
So result will be slightly different for the both functions (two rates stored in the TCURR: e.g. JPY->USD rate is 0.00880, USD->JPY rate is 122.00000). Better to use CONVERT_TO_LOCAL_CURRENCY, because multiplication is more exact operation than division.
Both conversion functions can return also selected rate and factors

Code:
FORM DETERMINE_DECIMALS USING  I_CURR
                               I_RATE
                               O_RATE.
* Declare local variables
  DATA: L_DEC LIKE TCURX-CURRDEC.

  CALL FUNCTION 'FWOS_CURRENCY_DECIMALS_READ'
       EXPORTING
            I_CURRENCY         = I_CURR
       IMPORTING
            E_DECIMALS         = L_DEC
       EXCEPTIONS
            I_CURRENCY_INITIAL = 1
            OTHERS             = 2.

  CASE L_DEC.
    WHEN '0'.
      O_RATE = I_RATE * '100'.
    WHEN '1'.
      O_RATE = I_RATE * '10'.
    WHEN '2'.
      O_RATE = I_RATE * '1'.
    WHEN '3'.
      O_RATE = I_RATE * '0.1'.
    WHEN '4'.
      O_RATE = I_RATE * '0.01'.
  ENDCASE.

ENDFORM.                    " determine_decimals

CONVERT_TO_LOCAL_CURRENCY

Code:
DATA : t_amount(16) TYPE p value '1000'.
DATA : l_amount(16) TYPE p.
 
CALL FUNCTION 'CONVERT_TO_LOCAL_CURRENCY'
  EXPORTING
*   CLIENT                  = SY-MANDT
    date                    = sy-datum
    foreign_amount          = t_amount
    foreign_currency        = 'EUR'
    local_currency          = 'USD'
    TYPE_OF_RATE            = 'M'
 IMPORTING
*   EXCHANGE_RATE           =
*   FOREIGN_FACTOR          =
    LOCAL_AMOUNT            =  l_amount
*   LOCAL_FACTOR            =
*   EXCHANGE_RATEX          =
*   FIXED_RATE              =
*   DERIVED_RATE_TYPE       =
* EXCEPTIONS
*   NO_RATE_FOUND           = 1
*   OVERFLOW                = 2
*   NO_FACTORS_FOUND        = 3
*   NO_SPREAD_FOUND         = 4
*   DERIVED_2_TIMES         = 5
*   OTHERS                  = 6
          .
IF NOT sy-subrc IS INITIAL.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
 
WRITE : l_amount.

CONVERT_TO_FOREIGN_CURRENCY
Both CONVERT_TO_LOCAL_CURRENCY and CONVERT_TO_FOREIGN_CURRENCY use complicated logic considering contents of the TCURX (Decimal Places in Currencies), TCURR (Exchange Rates), TCURF (Conversion Factors) and other tables, _ conversion rules etc. Often functions return error message if information in the tables is inconsistent or not maintained.
To simple currency conversion can be used direct calculation based on the tables TCURR and TCURX.
See also BAPIs:
Currency (since 4.0A) - to get currencies and decimals
ExchangeRate (since 4.5A) - to get exchange rates and factors for a date

Code:
REPORT Z_CURR_CONV.

data: w_dec1 like tcurx-currdec,
w_amt like ekpo-netwr,
w_rate like tcurr-ukurs,
w_fact1 like tcurr-ffact,
w_fact2 like tcurr-ffact.

parameters:
p_curr1 like tcurc-waers default 'RUB',
p_amt1 like ekpo-netwr default '7895',
p_curr2 like tcurc-waers default 'USD',
pdate like sy-datum default '20030311'.

end-of-selection.
write:
/ 'Entered:',
/ ' from currency:', p_amt1 currency p_curr1, p_curr1, '<-', p_amt1,
/ ' to currency :', p_curr2,
/ ' on :', pdate.
CALL FUNCTION 'FWOS_CURRENCY_DECIMALS_READ'
  EXPORTING
    I_CURRENCY = p_curr1
  IMPORTING
    E_DECIMALS = w_dec1
  EXCEPTIONS
    I_CURRENCY_INITIAL = 1
    OTHERS = 2.
IF NOT sy-subrc IS INITIAL.
  w_dec1 = 2.
ENDIF.
p_amt1 = p_amt1 * 10 ** ( w_dec1 - 2 ).

write:
/ 'after adjusting using currency decimals:',
/ ' from currency', p_amt1 currency p_curr1, p_curr1,
'<-', p_amt1, '(', w_dec1, ')'.

CALL FUNCTION 'CONVERT_TO_FOREIGN_CURRENCY'
  EXPORTING
*  CLIENT = SY-MANDT
    DATE = pdate
    FOREIGN_CURRENCY = p_curr2
    LOCAL_AMOUNT = p_amt1
    LOCAL_CURRENCY = p_curr1
*  RATE = 0
*  TYPE_OF_RATE = 'M'
*  READ_TCURR = 'X'
  IMPORTING
    EXCHANGE_RATE = w_rate
    FOREIGN_AMOUNT = w_amt
    FOREIGN_FACTOR = w_fact1
    LOCAL_FACTOR = w_fact2
*  EXCHANGE_RATEX =
*  DERIVED_RATE_TYPE =
*  FIXED_RATE =
  EXCEPTIONS
    NO_RATE_FOUND = 1
    OVERFLOW = 2
    NO_FACTORS_FOUND = 3
    NO_SPREAD_FOUND = 4
    DERIVED_2_TIMES = 5
    OTHERS = 6.
IF NOT sy-subrc IS INITIAL.
  write: / 'Conversion to for.curr. failed:',
    p_curr1, '->', p_curr2, 'err.code=', sy-subrc.
ELSE.
  write: / 'to For.curr:', p_amt1 currency p_curr1, p_curr1, '->',
    w_amt currency p_curr2, p_curr2,
    '(', w_rate, ')', w_fact1, w_fact2.
ENDIF.

CALL FUNCTION 'CONVERT_TO_LOCAL_CURRENCY'
  EXPORTING
*  CLIENT = SY-MANDT
    DATE = pdate
    FOREIGN_AMOUNT = p_amt1
    FOREIGN_CURRENCY = p_curr1
    LOCAL_CURRENCY = p_curr2
*  RATE = 0
*  TYPE_OF_RATE = 'M'
*  READ_TCURR = 'X'
  IMPORTING
    EXCHANGE_RATE = w_rate
    FOREIGN_FACTOR = w_fact1
    LOCAL_AMOUNT = w_amt
    LOCAL_FACTOR = w_fact2
*  EXCHANGE_RATEX =
*  FIXED_RATE =
*  DERIVED_RATE_TYPE =
  EXCEPTIONS
    NO_RATE_FOUND = 1
    OVERFLOW = 2
    NO_FACTORS_FOUND = 3
    NO_SPREAD_FOUND = 4
    DERIVED_2_TIMES = 5
    OTHERS = 6.

IF NOT sy-subrc IS INITIAL.
  write: / 'Conversion to loc.curr. failed:',
    p_curr1, '->', p_curr2, 'err.code=', sy-subrc.
ELSE.
  write: / 'to Loc.curr:', p_amt1 currency p_curr1, p_curr1, '->',
    w_amt currency p_curr2, p_curr2,
    '(', w_rate, ')', w_fact1, w_fact2.
ENDIF.

write / 'End Of Report'.


SPELL_AMOUNT - Convert numbers and figures in words convert an amount into words
convert a number into words (CURRENCY=space)
set decimal point in an amount and return number of decimals for the currency (LANGUAGE=space)

FM HR_IN_CHG_INR_WRDS

Code:

call function 'HR_IN_CHG_INR_WRDS'
  exporting
    amt_in_num               = '1234567.89'
 IMPORTING
   AMT_IN_WORDS             =  amt_in_words
 EXCEPTIONS
   DATA_TYPE_MISMATCH       = 1
   OTHERS                   = 2.
 
IF NOT sy-subrc IS INITIAL.
 MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
endif.


READ_EXCHANGE_RATE - Determine exchange rate from table TCURR

The exchange rate valid on the specified date is read from table TCURR according to the currency key and the exchange rate type. The ratios for the currency units from table TCURF are transferred to the calling program in addition to the exchange rate determined. if exchange rate fixing is defined for the exchange rate type TYPE_OF_RATE, this information is transferred to the calling program.
Code:
REPORT Z_CURRENCY_CONVERSION.

DATA: gd_fcurr TYPE tcurr-fcurr,
gd_tcurr TYPE tcurr-tcurr,
gd_date TYPE sy-datum,
gd_value TYPE i.

gd_fcurr = 'EUR'.
gd_tcurr = 'GBP'.
gd_date = sy-datum.
gd_value = 10.

PERFORM currency_conversion USING gd_fcurr
gd_tcurr
gd_date
CHANGING gd_value.

* Convert value to Currency value
*&---------------------------------------------------------------------*
*& Form currency_conversion
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_GD_FCURR text
* -->P_GD_TCURR text
* -->P_GD_DATE text
* <--P_GD_VALUE text
*----------------------------------------------------------------------*
FORM currency_conversion USING p_fcurr
p_tcurr
p_date
CHANGING p_value.

DATA: t_er TYPE tcurr-ukurs,
t_ff TYPE tcurr-ffact,
t_lf TYPE tcurr-tfact,
t_vfd TYPE datum,
ld_erate(12) TYPE c.

CALL FUNCTION 'READ_EXCHANGE_RATE'
EXPORTING
* CLIENT = SY-MANDT
date = p_date
foreign_currency = p_fcurr
local_currency = p_tcurr
TYPE_OF_RATE = 'M'
* EXACT_DATE = ' '
IMPORTING
exchange_rate = t_er
foreign_factor = t_ff
local_factor = t_lf
valid_from_date = t_vfd
* DERIVED_RATE_TYPE =
* FIXED_RATE =
* OLDEST_RATE_FROM =
EXCEPTIONS
no_rate_found = 1
no_factors_found = 2
no_spread_found = 3
derived_2_times = 4
overflow = 5
zero_rate = 6
OTHERS = 7
.
IF sy-subrc EQ 0.
ld_erate = t_er / ( t_ff / t_lf ).
p_value = p_value * ld_erate.
ENDIF.
ENDFORM. " currency_conversion


CURRENCY_AMOUNT_DISPLAY_TO_SAP
Convert currency value from display to SAP

The following code shows how CURRENCY_AMOUNT_DISPLAY_TO_SAP can be used. You pass it a Currecny code(WAERS) and the displayed currency value. It will convert the value so that it can be stored in SAP. Without using this function module the value stored in SAP is not guaranteed to be correct. Ie 28000 JPY is stored within SAP as 280.

Code:
*Data declaration
*------------
* WMTO_S-AMOUNT =  Type DEC :: length 15 :: Deciamls 4
PARAMETER: p_curr   like TCURC-WAERS,     "Display currency
                 p_disval like WMTO_S-AMOUNT.  "Internal Amount

DATA:   gd_intval  like WMTO_S-AMOUNT. "Display Amount

CALL FUNCTION 'CURRENCY_AMOUNT_DISPLAY_TO_SAP'
  EXPORTING
    currency        = p_curr
    amount_display  = p_disval
  IMPORTING
    AMOUNT_INTERNAL = gd_intval
  EXCEPTIONS
    INTERNAL_ERROR  = 1
    OTHERS          = 2.
IF NOT sy-subrc IS INITIAL.
* You are now able to store the return value into an SAP table.
ENDIF.

************************************************************************
*Start-of-selection.
START-OF-SELECTION.

write:/(30)  'Value Displayed on screen in SAP:', p_disval,
/(30)  'Currency:',  p_curr,
/(30)  'Internal SAP value', gd_intval.


CURRENCY_AMOUNT_SAP_TO_DISPLAY
Convert currency value from SAP to display

The following code shows how CURRENCY_AMOUNT_SAP_TO_DISPLAY can be used. You pass it a Currecny code(WAERS) and an SAP stored currency value. It will convert the value into its correct currecny value. Without using this function module you are not guaranteed to be using the correct value as it is often missing a number of zeroes.
Ie 28000 JPY is stored within SAP as 280.

Code:
* DATA declaration
*-----------------
* WMTO_S-AMOUNT =  Type DEC :: length 15 :: Deciamls 4
parameter: p_discur like TCURC-WAERS,     "Display currency
                 p_intval like WMTO_S-AMOUNT.   "Internal Amount

DATA:      gd_disval  like WMTO_S-AMOUNT. "Display Amount

************************************************************************
*Start-of-selection.
START-OF-SELECTION.
CALL FUNCTION 'CURRENCY_AMOUNT_SAP_TO_DISPLAY'
EXPORTING
currency        = p_discur
amount_internal = p_intval
IMPORTING
AMOUNT_DISPLAY   = gd_disval
EXCEPTIONS
INTERNAL_ERROR   = 1
OTHERS           = 2.


IF sy-subrc EQ 0.
*  You are now able to manipulate the returned value.
*                         Ie Convert it to GBP


*  Without using this function module you would only be manipulating the
*  SAP stored value, which is often missing a number of zeroes.
*       Ie 28000 JPY is stored within SAP as 280.
ENDIF.

************************************************************************
*End-of-selection.
END-OF-SELECTION.
write:/(30)  'Value stored in SAP:', p_intval,
/(30)  'Displayed currency:',  p_discur,
/(30)  'Ammount is displayed Currency:', gd_disval.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Function Modules | Функциональные модули All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.