Tuesday, August 7, 2018

Download ALV report display data in SAP ABAP.

*&---------------------------------------------------------------------*
*& Report  ZSW_DOWNLOAD
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZSW_DOWNLOAD.

TABLES VBAK,VBAP,VBFA,LIKP,LIPS.

TYPESBEGIN OF TY_VBAK,
        VBELN TYPE VBAK-VBELN,
        VKORG TYPE VBAK-VKORG,
        VTWEG TYPE VBAK-VTWEG,
        SPART TYPE VBAK-SPART,
      END OF TY_VBAK.
TYPESBEGIN OF TY_VBAP,
        VBELN TYPE VBAP-VBELN,
        POSNR TYPE VBAP-POSNR,
        MATNR TYPE VBAP-MATNR,
        ZMENG TYPE VBAP-ZMENG,
        NETPR TYPE VBAP-NETPR,
      END OF TY_VBAP.
TYPESBEGIN OF TY_LIKP,
        VBELN TYPE LIKP-VBELN,
      END OF TY_LIKP.
TYPESBEGIN OF TY_LIPS,
        VBELN TYPE LIPS-VBELN,
        POSNR TYPE LIPS-POSNR,
        LFIMG TYPE LIPS-LFIMG,
        NETPR TYPE LIPS-NETPR,
      END OF TY_LIPS.
TYPESBEGIN OF TY_VBFA,
        VBELV TYPE VBFA-VBELV,
        POSNV TYPE VBFA-POSNV,
        VBELN TYPE VBFA-VBELN,
        POSNN TYPE VBFA-POSNN,
        VBTYP_N TYPE VBFA-VBTYP_N,
      END OF TY_VBFA.
TYPESBEGIN OF TY_FINAL,
        VBELN TYPE VBAP-VBELN,
        POSNR TYPE VBAP-POSNR,
        MATNR TYPE VBAP-MATNR,
        ZMENG TYPE VBAP-ZMENG,
        NETPR TYPE VBAP-NETPR,
        VBELN1 TYPE LIPS-VBELN,
        POSNR1 TYPE LIPS-POSNR,
        LFIMG TYPE LIPS-LFIMG,
        NETPR1 TYPE LIPS-NETPR,
      END OF TY_FINAL.

DATA I_VBAK TYPE TABLE OF TY_VBAK,WA_VBAK TYPE TY_VBAK.
DATA I_VBAP TYPE TABLE OF TY_VBAP,WA_VBAP TYPE TY_VBAP.
DATA I_LIKP TYPE TABLE OF TY_LIKP,WA_LIKP TYPE TY_LIKP.
DATA I_LIPS TYPE TABLE OF TY_LIPS,WA_LIPS TYPE TY_LIPS.
DATA I_VBFA TYPE TABLE OF TY_VBFA,WA_VBFA TYPE TY_VBFA.
DATA I_FINAL TYPE TABLE OF TY_FINAL,WA_FINAL TYPE TY_FINAL.

DATA V_TITLE TYPE STRING.
DATA V_FNAME TYPE STRING.

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS S_VBELN FOR VBAK-VBELN NO-EXTENSION.
SELECT-OPTIONS S_VKORG FOR VBAK-VKORG NO INTERVALS NO-EXTENSION.
SELECT-OPTIONS S_VTWEG FOR VBAK-VTWEG NO INTERVALS NO-EXTENSION.
SELECT-OPTIONS S_SPART FOR VBAK-SPART NO INTERVALS NO-EXTENSION.
SELECTION-SCREEN END OF BLOCK B1.
SELECTION-SCREEN SKIP.

PARAMETERS P_FILE TYPE RLGRAP-FILENAME.
PARAMETERS DOWNLOAD AS CHECKBOX.

INITIALIZATION.
  V_TITLE 'SALES DOCUMENT:ITEM DATA AND DELIVERY DETAILS'.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
  PERFORM GET_FILE_PATH.

AT SELECTION-SCREEN ON HELP-REQUEST FOR P_FILE.
  PERFORM GET_HELP_FOR_FILE_PATH.

AT SELECTION-SCREEN.
  PERFORM VALIDATE_SALESDOCUMENT.
  PERFORM VALIDATE_SALESORGANIZATION.
  PERFORM VALIDATE_DISTRIBUTIONCHANNEL.
  PERFORM VALIDATE_DIVISION.

START-OF-SELECTION.
  PERFORM GET_DATA.
  PERFORM DISPLAY_DATA.
  PERFORM DOWNLOAD_DATA.

END-OF-SELECTION.

TOP-OF-PAGE.
  PERFORM DISPLAY_HEADING.

END-OF-PAGE.
  PERFORM DISPLAY_FOOTER.


FORM GET_FILE_PATH .
  CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
    EXPORTING
      PROGRAM_NAME  SYST-REPID
      DYNPRO_NUMBER SYST-DYNNR
      FIELD_NAME    P_FILE
    CHANGING
      FILE_NAME     P_FILE.
ENDFORM.                    " GET_FILE_PATH

FORM GET_HELP_FOR_FILE_PATH .
  MESSAGE 'PLEASE SELECT FILE PATH' TYPE 'I'.
ENDFORM.                    " GET_HELP_FOR_FILE_PATH

FORM VALIDATE_SALESDOCUMENT .
  SELECT SINGLE VBELN FROM VBAK INTO WA_VBAK WHERE VBELN IN S_VBELN.
  IF SY-SUBRC <> 0.
    MESSAGE 'INVALID SALES DOCUMENT NO' TYPE 'I'.
  ENDIF.
ENDFORM.                    " VALIDATE_SALESDOCUMENT

FORM VALIDATE_SALESORGANIZATION .
  SELECT VKORG FROM VBAK INTO WA_VBAK UP TO ROWS WHERE VKORG IN S_VKORG.
  ENDSELECT.
  IF SY-SUBRC <> 0.
    MESSAGE 'INVALID SALES ORGANIZATION' TYPE 'I'.
  ENDIF.
ENDFORM.                    " VALIDATE_SALESORGANIZATION

FORM VALIDATE_DISTRIBUTIONCHANNEL.
  SELECT VTWEG FROM VBAK INTO WA_VBAK UP TO ROWS WHERE VTWEG IN S_VTWEG.
  ENDSELECT.
  IF SY-SUBRC <> 0.
    MESSAGE 'INVALID DISTRIBUTION CHANNEL' TYPE 'I'.
  ENDIF.
ENDFORM.                    " VALIDATE_DISTRIBUTIONCHANNEL

FORM VALIDATE_DIVISION .
  SELECT SPART FROM VBAK INTO WA_VBAK UP TO ROWS WHERE SPART IN S_SPART.
  ENDSELECT.
  IF SY-SUBRC <> 0.
    MESSAGE 'INVALID DIVISION' TYPE 'I'.
  ENDIF.
ENDFORM.                    " VALIDATE_DIVISION

FORM GET_DATA .

  SELECT VBELN VKORG VTWEG SPART FROM VBAK INTO TABLE I_VBAK WHERE VBELN IN S_VBELN AND VKORG IN S_VKORG
                                                               AND VTWEG IN S_VTWEG AND SPART IN S_SPART.
  IF I_VBAK IS NOT INITIAL.

    SELECT VBELN POSNR MATNR ZMENG NETPR FROM VBAP INTO TABLE I_VBAP FOR ALL ENTRIES IN I_VBAK
                                                                    WHERE VBELN I_VBAK-VBELN.

    SELECT VBELV POSNV VBELN POSNN VBTYP_N FROM VBFA INTO TABLE I_VBFA FOR ALL ENTRIES IN I_VBAP
                                                    WHERE VBELV I_VBAP-VBELN AND VBTYP_N 'J'.

    SELECT VBELN FROM LIKP INTO TABLE I_LIKP FOR ALL ENTRIES IN I_VBFA WHERE VBELN I_VBFA-VBELN.


    SELECT VBELN POSNR LFIMG NETPR FROM LIPS INTO TABLE I_LIPS FOR ALL ENTRIES IN I_LIKP
                                                              WHERE VBELN I_LIKP-VBELN.
    SORT I_VBAP.
    SORT I_LIPS.

    LOOP AT I_VBFA INTO WA_VBFA.
      READ TABLE I_LIPS INTO WA_LIPS WITH KEY VBELN WA_VBFA-VBELN POSNR WA_VBFA-POSNN BINARY SEARCH.
      READ TABLE I_VBAP INTO WA_VBAP WITH KEY VBELN WA_VBFA-VBELV POSNR WA_VBFA-POSNV BINARY SEARCH.
      WA_FINAL-VBELN WA_VBAP-VBELN.
      WA_FINAL-POSNR WA_VBAP-POSNR.
      WA_FINAL-MATNR WA_VBAP-MATNR.
      WA_FINAL-ZMENG WA_VBAP-ZMENG.
      WA_FINAL-NETPR WA_VBAP-NETPR.
      WA_FINAL-VBELN1 WA_LIPS-VBELN.
      WA_FINAL-POSNR1 WA_LIPS-POSNR.
      WA_FINAL-LFIMG WA_LIPS-LFIMG.
      WA_FINAL-NETPR1 WA_LIPS-NETPR.
      APPEND WA_FINAL TO I_FINAL.
      CLEAR WA_FINAL.
    ENDLOOP.
  ENDIF.

ENDFORM.                    " GET_DATA

FORM DISPLAY_DATA .
  LOOP AT I_FINAL INTO WA_FINAL.
    WRITE / WA_FINAL-VBELN,WA_FINAL-POSNR,WA_FINAL-MATNR,WA_FINAL-ZMENG,WA_FINAL-NETPR,
              WA_FINAL-VBELN1,WA_FINAL-POSNR1,WA_FINAL-LFIMG,WA_FINAL-NETPR.
  ENDLOOP.
ENDFORM.                    " DISPLAY_DATA

FORM DOWNLOAD_DATA .
  IF DOWNLOAD 'X'.
    V_FNAME P_FILE.
    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        FILENAME              V_FNAME
        FILETYPE              'ASC'
        WRITE_FIELD_SEPARATOR 'X'
      TABLES
        DATA_TAB              I_FINAL.
    IF SY-SUBRC 0.
      MESSAGE 'DATA SUCCESSFULLY DOWNLOADED' TYPE 'I'.
    ENDIF.
  ENDIF.
ENDFORM.                    " DOWNLOAD_DATA

FORM DISPLAY_HEADING .
  WRITESY-ULINE.
  WRITE :/45 V_TITLE.
  WRITESY-ULINE.
ENDFORM.                    " DISPLAY_HEADING

FORM DISPLAY_FOOTER .
  WRITESY-ULINE.
  WRITE :/45 'INTEL GROUP OF COMPANIES'.
  WRITESY-ULINE.
ENDFORM.                    " DISPLAY_FOOTER

Interactive ALV report in sap abap.

Interactive ALV report in sap abap
*&---------------------------------------------------------------------*
*& Report  ZSW_IN_ALV
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZSW_IN_ALV.

*REPORT  zsr_test.

TABLESekkoekpo.
TYPE-POOLSslis.

TYPESBEGIN OF ty_ekko,
        ebeln TYPE ekko-ebeln,
        bukrs TYPE ekko-bukrs,
        lifnr TYPE ekko-lifnr,
       END OF ty_ekko,

       BEGIN OF ty_out_ekko,
        sel,
        ebeln TYPE ekko-ebeln,
        bukrs TYPE ekko-bukrs,
        lifnr TYPE ekko-lifnr,
       END OF ty_out_ekko,

       BEGIN OF ty_ekpo,
         ebeln TYPE ekpo-ebeln,
         ebelp TYPE ekpo-ebelp,
         matnr TYPE ekpo-matnr,
         werks TYPE ekpo-werks,
         lgort TYPE ekpo-lgort,
         menge TYPE ekpo-menge,
         meins TYPE ekpo-meins,
       END OF ty_ekpo,

       BEGIN OF ty_out_ekpo,
         sel,
         ebeln TYPE ekko-ebeln,
         ebelp TYPE ekpo-ebelp,
         matnr TYPE ekpo-matnr,
         werks TYPE ekpo-werks,
         lgort TYPE ekpo-lgort,
         menge TYPE ekpo-menge,
         meins TYPE ekpo-meins,
       END OF ty_out_ekpo,

       BEGIN OF ty_ebeln,
         ebeln TYPE ekpo-ebeln,
       END OF ty_ebeln.

DATAwa_ekko TYPE ty_ekko,
      wa_ekpo TYPE ty_ekpo,
      it_ekko TYPE STANDARD TABLE OF ty_ekko,
      it_ekpo TYPE STANDARD TABLE OF ty_ekpo,

      wa_out_ekko TYPE ty_out_ekko,
      wa_out_ekpo TYPE ty_out_ekpo,
      wa_ebeln    TYPE ty_ebeln,
      it_out_ekko TYPE STANDARD TABLE OF ty_out_ekko,
      it_out_ekpo TYPE STANDARD TABLE OF ty_out_ekpo,
      it_ebeln    TYPE STANDARD TABLE OF ty_ebeln,

      wa_fcat_ekko TYPE slis_fieldcat_alv,
      wa_fcat_ekpo TYPE slis_fieldcat_alv,
      it_fcat_ekko TYPE slis_t_fieldcat_alv,
      it_fcat_ekpo TYPE slis_t_fieldcat_alv,

      wa_layout TYPE slis_layout_alv,

      wa_top_ekko TYPE slis_listheader,
      wa_top_ekpo TYPE slis_listheader,
      it_top_ekko TYPE slis_t_listheader,
      it_top_ekpo TYPE slis_t_listheader,

      wa_event TYPE slis_alv_event,
      wa_event_ekpo TYPE slis_alv_event,
      it_event TYPE slis_t_event,
      it_event_ekpo TYPE slis_t_event,

      r_ucomm    TYPE sy-ucomm,
      rs_selfield TYPE slis_selfield,
      v_selfield TYPE slis_selfield-value,
      v_ebeln TYPE ekko-ebeln,
      v_prog  TYPE sy-repid,
      v_name  TYPE sy-uname.

INITIALIZATION.
  v_prog sy-repid.
  v_name sy-uname.

  SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
  SELECT-OPTIONS   s_ebeln FOR ekko-ebeln OBLIGATORY.
  SELECTION-SCREEN END OF BLOCK b1.

START-OF-SELECTION.
  PERFORM get_ekko.
  PERFORM fieldcat_ekko.
  PERFORM layout.
  PERFORM event_ekko.
  PERFORM grid_ekko.
  PERFORM ucomm_ekko USING    r_ucomm
                     CHANGING rs_selfield.

*&---------------------------------------------------------------------*
*&      Form  GET_EKKO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_ekko .

  REFRESH it_ekko.
  SELECT ebeln bukrs lifnr
    FROM ekko INTO TABLE it_ekko
    WHERE ebeln IN s_ebeln.

  IF sy-subrc 0.
    SORT it_ekko BY ebeln.
    REFRESH it_out_ekko.

    LOOP AT it_ekko INTO wa_ekko.
      wa_out_ekko-ebeln wa_ekko-ebeln.
      wa_out_ekko-bukrs wa_ekko-bukrs.
      wa_out_ekko-lifnr wa_ekko-lifnr.
      APPEND wa_out_ekko TO it_out_ekko.
      CLEARwa_out_ekkowa_ekko.
    ENDLOOP.

  ELSE.
    MESSAGE 'Purchase Order doesn''t exist' TYPE 'I'.
  ENDIF.

ENDFORM.                    " GET_EKKO
*&---------------------------------------------------------------------*
*&      Form  FIELDCAT_EKKO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM fieldcat_ekko .

  CLEAR wa_fcat_ekko.
  REFRESH it_fcat_ekko.

  IF it_out_ekko IS NOT INITIAL.
    DATA lv_col TYPE VALUE 0.

    lv_col                 + lv_col.
    wa_fcat_ekko-col_pos   lv_col.
    wa_fcat_ekko-fieldname 'EBELN'.
    wa_fcat_ekko-tabname   'IT_OUT_EKKO'.
    wa_fcat_ekko-seltext_l 'Purchase Order'.
    APPEND wa_fcat_ekko TO it_fcat_ekko.
    CLEAR wa_fcat_ekko.

    lv_col                 + lv_col.
    wa_fcat_ekko-col_pos   lv_col.
    wa_fcat_ekko-fieldname 'BUKRS'.
    wa_fcat_ekko-tabname   'IT_OUT_EKKO'.
    wa_fcat_ekko-seltext_l 'Company Code'.
    APPEND wa_fcat_ekko TO it_fcat_ekko.
    CLEAR wa_fcat_ekko.

    lv_col                 + lv_col.
    wa_fcat_ekko-col_pos   lv_col.
    wa_fcat_ekko-fieldname 'LIFNR'.
    wa_fcat_ekko-tabname   'IT_OUT_EKKO'.
    wa_fcat_ekko-seltext_l 'Vendor'.
    APPEND wa_fcat_ekko TO it_fcat_ekko.
    CLEAR wa_fcat_ekko.
  ENDIF.

ENDFORM.                    " FIELDCAT_EKKO
*&---------------------------------------------------------------------*
*&      Form  LAYOUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM layout .

  wa_layout-zebra 'X'.
  wa_layout-colwidth_optimize 'X'.
  wa_layout-box_fieldname 'SEL'.

ENDFORM.                    " LAYOUT
*&---------------------------------------------------------------------*
*&      Form  EVENT_EKKO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM event_ekko .

  REFRESH it_event.

  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
*  EXPORTING
*    I_LIST_TYPE           = 0
   IMPORTING
     et_events             it_event
   EXCEPTIONS
     list_type_wrong       1
     OTHERS                2.

  IF it_event IS NOT INITIAL.
    CLEAR wa_event.
    READ TABLE it_event INTO wa_event
    WITH KEY name 'USER_COMMAND'(001).

    IF sy-subrc 0.
      wa_event-form 'UCOMM_EKKO'.
      MODIFY it_event FROM wa_event
      INDEX sy-tabix TRANSPORTING form.
    ENDIF.
  ENDIF.

ENDFORM.                    " EVENT_EKKO
*&---------------------------------------------------------------------*
*&      Form  GRID_EKKO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM grid_ekko .

  IF    it_out_ekko IS NOT INITIAL
    AND it_fcat_ekko IS NOT INITIAL.

    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
     EXPORTING
*       I_INTERFACE_CHECK                 = ' '
*       I_BYPASSING_BUFFER                = ' '
*       I_BUFFER_ACTIVE                   = ' '
        i_callback_program                v_prog
        i_callback_pf_status_set          'PF_STATUS'
        i_callback_user_command           'UCOMM_EKKO'
        i_callback_top_of_page            'TOP_EKKO'
*       I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
*       I_CALLBACK_HTML_END_OF_LIST       = ' '
*       I_STRUCTURE_NAME                  =
*       I_BACKGROUND_ID                   = ' '
*       I_GRID_TITLE                      =
*       I_GRID_SETTINGS                   =
        is_layout                         wa_layout
        it_fieldcat                       it_fcat_ekko
*       IT_EXCLUDING                      =
*       IT_SPECIAL_GROUPS                 =
*       IT_SORT                           =
*       IT_FILTER                         =
*       IS_SEL_HIDE                       =
*       I_DEFAULT                         = 'X'
*       I_SAVE                            = ' '
*       IS_VARIANT                        =
        it_events                         it_event
*       IT_EVENT_EXIT                     =
*       IS_PRINT                          =
*       IS_REPREP_ID                      =
*       I_SCREEN_START_COLUMN             = 0
*       I_SCREEN_START_LINE               = 0
*       I_SCREEN_END_COLUMN               = 0
*       I_SCREEN_END_LINE                 = 0
*       I_HTML_HEIGHT_TOP                 = 0
*       I_HTML_HEIGHT_END                 = 0
*       IT_ALV_GRAPHICS                   =
*       IT_HYPERLINK                      =
*       IT_ADD_FIELDCAT                   =
*       IT_EXCEPT_QINFO                   =
*       IR_SALV_FULLSCREEN_ADAPTER        =
*     IMPORTING
*       E_EXIT_CAUSED_BY_CALLER           =
*       ES_EXIT_CAUSED_BY_USER            =
      TABLES
        t_outtab                          it_out_ekko
      EXCEPTIONS
        program_error                     1
        OTHERS                            2.
  ENDIF.

ENDFORM.                    " GRID_EKKO

*&---------------------------------------------------------------------*
*&      Form  pf_status
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM pf_status USING ut_extab TYPE slis_t_extab.
  SET PF-STATUS 'PF_EKKO'.
ENDFORM.                    "pf_status

*&---------------------------------------------------------------------*
*&      Form  top_ekko
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM top_ekko.

  CLEAR wa_top_ekko.
  REFRESH it_top_ekko.

  DATA date TYPE char12.

  CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
    EXPORTING
      date_internal            sy-datum
    IMPORTING
      date_external            date
    EXCEPTIONS
      date_internal_is_invalid 1
      OTHERS                   2.

  wa_top_ekko-typ 'H'.
  wa_top_ekko-info 'Purchase Order Header'.
  APPEND wa_top_ekko TO it_top_ekko.
  CLEAR wa_top_ekko.

  wa_top_ekko-typ 'S'.
  wa_top_ekko-info 'Report: '.
  CONCATENATE wa_top_ekko-info v_prog
  INTO wa_top_ekko-info.
  APPEND wa_top_ekko TO it_top_ekko.
  CLEAR wa_top_ekko.

  wa_top_ekko-typ 'S'.
  wa_top_ekko-info 'User Name: '.
  CONCATENATE wa_top_ekko-info v_name
  INTO wa_top_ekko-info.
  APPEND wa_top_ekko TO it_top_ekko.
  CLEAR wa_top_ekko.

  wa_top_ekko-typ 'S'.
  wa_top_ekko-info 'Date: '.
  CONCATENATE wa_top_ekko-info date
  INTO wa_top_ekko-info.
  APPEND wa_top_ekko TO it_top_ekko.
  CLEAR wa_top_ekko.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary       it_top_ekko
*     I_LOGO                   =
*     I_END_OF_LIST_GRID       =
*     I_ALV_FORM               =
            .

ENDFORM.                    "top_ekko
*&---------------------------------------------------------------------*
*&      Form  UCOMM_EKKO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_R_UCOMM  text
*      <--P_RS_SELFIELD  text
*----------------------------------------------------------------------*
FORM ucomm_ekko  USING    r_ucomm_ekko     TYPE sy-ucomm
                 CHANGING rs_selfield_ekko TYPE slis_selfield.

  CASE r_ucomm_ekko.
    WHEN 'DISP'.
      REFRESHit_out_ekpoit_ebeln.
      LOOP AT it_out_ekko INTO wa_out_ekko
        WHERE sel 'X'.

        wa_ebeln-ebeln wa_out_ekko-ebeln.
        APPEND wa_ebeln TO it_ebeln.
        CLEAR wa_ebeln.
      ENDLOOP.

      PERFORM get_ekpo.
      PERFORM fieldcat_ekpo.
      PERFORM layout.
      PERFORM grid_ekpo.

    WHEN 'BACK' OR 'EXIT' OR 'CANCEL' OR 'E'.
      REFRESH it_out_ekko.
      LEAVE TO SCREEN 0.
  ENDCASE.

ENDFORM.                    " UCOMM_EKKO
*&---------------------------------------------------------------------*
*&      Form  GET_EKPO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_ekpo .

  IF it_ebeln IS NOT INITIAL.
    REFRESH it_ekpo.

    SELECT ebeln ebelp matnr werks
           lgort menge meins
      FROM ekpo INTO TABLE it_ekpo
      FOR ALL ENTRIES IN it_ebeln
      WHERE ebeln it_ebeln-ebeln.

    IF sy-subrc 0.
      SORT it_ekpo BY ebeln.

      LOOP AT it_ekpo INTO wa_ekpo.
        AT NEW ebeln.
          wa_out_ekpo-ebeln wa_ekpo-ebeln.
        ENDAT.
        wa_out_ekpo-ebelp wa_ekpo-ebelp.
        wa_out_ekpo-matnr wa_ekpo-matnr.
        wa_out_ekpo-werks wa_ekpo-werks.
        wa_out_ekpo-lgort wa_ekpo-lgort.
        wa_out_ekpo-menge wa_ekpo-menge.
        wa_out_ekpo-meins wa_ekpo-meins.
        APPEND wa_out_ekpo TO it_out_ekpo.
        CLEARwa_out_ekpowa_ekpo.
      ENDLOOP.
    ENDIF.
  ENDIF.

ENDFORM.                    " GET_EKPO
*&---------------------------------------------------------------------*
*&      Form  FIELDCAT_EKPO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM fieldcat_ekpo .

  CLEAR wa_fcat_ekpo.
  REFRESH it_fcat_ekpo.

  IF it_out_ekpo IS NOT INITIAL.
    DATA lv_col TYPE VALUE 0.

    lv_col                 + lv_col.
    wa_fcat_ekpo-col_pos   lv_col.
    wa_fcat_ekpo-fieldname 'EBELN'.
    wa_fcat_ekpo-tabname   'IT_OUT_EKPO'.
    wa_fcat_ekpo-seltext_l 'Purchase Order'.
    APPEND wa_fcat_ekpo TO it_fcat_ekpo.
    CLEAR wa_fcat_ekpo.

    lv_col                 + lv_col.
    wa_fcat_ekpo-col_pos   lv_col.
    wa_fcat_ekpo-fieldname 'EBELP'.
    wa_fcat_ekpo-tabname   'IT_OUT_EKPO'.
    wa_fcat_ekpo-seltext_l 'PO Item'.
    APPEND wa_fcat_ekpo TO it_fcat_ekpo.
    CLEAR wa_fcat_ekpo.

    lv_col                 + lv_col.
    wa_fcat_ekpo-col_pos   lv_col.
    wa_fcat_ekpo-fieldname 'MATNR'.
    wa_fcat_ekpo-tabname   'IT_OUT_EKPO'.
    wa_fcat_ekpo-seltext_l 'Material'.
    APPEND wa_fcat_ekpo TO it_fcat_ekpo.
    CLEAR wa_fcat_ekpo.

    lv_col                 + lv_col.
    wa_fcat_ekpo-col_pos   lv_col.
    wa_fcat_ekpo-fieldname 'WERKS'.
    wa_fcat_ekpo-tabname   'IT_OUT_EKPO'.
    wa_fcat_ekpo-seltext_l 'Plant'.
    APPEND wa_fcat_ekpo TO it_fcat_ekpo.
    CLEAR wa_fcat_ekpo.

    lv_col                 + lv_col.
    wa_fcat_ekpo-col_pos   lv_col.
    wa_fcat_ekpo-fieldname 'LGORT'.
    wa_fcat_ekpo-tabname   'IT_OUT_EKPO'.
    wa_fcat_ekpo-seltext_l 'Storage Location'.
    APPEND wa_fcat_ekpo TO it_fcat_ekpo.
    CLEAR wa_fcat_ekpo.

    lv_col                 + lv_col.
    wa_fcat_ekpo-col_pos   lv_col.
    wa_fcat_ekpo-fieldname 'MENGE'.
    wa_fcat_ekpo-tabname   'IT_OUT_EKPO'.
    wa_fcat_ekpo-seltext_l 'Quantity'.
    APPEND wa_fcat_ekpo TO it_fcat_ekpo.
    CLEAR wa_fcat_ekpo.

    lv_col                 + lv_col.
    wa_fcat_ekpo-col_pos   lv_col.
    wa_fcat_ekpo-fieldname 'MEINS'.
    wa_fcat_ekpo-tabname   'IT_OUT_EKPO'.
    wa_fcat_ekpo-seltext_l 'Unit'.
    APPEND wa_fcat_ekpo TO it_fcat_ekpo.
    CLEAR wa_fcat_ekpo.
  ENDIF.

ENDFORM.                    " FIELDCAT_EKPO
*&---------------------------------------------------------------------*
*&      Form  GRID_EKPO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM grid_ekpo .

  IF    it_out_ekpo IS NOT INITIAL
    AND it_fcat_ekpo IS NOT INITIAL.

    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
     EXPORTING
*     I_INTERFACE_CHECK                 = ' '
*     I_BYPASSING_BUFFER                = ' '
*     I_BUFFER_ACTIVE                   = ' '
      i_callback_program                v_prog
*     I_CALLBACK_PF_STATUS_SET          = ' '
*     I_CALLBACK_USER_COMMAND           = ' '
      i_callback_top_of_page            'TOP_EKPO'
*     I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
*     I_CALLBACK_HTML_END_OF_LIST       = ' '
*     I_STRUCTURE_NAME                  =
*     I_BACKGROUND_ID                   = ' '
*     I_GRID_TITLE                      =
*     I_GRID_SETTINGS                   =
      is_layout                         wa_layout
      it_fieldcat                       it_fcat_ekpo
*     IT_EXCLUDING                      =
*     IT_SPECIAL_GROUPS                 =
*     IT_SORT                           =
*     IT_FILTER                         =
*     IS_SEL_HIDE                       =
*     I_DEFAULT                         = 'X'
*     I_SAVE                            = ' '
*     IS_VARIANT                        =
*     IT_EVENTS                         =
*     IT_EVENT_EXIT                     =
*     IS_PRINT                          =
*     IS_REPREP_ID                      =
*     I_SCREEN_START_COLUMN             = 0
*     I_SCREEN_START_LINE               = 0
*     I_SCREEN_END_COLUMN               = 0
*     I_SCREEN_END_LINE                 = 0
*     I_HTML_HEIGHT_TOP                 = 0
*     I_HTML_HEIGHT_END                 = 0
*     IT_ALV_GRAPHICS                   =
*     IT_HYPERLINK                      =
*     IT_ADD_FIELDCAT                   =
*     IT_EXCEPT_QINFO                   =
*     IR_SALV_FULLSCREEN_ADAPTER        =
*   IMPORTING
*     E_EXIT_CAUSED_BY_CALLER           =
*     ES_EXIT_CAUSED_BY_USER            =
    TABLES
      t_outtab                          it_out_ekpo
    EXCEPTIONS
      program_error                     1
      OTHERS                            2.
  ENDIF.

ENDFORM.                    " GRID_EKPO
*&---------------------------------------------------------------------*
*&      Form  top_ekpo
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM top_ekpo.

  CLEAR wa_top_ekpo.
  REFRESH it_top_ekpo.

  wa_top_ekpo-typ 'H'.
  wa_top_ekpo-info 'Purchase Order Item Display'.
  APPEND wa_top_ekpo TO it_top_ekpo.
  CLEAR wa_top_ekpo.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary       it_top_ekpo
*     I_LOGO                   =
*     I_END_OF_LIST_GRID       =
*     I_ALV_FORM               =
            .

ENDFORM.                    "top_ekpo