REPORT zalv_dinamico.
TABLES : sscrfields.
*
*-------------------------------------------------------------------------------------------------------------------*
* DECLARACION DE TYPE-POOLS
*-------------------------------------------------------------------------------------------------------------------*
*
TYPE-POOLS : abap. " ABAP Language Type-Pool
TYPE-POOLS : slis. " Tipos globales creacion de ALV
*-------------------------------------------------------------------------------------------------------------------*
* DECLARACION DE INCLUDES
*-------------------------------------------------------------------------------------------------------------------*
INCLUDE : <icon>. " Definicion de Iconos - ASCII-Codes
INCLUDE : <cl_alv_control>. " Definicion de Estilos de Celdas ALV
*
*-------------------------------------------------------------------------------------------------------------------*
* DECLARACION DE TYPE-POOLS
*-------------------------------------------------------------------------------------------------------------------*
TYPE-POOLS : vrm.
TYPE-POOLS : sscr.
TYPE-POOLS : rsds.
*-------------------------------------------------------------------------------------------------------------------*
* DECLARACION DE INCLUDES
*-------------------------------------------------------------------------------------------------------------------*
*-------------------------------------------------------------------------------------------------------------------*
* CLASES ABAP
*-------------------------------------------------------------------------------------------------------------------*
CLASS cl_abap_char_utilities DEFINITION LOAD.
*-------------------------------------------------------------------------------------------------------------------*
* CONSTANTES : DEFINICION PARA SEPARADORES DE CAMPOS
*-------------------------------------------------------------------------------------------------------------------*
*
*VALORES DE VERADDERO/FALSO
CONSTANTS : co_falso TYPE c LENGTH 001 VALUE 'F'. " False
CONSTANTS : co_verdadero TYPE c LENGTH 001 VALUE 'T'. " Verdadero
*-------------------------------------------------------------------------------------------------------------------*
* DECLARACION DE DATOS GLOBALES
*-------------------------------------------------------------------------------------------------------------------*
*
DATA : gd_continuar TYPE c LENGTH 01 VALUE co_falso.
*
*VARIABLES PARA DYNPROS
DATA : ok_code TYPE sy-ucomm.
*
*-------------------------------------------------------------------------------------------------------------------*
* DECLARACION DE DATOS GLOBALES DINAMICOS :
*-------------------------------------------------------------------------------------------------------------------*
*
DATA : ref_line_type TYPE REF TO cl_abap_structdescr.
DATA : ref_tabletype TYPE REF TO cl_abap_tabledescr.
DATA : ref_wa TYPE REF TO data.
*
DATA : ref_itab TYPE REF TO data.
DATA : ref_out TYPE REF TO cl_salv_table.
*
DATA : ref_functions TYPE REF TO cl_salv_functions_list.
*
DATA : ref_header TYPE REF TO cl_salv_form_layout_grid.
DATA : ref_label TYPE REF TO cl_salv_form_label.
DATA : ref_tab TYPE string.
*
DATA : ld_tabla TYPE tabname.
*
FIELD-SYMBOLS : <fsym_itab> TYPE ANY TABLE.
FIELD-SYMBOLS : <fsym_field> TYPE ANY.
DATA : ls_functxt TYPE smp_dyntxt.
*
* DEFINICION DATOS LOCALES PARA TRATAMIENTO DINAMICO
DATA : lo_dynamic_struct TYPE REF TO cl_abap_structdescr.
DATA : lt_comp_tabla TYPE cl_abap_structdescr=>component_table.
DATA : lo_dref_comptbi TYPE REF TO data.
DATA : lo_dref_locstr TYPE REF TO data.
DATA : lo_dref_loctbi TYPE REF TO data.
*
*-------------------------------------------------------------------------------------------------------------------*
* DECLARACION DE TIPOS DE TABLAS - TABLAS INTERNAS :
*-------------------------------------------------------------------------------------------------------------------*
*
DATA : ld_selection_id TYPE rsdynsel-selid. " ID de selección para diferenciar entre varios conjuntos
*
DATA : ld_tabclass TYPE tabclass. " Clase tabla
DATA : ld_tablename TYPE string.
DATA : ld_num_fields TYPE i. " numero de campos
*
DATA : lt_expressions TYPE rsds_texpr. " Expresiones
DATA : lt_field_ranges TYPE rsds_trange. " Rangos
DATA : ls_field_ranges LIKE LINE OF lt_field_ranges.
*
DATA : lt_tables_ini TYPE STANDARD TABLE OF rsdstabs. " Init: tablas
DATA : sl_tables_ini LIKE LINE OF lt_tables_ini.
*
DATA : lt_fields_ini TYPE STANDARD TABLE OF rsdsfields. " Init: campos
DATA : sl_fields_ini LIKE LINE OF lt_fields_ini.
*
DATA : lt_where_cond TYPE rsds_twhere. " Datos para la tabla where
DATA : ls_where_clause LIKE LINE OF lt_where_cond.
*
DATA : lt_where_tab TYPE STANDARD TABLE OF rsds_where_tab.
DATA : ls_where_tab LIKE LINE OF lt_where_tab.
*
DATA : lt_tabfields_not_display TYPE TABLE OF rsdsfields.
DATA : ls_tabfields_not_display LIKE LINE OF lt_tabfields_not_display.
*
DATA : gd_titulo_externo TYPE c LENGTH 080.
*
*-------------------------------------------------------------------------------------------------------------------*
* DECLARACION DE FIELD-SYMBOLS
*-------------------------------------------------------------------------------------------------------------------*
*
* DEFINICION FIELD-SYMBOLS LOCALES PARA TRATAMIENTO DINAMICO
FIELD-SYMBOLS : <fs_valor_campo> TYPE ANY.
*
FIELD-SYMBOLS : <fs_valor_struc> TYPE ANY.
FIELD-SYMBOLS : <fs_comp_tabla> LIKE LINE OF lt_comp_tabla.
*
FIELD-SYMBOLS : <fs_dynamic_tbi> TYPE ANY TABLE.
FIELD-SYMBOLS : <fs_dynamic_str> TYPE ANY.
*-------------------------------------------------------------------------------------------------------------------*
*
*-------------------------------------------------------------------------------------------------------------------*
*
PARAMETERS : pa_tab TYPE dd02l-tabname.
*-----------------------------------------------------------------------------------------------------------------*
* START-OF-SELECTION.
*-----------------------------------------------------------------------------------------------------------------*
START-OF-SELECTION.
*
ld_tablename = pa_tab.
*
CLEAR : ld_tabclass.
SELECT SINGLE tabclass INTO ld_tabclass
FROM dd02l
WHERE tabname EQ ld_tablename
AND as4local EQ 'A'
AND tabclass EQ 'TRANSP'.
*
IF ( sy-subrc EQ 0 ) AND NOT ( ld_tabclass IS INITIAL ).
*
PERFORM get_data_parametros_dynpro.
*
PERFORM actualiza_titulo.
*
CALL FUNCTION 'FREE_SELECTIONS_INIT'
EXPORTING
kind = 'T'
expressions = lt_expressions
IMPORTING
selection_id = ld_selection_id
field_ranges = lt_field_ranges
TABLES
tables_tab = lt_tables_ini
fields_tab = lt_fields_ini
tabfields_not_display = lt_tabfields_not_display
EXCEPTIONS
fields_incomplete = 1
fields_no_join = 2
field_not_found = 3
no_tables = 4
table_not_found = 5
expression_not_supported = 6
incorrect_expression = 7
illegal_kind = 8
area_not_found = 9
inconsistent_area = 10
kind_f_no_fields_left = 11
kind_f_no_fields = 12
too_many_fields = 13
dup_field = 14
field_no_type = 15
field_ill_type = 16
dup_event_field = 17
node_not_in_ldb = 18
area_no_field = 19
OTHERS = 20.
IF ( sy-subrc NE 0 ).
ELSE.
ENDIF.
* MOSTRAR PANTALLA DE SELECCIÓN LIBRE
CALL FUNCTION 'FREE_SELECTIONS_DIALOG'
EXPORTING
selection_id = ld_selection_id
title = sy-title
frame_text = 'Parametros de Seleccion'
status = '1'
as_window = abap_false
no_intervals = abap_true
IMPORTING
where_clauses = lt_where_cond
expressions = lt_expressions
field_ranges = lt_field_ranges
number_of_active_fields = ld_num_fields
TABLES
fields_tab = lt_fields_ini
EXCEPTIONS
internal_error = 1
no_action = 2
selid_not_found = 3
illegal_status = 4
OTHERS = 5.
IF ( sy-subrc EQ 0 ).
gd_continuar = co_verdadero.
ENDIF.
ELSE.
gd_continuar = co_falso.
ENDIF.
*
IF ( gd_continuar EQ co_verdadero ).
*
IF ( gd_continuar EQ co_verdadero ).
PERFORM get_main_data.
*
IF ( <fs_dynamic_tbi>[] IS INITIAL ).
MESSAGE i899(v1) WITH 'Sin Datos' DISPLAY LIKE 'I'.
gd_continuar = co_falso.
ENDIF.
ELSE.
gd_continuar = co_falso.
ENDIF.
ENDIF.
*
IF ( gd_continuar EQ co_falso ).
LEAVE TO SCREEN 0.
ENDIF.
*
*-----------------------------------------------------------------------------------------------------------------*
* END-OF-SELECTION
*-----------------------------------------------------------------------------------------------------------------*
END-OF-SELECTION.
PERFORM show_data_alv USING ld_tablename.
*-----------------------------------------------------------------------------------------------------------------*
* INCLUDES DE RUTINAS FORM
*-----------------------------------------------------------------------------------------------------------------*
*
*-------------------------------------------------------------------------------------------------------------------*
* FORM : GET_MAIN_DATA
*-------------------------------------------------------------------------------------------------------------------*
FORM get_main_data.
*
DATA : lt_field TYPE ddfields.
*
DATA : ld_existe_ori TYPE c LENGTH 001 VALUE co_falso.
DATA : ld_varaux TYPE string.
DATA : lt_where_aux TYPE STANDARD TABLE OF rsdswhere.
DATA : ls_where_aux LIKE LINE OF lt_where_aux.
DATA : ld_cantregi TYPE sy-tabix.
DATA : lt_campos TYPE STANDARD TABLE OF string.
DATA : ld_campo TYPE string.
DATA : type_descr TYPE REF TO cl_abap_typedescr.
DATA : struct_descr TYPE REF TO cl_abap_structdescr.
DATA : components TYPE cl_abap_structdescr=>component_table.
DATA : component LIKE LINE OF components.
DATA : error TYPE REF TO cx_root.
*
* DEFINICION DATOS LOCALES PARA TRATAMIENTO DINAMICO
DATA : lo_dref_locstr TYPE REF TO data.
DATA : lo_dref_loctbi TYPE REF TO data.
DATA : ld_tabname TYPE tabname.
*
ld_tabname = ld_tablename.
*
FIELD-SYMBOLS : <fs_dfies> TYPE dfies.
FIELD-SYMBOLS : <fs_where> TYPE rsdswhere.
*
* GENERACION DINAMICA DE LA ESTRUCTURA
CREATE DATA lo_dref_loctbi TYPE STANDARD TABLE OF (ld_tabname) WITH NON-UNIQUE DEFAULT KEY.
ASSIGN lo_dref_loctbi->* TO <fs_dynamic_tbi>.
*
CREATE DATA lo_dref_locstr LIKE LINE OF <fs_dynamic_tbi>.
ASSIGN lo_dref_locstr->* TO <fs_dynamic_str>.
*
IF ( sy-subrc EQ 0 ).
TRY.
*
CALL METHOD zcl_mm_blueyonder=>get_fields_structure
EXPORTING
iv_name = ld_tabname
IMPORTING
et_fields = lt_field[].
LOOP AT lt_field ASSIGNING <fs_dfies>.
IF ( <fs_dfies>-datatype EQ 'CLNT' ) OR ( <fs_dfies>-datatype EQ 'STRU' ).
ELSE.
CLEAR : ld_campo.
ld_campo = <fs_dfies>-fieldname.
APPEND ld_campo TO lt_campos.
ENDIF.
ENDLOOP.
IF NOT ( lt_campos[] IS INITIAL ).
IF ( LINES( lt_where_cond ) GT 0 ).
LOOP AT lt_where_cond INTO ls_where_clause WHERE tablename EQ ld_tablename.
APPEND LINES OF ls_where_clause-where_tab[] TO lt_where_aux[].
ENDLOOP.
SELECT *
INTO CORRESPONDING FIELDS OF TABLE <fs_dynamic_tbi>[]
FROM (ld_tablename)
WHERE (lt_where_aux) ORDER BY PRIMARY KEY.
ELSE.
SELECT *
INTO CORRESPONDING FIELDS OF TABLE <fs_dynamic_tbi>[]
FROM (ld_tablename)
WHERE (lt_where_aux) ORDER BY PRIMARY KEY.
ENDIF.
ENDIF.
CATCH cx_sy_move_cast_error INTO error.
MESSAGE error TYPE 'I' DISPLAY LIKE 'E'.
RETURN.
ENDTRY.
*
ENDIF.
*
ENDFORM. "
*-------------------------------------------------------------------------------------------------------------------*
* FORM : GET_DATA_PARAMETROS_DYNPRO
*-------------------------------------------------------------------------------------------------------------------*
FORM get_data_parametros_dynpro.
*
DATA : ld_tabname TYPE tabname.
DATA : lt_field TYPE ddfields.
*
DATA : ld_ddtext TYPE ddtext.
*
FIELD-SYMBOLS : <fs_dfies> TYPE dfies.
*
CLEAR : lt_expressions[], lt_field_ranges[], lt_tables_ini[], lt_fields_ini[],
lt_where_cond[], lt_where_tab[], lt_tabfields_not_display[].
*
sl_tables_ini-prim_tab = ld_tablename.
APPEND sl_tables_ini TO lt_tables_ini.
*
ld_tabname = ld_tablename.
CALL METHOD zcl_mm_blueyonder=>get_fields_structure
EXPORTING
iv_name = ld_tabname
IMPORTING
et_fields = lt_field[].
*
LOOP AT lt_field ASSIGNING <fs_dfies>.
IF ( <fs_dfies>-keyflag EQ abap_true ) AND ( <fs_dfies>-datatype NE 'CLNT' ).
CLEAR : sl_fields_ini.
sl_fields_ini-tablename = ld_tabname.
sl_fields_ini-fieldname = <fs_dfies>-fieldname.
sl_fields_ini-type = 'S'.
sl_fields_ini-where_leng = <fs_dfies>-leng.
APPEND sl_fields_ini TO lt_fields_ini.
ENDIF.
ENDLOOP.
*
IF ( gd_titulo_externo IS INITIAL ).
CLEAR : ld_ddtext.
SELECT SINGLE ddtext INTO ld_ddtext
FROM dd02t
WHERE tabname EQ ld_tabname
AND ddlanguage EQ sy-langu
AND as4local EQ 'A'.
IF ( sy-subrc EQ 0 ) AND ( ld_ddtext IS INITIAL ).
gd_titulo_externo = ld_ddtext.
ENDIF.
ENDIF.
*
ENDFORM. "
*-------------------------------------------------------------------------------------------------------------------*
* FORM : ACTUALIZA_TITULO
*-------------------------------------------------------------------------------------------------------------------*
FORM actualiza_titulo.
*
DATA : ld_titu_reporte TYPE string.
DATA : ld_nomb_reporte TYPE string.
DATA : ld_param_01 TYPE c LENGTH 030 .
DATA : ld_param_02 TYPE c LENGTH 030 .
DATA : ld_param_03 TYPE c LENGTH 030 .
DATA : ld_param_04 TYPE c LENGTH 030 .
*
DATA : ld_msg_aux_01 TYPE string.
DATA : ld_msg_aux_02 TYPE string.
DATA : ld_msg_aux_03 TYPE string.
*
DATA : ld_tabname TYPE tabname.
DATA : ld_ddtext TYPE ddtext.
SET TITLEBAR 'T_01'.
*
ld_titu_reporte = 'T_01'.
ld_nomb_reporte = 'ZALV_DINAMICO'.
*
ld_tabname = ld_tablename.
*
CLEAR : ld_ddtext.
SELECT SINGLE ddtext INTO ld_ddtext
FROM dd02t
WHERE tabname EQ ld_tabname
AND ddlanguage EQ sy-langu
AND as4local EQ 'A'.
*
ld_param_01 = 'Tabla :'.
CONCATENATE ld_tabname '-' '(' INTO ld_param_02 SEPARATED BY space.
CONCATENATE ld_ddtext ')' INTO ld_param_03.
SET TITLEBAR ld_titu_reporte OF PROGRAM ld_nomb_reporte WITH ld_param_01 ld_param_02 ld_param_03 ld_param_04.
*
ENDFORM. "
*-------------------------------------------------------------------------------------------------------------------*
* FORM : SHOW_DATA_ALV
*-------------------------------------------------------------------------------------------------------------------*
FORM show_data_alv USING p_ld_tablename.
*
CLEAR : ld_tabla.
ld_tabla = p_ld_tablename.
*
* CREAR TIPO LINEA Y TIPO TABLA CON : RTTC
ref_line_type ?= cl_abap_typedescr=>describe_by_name( ld_tabla ).
ref_tabletype = cl_abap_tabledescr=>create( p_line_type = ref_line_type ).
*
* OBJETO CREADO CON : RTTC.
CREATE DATA ref_itab TYPE HANDLE ref_tabletype.
CREATE DATA ref_wa TYPE HANDLE ref_line_type.
*
* ASIGNAR TIPO
ASSIGN ref_itab->* TO <fsym_itab>.
*
* LLAMAR ALV.
TRY.
CALL METHOD cl_salv_table=>factory
IMPORTING
r_salv_table = ref_out
CHANGING
t_table = <fs_dynamic_tbi>.
CATCH cx_salv_msg .
ENDTRY.
*
ref_functions = ref_out->get_functions( ).
ref_functions->set_all( if_salv_c_bool_sap=>true ).
*
CREATE OBJECT ref_header.
CREATE OBJECT ref_label.
*
ref_label = ref_header->create_label( row = 1 column = 1 ).
CONCATENATE 'Contenido :' ld_tabla INTO ref_tab SEPARATED BY space.
ref_label->set_text( ref_tab ).
ref_out->set_top_of_list( ref_header ).
ref_out->set_top_of_list_print( ref_header ).
ref_out->display( ).
*
* LIMPIAR FIELD SYMBOLS
UNASSIGN : <fsym_itab>.
FREE ref_out.
*
ENDFORM. "