This report program can be used to display sales order for the given customers Using ALV Tree display (3 Levels).
Function Module Used are :
'RS_TREE_CONSTRUCT'
'RS_TREE_LIST_DISPLAY'
SAMPLE PROGRAM
REPORT ztest_alv_tree.
TABLES : kna1.
TYPES : BEGIN OF ts_vbak,
vbeln TYPE vbak-vbeln, "Sales Order
audat TYPE vbak-audat, "Order Date
kunnr TYPE vbak-kunnr, "Customer No.
END OF ts_vbak,
BEGIN OF ts_vbap,
vbeln TYPE vbap-vbeln,
posnr TYPE vbap-posnr, "Order Item
kwmeng TYPE vbap-kwmeng, "Net weight
netpr TYPE vbap-netpr, "Item Price
END OF ts_vbap,
BEGIN OF ts_kna1,
kunnr TYPE kna1-kunnr,
name1 TYPE kna1-name1, "Customer Name
END OF ts_kna1.
DATA : lt_node TYPE TABLE OF snodetext,
lt_vbak TYPE TABLE OF ts_vbak,
lt_vbap TYPE TABLE OF ts_vbap,
lt_kna1 TYPE TABLE OF ts_kna1.
* WORKAREA DECLARATION
DATA : ls_node TYPE snodetext,
ls_vbak TYPE ts_vbak,
ls_vbap TYPE ts_vbap,
ls_kna1 TYPE ts_kna1.
* VARIABLE DECLARATION
DATA : w_value(15) TYPE C,
w_qty TYPE kwmeng,
w_amt TYPE netpr.
* SELECTION SCREEN
SELECT-OPTIONS : s_kunnr FOR kna1-kunnr.
START-OF-SELECTION.
PERFORM get_kna1.
PERFORM get_vbak.
PERFORM get_vbap.
PERFORM build_tree.
END-OF-SELECTION.
PERFORM display_tree.
*&---------------------------------------------------------------------*
*& Form get_vbak
*&---------------------------------------------------------------------
FORM get_vbak .
SELECT vbeln
audat
kunnr
FROM vbak
INTO TABLE lt_vbak
FOR ALL ENTRIES IN lt_kna1
WHERE kunnr EQ lt_kna1-kunnr.
DATA:v_line TYPE i.
v_line = LINES( lt_vbak ).
IF LINES( lt_vbak ) EQ 0.
MESSAGE 'No Orders found'(001) TYPE 'I'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM. " get_vbak
*&---------------------------------------------------------------------*
*& Form build_tree
*&---------------------------------------------------------------------
FORM build_tree .
CLEAR : lt_node, ls_node.
SORT : lt_vbak BY kunnr vbeln,
lt_vbap BY vbeln posnr,
lt_kna1 BY kunnr.
ls_node-type = 'T'.
ls_node-name = 'Orders of Customers'(002).
ls_node-tlevel = '01'.
ls_node-nlength = '20'.
ls_node-color = '5'.
ls_node-text = 'Orders'(003).
ls_node-tlength = '10'.
ls_node-tcolor = '3'.
APPEND ls_node TO lt_node.
CLEAR ls_node.
ls_node-type = 'P'.
ls_node-tlevel = '2'.
ls_node-text = 'Customer No'(004).
ls_node-tlength = '15'.
ls_node-tcolor = '7'.
ls_node-text1 = 'Customer Name'(005).
ls_node-tlength1 = '30'.
ls_node-tcolor1 = '7'.
APPEND ls_node TO lt_node.
CLEAR ls_node.
LOOP AT lt_kna1 INTO ls_kna1.
DATA : w_flg TYPE c,
w_flg1 TYPE c.
ls_node-type = 'P'.
ls_node-tlevel = '2'.
ls_node-text = ls_kna1-kunnr.
ls_node-tlength = '15'.
ls_node-tcolor = '1'.
ls_node-text1 = ls_kna1-name1.
ls_node-tlength1 = '30'.
ls_node-tcolor1 = '1'.
APPEND ls_node TO lt_node.
CLEAR ls_node.
LOOP AT lt_vbak INTO ls_vbak WHERE kunnr EQ ls_kna1-kunnr.
IF w_flg1 IS INITIAL.
w_flg1 = 'X'.
ls_node-type = 'P'.
ls_node-tlevel = '3'.
ls_node-text = 'Sales Order'(006).
ls_node-tlength = '15'.
ls_node-tcolor = '7'.
ls_node-text1 = 'Order Date'(007).
ls_node-tlength1 = '15'.
ls_node-tcolor1 = '7'.
APPEND ls_node TO lt_node.
CLEAR ls_node.
ENDIF.
ls_node-type = 'P'.
ls_node-tlevel = '3'.
ls_node-text = ls_vbak-vbeln.
ls_node-tlength = 15.
ls_node-tcolor = 5.
CLEAR w_value.
WRITE ls_vbak-audat TO w_value.
ls_node-text1 = w_value.
ls_node-tlength1 = 15.
ls_node-tcolor1 = 5.
APPEND ls_node TO lt_node.
CLEAR : w_qty, w_amt.
LOOP AT lt_vbap INTO ls_vbap WHERE vbeln EQ ls_vbak-vbeln.
IF w_flg IS INITIAL.
w_flg = 'X'.
ls_node-type = 'P'.
ls_node-tlevel = '4'.
ls_node-text = 'Item'(008).
ls_node-tlength = '11'.
ls_node-tcolor = '7'.
ls_node-text1 = 'Net Weight'(009).
ls_node-tlength1 = '15'.
ls_node-tcolor1 = '7'.
ls_node-text2 = 'Net Price'(010).
ls_node-tlength2 = '15'.
ls_node-tcolor2 = '7'.
APPEND ls_node TO lt_node.
CLEAR ls_node.
ENDIF.
ls_node-type = 'P'.
ls_node-tlevel = '4'.
ls_node-text = ls_vbap-posnr.
ls_node-tlength = '11'.
ls_node-tcolor = '2'.
CLEAR w_value.
w_value = ls_vbap-kwmeng.
ls_node-text1 = w_value.
ls_node-tlength1 = '15'.
ls_node-tcolor1 = '2'.
CLEAR w_value.
w_value = ls_vbap-netpr.
ls_node-text2 = w_value.
ls_node-tlength2 = '15'.
ls_node-tcolor2 = '2'.
APPEND ls_node TO lt_node.
CLEAR ls_node.
ADD ls_vbap-kwmeng TO w_qty.
ADD ls_vbap-netpr TO w_amt.
ENDLOOP.
IF w_qty IS NOT INITIAL.
ls_node-type = 'P'.
ls_node-tlevel = '4'.
ls_node-text = 'Total'(011).
ls_node-tlength = '11'.
ls_node-tcolor = '3'.
CLEAR w_value.
w_value = w_qty.
ls_node-text1 = w_value.
ls_node-tlength1 = '15'.
ls_node-tcolor1 = '3'.
CLEAR w_value.
w_value = w_amt.
ls_node-text2 = w_value.
ls_node-tlength2 = '15'.
ls_node-tcolor2 = '3'.
APPEND ls_node TO lt_node.
CLEAR ls_node.
ENDIF.
CLEAR w_flg.
ENDLOOP.
ENDLOOP.
ENDFORM. " build_tree
*&---------------------------------------------------------------------*
*& Form display_tree
*&---------------------------------------------------------------------
FORM display_tree .
CALL FUNCTION 'RS_TREE_CONSTRUCT'
TABLES
nodetab = lt_node
EXCEPTIONS
tree_failure = 1
id_not_found = 2
wrong_relationship = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
CALL FUNCTION 'RS_TREE_LIST_DISPLAY'
EXPORTING
callback_program = sy-repid.
ENDFORM. " display_tree
*&---------------------------------------------------------------------*
*& Form get_vbap
*&---------------------------------------------------------------------
FORM get_vbap .
SELECT vbeln
posnr
kwmeng
netpr
FROM vbap
INTO TABLE lt_vbap
FOR ALL ENTRIES IN lt_vbak
WHERE vbeln EQ lt_vbak-vbeln.
ENDFORM. " get_vbap
*&---------------------------------------------------------------------*
*& Form get_kna1
*&---------------------------------------------------------------------
FORM get_kna1 .
SELECT kunnr
name1
FROM kna1
INTO TABLE lt_kna1
WHERE kunnr IN s_kunnr.
IF sy-subrc NE 0.
MESSAGE 'No customers exists'(012) TYPE 'I'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM. " get_kna1
OUTPUT SCREEN:
Function Module Used are :
'RS_TREE_CONSTRUCT'
'RS_TREE_LIST_DISPLAY'
SAMPLE PROGRAM
REPORT ztest_alv_tree.
TABLES : kna1.
TYPES : BEGIN OF ts_vbak,
vbeln TYPE vbak-vbeln, "Sales Order
audat TYPE vbak-audat, "Order Date
kunnr TYPE vbak-kunnr, "Customer No.
END OF ts_vbak,
BEGIN OF ts_vbap,
vbeln TYPE vbap-vbeln,
posnr TYPE vbap-posnr, "Order Item
kwmeng TYPE vbap-kwmeng, "Net weight
netpr TYPE vbap-netpr, "Item Price
END OF ts_vbap,
BEGIN OF ts_kna1,
kunnr TYPE kna1-kunnr,
name1 TYPE kna1-name1, "Customer Name
END OF ts_kna1.
DATA : lt_node TYPE TABLE OF snodetext,
lt_vbak TYPE TABLE OF ts_vbak,
lt_vbap TYPE TABLE OF ts_vbap,
lt_kna1 TYPE TABLE OF ts_kna1.
* WORKAREA DECLARATION
DATA : ls_node TYPE snodetext,
ls_vbak TYPE ts_vbak,
ls_vbap TYPE ts_vbap,
ls_kna1 TYPE ts_kna1.
* VARIABLE DECLARATION
DATA : w_value(15) TYPE C,
w_qty TYPE kwmeng,
w_amt TYPE netpr.
* SELECTION SCREEN
SELECT-OPTIONS : s_kunnr FOR kna1-kunnr.
START-OF-SELECTION.
PERFORM get_kna1.
PERFORM get_vbak.
PERFORM get_vbap.
PERFORM build_tree.
END-OF-SELECTION.
PERFORM display_tree.
*&---------------------------------------------------------------------*
*& Form get_vbak
*&---------------------------------------------------------------------
FORM get_vbak .
SELECT vbeln
audat
kunnr
FROM vbak
INTO TABLE lt_vbak
FOR ALL ENTRIES IN lt_kna1
WHERE kunnr EQ lt_kna1-kunnr.
DATA:v_line TYPE i.
v_line = LINES( lt_vbak ).
IF LINES( lt_vbak ) EQ 0.
MESSAGE 'No Orders found'(001) TYPE 'I'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM. " get_vbak
*&---------------------------------------------------------------------*
*& Form build_tree
*&---------------------------------------------------------------------
FORM build_tree .
CLEAR : lt_node, ls_node.
SORT : lt_vbak BY kunnr vbeln,
lt_vbap BY vbeln posnr,
lt_kna1 BY kunnr.
ls_node-type = 'T'.
ls_node-name = 'Orders of Customers'(002).
ls_node-tlevel = '01'.
ls_node-nlength = '20'.
ls_node-color = '5'.
ls_node-text = 'Orders'(003).
ls_node-tlength = '10'.
ls_node-tcolor = '3'.
APPEND ls_node TO lt_node.
CLEAR ls_node.
ls_node-type = 'P'.
ls_node-tlevel = '2'.
ls_node-text = 'Customer No'(004).
ls_node-tlength = '15'.
ls_node-tcolor = '7'.
ls_node-text1 = 'Customer Name'(005).
ls_node-tlength1 = '30'.
ls_node-tcolor1 = '7'.
APPEND ls_node TO lt_node.
CLEAR ls_node.
LOOP AT lt_kna1 INTO ls_kna1.
DATA : w_flg TYPE c,
w_flg1 TYPE c.
ls_node-type = 'P'.
ls_node-tlevel = '2'.
ls_node-text = ls_kna1-kunnr.
ls_node-tlength = '15'.
ls_node-tcolor = '1'.
ls_node-text1 = ls_kna1-name1.
ls_node-tlength1 = '30'.
ls_node-tcolor1 = '1'.
APPEND ls_node TO lt_node.
CLEAR ls_node.
LOOP AT lt_vbak INTO ls_vbak WHERE kunnr EQ ls_kna1-kunnr.
IF w_flg1 IS INITIAL.
w_flg1 = 'X'.
ls_node-type = 'P'.
ls_node-tlevel = '3'.
ls_node-text = 'Sales Order'(006).
ls_node-tlength = '15'.
ls_node-tcolor = '7'.
ls_node-text1 = 'Order Date'(007).
ls_node-tlength1 = '15'.
ls_node-tcolor1 = '7'.
APPEND ls_node TO lt_node.
CLEAR ls_node.
ENDIF.
ls_node-type = 'P'.
ls_node-tlevel = '3'.
ls_node-text = ls_vbak-vbeln.
ls_node-tlength = 15.
ls_node-tcolor = 5.
CLEAR w_value.
WRITE ls_vbak-audat TO w_value.
ls_node-text1 = w_value.
ls_node-tlength1 = 15.
ls_node-tcolor1 = 5.
APPEND ls_node TO lt_node.
CLEAR : w_qty, w_amt.
LOOP AT lt_vbap INTO ls_vbap WHERE vbeln EQ ls_vbak-vbeln.
IF w_flg IS INITIAL.
w_flg = 'X'.
ls_node-type = 'P'.
ls_node-tlevel = '4'.
ls_node-text = 'Item'(008).
ls_node-tlength = '11'.
ls_node-tcolor = '7'.
ls_node-text1 = 'Net Weight'(009).
ls_node-tlength1 = '15'.
ls_node-tcolor1 = '7'.
ls_node-text2 = 'Net Price'(010).
ls_node-tlength2 = '15'.
ls_node-tcolor2 = '7'.
APPEND ls_node TO lt_node.
CLEAR ls_node.
ENDIF.
ls_node-type = 'P'.
ls_node-tlevel = '4'.
ls_node-text = ls_vbap-posnr.
ls_node-tlength = '11'.
ls_node-tcolor = '2'.
CLEAR w_value.
w_value = ls_vbap-kwmeng.
ls_node-text1 = w_value.
ls_node-tlength1 = '15'.
ls_node-tcolor1 = '2'.
CLEAR w_value.
w_value = ls_vbap-netpr.
ls_node-text2 = w_value.
ls_node-tlength2 = '15'.
ls_node-tcolor2 = '2'.
APPEND ls_node TO lt_node.
CLEAR ls_node.
ADD ls_vbap-kwmeng TO w_qty.
ADD ls_vbap-netpr TO w_amt.
ENDLOOP.
IF w_qty IS NOT INITIAL.
ls_node-type = 'P'.
ls_node-tlevel = '4'.
ls_node-text = 'Total'(011).
ls_node-tlength = '11'.
ls_node-tcolor = '3'.
CLEAR w_value.
w_value = w_qty.
ls_node-text1 = w_value.
ls_node-tlength1 = '15'.
ls_node-tcolor1 = '3'.
CLEAR w_value.
w_value = w_amt.
ls_node-text2 = w_value.
ls_node-tlength2 = '15'.
ls_node-tcolor2 = '3'.
APPEND ls_node TO lt_node.
CLEAR ls_node.
ENDIF.
CLEAR w_flg.
ENDLOOP.
ENDLOOP.
ENDFORM. " build_tree
*&---------------------------------------------------------------------*
*& Form display_tree
*&---------------------------------------------------------------------
FORM display_tree .
CALL FUNCTION 'RS_TREE_CONSTRUCT'
TABLES
nodetab = lt_node
EXCEPTIONS
tree_failure = 1
id_not_found = 2
wrong_relationship = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
CALL FUNCTION 'RS_TREE_LIST_DISPLAY'
EXPORTING
callback_program = sy-repid.
ENDFORM. " display_tree
*&---------------------------------------------------------------------*
*& Form get_vbap
*&---------------------------------------------------------------------
FORM get_vbap .
SELECT vbeln
posnr
kwmeng
netpr
FROM vbap
INTO TABLE lt_vbap
FOR ALL ENTRIES IN lt_vbak
WHERE vbeln EQ lt_vbak-vbeln.
ENDFORM. " get_vbap
*&---------------------------------------------------------------------*
*& Form get_kna1
*&---------------------------------------------------------------------
FORM get_kna1 .
SELECT kunnr
name1
FROM kna1
INTO TABLE lt_kna1
WHERE kunnr IN s_kunnr.
IF sy-subrc NE 0.
MESSAGE 'No customers exists'(012) TYPE 'I'.
LEAVE LIST-PROCESSING.
ENDIF.
ENDFORM. " get_kna1
OUTPUT SCREEN: