REPORT z_alv_switch_list_v2.
*---------------------------------------------------------------------*
* Button to switch alv list *
*---------------------------------------------------------------------*
* Author : Michel PIOUD *
* Email : mpioud@yahoo.fr HomePage : http://www.geocities.com/mpioud *
*---------------------------------------------------------------------*
TYPE-POOLS: slis. " ALV Global types
*---------------------------------------------------------------------*
CONSTANTS:
c_x VALUE 'X',
c_eb9 TYPE syucomm VALUE '&EB9'.
*---------------------------------------------------------------------*
TYPES :
BEGIN OF ty_vbak,
vkorg TYPE vbak-vkorg, " Sales organization
kunnr TYPE vbak-kunnr, " Sold-to party
vbeln TYPE vbak-vbeln, " Sales document
netwr TYPE vbak-netwr, " Net Value of the Sales Order
waerk TYPE vbak-waerk, " Document currency
END OF ty_vbak,
BEGIN OF ty_vbap,
vkorg TYPE vbak-vkorg, " Sales organization
kunnr TYPE vbak-kunnr, " Sold-to party
vbeln TYPE vbak-vbeln, " Sales document
posnr TYPE vbap-posnr, " Sales document item
matnr TYPE vbap-matnr, " Material number
arktx TYPE vbap-arktx, " Short text for sales order item
kwmeng TYPE vbap-kwmeng, " Order Quantity
netwr TYPE vbap-netwr, " Net value of the order item
waerk TYPE vbap-waerk, " SD document currency
END OF ty_vbap.
*---------------------------------------------------------------------*
DATA:
g_flag TYPE i VALUE 1,
gs_print_k TYPE slis_print_alv,
gs_print_p TYPE slis_print_alv,
gs_layout_k TYPE slis_layout_alv,
gs_layout_p TYPE slis_layout_alv,
gt_sort_k TYPE slis_t_sortinfo_alv,
gt_sort_p TYPE slis_t_sortinfo_alv,
gt_filter_k TYPE slis_t_filter_alv,
gt_filter_p TYPE slis_t_filter_alv,
gt_fieldcat_k TYPE slis_t_fieldcat_alv,
gt_fieldcat_p TYPE slis_t_fieldcat_alv.
DATA:
vbak TYPE vbak,
gt_vbak TYPE TABLE OF ty_vbak,
gt_vbap TYPE TABLE OF ty_vbap.
*---------------------------------------------------------------------*
SELECT-OPTIONS :
s_vkorg FOR vbak-vkorg, " Sales organization
s_kunnr FOR vbak-kunnr, " Sold-to party
s_vbeln FOR vbak-vbeln. " Sales document
SELECTION-SCREEN :
SKIP, BEGIN OF LINE,COMMENT 5(27) v_1 FOR FIELD p_max. "#EC NEEDED
PARAMETERS p_max(2) TYPE n DEFAULT '20' OBLIGATORY.
SELECTION-SCREEN END OF LINE.
*---------------------------------------------------------------------*
INITIALIZATION.
v_1 = 'Maximum of records to read'.
*---------------------------------------------------------------------*
START-OF-SELECTION.
PERFORM f_read_data.
PERFORM f_display_data.
*---------------------------------------------------------------------*
* Form f_read_data
*---------------------------------------------------------------------*
FORM f_read_data.
* Read Sales Document: Header Data
SELECT vkorg kunnr vbeln netwr waerk
UP TO p_max ROWS
INTO TABLE gt_vbak
FROM vbak
WHERE kunnr IN s_kunnr
AND vbeln IN s_vbeln
AND vkorg IN s_vkorg.
CHECK NOT gt_vbak[] IS INITIAL.
* Read Sales Document: Header Data and items
SELECT vkorg kunnr k~vbeln posnr matnr arktx kwmeng p~netwr p~waerk
INTO CORRESPONDING FIELDS OF TABLE gt_vbap
FROM vbak AS k
INNER JOIN vbap AS p
ON k~vbeln = p~vbeln
FOR ALL ENTRIES IN gt_vbak
WHERE k~vbeln = gt_vbak-vbeln.
ENDFORM. " F_READ_DATA
*---------------------------------------------------------------------*
* Form f_display_data
*---------------------------------------------------------------------*
FORM f_display_data.
DEFINE m_fieldcat_k.
add 1 to ls_fieldcat-col_pos.
ls_fieldcat-fieldname = &1.
ls_fieldcat-ref_tabname = &2.
ls_fieldcat-do_sum = &3.
ls_fieldcat-cfieldname = &4.
append ls_fieldcat to gt_fieldcat_k.
END-OF-DEFINITION.
DEFINE m_fieldcat_p.
add 1 to ls_fieldcat-col_pos.
ls_fieldcat-fieldname = &1.
ls_fieldcat-ref_tabname = &2.
ls_fieldcat-do_sum = &3.
ls_fieldcat-cfieldname = &4.
append ls_fieldcat to gt_fieldcat_p.
END-OF-DEFINITION.
DATA:
l_exit,
ls_fieldcat TYPE slis_fieldcat_alv,
ls_event_exit TYPE slis_event_exit,
lt_event_exit TYPE slis_t_event_exit.
m_fieldcat_k 'VKORG' 'VBAK' '' ''.
m_fieldcat_k 'KUNNR' 'VBAK' '' ''.
m_fieldcat_k 'VBELN' 'VBAK' '' ''.
m_fieldcat_k 'NETWR' 'VBAK' c_x 'WAERK'.
m_fieldcat_k 'WAERK' 'VBAK' '' ''.
m_fieldcat_p 'VKORG' 'VBAK' '' ''.
m_fieldcat_p 'KUNNR' 'VBAK' '' ''.
m_fieldcat_p 'VBELN' 'VBAK' '' ''.
m_fieldcat_p 'MATNR' 'VBAP' '' ''.
m_fieldcat_p 'ARKTX' 'VBAP' '' '' .
m_fieldcat_p 'KWMENG' 'VBAP' '' ''.
m_fieldcat_p 'NETWR' 'VBAP' c_x 'WAERK'.
m_fieldcat_p 'WAERK' 'VBAP' c_x ''.
gs_layout_k-zebra = c_x.
gs_layout_k-cell_merge = c_x.
gs_layout_k-colwidth_optimize = c_x.
gs_layout_k-group_change_edit = c_x.
gs_layout_p = gs_layout_k.
* Activate switch button
CLEAR ls_event_exit.
ls_event_exit-ucomm = c_eb9. " Button switch
ls_event_exit-after = c_x.
APPEND ls_event_exit TO lt_event_exit.
gs_print_k-no_print_selinfos = c_x. " Display no selection infos
gs_print_k-no_print_listinfos = c_x. " Display no listinfos
gs_print_p = gs_print_k.
l_exit = c_x.
WHILE l_exit = c_x.
IF g_flag = 1.
* Display Header Data
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-cprog
i_callback_user_command = 'USER_COMMAND'
i_callback_pf_status_set = 'PF_STATUS_SET'
is_layout = gs_layout_k
is_print = gs_print_k
it_fieldcat = gt_fieldcat_k
it_sort = gt_sort_k
it_filter = gt_filter_k
it_event_exit = lt_event_exit
i_save = 'A'
IMPORTING
e_exit_caused_by_caller = l_exit
TABLES
t_outtab = gt_vbak.
ELSE.
* Display Item Data
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-cprog
i_callback_user_command = 'USER_COMMAND'
i_callback_pf_status_set = 'PF_STATUS_SET'
is_layout = gs_layout_p
is_print = gs_print_p
it_fieldcat = gt_fieldcat_p
it_sort = gt_sort_p
it_filter = gt_filter_p
it_event_exit = lt_event_exit
i_save = 'A'
IMPORTING
e_exit_caused_by_caller = l_exit
TABLES
t_outtab = gt_vbap.
ENDIF.
g_flag = g_flag * -1.
ENDWHILE.
ENDFORM. " F_DISPLAY_DATA
*---------------------------------------------------------------------*
* FORM USER_COMMAND *
*---------------------------------------------------------------------*
FORM user_command USING u_ucomm TYPE syucomm
us_selfield TYPE slis_selfield. "#EC CALLED
CASE u_ucomm.
WHEN c_eb9.
us_selfield-exit = c_x.
IF g_flag = 1.
* Read and Save current ALV list information
CALL FUNCTION 'REUSE_ALV_GRID_LAYOUT_INFO_GET'
IMPORTING
es_layout = gs_layout_k
et_fieldcat = gt_fieldcat_k
et_sort = gt_sort_k
et_filter = gt_filter_k
es_print = gs_print_k
EXCEPTIONS
no_infos = 1
program_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.
ELSE.
* Read and Save current ALV list information
CALL FUNCTION 'REUSE_ALV_GRID_LAYOUT_INFO_GET'
IMPORTING
es_layout = gs_layout_p
et_fieldcat = gt_fieldcat_p
et_sort = gt_sort_p
et_filter = gt_filter_p
es_print = gs_print_p
EXCEPTIONS
no_infos = 1
program_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.
ENDIF.
ENDCASE.
ENDFORM. " USER_COMMAND
*---------------------------------------------------------------------*
* FORM PF_STATUS_SET *
*---------------------------------------------------------------------*
FORM pf_status_set USING ut_extab TYPE slis_t_extab. "#EC CALLED
* Display switch button
DELETE ut_extab WHERE fcode = c_eb9.
SET PF-STATUS 'STANDARD_FULLSCREEN' OF PROGRAM 'SAPLKKBL'
EXCLUDING ut_extab.
ENDFORM. " PF_STATUS_SET
**************** END OF PROGRAM Z_ALV_SWITCH_LIST_V2 *******************
^-^