Sunday, September 8, 2013

Nesting ALV Report

ABAP code:

report ZNESTINGALVRPT .

tables: ekko, ekpo.
type-pools: slis.

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.

data: wa_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,
      it_out_ekko type standard table of ty_out_ekko,
      it_out_ekpo type standard table of ty_out_ekpo,

      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_ekko type slis_alv_event,
      wa_event_ekpo type slis_alv_event,
      it_event_ekko 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.

top-of-page.
  perform top_ekko.

top-of-page during line-selection.
  perform top_ekpo.

*&---------------------------------------------------------------------*
*&      Form  ucomm_ekko
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->R_UCOMM      text
*      -->RS_ESLFIELD  text
*----------------------------------------------------------------------*
form ucomm_ekko using    r_ucomm_ekko     type sy-ucomm
                changing rs_selfield_ekko type slis_selfield.

  case r_ucomm_ekko.
    when '&IC1'.
      if rs_selfield_ekko-fieldname = 'EBELN'.
        clear v_selfield.
        v_selfield = rs_selfield_ekko-value.
        perform conversion_po.
        perform get_ekpo.
        perform fieldcat_ekpo.
        perform layout.
        perform event_ekpo.
        perform grid_ekpo.
        perform ucomm_ekpo using r_ucomm
                           changing rs_selfield.
      else.
        message 'Invalid Field' type 'S'.
      endif.
  endcase.
endform.                    "ucomm_ekko
*&---------------------------------------------------------------------*
*&      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.
      clear: wa_out_ekko, wa_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 i value 0.

    lv_col                 = 1 + 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                 = 1 + 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                 = 1 + 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_ekko.

  call function 'REUSE_ALV_EVENTS_GET'
*  EXPORTING
*    I_LIST_TYPE           = 0
   importing
     et_events             = it_event_ekko
   exceptions
     list_type_wrong       = 1
     others                = 2.

  if it_event_ekko is not initial.
    clear wa_event_ekko.
    read table it_event_ekko into wa_event_ekko
    with key name = 'USER_COMMAND'.

    if sy-subrc = 0.
      wa_event_ekko-form = 'UCOMM_EKKO'.
      modify it_event_ekko from wa_event_ekko
      index sy-tabix transporting form.
    endif.

    clear wa_event_ekko.
    read table it_event_ekko into wa_event_ekko
    with key name = 'TOP_OF_PAGE'.

    if sy-subrc = 0.
      wa_event_ekko-form = 'TOP_EKKO'.
      modify it_event_ekko from wa_event_ekko
      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_LIST_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        = 'UCOMM_EKKO'
*      I_STRUCTURE_NAME               =
       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_ekko
*      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
*      IR_SALV_LIST_ADAPTER           =
*      IT_EXCEPT_QINFO                =
*      I_SUPPRESS_EMPTY_DATA          = ABAP_FALSE
*    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  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  CONVERSION_PO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form conversion_po .

  clear v_ebeln.

  call function 'CONVERSION_EXIT_ALPHA_INPUT'
    exporting
      input  = v_selfield
    importing
      output = v_ebeln.

endform.                    " CONVERSION_PO
*&---------------------------------------------------------------------*
*&      Form  GET_EKPO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_ekpo .

  if v_ebeln is not initial.
    refresh it_ekpo.
    select ebeln ebelp matnr werks lgort menge meins
      from ekpo into table it_ekpo
      where ebeln = v_ebeln.

    if sy-subrc = 0.
      sort it_ekpo by ebelp.
      refresh it_out_ekpo.

      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.
        clear: wa_out_ekpo, wa_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 i value 0.

    lv_col                 = 1 + 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                 = 1 + 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                 = 1 + 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                 = 1 + 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                 = 1 + 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                 = 1 + 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 = 'PO Quantity'.
    append wa_fcat_ekpo to it_fcat_ekpo.
    clear wa_fcat_ekpo.

    lv_col                 = 1 + 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 of Measure'.
    append wa_fcat_ekpo to it_fcat_ekpo.
    clear wa_fcat_ekpo.
  endif.

endform.                    " FIELDCAT_EKPO
*&---------------------------------------------------------------------*
*&      Form  EVENT_EKPO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form event_ekpo .

  refresh it_event_ekpo.

  call function 'REUSE_ALV_EVENTS_GET'
* EXPORTING
*   I_LIST_TYPE           = 0
   importing
     et_events             = it_event_ekpo
   exceptions
     list_type_wrong       = 1
     others                = 2.

  if it_event_ekpo is not initial.
    clear wa_event_ekpo.
    read table it_event_ekpo into wa_event_ekpo
    with key name = 'USER_COMMAND'.

    if sy-subrc = 0.
      wa_event_ekpo-form = 'UCOMM_EKPO'.
      modify it_event_ekpo from wa_event_ekpo
      index sy-tabix transporting form.
    endif.

    clear wa_event_ekpo.
    read table it_event_ekpo into wa_event_ekpo
    with key name = 'TOP_OF_PAGE'.

    if sy-subrc = 0.
      wa_event_ekpo-form = 'TOP_EKPO'.
      modify it_event_ekpo from wa_event_ekpo
      index sy-tabix transporting form.
    endif.
  endif.

endform.                    " EVENT_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_LIST_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        = 'UCOMM_EKPO'
*      I_STRUCTURE_NAME               =
       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_ekpo
*      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
*      IR_SALV_LIST_ADAPTER           =
*      IT_EXCEPT_QINFO                =
*      I_SUPPRESS_EMPTY_DATA          = ABAP_FALSE
*    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 wise 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
*&---------------------------------------------------------------------*
*&      Form  UCOMM_EKPO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_R_UCOM  text
*      <--P_RS_SELFIELD  text
*----------------------------------------------------------------------*
form ucomm_ekpo  using    r_ucomm_ekpo      type sy-ucomm
                 changing rs_selfield_ekpo  type slis_selfield.

  case r_ucomm_ekpo.
    when '&IC1'.
      if rs_selfield_ekpo-fieldname = 'EBELN'.

        set parameter id 'BES' field v_ebeln.
        call transaction 'ME23N'.

      else.
        message 'Invalid Field' type 'S'.
      endif.
  endcase.


endform.                    " UCOMM_EKPO



OUTPUT: 



                                        Run the program. Get the output like below


   Select and double click on PO number. You will get the details of the selected PO number.