¿Como actualizar o crear un tipo de cambio en S4/Hana Public Cloud

En este ejemplo vamos a ver un ejemplo de codigo para crear o actualizar de un tipo de cambio en S4Hana Public 

CLASS zexchangerates DEFINITION

PUBLIC

FINAL

CREATE PUBLIC .

PUBLIC SECTION.

INTERFACES if_oo_adt_classrun .

PROTECTED SECTION.

PRIVATE SECTION.

ENDCLASS.

CLASS zexchangerates IMPLEMENTATION.

METHOD if_oo_adt_classrun~main.

* TYPES: BEGIN OF ty_entry,

* level TYPE i,

* parent TYPE string,

* name TYPE string,

* attr TYPE string,

* value TYPE string,

* END OF ty_entry.

*

* DATA: g_rates TYPE cl_exchange_rates=>ty_exchange_rates.

* CONSTANTS: gc_rate_type TYPE cl_exchange_rates=>ty_exchange_rate-rate_type VALUE 'M',

* gc_base TYPE cl_exchange_rates=>ty_exchange_rate-from_curr VALUE 'EUR'.

*

* DATA: w_entry TYPE ty_entry,

* w_rate TYPE cl_exchange_rates=>ty_exchange_rate,

* factor TYPE i_exchangeratefactorsrawdata,

* rate_to_store(16) TYPE p DECIMALS 5,

* l_result TYPE cl_exchange_rates=>ty_messages.

* w_rate-from_factor = factor-numberofsourcecurrencyunits.

* w_rate-to_factor = factor-numberoftargetcurrencyunits.

* w_rate-from_factor_v = 0.

* w_rate-to_factor_v = 0.

* rate_to_store = w_entry-value * factor-numberofsourcecurrencyunits / factor-numberoftargetcurrencyunits.

* w_rate-exch_rate = rate_to_store.

* w_rate-exch_rate_v = 0.

* APPEND w_rate TO g_rates.

*

"------------------------------------------------------------

" 1) Declarar estructuras/tables de la API CL_EXCHANGE_RATES

"------------------------------------------------------------

DATA: lt_exchange_rates TYPE cl_exchange_rates=>ty_exchange_rates,

ls_exchange_rate TYPE cl_exchange_rates=>ty_exchange_rate,

lt_messages TYPE cl_exchange_rates=>ty_messages.

"------------------------------------------------------------

" 2) Llenar UN registro de tipo de cambio (datos quemados)

" IMPORTANTE:

" - Los nombres de campos de ty_exchange_rate están

" basados en TCURR (KURST, FCURR, TCURR, GDATU, UKURS,

" FFACT, TFACT, ...).

" - Si ADT marca algún campo como inexistente, pulsa F2

" sobre el tipo cl_exchange_rates=>ty_exchange_rate

" y ajusta los nombres según tu sistema.

"------------------------------------------------------------

ls_exchange_rate = VALUE #(

from_factor = 1

rate_type = 'PEP'

from_curr = 'USD'

to_currncy = 'PEN'

valid_from = sy-datum

to_factor = 1

from_factor_v = 0

to_factor_v = 0

exch_rate = '3.60'

exch_rate_v = 0

).

APPEND ls_exchange_rate TO lt_exchange_rates.

"------------------------------------------------------------

" 3) Llamar al método estático PUT para grabar el registro

" - do_commit = abap_true → hace COMMIT WORK interno

"------------------------------------------------------------

TRY.

lt_messages = cl_exchange_rates=>put(

EXPORTING

exchange_rates = lt_exchange_rates

is_update_allowed = abap_true " opcional

is_fixed_rate_change_allowed = abap_true " opcional

* allowed_deviation = '000000.000000' " opcional

do_commit = abap_true

).

"--------------------------------------------------------

" 4) Mostrar mensajesx en la consola ADT

"--------------------------------------------------------

DATA(lv_text) = |Resultado de CL_EXCHANGE_RATES=>PUT:| && cl_abap_char_utilities=>newline.

LOOP AT lt_messages ASSIGNING FIELD-SYMBOL(<ls_msg>).

lv_text &&= |{ <ls_msg>-type } { <ls_msg>-id } { <ls_msg>-number } { <ls_msg>-message }| && cl_abap_char_utilities=>newline.

ENDLOOP.

out->write( lv_text ).

CATCH cx_exchange_rates INTO DATA(lx_exch).

out->write( |Error en CL_EXCHANGE_RATES: { lx_exch->get_text( ) }| ).

ENDTRY.

ENDMETHOD.

ENDCLASS.

¿Le ha resultado útil este artículo?