Saturday, December 18, 2010
Function module REUSE_ALV_HIERSEQ_LIST_DISPLAY is used for hierarchical seq report in ALV. Set 'X' to DO_SUM for the quantity field when generating fieldcatalog which gives total and build IT_SORT Table to get Subtotal.

REPORT  zvenkat_alv_4_hierarchi_subtot.
"Types
TYPES:
    BEGIN OF t_pa0001,         "Header table type
      pernr TYPE pa0001-pernr,
      bukrs TYPE pa0001-bukrs,
      werks TYPE pa0001-werks,
      persg TYPE pa0001-persg,
      persk TYPE pa0001-persk,
      abkrs TYPE pa0001-abkrs,
    expand TYPE c,
    END OF t_pa0001,
    BEGIN OF t_pa0008,        "Item table
      pernr TYPE pa0008-pernr,
      lga01 TYPE pa0008-lga01,
      bet01 TYPE pa0008-bet01,
    END OF t_pa0008.
"Work areas
DATA:
    w_pa0008 TYPE t_pa0008,
    w_pa0001 TYPE t_pa0001.
"Internal tables
DATA:
    i_pa0008 TYPE STANDARD TABLE OF t_pa0008,
    i_pa0001 TYPE STANDARD TABLE OF t_pa0001.
*&---------------------------------------------------------------------*
" ALV Declarations
*----------------------------------------------------------------------*
" Types Pools
TYPE-POOLS:
    slis.
" Types
TYPES:
    t_fieldcat         TYPE slis_fieldcat_alv,
    t_events           TYPE slis_alv_event,
    t_layout           TYPE slis_layout_alv,
    t_sort             TYPE  slis_sortinfo_alv,
    t_keyinfo          TYPE slis_keyinfo_alv.
" Workareas
DATA:
    w_fieldcat         TYPE t_fieldcat,
    w_events           TYPE t_events,
    w_sort             TYPE t_sort,
    w_layout           TYPE t_layout,
    w_keyinfo          TYPE t_keyinfo.
" Internal Tables
DATA:
    i_fieldcat         TYPE STANDARD TABLE OF t_fieldcat,
    i_events           TYPE STANDARD TABLE OF t_events,
    i_sort             TYPE STANDARD TABLE OF t_sort.

START-OF-SELECTION.
  PERFORM get_data.

END-OF-SELECTION.

  PERFORM build_fieldcatalog.        "Fieldcatalog
  PERFORM build_events.              "Events table
  PERFORM build_sort_tab.            "To sort table and get subtotal
  PERFORM build_relation_structure.  "Making relationship between header and Item table
  PERFORM display_data.

*&---------------------------------------------------------------------*
*&      Form  get_data
*&---------------------------------------------------------------------*
FORM get_data.
  DATA:l_0008 TYPE pa0008 OCCURS 0 WITH HEADER LINE.

  SELECT *
  FROM pa0001
  INTO CORRESPONDING FIELDS OF TABLE i_pa0001
  UP TO 50 ROWS
  WHERE begda LE sy-datum
  AND endda   GE sy-datum.
  IF i_pa0001[] IS NOT INITIAL.
    SELECT pernr lga01 bet01
    FROM pa0008
    INTO CORRESPONDING FIELDS OF l_0008
    FOR ALL ENTRIES IN i_pa0001
    WHERE pernr = i_pa0001-pernr
    AND begda LE sy-datum
    AND endda GE sy-datum.

      w_pa0008-pernr = l_0008-pernr.
      w_pa0008-lga01 = l_0008-lga01.
      w_pa0008-bet01 = l_0008-bet01.
      APPEND w_pa0008 TO i_pa0008.
      CLEAR  w_pa0008.

      w_pa0008-pernr = l_0008-pernr.
      w_pa0008-lga01 = l_0008-lga02.
      w_pa0008-bet01 = l_0008-bet02.
      APPEND w_pa0008 TO i_pa0008.
      CLEAR  w_pa0008.

      w_pa0008-pernr = l_0008-pernr.
      w_pa0008-lga01 = l_0008-lga03.
      w_pa0008-bet01 = l_0008-bet03.
      APPEND w_pa0008 TO i_pa0008.
      CLEAR  w_pa0008.

      w_pa0008-pernr = l_0008-pernr.
      w_pa0008-lga01 = l_0008-lga04.
      w_pa0008-bet01 = l_0008-bet04.
      APPEND w_pa0008 TO i_pa0008.
      CLEAR  w_pa0008.

    ENDSELECT.
  ENDIF.




ENDFORM.                    "get_data
*&---------------------------------------------------------------------*
*&      Form  build_fieldcatalog
*&---------------------------------------------------------------------*
FORM build_fieldcatalog.
  PERFORM build_fcat USING:
  "Field    Int.Table    Text
  'PERNR' 'I_PA0001'   'PERNR',
  'BUKRS' 'I_PA0001'   'BUKRS',
  'WERKS' 'I_PA0001'   'WERKS',
  'PERSG' 'I_PA0001'   'PERSG',
  'PERSK' 'I_PA0001'   'PERSK',
  'ABKRS' 'I_PA0001'   'ABKRS',
  'PERNR' 'I_PA0008'   'PERNR', "Remove this if u dont want in the item table as well as it is there in the header table
  'LGA01' 'I_PA0008'   'LGA01',
  'BET01' 'I_PA0008'   'BET01'.

ENDFORM.                    "build_fieldcatalog
*&---------------------------------------------------------------------*
*&      Form  build_events
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM build_events.
  CLEAR:
  w_events, i_events[].
  w_events-name = 'TOP_OF_PAGE'.
  w_events-form = 'TOP_OF_PAGE'.
  APPEND w_events TO i_events.
  CLEAR  w_events.

ENDFORM.                    "build_events
*&---------------------------------------------------------------------*
*&      Form  build_relation_structure
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM build_relation_structure.

  w_keyinfo-header01 = 'PERNR'.
  w_keyinfo-item01   = 'PERNR'.

ENDFORM.                    "build_relation_structure
*&---------------------------------------------------------------------*
*&      Form  display_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM display_data.
  DATA:
  l_program TYPE sy-repid VALUE sy-repid.

  CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
    EXPORTING
      i_callback_program = l_program
      it_fieldcat        = i_fieldcat
      it_events          = i_events
      it_sort            = i_sort
      i_tabname_header   = 'I_PA0001'
      i_tabname_item     = 'I_PA0008'
      is_keyinfo         = w_keyinfo
    TABLES
      t_outtab_header    = i_pa0001
      t_outtab_item      = i_pa0008.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
ENDFORM.                    "display_data
*&---------------------------------------------------------------------*
*&      Form  build_fcat
*&---------------------------------------------------------------------*
FORM build_fcat  USING l_field l_tab l_text.

  w_fieldcat-fieldname = l_field.
  w_fieldcat-tabname   = l_tab.
  w_fieldcat-seltext_m = l_text.
  IF l_field = 'BET01'.
    w_fieldcat-do_sum = 'X'.
  ENDIF.
  APPEND w_fieldcat TO i_fieldcat.
  CLEAR  w_fieldcat.

ENDFORM.                    " build_fcat

*&---------------------------------------------------------------------*
*&      Form  top_of_page
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM top_of_page.

  DATA :
  i_header TYPE slis_t_listheader,
  w_header LIKE LINE OF i_header.

  DATA:l_date1 TYPE datum,
  l_date2 TYPE datum.

  w_header-typ = 'S'.
  w_header-info = sy-title.
  APPEND w_header TO i_header.
  CLEAR w_header.

  w_header-typ = 'H'.
  w_header-info = sy-repid.
  APPEND w_header TO i_header.
  CLEAR w_header.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = i_header
      i_logo             = 'ENJOYSAP_LOGO'.

ENDFORM.                    "top_of_page
*&---------------------------------------------------------------------*
*&      Form  BUILD_sort_tab
*&---------------------------------------------------------------------*
FORM build_sort_tab .

  CLEAR :i_sort[],w_sort.
  w_sort-spos      = 1.
  w_sort-fieldname = 'PERNR'.
  w_sort-tabname   = 'I_PA0001'. "header table
  w_sort-up        = 'X'.
  w_sort-subtot    = 'X'.
  APPEND w_sort TO i_sort.
  CLEAR w_sort.
ENDFORM.                    " BUILD_sort_tab

0 comments:

Post a Comment

Your useful comments, suggestions are appreciated.Your comments are moderated.

Followers

Contact Form

Name

Email *

Message *

Web Dynpro ABAP Book

An SAP Consultant

Follow US


Want to Contribute ?

If you are interested in writing about the new stuff you learn everyday while working, please write to the.sap.consultants@gmail.com.

Click on Contribution for more details.