Wednesday, August 29, 2018

SIMPLE ALV HIERARCHICAL REPORT

ALV Hierarchical Report

We can prepare a report which can display hierarchical data. There will be a header record and hierarchically there will be item wise record. This can be done by a function module REUSE_ALV_HIERSEQ_LIST_DISPLAY.


 REPORT ZALV_HIERACHY.
TABLES: ekko.


TYPE-POOLS: slis.

*------Declaration of internal table structures------------------------*
TYPES: BEGIN OF ty_ekko,
        ebeln TYPE ekko-ebeln,
        bukrs TYPE ekko-bukrs,
        aedat TYPE ekko-aedat,
        ernam TYPE ekko-ernam,
        lifnr TYPE ekko-lifnr,
        bedat TYPE ekko-bedat,
        expand,               "Expand operation on header list
       END OF ty_ekko,

       BEGIN OF ty_ekpo,
        ebeln TYPE ekpo-ebeln,
        ebelp TYPE ekpo-ebelp,
        aedat TYPE ekpo-aedat,
        matkl TYPE ekpo-matkl,
        menge TYPE ekpo-menge,
        meins TYPE ekpo-meins,
        netpr TYPE ekpo-netpr,
        peinh TYPE ekpo-peinh,
       END OF ty_ekpo.

DATA:

      it_ekko TYPE TABLE OF ty_ekko,
      it_ekpo TYPE TABLE OF ty_ekpo,

      wa_fcat TYPE slis_fieldcat_alv,
      it_fcat TYPE slis_t_fieldcat_alv,

        wa_layout TYPE slis_layout_alv,

      "Key information for hierarchical list
      wa_key TYPE slis_keyinfo_alv.


  SELECT-OPTIONS: s_ebeln FOR ekko-ebeln.

*-----Event Start of Selection-----------------------------------------*
START-OF-SELECTION.
  PERFORM get_ekko.
  PERFORM get_ekpo.
  PERFORM field_catalog.
  PERFORM alv_layout.
  PERFORM key_info.
  PERFORM alv_hierseq_list_display.

*&---------------------------------------------------------------------*
*&      Form  get_ekko
*&---------------------------------------------------------------------*
*       Get data from PO header
*----------------------------------------------------------------------*
FORM get_ekko .

  IF s_ebeln[] IS NOT INITIAL.
    SELECT ebeln bukrs aedat ernam lifnr bedat
      FROM ekko INTO TABLE it_ekko
      WHERE ebeln IN s_ebeln.

    IF sy-subrc = 0.
      SORT it_ekko.
    ELSE.
      MESSAGE 'PO doesn''t exist' TYPE 'I'.
    ENDIF.

  ELSE.
    MESSAGE 'Enter a Valid PO' TYPE 'I'.
  ENDIF.

ENDFORM.                    " get_ekko
*&---------------------------------------------------------------------*
*&      Form  get_ekpo
*&---------------------------------------------------------------------*
*       Get data from PO item
*----------------------------------------------------------------------*
FORM get_ekpo .

  IF it_ekko IS NOT INITIAL.
    SELECT ebeln ebelp aedat matkl menge meins netpr peinh
      FROM ekpo INTO TABLE it_ekpo
      FOR ALL ENTRIES IN it_ekko
      WHERE ebeln = it_ekko-ebeln.

    IF sy-subrc = 0.
      SORT it_ekpo.
    ENDIF.
  ENDIF.

ENDFORM.                    " get_ekpo
**-------------------------------------------------
FORM field_catalog.

  DATA lv_col TYPE i VALUE 0.

  IF it_ekko IS NOT INITIAL.

    lv_col            = lv_col + 1.
    wa_fcat-col_pos   = lv_col.
    wa_fcat-fieldname = 'EBELN'.
    wa_fcat-tabname   = 'IT_EKKO'.
    wa_fcat-seltext_l = 'Purchase Order'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    lv_col            = lv_col + 1.
    wa_fcat-col_pos   = lv_col.
    wa_fcat-fieldname = 'BUKRS'.
    wa_fcat-tabname   = 'IT_EKKO'.
    wa_fcat-seltext_l = 'Company Code'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    lv_col            = lv_col + 1.
    wa_fcat-col_pos   = lv_col.
    wa_fcat-fieldname = 'AEDAT'.
    wa_fcat-tabname   = 'IT_EKKO'.
    wa_fcat-seltext_l = 'Creation Date'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    lv_col            = lv_col + 1.
    wa_fcat-col_pos   = lv_col.
    wa_fcat-fieldname = 'ERNAM'.
    wa_fcat-tabname   = 'IT_EKKO'.
    wa_fcat-seltext_l = 'Created By'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    lv_col            = lv_col + 1.
    wa_fcat-col_pos   = lv_col.
    wa_fcat-fieldname = 'LIFNR'.
    wa_fcat-tabname   = 'IT_EKKO'.
    wa_fcat-seltext_l = 'Vendor'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    lv_col            = lv_col + 1.
    wa_fcat-col_pos   = lv_col.
    wa_fcat-fieldname = 'BEDAT'.
    wa_fcat-tabname   = 'IT_EKKO'.
    wa_fcat-seltext_l = 'Purchasing Document Date'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.
  ENDIF.

  IF it_ekpo IS NOT INITIAL.

    lv_col            = lv_col + 1.
    wa_fcat-col_pos   = lv_col.
    wa_fcat-fieldname = 'EBELN'.
    wa_fcat-tabname   = 'IT_EKPO'.
    wa_fcat-seltext_l = 'Purchase Order'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    lv_col            = lv_col + 1.
    wa_fcat-col_pos   = lv_col.
    wa_fcat-fieldname = 'EBELP'.
    wa_fcat-tabname   = 'IT_EKPO'.
    wa_fcat-seltext_l = 'Item'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    lv_col            = lv_col + 1.
    wa_fcat-col_pos   = lv_col.
    wa_fcat-fieldname = 'MATKL'.
    wa_fcat-tabname   = 'IT_EKPO'.
    wa_fcat-seltext_l = 'Material Group'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    lv_col            = lv_col + 1.
    wa_fcat-col_pos   = lv_col.
    wa_fcat-fieldname = 'AEDAT'.
    wa_fcat-tabname   = 'IT_EKPO'.
    wa_fcat-seltext_l = 'Creation Date'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    lv_col            = lv_col + 1.
    wa_fcat-col_pos   = lv_col.
    wa_fcat-fieldname = 'MENGE'.
    wa_fcat-tabname   = 'IT_EKPO'.
    wa_fcat-seltext_l = 'PO Quantity'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    lv_col            = lv_col + 1.
    wa_fcat-col_pos   = lv_col.
    wa_fcat-fieldname = 'MEINS'.
    wa_fcat-tabname   = 'IT_EKPO'.
    wa_fcat-seltext_l = 'Unit'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    lv_col            = lv_col + 1.
    wa_fcat-col_pos   = lv_col.
    wa_fcat-fieldname = 'NETPR'.
    wa_fcat-tabname   = 'IT_EKPO'.
    wa_fcat-seltext_l = 'Net Price'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    lv_col            = lv_col + 1.
    wa_fcat-col_pos   = lv_col.
    wa_fcat-fieldname = 'PEINH'.
    wa_fcat-tabname   = 'IT_EKPO'.
    wa_fcat-seltext_l = 'Price Unit'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.
  ENDIF.

ENDFORM.                    "field_catalog
*&---------------------------------------------------------------------*
*&      Form  alv_layout
*&---------------------------------------------------------------------*
*       ALV layout
*----------------------------------------------------------------------*
FORM alv_layout .

  wa_layout-zebra             = 'X'.
  wa_layout-colwidth_optimize = 'X'.
  wa_layout-expand_fieldname  = 'EXPAND'.
  wa_layout-window_titlebar   = 'Hierarchical PO display'.

ENDFORM.                    " alv_layout

*&      Form  key_info

*       Key information which is passed to ALV
*----------------------------------------------------------------------*
FORM key_info.

  wa_key-header01 = 'EBELN'. "Purchase Order number
  wa_key-item01   = 'EBELN'. "is the key for header & item table

ENDFORM.                    "key_info
*&---------------------------------------------------------------------*
*&      Form  alv_hierseq_list_display
*&---------------------------------------------------------------------*
*       Calling the ALV Hierseq List Display
*----------------------------------------------------------------------*
FORM alv_hierseq_list_display .

  IF it_fcat IS NOT INITIAL.

    CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
      EXPORTING
        i_callback_program = sy-repid
        is_layout          = wa_layout
        it_fieldcat        = it_fcat
        i_tabname_header   = 'IT_EKKO'
        i_tabname_item     = 'IT_EKPO'
        is_keyinfo         = wa_key
      TABLES
        t_outtab_header    = it_ekko
        t_outtab_item      = it_ekpo
      EXCEPTIONS
        program_error      = 1
        OTHERS             = 2.

    IF sy-subrc <> 0.
      MESSAGE 'Internal Error' TYPE 'I'.
    ENDIF.
  ENDIF.

ENDFORM.



 then the output will be visible as :