Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

ALV

Former Member
0 Likes
375

Hi,

i'm diplay a report using ALV which includes a header(top-of-list), a footer(end of list) and the body of the report(first internal table).

now i need to include another report(from second internal table ) with the same format after the first report. how can i do that.

your help would be appreciated.

Thanks,

kranthi

1 ACCEPTED SOLUTION
Read only

former_member221770
Contributor
0 Likes
358

Hi Kranthi,

As Rich said you can do this using the ALV Block FM's. Ihave attached a sample program below. I use both the sequentional and hierarchy type ALV lists here. This should get you on the right track.

Hope this helps.

Cheers,

Pat.


* This program is an example of how the ALV Block Display works.
* It will display 3 ALV Report "Blocks" (Customer Details, Vendor
* Details and Sales Orders) on the one Screen

REPORT  zpat_alv_block.
************************************************************************
* Types
************************************************************************
TYPE-POOLS: kkblo.

************************************************************************
* Database Tables
************************************************************************
TABLES: kna1,  "General Customer Master
        lfa1,  "General Vendor Master
        vbak,  "Sales Order Header
        vbap.  "Sales Order Item

************************************************************************
* Structures
************************************************************************
DATA: st_fieldcat     TYPE slis_fieldcat_alv. "Fieldcatalog Structure
DATA: st_kna1_layout  TYPE slis_layout_alv.   "Customer Layout Structure
DATA: st_lfa1_layout  TYPE slis_layout_alv.   "Vendor Layout Structure
DATA: st_vbap_layout  TYPE slis_layout_alv.   "Sales Order Layout St.
DATA: st_events       TYPE slis_alv_event.    "Event Structure
DATA: st_keyinfo      TYPE slis_keyinfo_alv.  "Link Between Hdr & Item

************************************************************************
* Internal tables
************************************************************************

* Fieldcatalog for Customers
DATA: tbl_kna1_fieldcat TYPE slis_t_fieldcat_alv.

* Fieldcatalog for Vendors
DATA: tbl_lfa1_fieldcat TYPE slis_t_fieldcat_alv.

* Fieldcatalog for Sales Orders
DATA: tbl_vbap_fieldcat TYPE slis_t_fieldcat_alv.

* Internal Table for Event Table 1 (Customer)
DATA: tbl_kna1_events TYPE slis_t_event.

* Internal Table for Event Table 2 (Vendor)
DATA: tbl_lfa1_events TYPE slis_t_event.

* Internal Table for Event Table 3 (Sales Order)
DATA: tbl_vbap_events TYPE slis_t_event.

* Internal Table to hold Customer Data
DATA: BEGIN OF tbl_kna1 OCCURS 0.
        INCLUDE STRUCTURE kna1.
DATA: END OF tbl_kna1.

* Internal Table to hold Vendor Data
DATA: BEGIN OF tbl_lfa1 OCCURS 0.
        INCLUDE STRUCTURE lfa1.
DATA: END OF tbl_lfa1.

* Internal Table to hold Sales Order Header
DATA: BEGIN OF tbl_vbak OCCURS 0.
        INCLUDE STRUCTURE vbak.
DATA: END OF tbl_vbak.

* Internal Table to hold Sales Order Item
DATA: BEGIN OF tbl_vbap OCCURS 0.
        INCLUDE STRUCTURE vbap.
DATA: END OF tbl_vbap.

************************************************************************
* Constants
************************************************************************
CONSTANTS: c_y   VALUE 'X'. "Yes
CONSTANTS: c_n   VALUE ' '. "No

************************************************************************
* Simple Variables
************************************************************************
DATA: field_name(30) TYPE c,         "Selected Field
      g_repid        LIKE sy-repid,  "Report Name
      g_pos          TYPE i.         "Position (Field Catalog)

************************************************************************
* Start of Selection
************************************************************************
START-OF-SELECTION.

* Get the Report Name
  g_repid = sy-repid.

* Get the data
  PERFORM get_data.

************************************************************************
* End of Selection
************************************************************************
END-OF-SELECTION.

* Get the Field Catalog
  PERFORM create_fieldcat.

* Get the Layouts
  PERFORM create_layout.

* Get the User Interaction Events
  PERFORM get_events.

* Get the Key Info for Hierarchy Display (Sales Orders)
  PERFORM get_keyinfo.

* Display the Report
  PERFORM create_report.


************************************************************************
* Subroutines
************************************************************************

*&---------------------------------------------------------------------*
*&      Form  create_report
*&---------------------------------------------------------------------*
*       Create the Reports
*----------------------------------------------------------------------*
FORM create_report.

  CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_INIT'
       EXPORTING
            i_callback_program       = g_repid
*            i_callback_pf_status_set = 'SET_STATUS'
            i_callback_user_command  = 'PROCESS_USER_COMMANDS'.

  IF NOT tbl_kna1[] IS INITIAL.
* Display the Customer ALV Block
    CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND'
         EXPORTING
              is_layout   = st_kna1_layout
              it_fieldcat = tbl_kna1_fieldcat
              i_tabname   = 'TBL_KNA1'
              it_events   = tbl_kna1_events
              i_text      = 'Customers'
         TABLES
              t_outtab    = tbl_kna1.
  ENDIF.

  IF NOT tbl_lfa1[] IS INITIAL.
* Display the Vendor ALV Block
    CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND'
         EXPORTING
              is_layout   = st_lfa1_layout
              it_fieldcat = tbl_lfa1_fieldcat
              i_tabname   = 'TBL_LFA1'
              it_events   = tbl_lfa1_events
              i_text      = 'Vendors'
         TABLES
              t_outtab    = tbl_lfa1.
  ENDIF.

  IF NOT tbl_vbap[] IS INITIAL.
* Display the Sales Order ALV Block
    CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_HS_APPEND'
         EXPORTING
              is_layout        = st_vbap_layout
              it_fieldcat      = tbl_vbap_fieldcat
              is_keyinfo       = st_keyinfo
              i_header_tabname = 'TBL_VBAK'
              i_item_tabname   = 'TBL_VBAP'
              it_events        = tbl_vbap_events
              i_text           = 'Sales Orders'
         TABLES
              t_outtab_header  = tbl_vbak
              t_outtab_item    = tbl_vbap.

  ENDIF.
* Display the Defined Blocks
  CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_DISPLAY'.

ENDFORM.                    " create_report
*&---------------------------------------------------------------------*
*&      Form  get_data
*&---------------------------------------------------------------------*
*       The subroutine name says "Get Data" so I wonder what the routine
*       does....
*----------------------------------------------------------------------*
FORM get_data.

* Get the Customer Data
  SELECT *
         INTO CORRESPONDING FIELDS OF TABLE tbl_kna1
         FROM kna1
         UP TO 15 ROWS.

* Get the Vendor Data
  SELECT *
         INTO CORRESPONDING FIELDS OF TABLE tbl_lfa1
         FROM lfa1
         UP TO 15 ROWS.

* Get the Sales Order Header Data
  SELECT *
         INTO CORRESPONDING FIELDS OF TABLE tbl_vbak
         FROM vbak
         UP TO 10 ROWS.

  CHECK NOT tbl_vbak[] IS INITIAL.
* Get the Sales Order Item Data
  SELECT *
         INTO CORRESPONDING FIELDS OF TABLE tbl_vbap
         FROM vbap
         FOR ALL ENTRIES IN tbl_vbak
         WHERE vbeln = tbl_vbak-vbeln.

ENDFORM.                    " get_data
*&---------------------------------------------------------------------*
*&      Form  create_fieldcat
*&---------------------------------------------------------------------*
*       Create te Field Catalog...duh!
*----------------------------------------------------------------------*
FORM create_fieldcat.

  IF NOT tbl_kna1[] IS INITIAL.
* Only populate the Customer Field Catalog if TBL_KNA1 has data
    PERFORM get_kna1_fieldcat.
  ENDIF.

  IF NOT tbl_lfa1[] IS INITIAL.
* Only populate the Vendor Field Catalog if TBL_LFA1 has data
    PERFORM get_lfa1_fieldcat.
  ENDIF.

  IF NOT tbl_vbap[] IS INITIAL.
* Only populate the Sales Order Field Catalog if TBL_VBAP has data
    PERFORM get_vbap_fieldcat.
  ENDIF.

ENDFORM.                    " create_fieldcat
*&---------------------------------------------------------------------*
*&      Form  get_kna1_fieldcat
*&---------------------------------------------------------------------*
*       Populate Customer Field Catalog
*----------------------------------------------------------------------*
FORM get_kna1_fieldcat.

  CLEAR g_pos.
  PERFORM write_kna1_fieldcat USING 'KUNNR' 'TBL_KNA1' 'KNA1' c_y
                                    c_n c_y c_n c_n.

  PERFORM write_kna1_fieldcat USING 'NAME1' 'TBL_KNA1' 'KNA1' c_n
                                    c_n c_n c_n c_n.

  PERFORM write_kna1_fieldcat USING 'STRAS' 'TBL_KNA1' 'KNA1' c_n
                                    c_n c_n c_n c_n.

ENDFORM.                    " get_kna1_fieldcat
*&---------------------------------------------------------------------*
*&      Form  write_kna1_fieldcat
*&---------------------------------------------------------------------*
*       Append the Customer Field Catalog
*----------------------------------------------------------------------*
*      -->name   Field name
*      -->tab    Table name
*      -->st     Structure Name
*      -->key    Is this field a Key?
*      -->emp    Emphasize
*      -->hot    Hotspot
*      -->sum    Do_sum
*      -->hide   No_out
*----------------------------------------------------------------------*
FORM write_kna1_fieldcat USING name
                               tab
                               st
                               key
                               emp
                               hot
                               sum
                               hide.

  g_pos = g_pos + 1.

  st_fieldcat-fieldname   = name.
  st_fieldcat-tabname     = tab.
  st_fieldcat-ref_tabname = st.
  st_fieldcat-key         = key.
  st_fieldcat-col_pos     = g_pos.
  st_fieldcat-emphasize   = emp.
  st_fieldcat-hotspot     = hot.
  st_fieldcat-do_sum      = sum.
  st_fieldcat-no_out      = hide.
  APPEND st_fieldcat TO tbl_kna1_fieldcat.
  CLEAR st_fieldcat.

ENDFORM.                    " write_kna1_fieldcat
*&---------------------------------------------------------------------*
*&      Form  get_lfa1_fieldcat
*&---------------------------------------------------------------------*
*       Populate Vendor Field Catalog
*----------------------------------------------------------------------*
FORM get_lfa1_fieldcat.

  CLEAR g_pos.
  PERFORM write_lfa1_fieldcat USING 'LIFNR' 'TBL_LFA1' 'LFA1' c_y
                                    c_n c_y c_n c_n.

  PERFORM write_lfa1_fieldcat USING 'NAME1' 'TBL_LFA1' 'LFA1' c_n
                                    c_n c_n c_n c_n.

  PERFORM write_lfa1_fieldcat USING 'STRAS' 'TBL_LFA1' 'LFA1' c_n
                                    c_n c_n c_n c_n.

ENDFORM.                    " get_lfa1_fieldcat
*&---------------------------------------------------------------------*
*&      Form  write_lfa1_fieldcat
*&---------------------------------------------------------------------*
*       Append the Vendor Field Catalog
*----------------------------------------------------------------------*
*      -->name   Field name
*      -->tab    Table name
*      -->st     Structure Name
*      -->key    Is this field a Key?
*      -->emp    Emphasize
*      -->hot    Hotspot
*      -->sum    Do_sum
*      -->hide   No_out
*----------------------------------------------------------------------*
FORM write_lfa1_fieldcat USING name
                               tab
                               st
                               key
                               emp
                               hot
                               sum
                               hide.

  g_pos = g_pos + 1.

  st_fieldcat-fieldname   = name.
  st_fieldcat-tabname     = tab.
  st_fieldcat-ref_tabname = st.
  st_fieldcat-key         = key.
  st_fieldcat-col_pos     = g_pos.
  st_fieldcat-emphasize   = emp.
  st_fieldcat-hotspot     = hot.
  st_fieldcat-do_sum      = sum.
  st_fieldcat-no_out      = hide.
  APPEND st_fieldcat TO tbl_lfa1_fieldcat.
  CLEAR st_fieldcat.

ENDFORM.                    " write_lfa1_fieldcat
*&---------------------------------------------------------------------*
*&      Form  get_vbap_fieldcat
*&---------------------------------------------------------------------*
*       Populate the Sales Order Field Catalog
*----------------------------------------------------------------------*
FORM get_vbap_fieldcat.

  CLEAR g_pos.

* Sales Order Header
  PERFORM write_vbap_fieldcat USING 'VBELN' 'TBL_VBAK' 'VBAK' c_y
                                    c_n c_y c_n c_n.
  PERFORM write_vbap_fieldcat USING 'AUART' 'TBL_VBAK' 'VBAK' c_n
                                    c_n c_n c_n c_n.
  PERFORM write_vbap_fieldcat USING 'VKORG' 'TBL_VBAK' 'VBAK' c_n
                                    c_n c_n c_n c_n.
  PERFORM write_vbap_fieldcat USING 'KUNNR' 'TBL_VBAK' 'VBAK' c_n
                                    c_n c_n c_n c_n.

  CLEAR g_pos.
* Sales Order Item
  PERFORM write_vbap_fieldcat USING 'POSNR' 'TBL_VBAP' 'VBAP' c_y
                                    c_n c_n c_n c_n.
  PERFORM write_vbap_fieldcat USING 'MATNR' 'TBL_VBAP' 'VBAP' c_n
                                    c_n c_n c_n c_n.
  PERFORM write_vbap_fieldcat USING 'MATNR' 'TBL_VBAP' 'VBAP' c_n
                                    c_n c_n c_n c_n.
  PERFORM write_vbap_fieldcat USING 'ZMENG' 'TBL_VBAP' 'VBAP' c_n
                                    c_n c_n c_n c_n.
  PERFORM write_vbap_fieldcat USING 'MEINS' 'TBL_VBAP' 'VBAP' c_n
                                    c_n c_n c_n c_n.

ENDFORM.                    " get_vbap_fieldcat
*&---------------------------------------------------------------------*
*&      Form  write_vbap_fieldcat
*&---------------------------------------------------------------------*
*       Append the Sales Order Field Catalog
*----------------------------------------------------------------------*
*      -->name   Field name
*      -->tab    Table name
*      -->st     Structure Name
*      -->key    Is this field a Key?
*      -->emp    Emphasize
*      -->hot    Hotspot
*      -->sum    Do_sum
*      -->hide   No_out
*----------------------------------------------------------------------*
FORM write_vbap_fieldcat USING name
                               tab
                               st
                               key
                               emp
                               hot
                               sum
                               hide.

  g_pos = g_pos + 1.

  st_fieldcat-fieldname   = name.
  st_fieldcat-tabname     = tab.
  st_fieldcat-ref_tabname = st.
  st_fieldcat-key         = key.
  st_fieldcat-col_pos     = g_pos.
  st_fieldcat-emphasize   = emp.
  st_fieldcat-hotspot     = hot.
  st_fieldcat-do_sum      = sum.
  st_fieldcat-no_out      = hide.
  APPEND st_fieldcat TO tbl_vbap_fieldcat.
  CLEAR st_fieldcat.

ENDFORM.                    " write_vbap_fieldcat
*&---------------------------------------------------------------------*
*&      Form  create_layout
*&---------------------------------------------------------------------*
*       Populate the Layout Structures
*----------------------------------------------------------------------*
FORM create_layout.

  IF NOT tbl_kna1[] IS INITIAL.
* Only populate Customer Layout Structure if there are Customer Records
    PERFORM get_kna1_layout.
  ENDIF.

  IF NOT tbl_lfa1[] IS INITIAL.
* Only populate Vendor Layout Structure if there are Vendor Records
    PERFORM get_lfa1_layout.
  ENDIF.

  IF NOT tbl_vbap[] IS INITIAL.
* Only populate Sales Order Layout Structure if there are Sales Order
* Records
    PERFORM get_vbap_layout.
  ENDIF.

ENDFORM.                    " create_layout
*&---------------------------------------------------------------------*
*&      Form  get_kna1_layout
*&---------------------------------------------------------------------*
*       Customer Layout Structures
*----------------------------------------------------------------------*
FORM get_kna1_layout.

  st_kna1_layout-colwidth_optimize = c_y.

ENDFORM.                    " get_kna1_layout
*&---------------------------------------------------------------------*
*&      Form  get_lfa1_layout
*&---------------------------------------------------------------------*
*       Vendor Layout Structures
*----------------------------------------------------------------------*
FORM get_lfa1_layout.

  st_lfa1_layout-colwidth_optimize = c_y.

ENDFORM.                    " get_lfa1_layout
*&---------------------------------------------------------------------*
*&      Form  get_vbap_layout
*&---------------------------------------------------------------------*
*       Sales Order Layout Structures
*----------------------------------------------------------------------*
FORM get_vbap_layout.

  st_vbap_layout-colwidth_optimize = c_y.

ENDFORM.                    " get_vbap_layout

*&---------------------------------------------------------------------*
*&      Form  get_events
*&---------------------------------------------------------------------*
*       Define the Report Events
*----------------------------------------------------------------------*
FORM get_events.

  IF NOT tbl_kna1[] IS INITIAL.
* Only define Customer Events if there are records
    PERFORM get_kna1_events.
  ENDIF.

  IF NOT tbl_lfa1[] IS INITIAL.
* Only define Vendor Events if there are records
    PERFORM get_lfa1_events.
  ENDIF.

  IF NOT tbl_vbap[] IS INITIAL.
* Only define Sales Order Events if there are records
    PERFORM get_vbap_events.
  ENDIF.

ENDFORM.                    " get_events
*&---------------------------------------------------------------------*
*&      Form  get_kna1_events
*&---------------------------------------------------------------------*
*       What are the Events for the Customer ALV Block?
*----------------------------------------------------------------------*
FORM get_kna1_events.

  CLEAR st_events.
  st_events-name = slis_ev_top_of_list.
  st_events-form = 'CUSTOMERS_TOP'.
  APPEND st_events TO tbl_kna1_events.

ENDFORM.                    " get_kna1_events
*&---------------------------------------------------------------------*
*&      Form  get_lfa1_events
*&---------------------------------------------------------------------*
*       What are the Events for the Vendor ALV Block?
*----------------------------------------------------------------------*
FORM get_lfa1_events.

  CLEAR st_events.
  st_events-name = slis_ev_top_of_list.
  st_events-form = 'VENDORS_TOP'.
  APPEND st_events TO tbl_lfa1_events.

ENDFORM.                    " get_lfa1_events
*&---------------------------------------------------------------------*
*&      Form  get_vbap_events
*&---------------------------------------------------------------------*
*       What are the Events for the Sales Order ALV Block?
*----------------------------------------------------------------------*
FORM get_vbap_events.

  CLEAR st_events.
  st_events-name = slis_ev_top_of_list.
  st_events-form = 'SALESORDERS_TOP'.
  APPEND st_events TO tbl_vbap_events.

ENDFORM.                    " get_vbap_events
*&---------------------------------------------------------------------*
*&      Form  customers_top
*&---------------------------------------------------------------------*
*       TOP OF LIST Event for Customer ALV
*----------------------------------------------------------------------*
FORM customers_top.

  WRITE:/ 'Customer List'.

ENDFORM.                    " customers_top
*&---------------------------------------------------------------------*
*&      Form  vendors_top
*&---------------------------------------------------------------------*
*       TOP OF LIST Event for Vendor ALV
*----------------------------------------------------------------------*
FORM vendors_top.

  WRITE:/ 'Vendor List'.

ENDFORM.                    " vendors_top
*&---------------------------------------------------------------------*
*&      Form  salesorders_top
*&---------------------------------------------------------------------*
*       TOP OF LIST Event for Vendor ALV
*----------------------------------------------------------------------*
FORM salesorders_top.

  WRITE:/ 'Sales Order List'.

ENDFORM.                    " salesorders_top
*&---------------------------------------------------------------------*
*&      Form  get_keyinfo
*&---------------------------------------------------------------------*
*       This defines the "link" between the "header" and "item" tables
*----------------------------------------------------------------------*
FORM get_keyinfo.

  st_keyinfo-header01 = 'VBELN'.

ENDFORM.                    " get_keyinfo
*&---------------------------------------------------------------------*
*&      Form  process_user_commands
*&---------------------------------------------------------------------*
*       This subroutine is called when there is user interaction during
*       the report output
*---------------------------------------------------------------------*
*  -->  SYST-UCOMM   User Command
*  -->  SELFIELD     Selected Field
*---------------------------------------------------------------------*
FORM process_user_commands USING syst-ucomm LIKE syst-ucomm
                                 selfield   TYPE slis_selfield.

  CASE syst-ucomm.

    WHEN '&IC1'.    "Double Click

      IF selfield-tabname = 'TBL_KNA1'.
* Display the Customer Record via XD03
        READ TABLE tbl_kna1 INDEX selfield-tabindex.
        CHECK tbl_kna1-kunnr NE 0.
        SET PARAMETER ID 'KUN' FIELD tbl_kna1-kunnr.
        CALL TRANSACTION 'XD03' AND SKIP FIRST SCREEN.

      ELSEIF selfield-tabname = 'TBL_LFA1'.
* Display the Vendor Record via XK03
        READ TABLE tbl_lfa1 INDEX selfield-tabindex.
        CHECK tbl_lfa1-lifnr NE 0.
        SET PARAMETER ID 'LIF' FIELD tbl_lfa1-lifnr.
        CALL TRANSACTION 'XK03' AND SKIP FIRST SCREEN.
      ELSEIF selfield-tabname = 'TBL_VBAK'.
* Display the Sales Order Record via VA03
        READ TABLE tbl_vbak INDEX selfield-tabindex.
        CHECK tbl_vbak-vbeln NE 0.
        SET PARAMETER ID 'AUN' FIELD tbl_vbak-vbeln.
        CALL TRANSACTION 'VA03' AND SKIP FIRST SCREEN.
      ENDIF.

    WHEN OTHERS.

  ENDCASE.

ENDFORM.                    " process_user_commands

2 REPLIES 2
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
358

I assume that you are using the function module for ALV grid? I don't think that you can do this using the FM. You can do it using the OO based ALV, but then I don't know if you can do TOP OF PAGE, and END OF PAGE. You may be able to do this using the function module for the BLOCK LIST.

Regards,

Rich Heilman

Read only

former_member221770
Contributor
0 Likes
359

Hi Kranthi,

As Rich said you can do this using the ALV Block FM's. Ihave attached a sample program below. I use both the sequentional and hierarchy type ALV lists here. This should get you on the right track.

Hope this helps.

Cheers,

Pat.


* This program is an example of how the ALV Block Display works.
* It will display 3 ALV Report "Blocks" (Customer Details, Vendor
* Details and Sales Orders) on the one Screen

REPORT  zpat_alv_block.
************************************************************************
* Types
************************************************************************
TYPE-POOLS: kkblo.

************************************************************************
* Database Tables
************************************************************************
TABLES: kna1,  "General Customer Master
        lfa1,  "General Vendor Master
        vbak,  "Sales Order Header
        vbap.  "Sales Order Item

************************************************************************
* Structures
************************************************************************
DATA: st_fieldcat     TYPE slis_fieldcat_alv. "Fieldcatalog Structure
DATA: st_kna1_layout  TYPE slis_layout_alv.   "Customer Layout Structure
DATA: st_lfa1_layout  TYPE slis_layout_alv.   "Vendor Layout Structure
DATA: st_vbap_layout  TYPE slis_layout_alv.   "Sales Order Layout St.
DATA: st_events       TYPE slis_alv_event.    "Event Structure
DATA: st_keyinfo      TYPE slis_keyinfo_alv.  "Link Between Hdr & Item

************************************************************************
* Internal tables
************************************************************************

* Fieldcatalog for Customers
DATA: tbl_kna1_fieldcat TYPE slis_t_fieldcat_alv.

* Fieldcatalog for Vendors
DATA: tbl_lfa1_fieldcat TYPE slis_t_fieldcat_alv.

* Fieldcatalog for Sales Orders
DATA: tbl_vbap_fieldcat TYPE slis_t_fieldcat_alv.

* Internal Table for Event Table 1 (Customer)
DATA: tbl_kna1_events TYPE slis_t_event.

* Internal Table for Event Table 2 (Vendor)
DATA: tbl_lfa1_events TYPE slis_t_event.

* Internal Table for Event Table 3 (Sales Order)
DATA: tbl_vbap_events TYPE slis_t_event.

* Internal Table to hold Customer Data
DATA: BEGIN OF tbl_kna1 OCCURS 0.
        INCLUDE STRUCTURE kna1.
DATA: END OF tbl_kna1.

* Internal Table to hold Vendor Data
DATA: BEGIN OF tbl_lfa1 OCCURS 0.
        INCLUDE STRUCTURE lfa1.
DATA: END OF tbl_lfa1.

* Internal Table to hold Sales Order Header
DATA: BEGIN OF tbl_vbak OCCURS 0.
        INCLUDE STRUCTURE vbak.
DATA: END OF tbl_vbak.

* Internal Table to hold Sales Order Item
DATA: BEGIN OF tbl_vbap OCCURS 0.
        INCLUDE STRUCTURE vbap.
DATA: END OF tbl_vbap.

************************************************************************
* Constants
************************************************************************
CONSTANTS: c_y   VALUE 'X'. "Yes
CONSTANTS: c_n   VALUE ' '. "No

************************************************************************
* Simple Variables
************************************************************************
DATA: field_name(30) TYPE c,         "Selected Field
      g_repid        LIKE sy-repid,  "Report Name
      g_pos          TYPE i.         "Position (Field Catalog)

************************************************************************
* Start of Selection
************************************************************************
START-OF-SELECTION.

* Get the Report Name
  g_repid = sy-repid.

* Get the data
  PERFORM get_data.

************************************************************************
* End of Selection
************************************************************************
END-OF-SELECTION.

* Get the Field Catalog
  PERFORM create_fieldcat.

* Get the Layouts
  PERFORM create_layout.

* Get the User Interaction Events
  PERFORM get_events.

* Get the Key Info for Hierarchy Display (Sales Orders)
  PERFORM get_keyinfo.

* Display the Report
  PERFORM create_report.


************************************************************************
* Subroutines
************************************************************************

*&---------------------------------------------------------------------*
*&      Form  create_report
*&---------------------------------------------------------------------*
*       Create the Reports
*----------------------------------------------------------------------*
FORM create_report.

  CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_INIT'
       EXPORTING
            i_callback_program       = g_repid
*            i_callback_pf_status_set = 'SET_STATUS'
            i_callback_user_command  = 'PROCESS_USER_COMMANDS'.

  IF NOT tbl_kna1[] IS INITIAL.
* Display the Customer ALV Block
    CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND'
         EXPORTING
              is_layout   = st_kna1_layout
              it_fieldcat = tbl_kna1_fieldcat
              i_tabname   = 'TBL_KNA1'
              it_events   = tbl_kna1_events
              i_text      = 'Customers'
         TABLES
              t_outtab    = tbl_kna1.
  ENDIF.

  IF NOT tbl_lfa1[] IS INITIAL.
* Display the Vendor ALV Block
    CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND'
         EXPORTING
              is_layout   = st_lfa1_layout
              it_fieldcat = tbl_lfa1_fieldcat
              i_tabname   = 'TBL_LFA1'
              it_events   = tbl_lfa1_events
              i_text      = 'Vendors'
         TABLES
              t_outtab    = tbl_lfa1.
  ENDIF.

  IF NOT tbl_vbap[] IS INITIAL.
* Display the Sales Order ALV Block
    CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_HS_APPEND'
         EXPORTING
              is_layout        = st_vbap_layout
              it_fieldcat      = tbl_vbap_fieldcat
              is_keyinfo       = st_keyinfo
              i_header_tabname = 'TBL_VBAK'
              i_item_tabname   = 'TBL_VBAP'
              it_events        = tbl_vbap_events
              i_text           = 'Sales Orders'
         TABLES
              t_outtab_header  = tbl_vbak
              t_outtab_item    = tbl_vbap.

  ENDIF.
* Display the Defined Blocks
  CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_DISPLAY'.

ENDFORM.                    " create_report
*&---------------------------------------------------------------------*
*&      Form  get_data
*&---------------------------------------------------------------------*
*       The subroutine name says "Get Data" so I wonder what the routine
*       does....
*----------------------------------------------------------------------*
FORM get_data.

* Get the Customer Data
  SELECT *
         INTO CORRESPONDING FIELDS OF TABLE tbl_kna1
         FROM kna1
         UP TO 15 ROWS.

* Get the Vendor Data
  SELECT *
         INTO CORRESPONDING FIELDS OF TABLE tbl_lfa1
         FROM lfa1
         UP TO 15 ROWS.

* Get the Sales Order Header Data
  SELECT *
         INTO CORRESPONDING FIELDS OF TABLE tbl_vbak
         FROM vbak
         UP TO 10 ROWS.

  CHECK NOT tbl_vbak[] IS INITIAL.
* Get the Sales Order Item Data
  SELECT *
         INTO CORRESPONDING FIELDS OF TABLE tbl_vbap
         FROM vbap
         FOR ALL ENTRIES IN tbl_vbak
         WHERE vbeln = tbl_vbak-vbeln.

ENDFORM.                    " get_data
*&---------------------------------------------------------------------*
*&      Form  create_fieldcat
*&---------------------------------------------------------------------*
*       Create te Field Catalog...duh!
*----------------------------------------------------------------------*
FORM create_fieldcat.

  IF NOT tbl_kna1[] IS INITIAL.
* Only populate the Customer Field Catalog if TBL_KNA1 has data
    PERFORM get_kna1_fieldcat.
  ENDIF.

  IF NOT tbl_lfa1[] IS INITIAL.
* Only populate the Vendor Field Catalog if TBL_LFA1 has data
    PERFORM get_lfa1_fieldcat.
  ENDIF.

  IF NOT tbl_vbap[] IS INITIAL.
* Only populate the Sales Order Field Catalog if TBL_VBAP has data
    PERFORM get_vbap_fieldcat.
  ENDIF.

ENDFORM.                    " create_fieldcat
*&---------------------------------------------------------------------*
*&      Form  get_kna1_fieldcat
*&---------------------------------------------------------------------*
*       Populate Customer Field Catalog
*----------------------------------------------------------------------*
FORM get_kna1_fieldcat.

  CLEAR g_pos.
  PERFORM write_kna1_fieldcat USING 'KUNNR' 'TBL_KNA1' 'KNA1' c_y
                                    c_n c_y c_n c_n.

  PERFORM write_kna1_fieldcat USING 'NAME1' 'TBL_KNA1' 'KNA1' c_n
                                    c_n c_n c_n c_n.

  PERFORM write_kna1_fieldcat USING 'STRAS' 'TBL_KNA1' 'KNA1' c_n
                                    c_n c_n c_n c_n.

ENDFORM.                    " get_kna1_fieldcat
*&---------------------------------------------------------------------*
*&      Form  write_kna1_fieldcat
*&---------------------------------------------------------------------*
*       Append the Customer Field Catalog
*----------------------------------------------------------------------*
*      -->name   Field name
*      -->tab    Table name
*      -->st     Structure Name
*      -->key    Is this field a Key?
*      -->emp    Emphasize
*      -->hot    Hotspot
*      -->sum    Do_sum
*      -->hide   No_out
*----------------------------------------------------------------------*
FORM write_kna1_fieldcat USING name
                               tab
                               st
                               key
                               emp
                               hot
                               sum
                               hide.

  g_pos = g_pos + 1.

  st_fieldcat-fieldname   = name.
  st_fieldcat-tabname     = tab.
  st_fieldcat-ref_tabname = st.
  st_fieldcat-key         = key.
  st_fieldcat-col_pos     = g_pos.
  st_fieldcat-emphasize   = emp.
  st_fieldcat-hotspot     = hot.
  st_fieldcat-do_sum      = sum.
  st_fieldcat-no_out      = hide.
  APPEND st_fieldcat TO tbl_kna1_fieldcat.
  CLEAR st_fieldcat.

ENDFORM.                    " write_kna1_fieldcat
*&---------------------------------------------------------------------*
*&      Form  get_lfa1_fieldcat
*&---------------------------------------------------------------------*
*       Populate Vendor Field Catalog
*----------------------------------------------------------------------*
FORM get_lfa1_fieldcat.

  CLEAR g_pos.
  PERFORM write_lfa1_fieldcat USING 'LIFNR' 'TBL_LFA1' 'LFA1' c_y
                                    c_n c_y c_n c_n.

  PERFORM write_lfa1_fieldcat USING 'NAME1' 'TBL_LFA1' 'LFA1' c_n
                                    c_n c_n c_n c_n.

  PERFORM write_lfa1_fieldcat USING 'STRAS' 'TBL_LFA1' 'LFA1' c_n
                                    c_n c_n c_n c_n.

ENDFORM.                    " get_lfa1_fieldcat
*&---------------------------------------------------------------------*
*&      Form  write_lfa1_fieldcat
*&---------------------------------------------------------------------*
*       Append the Vendor Field Catalog
*----------------------------------------------------------------------*
*      -->name   Field name
*      -->tab    Table name
*      -->st     Structure Name
*      -->key    Is this field a Key?
*      -->emp    Emphasize
*      -->hot    Hotspot
*      -->sum    Do_sum
*      -->hide   No_out
*----------------------------------------------------------------------*
FORM write_lfa1_fieldcat USING name
                               tab
                               st
                               key
                               emp
                               hot
                               sum
                               hide.

  g_pos = g_pos + 1.

  st_fieldcat-fieldname   = name.
  st_fieldcat-tabname     = tab.
  st_fieldcat-ref_tabname = st.
  st_fieldcat-key         = key.
  st_fieldcat-col_pos     = g_pos.
  st_fieldcat-emphasize   = emp.
  st_fieldcat-hotspot     = hot.
  st_fieldcat-do_sum      = sum.
  st_fieldcat-no_out      = hide.
  APPEND st_fieldcat TO tbl_lfa1_fieldcat.
  CLEAR st_fieldcat.

ENDFORM.                    " write_lfa1_fieldcat
*&---------------------------------------------------------------------*
*&      Form  get_vbap_fieldcat
*&---------------------------------------------------------------------*
*       Populate the Sales Order Field Catalog
*----------------------------------------------------------------------*
FORM get_vbap_fieldcat.

  CLEAR g_pos.

* Sales Order Header
  PERFORM write_vbap_fieldcat USING 'VBELN' 'TBL_VBAK' 'VBAK' c_y
                                    c_n c_y c_n c_n.
  PERFORM write_vbap_fieldcat USING 'AUART' 'TBL_VBAK' 'VBAK' c_n
                                    c_n c_n c_n c_n.
  PERFORM write_vbap_fieldcat USING 'VKORG' 'TBL_VBAK' 'VBAK' c_n
                                    c_n c_n c_n c_n.
  PERFORM write_vbap_fieldcat USING 'KUNNR' 'TBL_VBAK' 'VBAK' c_n
                                    c_n c_n c_n c_n.

  CLEAR g_pos.
* Sales Order Item
  PERFORM write_vbap_fieldcat USING 'POSNR' 'TBL_VBAP' 'VBAP' c_y
                                    c_n c_n c_n c_n.
  PERFORM write_vbap_fieldcat USING 'MATNR' 'TBL_VBAP' 'VBAP' c_n
                                    c_n c_n c_n c_n.
  PERFORM write_vbap_fieldcat USING 'MATNR' 'TBL_VBAP' 'VBAP' c_n
                                    c_n c_n c_n c_n.
  PERFORM write_vbap_fieldcat USING 'ZMENG' 'TBL_VBAP' 'VBAP' c_n
                                    c_n c_n c_n c_n.
  PERFORM write_vbap_fieldcat USING 'MEINS' 'TBL_VBAP' 'VBAP' c_n
                                    c_n c_n c_n c_n.

ENDFORM.                    " get_vbap_fieldcat
*&---------------------------------------------------------------------*
*&      Form  write_vbap_fieldcat
*&---------------------------------------------------------------------*
*       Append the Sales Order Field Catalog
*----------------------------------------------------------------------*
*      -->name   Field name
*      -->tab    Table name
*      -->st     Structure Name
*      -->key    Is this field a Key?
*      -->emp    Emphasize
*      -->hot    Hotspot
*      -->sum    Do_sum
*      -->hide   No_out
*----------------------------------------------------------------------*
FORM write_vbap_fieldcat USING name
                               tab
                               st
                               key
                               emp
                               hot
                               sum
                               hide.

  g_pos = g_pos + 1.

  st_fieldcat-fieldname   = name.
  st_fieldcat-tabname     = tab.
  st_fieldcat-ref_tabname = st.
  st_fieldcat-key         = key.
  st_fieldcat-col_pos     = g_pos.
  st_fieldcat-emphasize   = emp.
  st_fieldcat-hotspot     = hot.
  st_fieldcat-do_sum      = sum.
  st_fieldcat-no_out      = hide.
  APPEND st_fieldcat TO tbl_vbap_fieldcat.
  CLEAR st_fieldcat.

ENDFORM.                    " write_vbap_fieldcat
*&---------------------------------------------------------------------*
*&      Form  create_layout
*&---------------------------------------------------------------------*
*       Populate the Layout Structures
*----------------------------------------------------------------------*
FORM create_layout.

  IF NOT tbl_kna1[] IS INITIAL.
* Only populate Customer Layout Structure if there are Customer Records
    PERFORM get_kna1_layout.
  ENDIF.

  IF NOT tbl_lfa1[] IS INITIAL.
* Only populate Vendor Layout Structure if there are Vendor Records
    PERFORM get_lfa1_layout.
  ENDIF.

  IF NOT tbl_vbap[] IS INITIAL.
* Only populate Sales Order Layout Structure if there are Sales Order
* Records
    PERFORM get_vbap_layout.
  ENDIF.

ENDFORM.                    " create_layout
*&---------------------------------------------------------------------*
*&      Form  get_kna1_layout
*&---------------------------------------------------------------------*
*       Customer Layout Structures
*----------------------------------------------------------------------*
FORM get_kna1_layout.

  st_kna1_layout-colwidth_optimize = c_y.

ENDFORM.                    " get_kna1_layout
*&---------------------------------------------------------------------*
*&      Form  get_lfa1_layout
*&---------------------------------------------------------------------*
*       Vendor Layout Structures
*----------------------------------------------------------------------*
FORM get_lfa1_layout.

  st_lfa1_layout-colwidth_optimize = c_y.

ENDFORM.                    " get_lfa1_layout
*&---------------------------------------------------------------------*
*&      Form  get_vbap_layout
*&---------------------------------------------------------------------*
*       Sales Order Layout Structures
*----------------------------------------------------------------------*
FORM get_vbap_layout.

  st_vbap_layout-colwidth_optimize = c_y.

ENDFORM.                    " get_vbap_layout

*&---------------------------------------------------------------------*
*&      Form  get_events
*&---------------------------------------------------------------------*
*       Define the Report Events
*----------------------------------------------------------------------*
FORM get_events.

  IF NOT tbl_kna1[] IS INITIAL.
* Only define Customer Events if there are records
    PERFORM get_kna1_events.
  ENDIF.

  IF NOT tbl_lfa1[] IS INITIAL.
* Only define Vendor Events if there are records
    PERFORM get_lfa1_events.
  ENDIF.

  IF NOT tbl_vbap[] IS INITIAL.
* Only define Sales Order Events if there are records
    PERFORM get_vbap_events.
  ENDIF.

ENDFORM.                    " get_events
*&---------------------------------------------------------------------*
*&      Form  get_kna1_events
*&---------------------------------------------------------------------*
*       What are the Events for the Customer ALV Block?
*----------------------------------------------------------------------*
FORM get_kna1_events.

  CLEAR st_events.
  st_events-name = slis_ev_top_of_list.
  st_events-form = 'CUSTOMERS_TOP'.
  APPEND st_events TO tbl_kna1_events.

ENDFORM.                    " get_kna1_events
*&---------------------------------------------------------------------*
*&      Form  get_lfa1_events
*&---------------------------------------------------------------------*
*       What are the Events for the Vendor ALV Block?
*----------------------------------------------------------------------*
FORM get_lfa1_events.

  CLEAR st_events.
  st_events-name = slis_ev_top_of_list.
  st_events-form = 'VENDORS_TOP'.
  APPEND st_events TO tbl_lfa1_events.

ENDFORM.                    " get_lfa1_events
*&---------------------------------------------------------------------*
*&      Form  get_vbap_events
*&---------------------------------------------------------------------*
*       What are the Events for the Sales Order ALV Block?
*----------------------------------------------------------------------*
FORM get_vbap_events.

  CLEAR st_events.
  st_events-name = slis_ev_top_of_list.
  st_events-form = 'SALESORDERS_TOP'.
  APPEND st_events TO tbl_vbap_events.

ENDFORM.                    " get_vbap_events
*&---------------------------------------------------------------------*
*&      Form  customers_top
*&---------------------------------------------------------------------*
*       TOP OF LIST Event for Customer ALV
*----------------------------------------------------------------------*
FORM customers_top.

  WRITE:/ 'Customer List'.

ENDFORM.                    " customers_top
*&---------------------------------------------------------------------*
*&      Form  vendors_top
*&---------------------------------------------------------------------*
*       TOP OF LIST Event for Vendor ALV
*----------------------------------------------------------------------*
FORM vendors_top.

  WRITE:/ 'Vendor List'.

ENDFORM.                    " vendors_top
*&---------------------------------------------------------------------*
*&      Form  salesorders_top
*&---------------------------------------------------------------------*
*       TOP OF LIST Event for Vendor ALV
*----------------------------------------------------------------------*
FORM salesorders_top.

  WRITE:/ 'Sales Order List'.

ENDFORM.                    " salesorders_top
*&---------------------------------------------------------------------*
*&      Form  get_keyinfo
*&---------------------------------------------------------------------*
*       This defines the "link" between the "header" and "item" tables
*----------------------------------------------------------------------*
FORM get_keyinfo.

  st_keyinfo-header01 = 'VBELN'.

ENDFORM.                    " get_keyinfo
*&---------------------------------------------------------------------*
*&      Form  process_user_commands
*&---------------------------------------------------------------------*
*       This subroutine is called when there is user interaction during
*       the report output
*---------------------------------------------------------------------*
*  -->  SYST-UCOMM   User Command
*  -->  SELFIELD     Selected Field
*---------------------------------------------------------------------*
FORM process_user_commands USING syst-ucomm LIKE syst-ucomm
                                 selfield   TYPE slis_selfield.

  CASE syst-ucomm.

    WHEN '&IC1'.    "Double Click

      IF selfield-tabname = 'TBL_KNA1'.
* Display the Customer Record via XD03
        READ TABLE tbl_kna1 INDEX selfield-tabindex.
        CHECK tbl_kna1-kunnr NE 0.
        SET PARAMETER ID 'KUN' FIELD tbl_kna1-kunnr.
        CALL TRANSACTION 'XD03' AND SKIP FIRST SCREEN.

      ELSEIF selfield-tabname = 'TBL_LFA1'.
* Display the Vendor Record via XK03
        READ TABLE tbl_lfa1 INDEX selfield-tabindex.
        CHECK tbl_lfa1-lifnr NE 0.
        SET PARAMETER ID 'LIF' FIELD tbl_lfa1-lifnr.
        CALL TRANSACTION 'XK03' AND SKIP FIRST SCREEN.
      ELSEIF selfield-tabname = 'TBL_VBAK'.
* Display the Sales Order Record via VA03
        READ TABLE tbl_vbak INDEX selfield-tabindex.
        CHECK tbl_vbak-vbeln NE 0.
        SET PARAMETER ID 'AUN' FIELD tbl_vbak-vbeln.
        CALL TRANSACTION 'VA03' AND SKIP FIRST SCREEN.
      ENDIF.

    WHEN OTHERS.

  ENDCASE.

ENDFORM.                    " process_user_commands