Académique Documents
Professionnel Documents
Culture Documents
Step by step
Libretilla MarcosJ
FUNCIONES ALV.
*&---------------------------------------------------------------------*
*&
Form BUILD_LAYOUT
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM build_layout .
gs_layout-colwidth_optimize = 'X'. "Optimizar ancho del listado
gs_layout-zebra = 'X'.
"Mostrar lneas tipo cebra.
gs_layout-detail_popup = 'X'. "Mostrar opcin de informacin detalle
ENDFORM.
" BUILD_LAYOUT
*&---------------------------------------------------------------------*
*&
Form call_alv
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->ALV_EMPLOYEE text
*----------------------------------------------------------------------*
FORM call_alv TABLES alv_employee.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = z_pruebareportalv
is_layout
= gs_layout
it_fieldcat
= gt_fieldcat[]
TABLES
t_outtab
= i_aux
EXCEPTIONS
program_error
=1
OTHERS
= 2.
IF sy-subrc <> 0.
MESSAGE i799(cu) WITH 'Error creando el ALV'(008) 'Cdigo'(009) sy-subrc.
ENDIF.
ENDFORM.
"call_alv
*&---------------------------------------------------------------------*
*&
Form BUILD_HEADER
*&---------------------------------------------------------------------*
Libretilla MarcosJ
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM build_header .
* explain field description to alv
CLEAR gt_fieldcat.
REFRESH gt_fieldcat.
fieldcat_ln-fieldname = 'PERNR'.
fieldcat_ln-seltext_l = 'NAME'(003).
APPEND fieldcat_ln TO gt_fieldcat.
fieldcat_ln-fieldname = 'NAME'.
fieldcat_ln-seltext_l = 'Date'(002).
APPEND fieldcat_ln TO gt_fieldcat.
fieldcat_ln-fieldname = 'LASTNAME'.
fieldcat_ln-seltext_l ='Time'(005).
APPEND fieldcat_ln TO gt_fieldcat.
fieldcat_ln-fieldname = 'BIRTH_DATE'.
fieldcat_ln-seltext_l ='Correlative Number'(006).
APPEND fieldcat_ln TO gt_fieldcat.
fieldcat_ln-fieldname = 'STREET_&_HOUSEN'.
fieldcat_ln-seltext_l ='Correct Execution'(007).
APPEND fieldcat_ln TO gt_fieldcat.
fieldcat_ln-fieldname = 'CITY'.
fieldcat_ln-seltext_l ='COMMENTS'(008).
APPEND fieldcat_ln TO gt_fieldcat.
fieldcat_ln-fieldname = 'COUNTRY'.
fieldcat_ln-seltext_l ='COMMENTS'(008).
APPEND fieldcat_ln TO gt_fieldcat.
* data sorting and subtotal
DATA: gs_sort TYPE slis_sortinfo_alv.
CLEAR gs_sort.
gs_sort-fieldname = 'SAP_SD_SEQNR'.
gs_sort-spos
= 7.
gs_sort-up
= 'X'.
APPEND gs_sort TO gt_sort.
ENDFORM.
" BUILD_HEADER
Libretilla MarcosJ
CREACIN DE ESTRUCTURAS1
Abriremos la transaccin se11. Escogeremos la opcin DATA TYPE y le
pondremos nombre. Ahora haremos clic en create.
*Para crear una tabla en ABAP a partir de una estructura utilizaremos el siguente trozo
de cdigo:
DATA: tabla TYPE estructura.
Transaccin SE11.
Libretilla MarcosJ
En el WHERE ponemos esta condicin para que slo nos muestre los empleados activos.
DATUM es una variable del sistema que almacena la fecha actual.
4
Esta condicin hace que se recorra el subtipo 1 del infotipo 6. Para hacerlo menos complejo creo una
constante llamada subty1 con el valor 0001: CONSTANTS: c_subty1 LIKE pa0006-subty VALUE '1'.
3
Libretilla MarcosJ
Una vez hecho esto y para comprobar que existen valores utilizamos la variable del
sistema sy-subrc, que siempre que haya valores tendr 0 como valor, as una vez
recorramos la pa0000 escribiremos la condicin:
IF sy-subrc eq 0.
Tras esta lnea recorreremos nuestras otras dos tablas y pondremos el else como:
ELSE.
MESSAGE a000(z1) WITH text-001.
ENDIF.
Libretilla MarcosJ
z_pruebareportalv.
*infotypes:0001,0002,0006.
TYPE-POOLS: slis.
CONSTANTS: c_subty1 LIKE pa0006-subty VALUE '1'.
DATA: i_pa0000 LIKE pa0000 OCCURS 0 WITH HEADER LINE,
i_pa0002 LIKE pa0002 OCCURS 0 WITH HEADER LINE,
i_pa0006 LIKE pa0006 OCCURS 0 WITH HEADER LINE,
i_aux LIKE zprueba_estructura2 OCCURS 0 WITH HEADER LINE.
DATA gr_alvgrid TYPE REF TO cl_gui_alv_grid.
* Name of the custom control added on the screen
DATA gc_custom_control_name TYPE scrfname VALUE 'CC_ALV'.
* Custom container instance reference
DATA gr_ccontainer TYPE REF TO cl_gui_custom_container.
* Field catalog
DATA gt_fieldcat TYPE slis_t_fieldcat_alv.
* Layout structure
DATA gs_layout TYPE slis_layout_alv.
*
i_tab LIKE zprueba_estructura2 OCCURS 0 WITH HEADER LINE.
INITIALIZATION.
START-OF-SELECTION.
PERFORM retrieve_infotypes.
END-OF-SELECTION.
IF NOT i_pa0000[] IS INITIAL.
PERFORM lectura_inserccion.
ENDIF.
*
*
*
*
PERFORM mostrar_write.
PERFORM display_alv.
PERFORM display_alv_funciones tables gt_fieldcat
using
gs_layout.
PERFORM display_alv_funciones.
*&---------------------------------------------------------------------*
*&
Form RETRIEVE_INFOTYPES
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM retrieve_infotypes.
SELECT
FROM
INTO
WHERE
AND
*
pa0000
TABLE i_pa0000
begda <= sy-datum
endda >= sy-datum.
Libretilla MarcosJ
IF sy-subrc EQ 0.
SELECT *
FROM pa0002
INTO TABLE i_pa0002
FOR ALL ENTRIES IN i_pa0000
WHERE pernr EQ i_pa0000-pernr
AND begda <= sy-datum
AND endda >= sy-datum.
IF sy-subrc EQ 0.
SELECT *
FROM pa0006
INTO TABLE i_pa0006
FOR ALL ENTRIES IN i_pa0000
WHERE pernr EQ i_pa0000-pernr
AND subty EQ c_subty1
AND begda <= sy-datum
AND endda >= sy-datum.
ENDIF.
ELSE.
MESSAGE a000(z1) WITH text-001.
ENDIF.
ENDFORM.
" RETRIEVE_INFOTYPES
*&---------------------------------------------------------------------*
*&
Form LECTURA_INSERCCION
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM lectura_inserccion.
REFRESH i_aux.
LOOP AT i_pa0000.
CLEAR i_aux.
READ TABLE i_pa0000 WITH KEY pernr = i_pa0000-pernr.
IF sy-subrc EQ 0.
i_aux-pernr = i_pa0000-pernr.
ENDIF.
READ TABLE i_pa0002 WITH KEY pernr = i_pa0000-pernr.
IF sy-subrc EQ 0.
i_aux-name = i_pa0002-vorna.
i_aux-lastname = i_pa0002-nachn.
i_aux-birth_date = i_pa0002-gbdat.
ENDIF.
READ TABLE i_pa0006 WITH KEY pernr = i_pa0000-pernr.
IF sy-subrc EQ 0.
i_aux-street_&_housen = i_pa0006-stras.
i_aux-city = i_pa0006-ort01.
i_aux-country = i_pa0006-land1.
ENDIF.
APPEND i_aux.
ENDLOOP.
ENDFORM.
" LECTURA_INSERCCION
*&---------------------------------------------------------------------*
Libretilla MarcosJ
*&
Form DISPLAY_ALV
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM display_alv.
IF gr_alvgrid IS INITIAL.
* create custom container instance
CREATE OBJECT gr_ccontainer
EXPORTING
container_name = gc_custom_control_name.
*
EXCEPTIONS
*
CNTL_ERROR
= 1
*
CNTL_SYSTEM_ERROR = 2
*
CREATE_ERROR
= 3
*
LIFETIME_ERROR
= 4
*
LIFETIME_DYNPRO_DYNPRO_LINK = 5
*
others
= 6.
IF sy-subrc <> 0.
ENDIF.
CREATE OBJECT gr_alvgrid
EXPORTING
i_parent
= gr_ccontainer
EXCEPTIONS
error_cntl_create = 1
error_cntl_init
= 2
error_cntl_link
= 3
error_dp_create
= 4
OTHERS
= 5.
IF sy-subrc <> 0.
ENDIF.
* Preparing field catalog.
*
PERFORM prepare_field_catalog CHANGING gt_fieldcat.
* Preparing layout structure
*
PERFORM prepare_layout CHANGING gs_layout.
*
*
*
*
*
*
ENDFORM.
" DISPLAY_ALV
*&---------------------------------------------------------------------*
*&
Form PREPARE_FIELD_CATALOG
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
<--P_GT_FIELDCAT text
*----------------------------------------------------------------------*
FORM prepare_field_catalog CHANGING p_gt_fieldcat TYPE lvc_t_fcat.
Libretilla MarcosJ
*
*
*
*
*
*
*
*
*
*
*
IF sy-subrc <> 0.
ENDIF.
ENDFORM.
" PREPARE_FIELD_CATALOG
*&---------------------------------------------------------------------*
*&
Form PREPARE_LAYOUT
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
<--P_GS_LAYOUT text
*----------------------------------------------------------------------*
FORM prepare_layout CHANGING p_gs_layout TYPE lvc_s_layo.
p_gs_layout-zebra = 'X'.
p_gs_layout-grid_title = 'ALV report'.
p_gs_layout-smalltitle = 'X'.
ENDFORM.
" PREPARE_LAYOUT
*&---------------------------------------------------------------------*
*&
Form MOSTRAR_WRITE
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM mostrar_write .
LOOP AT i_aux.
WRITE: /
i_aux-pernr,
i_aux-name,
i_aux-lastname,
i_aux-birth_date,
10
Libretilla MarcosJ
i_aux-street_&_housen,
i_aux-city,
i_aux-country.
ENDLOOP.
ENDFORM.
" MOSTRAR_WRITE
*&---------------------------------------------------------------------*
*&
Form SHOW_ALV
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->P_P_GT_FIELDCAT text
*
-->P_P_GS_LAYOUT text
*----------------------------------------------------------------------*
FORM show_alv USING
p_gt_fieldcat
p_gs_layout.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
is_layout
= p_gs_layout
it_fieldcat
= p_gt_fieldcat
TABLES
t_outtab
= i_aux
EXCEPTIONS
program_error = 1
OTHERS
= 2.
ENDFORM.
" SHOW_ALV
*&---------------------------------------------------------------------*
*&
Form DISPLAY_ALV_FUNCIONES
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
<--P_GT_FIELDCAT text
*
<--P_GS_LAYOUT text
*----------------------------------------------------------------------*
form DISPLAY_ALV_FUNCIONES.
*
*
tables p_gt_fieldcat
using p_gs_layout.
PERFORM prepare_layout2
PERFORM prepare_layout2.
*
*
CHANGING p_gs_layout.
endform.
" DISPLAY_ALV_FUNCIONES
*&---------------------------------------------------------------------*
*&
Form PREPARE_FIELD_CATALOG2
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
<--P_P_GT_FIELDCAT text
*----------------------------------------------------------------------*
*form PREPARE_FIELD_CATALOG2 tables p_gt_fieldcat.
form PREPARE_FIELD_CATALOG2.
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
11
Libretilla MarcosJ
*
*
*
*
*
*
I_PROGRAM_NAME
I_INTERNAL_TABNAME
i_structure_name
I_CLIENT_NEVER_DISPLAY
I_INCLNAME
I_BYPASSING_BUFFER
I_BUFFER_ACTIVE
CHANGING
ct_fieldcat
EXCEPTIONS
inconsistent_interface
program_error
OTHERS
.
endform.
" PREPARE_FIELD_CATALOG2
*&---------------------------------------------------------------------*
*&
Form PREPARE_LAYOUT2
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
<--P_P_GS_LAYOUT text
*----------------------------------------------------------------------*
*form PREPARE_LAYOUT2 changing p_gs_layout.
form PREPARE_LAYOUT2.
*
*
gs_layout-zebra = 'X'.
gs_layout-grid_title = 'ALV report'.
gs_layout-smalltitle = 'X'.
endform.
" PREPARE_LAYOUT2
*&---------------------------------------------------------------------*
*&
Form SHOW_ALV2
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->P_P_GT_FIELDCAT text
*
-->P_P_GS_LAYOUT text
*----------------------------------------------------------------------*
form SHOW_ALV2.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
is_layout
= gs_layout
it_fieldcat
= gt_fieldcat
TABLES
t_outtab
= i_aux
EXCEPTIONS
program_error = 1
OTHERS
= 2.
endform.
" SHOW_ALV2
12
Libretilla MarcosJ
* from p0000
between pnpbegda and pnpendda.
*CODIGO*
endprovide.
*CODIGO*
ENDLOOP.
Siendo el cdigo:
i_aux-personal_number
= p0001-pernr.
i_aux-company_code = p0001-bukrs.
i_aux-personal_area = p0001-werks.
i_aux-employee_group = p0001-persg.
i_aux-employee_subgroup = p0001-persk.
i_aux-organizational_key = p0001-vdsk1.
i_aux-payroll_area = p0001-abkrs.
i_aux-first_name = p0002-vorna.
i_aux-last_name = p0002-nachn.
i_aux-street_&_housen = p0006-stras.
i_aux-city = p0006-ort01.
i_aux-country = p0006-land1.
i_aux-bank_key = p0009-bankl.
i_aux-bank_account = p0009-bankn.
i_aux-payroll_area = p0009-bkont.
APPEND i_aux.
13
Libretilla MarcosJ
TABLES:pernr.
INFOTYPES:0000,0001,0002,0006,0009.
DATA: i_aux LIKE zstructuretest OCCURS 0 WITH HEADER LINE.
DATA gr_alvgrid TYPE REF TO cl_gui_alv_grid.
* Name of the custom control added on the screen
DATA gc_custom_control_name TYPE scrfname VALUE 'CC_ALV'.
* Custom container instance reference
DATA gr_ccontainer TYPE REF TO cl_gui_custom_container.
* Field catalog
DATA gt_fieldcat TYPE slis_t_fieldcat_alv.
* Layout structure
DATA gs_layout TYPE slis_layout_alv.
INITIALIZATION.
START-OF-SELECTION.
GET pernr.
PERFORM lectura_inserccion.
END-OF-SELECTION.
PERFORM write.
PERFORM display_alv_funciones.
*&---------------------------------------------------------------------*
*&
Form LECTURA_INSERCCION
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM lectura_inserccion .
*podemos usar un loop o un provide para realizar la inserccion de los datos en
la
*nuestra tabla i_aux.
* Asi seria con un provide
*provide * from p0000
* between pnpbegda and pnpendda.
* aqui vendria nuestro codigo
* endprovide.
LOOP AT p0000 WHERE begda >= pnpbegda
AND endda <= pnpendda.
i_aux-personal_number = p0001-pernr.
i_aux-company_code = p0001-bukrs.
i_aux-personal_area = p0001-werks.
14
Libretilla MarcosJ
i_aux-employee_group = p0001-persg.
i_aux-employee_subgroup = p0001-persk.
i_aux-organizational_key = p0001-vdsk1.
i_aux-payroll_area = p0001-abkrs.
i_aux-first_name = p0002-vorna.
i_aux-last_name = p0002-nachn.
i_aux-street_&_housen = p0006-stras.
i_aux-city = p0006-ort01.
i_aux-country = p0006-land1.
i_aux-bank_key = p0009-bankl.
i_aux-bank_account = p0009-bankn.
i_aux-payroll_area = p0009-bkont.
APPEND i_aux.
ENDLOOP.
ENDFORM.
" LECTURA_INSERCCION
*&---------------------------------------------------------------------*
*&
Form WRITE
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM write .
DELETE ADJACENT DUPLICATES FROM I_AUX.
LOOP AT i_aux.
WRITE: /
i_aux-personal_number,
i_aux-company_code,
i_aux-personal_area,
i_aux-employee_group,
i_aux-employee_subgroup,
i_aux-organizational_key,
i_aux-payroll_area,
i_aux-first_name,
i_aux-last_name,
i_aux-street_&_housen,
i_aux-city,
i_aux-country,
i_aux-bank_key,
i_aux-bank_account,
i_aux-payroll_area.
ENDLOOP.
ENDFORM.
" WRITE
*&---------------------------------------------------------------------*
*&
Form DISPLAY_ALV_FUNCIONES
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
<--P_GT_FIELDCAT text
*
<--P_GS_LAYOUT text
*----------------------------------------------------------------------*
form DISPLAY_ALV_FUNCIONES.
*
*
tables p_gt_fieldcat
using p_gs_layout.
15
Libretilla MarcosJ
PERFORM prepare_layout2
PERFORM prepare_layout.
*
*
CHANGING p_gs_layout.
endform.
" DISPLAY_ALV_FUNCIONES
*&---------------------------------------------------------------------*
*&
Form PREPARE_FIELD_CATALOG
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
<--P_P_GT_FIELDCAT text
*----------------------------------------------------------------------*
*form PREPARE_FIELD_CATALOG tables p_gt_fieldcat.
form PREPARE_FIELD_CATALOG.
*
*
*
*
*
*
endform.
" PREPARE_FIELD_CATALOG
*&---------------------------------------------------------------------*
*&
Form PREPARE_LAYOUT
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
<--P_P_GS_LAYOUT text
*----------------------------------------------------------------------*
*form PREPARE_LAYOUT changing p_gs_layout.
form PREPARE_LAYOUT.
*
*
gs_layout-zebra = 'X'.
gs_layout-grid_title = 'ALV report'.
gs_layout-smalltitle = 'X'.
endform.
" PREPARE_LAYOUT
*&---------------------------------------------------------------------*
*&
Form SHOW_ALV
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->P_P_GT_FIELDCAT text
*
-->P_P_GS_LAYOUT text
*----------------------------------------------------------------------*
form SHOW_ALV.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
16
Libretilla MarcosJ
is_layout
it_fieldcat
TABLES
t_outtab
EXCEPTIONS
program_error
OTHERS
endform.
17