You are on page 1of 38

Classic Report of Multiple Tables

We have created a program which contains multiple tables like MARA, MARC and MARD. The materials
contain different Plant and Storage Location in MARC and MARD tables respectively. All those different
plant and storage location will be displayed with material number in output here.
In the program we have prepared internal table it_mara from MARA table based on the Select Option
material number range. If material number is entered then only the program will give an output.
After preparing valid information of it_mara the program selects data from MARC table into it_marc for
all entries in it_mara. Here we have to check the it_mara table if it is not initial. If we don't give this
checking then all records will be selected from MARC table and that would be wrong. Hence we can say
that this table checking is one of a prerequisites of For All Entries In statement.
Similarly after preparing the it_marc table we shall prepare the it_mard table from MARD for all entries
in it_marc. Similarly the table checking of it_marc must be there.
Now after getting all these plant and storage location information we shall loop into it_mara to make
the output table since MARA is a master table. Now one material can be stored different plants and
storage locations. So to populate output table we have to loop into it_marc and it_mard table. Here we
will loop into it_marc and it_mard with WHERE clause because we are going to fetch all records of plant
and storage location at one time. So where clause will help us to point out the particular material and
also will increase the performance.
Finally at the output population we have used control break statement like AT FIRST, AT END OF, AT
LAST. With the help of that we have synchronized the output in different lines.

Following is the coding of the classical report.


*&---------------------------------------------------------------------*
*& Report ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zsr_test NO STANDARD PAGE HEADING.
TABLES: mara, marc, mard.
TYPES: BEGIN OF ty_mara,
matnr TYPE mara-matnr,

mtart TYPE mara-mtart,


END OF ty_mara,
BEGIN OF ty_marc,
matnr TYPE marc-matnr,
werks TYPE marc-werks,
xchar TYPE marc-xchar,
END OF ty_marc,
BEGIN OF ty_mard,
matnr TYPE mard-matnr,
werks TYPE mard-werks,
lgort TYPE mard-lgort,
pstat TYPE mard-pstat,
END OF ty_mard,
BEGIN OF ty_out,
matnr TYPE marc-matnr,
werks TYPE marc-werks,
lgort TYPE mard-lgort,
mtart TYPE mara-mtart,
xchar TYPE marc-xchar,
pstat TYPE mard-pstat,
END OF ty_out.
DATA: wa_mara TYPE ty_mara,
wa_marc TYPE ty_marc,
wa_mard TYPE ty_mard,
wa_out TYPE ty_out,
it_mara TYPE STANDARD TABLE OF ty_mara,
it_marc TYPE STANDARD TABLE OF ty_marc,
it_mard TYPE STANDARD TABLE OF ty_mard,
it_out TYPE STANDARD TABLE OF ty_out,
v_prog TYPE sy-repid,
v_date TYPE sy-datum,
v_time TYPE sy-uzeit.
CONSTANTS: c_material TYPE char12 VALUE 'MATERIAL NO',
c_plant TYPE char5 VALUE 'PLANT',
c_storage TYPE char8 VALUE 'STORAGE',
c_type TYPE char6 VALUE 'M TYPE',

c_batch TYPE char6 VALUE 'BATCH',


c_maint TYPE char18 VALUE 'MAINTENANCE STATUS',
c_end TYPE char40 VALUE 'End of Material Details'.
INITIALIZATION.
v_prog = sy-repid.
v_date = sy-datum.
v_time = sy-uzeit.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
SELECT-OPTIONS: s_matnr FOR mara-matnr.
SELECTION-SCREEN END OF BLOCK b1.
START-OF-SELECTION.
PERFORM get_mara.
PERFORM get_marc.
PERFORM get_mard.
END-OF-SELECTION.
PERFORM get_output.
PERFORM display.
TOP-OF-PAGE.
PERFORM top_of_page.
*&---------------------------------------------------------------------*
*& Form GET_MARA
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM get_mara .
IF s_matnr IS NOT INITIAL.
SELECT matnr mtart
FROM mara INTO TABLE it_mara
WHERE matnr IN s_matnr.
IF sy-subrc = 0.
SORT it_mara BY matnr.

ELSE.
MESSAGE 'Material doesn''t exist' TYPE 'I'.
ENDIF.
ENDIF.
ENDFORM.
" GET_MARA
*&---------------------------------------------------------------------*
*& Form GET_MARC
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM get_marc .
IF it_mara IS NOT INITIAL.
SELECT matnr werks xchar
FROM marc INTO TABLE it_marc
FOR ALL ENTRIES IN it_mara
WHERE matnr = it_mara-matnr.
IF sy-subrc = 0.
SORT it_marc BY matnr.
ELSE.
MESSAGE 'Plant doesn''t exist' TYPE 'I'.
ENDIF.
ENDIF.
ENDFORM.
" GET_MARC
*&---------------------------------------------------------------------*
*& Form GET_MARD
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM get_mard .
IF it_marc IS NOT INITIAL.
SELECT matnr werks lgort pstat

FROM mard INTO TABLE it_mard


FOR ALL ENTRIES IN it_marc
WHERE matnr = it_marc-matnr
AND werks = it_marc-werks.
IF sy-subrc = 0.
SORT it_mard BY matnr.
ELSE.
MESSAGE 'Storage Location doesn''t exist' TYPE 'I'.
ENDIF.
ENDIF.
ENDFORM.
" GET_MARD
*&---------------------------------------------------------------------*
*& Form GET_OUTPUT
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM get_output .
IF it_mara IS NOT INITIAL.
LOOP AT it_mara INTO wa_mara.
wa_out-matnr = wa_mara-matnr.
wa_out-mtart = wa_mara-mtart.
LOOP AT it_marc INTO wa_marc
WHERE matnr = wa_mara-matnr.
wa_out-werks = wa_marc-werks.
wa_out-xchar = wa_marc-xchar.
LOOP AT it_mard INTO wa_mard
WHERE matnr = wa_marc-matnr
AND werks = wa_marc-werks.
wa_out-lgort = wa_mard-lgort.
wa_out-pstat = wa_mard-pstat.
APPEND wa_out TO it_out.
CLEAR: wa_out, wa_mara, wa_marc, wa_mard.
ENDLOOP.

ENDLOOP.
ENDLOOP.
ENDIF.
ENDFORM.
" GET_OUTPUT
*&---------------------------------------------------------------------*
*& Form DISPLAY
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM display .
IF it_out IS NOT INITIAL.
LOOP AT it_out INTO wa_out.
AT FIRST.
WRITE: / c_material,
21 c_plant,
27 c_storage,
37 c_type,
45 c_batch,
54 c_maint.
ULINE.
SKIP.
ENDAT.
WRITE: / wa_out-matnr,
21 wa_out-werks,
27 wa_out-lgort,
37 wa_out-mtart,
45 wa_out-xchar,
54 wa_out-pstat.
IF wa_out-matnr IS INITIAL.
AT END OF matnr.
SKIP.
ENDAT.
ENDIF.
AT LAST.

ULINE.
WRITE: / c_end.
ENDAT.
ENDLOOP.
ENDIF.
ENDFORM.
" DISPLAY
*&---------------------------------------------------------------------*
*& Form TOP_OF_PAGE
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM top_of_page .
WRITE: / v_prog,
/ v_date DD/MM/YYYY,
/ v_time.
ULINE.
ENDFORM.

Output:
1. Selection Screen -

2. Classical Display -

" TOP_OF_PAGE

Classic Interactive Report


Interactive program generally interacts with user on the output screen. Let us suppose we have an
output for a particular program/report. Now if we want details description by double clicking on a
particular field or clicking on a button then it will be called interaction with output. By doing this we can
go to the detailed report or secondary output. When we are on the secondary output then we can back
to our first out by clicking on Back button. We can generate 19 secondary list of output. Hence a total
of 20 output screen (1 Primary + 19 Secondary) can be generated by Interactive program.

We have a requirement where we have to prepare a primary output containing Purchase Order,
Company Code, Category, Type and Vendor with PO Info. The secondary list will be generated with PO
Item, Material, Plant, Storage Location, Material Group, Quantity and Unit of measure. The secondary
list will be generated if we double click on PO field in primary output.
We have mentioned the tables EKKO, EKPO and T161T. Now we have declared the structure types of
internal tables of it_ekko, it_text, it_out1 and it_ekpo. Here it_out1 contains the combination of two
internal tables it_ekko and it_text and it_ekpo is for the secondary output. For internal operation we
have declared work area of every internal tables.
Next we have mentioned the event INITIALIZATION. Under this we are calling program name, user name
and system date of the program. We have declared here selection screen begin with block b1 and under
this we are mentioning obligatory select option.
Now under START-OF-SELECTION we are declaring all the operations for primary listing. We have made
4 subroutines. In get_ekko we select fields from EKKO into internal table it_ekko. Then in get_t161t we
select fields from t161t into internal table it_text for all entries in it_ekko. After that we are preparing
the primary output in the basic_output subroutine. Now we have made another subroutine for
displaying the primary output and that is disp_basic.
Hence the primary list is prepared now and now we have to create the secondary list. To make the
secondary list we are calling event AT LINE-SELECTION which raises functionality when user double clicks
in any field. Now we are mentioning GET CURSOR for that field and for that value of the field. When
these two are matches with Purchase Order (EBELN) field then we are calling two subroutines for
secondary list.
Subroutine get_ekpo Selects fields from EKPO into table it_ekpo for all entries in it_ekko. Then it
prepares the secondary output display in the subroutine of ekpo_output.
We also have raised events TOP-OF-PAGE which shows the header list of primary output and TOP-OFPAGE DURING LINE-SELECTION which shows the top list of secondary output. Here TOP-OF-PAGE
DURING LINE-SELECTION is used only for interactive reports. That means when user double clicks on the
field the TOP-OF-PAGE DURING LINE-SELECTION event triggers to make the secondary output header.

*&---------------------------------------------------------------------*
*& Report ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zsr_test.

TABLES: ekko, ekpo, t161t.


TYPES: BEGIN OF ty_ekko,
ebeln TYPE ekko-ebeln, "Purchase Order
bukrs TYPE ekko-bukrs, "Company Code
bstyp TYPE ekko-bstyp, "Category
bsart TYPE ekko-bsart, "Type
lifnr TYPE ekko-lifnr, "Vendor
END OF ty_ekko,
BEGIN OF ty_text,
spras TYPE t161t-spras,
bsart TYPE t161t-bsart,
bstyp TYPE t161t-bstyp,
batxt TYPE t161t-batxt, "PO Info
END OF ty_text,
BEGIN OF ty_ekpo,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp, "PO Item
matnr TYPE ekpo-matnr, "Material
werks TYPE ekpo-werks, "Plant
lgort TYPE ekpo-lgort, "Storage Location
matkl TYPE ekpo-matkl, "Material Group
menge TYPE ekpo-menge, "Quantity
meins TYPE ekpo-meins, "Unit
END OF ty_ekpo,
BEGIN OF ty_out1,
ebeln TYPE ekko-ebeln,
bukrs TYPE ekko-bukrs,
bstyp TYPE ekko-bstyp,
bsart TYPE ekko-bsart,
lifnr TYPE ekko-lifnr,
batxt TYPE t161t-batxt,
END OF ty_out1.
DATA: wa_ekko TYPE ty_ekko,
it_ekko TYPE STANDARD TABLE OF ty_ekko,
wa_text TYPE ty_text,
it_text TYPE STANDARD TABLE OF ty_text,
wa_out1 TYPE ty_out1,

it_out1 TYPE STANDARD TABLE OF ty_out1,


wa_ekpo TYPE ty_ekpo,
it_ekpo TYPE STANDARD TABLE OF ty_ekpo,
v_repid TYPE sy-repid,
v_user TYPE sy-uname,
v_date TYPE sy-datum,
v_field1 TYPE char40,
v_field2 TYPE char40,
v_value1 TYPE char40,
v_value2 TYPE char40.
INITIALIZATION.
v_repid = sy-repid.
v_user = sy-uname.
v_date = sy-datum.
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 get_t161t.
PERFORM basic_output.
PERFORM disp_basic.
AT LINE-SELECTION.
GET CURSOR FIELD v_field1 VALUE v_value1.
CASE v_field1.
WHEN 'WA_OUT1-EBELN'.
PERFORM get_ekpo.
PERFORM ekpo_output.
ENDCASE.
GET CURSOR FIELD v_field2 VALUE v_value2.
CASE v_field2.
WHEN 'WA_EKPO-MATNR'.
PERFORM get_mara.
ENDCASE.

TOP-OF-PAGE.
PERFORM top_page1.
TOP-OF-PAGE DURING LINE-SELECTION.
PERFORM top_page2.
*&---------------------------------------------------------------------*
*& Form get_ekko
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM get_ekko .
SELECT ebeln bukrs bstyp bsart lifnr
FROM ekko INTO TABLE it_ekko
WHERE ebeln IN s_ebeln.
IF sy-subrc = 0.
SORT it_ekko BY ebeln.
ELSE.
MESSAGE 'Purchase Order doesn''t exist.' TYPE 'I'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM.
" get_ekko
*&---------------------------------------------------------------------*
*& Form get_t161t
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM get_t161t .
IF it_ekko IS NOT INITIAL.
SELECT spras bsart bstyp batxt
FROM t161t INTO TABLE it_text
FOR ALL ENTRIES IN it_ekko

WHERE spras = sy-langu


AND bsart = it_ekko-bsart
AND bstyp = it_ekko-bstyp.
IF sy-subrc = 0.
SORT it_text BY bsart bstyp.
ENDIF.
ENDIF.
ENDFORM.
" get_t161t
*&---------------------------------------------------------------------*
*& Form basic_output
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM basic_output .
IF it_ekko IS NOT INITIAL.
LOOP AT it_ekko INTO wa_ekko.
wa_out1-ebeln = wa_ekko-ebeln.
wa_out1-bukrs = wa_ekko-bukrs.
wa_out1-bstyp = wa_ekko-bstyp.
wa_out1-bsart = wa_ekko-bsart.
wa_out1-lifnr = wa_ekko-lifnr.
READ TABLE it_text INTO wa_text
WITH KEY bsart = wa_ekko-bsart
bstyp = wa_ekko-bstyp BINARY SEARCH.
IF sy-subrc = 0.
wa_out1-batxt = wa_text-batxt.
ENDIF.
APPEND wa_out1 TO it_out1.
CLEAR: wa_out1, wa_ekko, wa_text.
ENDLOOP.
ENDIF.
ENDFORM.
" basic_output
*&---------------------------------------------------------------------*

*& Form disp_basic


*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM disp_basic .
IF it_out1 IS NOT INITIAL.
LOOP AT it_out1 INTO wa_out1.
AT FIRST.
WRITE: / 'Purchase Order',
20 'Company',
30 'Category',
40 'Type',
50 'Vendor',
65 'PO Info.'.
ULINE.
SKIP.
ENDAT.
WRITE: / wa_out1-ebeln,
20 wa_out1-bukrs,
33 wa_out1-bstyp,
40 wa_out1-bsart,
50 wa_out1-lifnr,
65 wa_out1-batxt.
AT LAST.
SKIP.
ULINE.
WRITE: /12 '~~End of Report~~'.
ENDAT.
ENDLOOP.
ENDIF.
ENDFORM.
" disp_basic
*&---------------------------------------------------------------------*
*& Form get_ekpo
*&---------------------------------------------------------------------*
*
text

*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM get_ekpo .
DATA: lv_ebeln TYPE ekko-ebeln.
IF v_value1 IS NOT INITIAL.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = v_value1
IMPORTING
output = lv_ebeln.
IF lv_ebeln IS NOT INITIAL.
SELECT ebeln ebelp matnr werks lgort
matkl menge meins
FROM ekpo INTO TABLE it_ekpo
WHERE ebeln = lv_ebeln.
IF sy-subrc <> 0.
MESSAGE 'PO Item doesn''t Exist.' TYPE 'I'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDIF.
ENDIF.
ENDFORM.
" get_ekpo
*&---------------------------------------------------------------------*
*& Form ekpo_output
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM ekpo_output .
IF it_ekpo IS NOT INITIAL.
LOOP AT it_ekpo INTO wa_ekpo.

AT FIRST.
WRITE: / 'Purchase Order',
20 'PO Item',
30 'Material',
48 'Plant',
55 'Storage',
65 'Material Group',
83 'PO Quantity',
100 'Unit'.
ULINE.
SKIP.
ENDAT.
WRITE: / wa_ekpo-ebeln,
20 wa_ekpo-ebelp,
30 wa_ekpo-matnr,
48 wa_ekpo-werks,
55 wa_ekpo-lgort,
70 wa_ekpo-matkl,
75 wa_ekpo-menge,
100 wa_ekpo-meins.
AT LAST.
SKIP.
ULINE.
WRITE: /12 '~~End of PO Item~~'.
ENDAT.
ENDLOOP.
ENDIF.
ENDFORM.
" ekpo_output
*&---------------------------------------------------------------------*
*& Form get_mara
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM get_mara .

DATA: lv_matnr TYPE mara-matnr.


IF v_value2 IS NOT INITIAL.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
EXPORTING
input
= v_value2
IMPORTING
output
= lv_matnr
EXCEPTIONS
length_error = 1
OTHERS
= 2.
IF lv_matnr IS NOT INITIAL.
SET PARAMETER ID 'MAT' FIELD lv_matnr.
CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN.
ENDIF.
ENDIF.
ENDFORM.
" get_mara
*&---------------------------------------------------------------------*
*& Form top_page1
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM top_page1 .
WRITE: / 'Purchase Order Header',
/ 'Date: ', 12 v_date DD/MM/YYYY,
/ 'User: ', 12 v_user,
/ 'Report: ', 12 v_repid.
ULINE.
SKIP.
ENDFORM.
" top_page1
*&---------------------------------------------------------------------*
*& Form top_page2
*&---------------------------------------------------------------------*
*
text

*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM top_page2 .
WRITE: / 'Purchase Order Item List',
/ 'Date: ', 12 v_date DD/MM/YYYY,
/ 'User: ', 12 v_user,
/ 'Report: ', 12 v_repid.
ULINE.
SKIP.
ENDFORM.
Below is the output:

Primary Listing:

" top_page2

If we double click on the PO in this list then following will be generated:

ALV List Interactive Report


Here is an interactive report program which displays the basic output (First Screen) for Purchase Order
header information. Then by double clicking the PO number we can go to secondary output which is the
item display of that PO. On the Item screen if we double click on PO then ME23N Transaction will open
with that particular PO.
In this reports User Command comes to fetch the double click operation. The output is in ALV List
format.
*&---------------------------------------------------------------------*
*& Report ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zsr_test.
TABLES: ekko, ekpo.
TYPE-POOLS: slis.
TYPES: BEGIN 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.
The output is follows:
Selection Screen:

" UCOMM_EKPO

First List (11 PO Headers):

Second List (by Double clicking on PO 14):

Third Output:

You might also like