0% found this document useful (0 votes)
166 views6 pages

Zfagl Copy Leading Ledger

This document contains a SAP report program that copies data from a leading ledger to another ledger for a given company code and fiscal year. It retrieves the source and target ledger numbers, validates the data exists, and allows the user to confirm overwriting existing data. If confirmed, it deletes existing data for the target ledger and fiscal year, then copies over the total balances table as well as the actual and planned line item tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
166 views6 pages

Zfagl Copy Leading Ledger

This document contains a SAP report program that copies data from a leading ledger to another ledger for a given company code and fiscal year. It retrieves the source and target ledger numbers, validates the data exists, and allows the user to confirm overwriting existing data. If confirmed, it deletes existing data for the target ledger and fiscal year, then copies over the total balances table as well as the actual and planned line item tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

*&---------------------------------------------------------------------*

*& Report FAGL_COPY_LEADING_LEDGER


*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT zfagl_copy_leading_ledger.

TABLES: faglflexa.

SELECTION-SCREEN BEGIN OF BLOCK main WITH FRAME TITLE text-001.


PARAMETERS: p_rldnr LIKE t881-rldnr OBLIGATORY,
p_bukrs LIKE bkpf-bukrs OBLIGATORY,
p_gjahr LIKE bkpf-gjahr OBLIGATORY.
SELECTION-SCREEN END OF BLOCK main.

DATA: ls_t881 TYPE t881,


ls_tabnames TYPE fagl_tabnames.
DATA: ld_leading TYPE rldnr,
g_mensaje(100) TYPE c,
g_respuesta TYPE c.

CALL FUNCTION 'FAGL_GET_LEADING_LEDGER'


IMPORTING
e_rldnr = ld_leading
EXCEPTIONS
not_found = 1
more_than_one = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

CALL FUNCTION 'FAGL_GET_TABLENAMES'


EXPORTING
i_ledger = ld_leading
IMPORTING
es_tabnames = ls_tabnames
EXCEPTIONS
not_found = 1
configuration_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
SELECT SINGLE * FROM faglflexa
WHERE rldnr = p_rldnr
AND rbukrs = p_bukrs
AND ryear = p_gjahr.

IF sy-subrc EQ 0.
CALL FUNCTION 'POPUP_TO_CONFIRM'
EXPORTING
text_question = 'Desea sobrescribir los datos?.'
text_button_1 = 'SI'
text_button_2 = 'NO'
IMPORTING
answer = g_respuesta
* TABLES
* PARAMETER =
EXCEPTIONS
TEXT_NOT_FOUND = 1
OTHERS = 2
.

CASE g_respuesta.
WHEN '1'.
" Delete faglflext
DELETE FROM faglflext WHERE ryear EQ p_gjahr
AND rldnr EQ p_rldnr
AND rbukrs EQ p_bukrs.
" Delete faglflexa
DELETE FROM faglflexa WHERE ryear EQ p_gjahr
AND rldnr EQ p_rldnr
AND rbukrs EQ p_bukrs.
" Delete faglflexp
DELETE FROM faglflexp WHERE ryear EQ p_gjahr
AND rldnr EQ p_rldnr
AND rbukrs EQ p_bukrs.
WHEN '2'.
EXIT.
ENDCASE.
ENDIF.
*
* CONCATENATE 'Ya existen datos para el ledger' p_rldnr 'La sociedad' p_bukrs 'y
el ao' p_gjahr
* INTO g_mensaje SEPARATED BY space.
* MESSAGE g_mensaje TYPE 'S'.

SELECT SINGLE * FROM t881 INTO ls_t881 WHERE rldnr = p_rldnr.


IF sy-subrc = 0.
* copy of total table (actual data + plan data)
PERFORM copy_move_table_tt_pi USING ld_leading p_rldnr
ls_tabnames-tot_table
ls_tabnames-pi_table.
COMMIT WORK.
* copy of actual line item table
PERFORM copy_move_table USING ld_leading p_rldnr
ls_tabnames-si_table.
COMMIT WORK.
* copy of plan line item table
PERFORM copy_move_table USING ld_leading p_rldnr
ls_tabnames-pi_table.
COMMIT WORK.
MESSAGE 'Datos cargados con xito' TYPE 'I'.
ELSE.
MESSAGE 'El ledger no existe' TYPE 'S'.
ENDIF.

**********************************************************************

*&---------------------------------------------------------------------*
*& Form copy_cust_table
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM copy_cust_table USING id_source_ledger TYPE rldnr
id_target_ledger TYPE rldnr
id_tabname TYPE tabname.

FIELD-SYMBOLS: <lt_table> TYPE table.


DATA: lt_table TYPE REF TO data.
FIELD-SYMBOLS: <ls_table> TYPE any.
DATA: ls_table TYPE REF TO data.
FIELD-SYMBOLS: <rldnr> TYPE rldnr.

CREATE DATA lt_table


TYPE STANDARD TABLE OF (id_tabname).
ASSIGN lt_table->* TO <lt_table>.
REFRESH <lt_table>.

SELECT * FROM (id_tabname) INTO TABLE <lt_table>


WHERE rldnr = id_source_ledger.

LOOP AT <lt_table> ASSIGNING <ls_table>.


ASSIGN COMPONENT 'RLDNR' OF STRUCTURE <ls_table> TO <rldnr>.
<rldnr> = id_target_ledger.
ENDLOOP.

INSERT (id_tabname) FROM TABLE <lt_table>.

ENDFORM. " copy_cust_table

*&---------------------------------------------------------------------*
*& Form copy_move_table
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM copy_move_table USING id_source_ledger TYPE rldnr
id_target_ledger TYPE rldnr
id_tabname TYPE tabname.

CONSTANTS: c_maxsize TYPE i VALUE '5000'.

FIELD-SYMBOLS: <lt_table> TYPE table.


DATA: lt_table TYPE REF TO data.
FIELD-SYMBOLS: <ls_table> TYPE any.
DATA: ls_table TYPE REF TO data.
FIELD-SYMBOLS: <rldnr> TYPE rldnr.
DATA: ld_ok TYPE c.
DATA: g_cursor TYPE cursor.

CREATE DATA lt_table


TYPE STANDARD TABLE OF (id_tabname).
ASSIGN lt_table->* TO <lt_table>.
REFRESH <lt_table>.

OPEN CURSOR WITH HOLD g_cursor FOR


SELECT * FROM (id_tabname) WHERE rldnr = id_source_ledger
AND ryear = p_gjahr
AND rbukrs = p_bukrs.
IF sy-subrc = 0.
ld_ok = 'X'.
ELSE.
CLEAR ld_ok.
ENDIF.

WHILE ld_ok = 'X'.

REFRESH <lt_table>.

FETCH NEXT CURSOR g_cursor


INTO TABLE <lt_table>
PACKAGE SIZE c_maxsize.
IF sy-subrc <> 0.
CLOSE CURSOR g_cursor.
CLEAR ld_ok.
EXIT.
ENDIF.

LOOP AT <lt_table> ASSIGNING <ls_table>.


ASSIGN COMPONENT 'RLDNR' OF STRUCTURE <ls_table> TO <rldnr>.
<rldnr> = id_target_ledger.
ENDLOOP.

INSERT (id_tabname) FROM TABLE <lt_table>.


* COMMIT WORK.

ENDWHILE.

REFRESH <lt_table>.

ENDFORM. " copy_move_table

*&---------------------------------------------------------------------*
*& Form copy_move_table_tt_pi
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM copy_move_table_tt_pi USING id_source_ledger TYPE rldnr
id_target_ledger TYPE rldnr
id_tabname TYPE tabname
id_tabname_pi TYPE tabname.

CONSTANTS: c_maxsize TYPE i VALUE '5000'.

FIELD-SYMBOLS: <lt_table> TYPE table,


<lt_table_pi> TYPE table,
<ls_table> TYPE any,
<ls_table_pi> TYPE any,
<ld_rldnr> TYPE rldnr.
DATA: lt_table TYPE REF TO data,
lt_table_pi TYPE REF TO data,
lt_si_table_add TYPE fagl_flag_table.
DATA: ls_table TYPE REF TO data,
ls_table_pi TYPE REF TO data.
DATA: ld_ok TYPE c,
ld_flag TYPE c.
DATA: g_cursor TYPE cursor.
CLEAR ld_flag.

CREATE DATA lt_table


TYPE STANDARD TABLE OF (id_tabname).
ASSIGN lt_table->* TO <lt_table>.
REFRESH <lt_table>.

CREATE DATA lt_table_pi


TYPE STANDARD TABLE OF (id_tabname_pi).
ASSIGN lt_table_pi->* TO <lt_table_pi>.
REFRESH <lt_table_pi>.

CREATE DATA ls_table_pi TYPE (id_tabname_pi).


ASSIGN ls_table_pi->* TO <ls_table_pi>.

* copy actual(!) and plan data of the total table via the
* plan line item table
OPEN CURSOR WITH HOLD g_cursor FOR
SELECT * FROM (id_tabname) WHERE rldnr = id_source_ledger
AND rbukrs = p_bukrs
AND ryear = p_gjahr.
IF sy-subrc = 0.
ld_ok = 'X'.
ELSE.
CLEAR ld_ok.
ENDIF.

WHILE ld_ok = 'X'.

REFRESH <lt_table>.
REFRESH <lt_table_pi>.
REFRESH lt_si_table_add.

FETCH NEXT CURSOR g_cursor


INTO TABLE <lt_table>
PACKAGE SIZE c_maxsize.
IF sy-subrc <> 0.
CLOSE CURSOR g_cursor.
CLEAR ld_ok.
EXIT.
ENDIF.

LOOP AT <lt_table> ASSIGNING <ls_table>.


ASSIGN COMPONENT 'RLDNR' OF STRUCTURE <ls_table> TO <ld_rldnr>.
<ld_rldnr> = id_target_ledger.
MOVE-CORRESPONDING <ls_table> TO <ls_table_pi>.
APPEND <ls_table_pi> TO <lt_table_pi>.
APPEND ld_flag TO lt_si_table_add. " no line item
ENDLOOP.

* Posting module
CALL FUNCTION 'G_INSERT_SI_AND_ADD_TO_TT'
EXPORTING
iv_table_name = id_tabname_pi
it_si_table = <lt_table_pi>
it_si_table_add = lt_si_table_add
EXCEPTIONS
table_not_installed = 1
no_poper_field = 2
wrong_call = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.

ENDIF.

ENDWHILE.

REFRESH <lt_table>.
REFRESH <lt_table_pi>.
REFRESH lt_si_table_add.

ENDFORM. "copy_move_table_tt_si

You might also like