You are on page 1of 7

TYPES : BEGIN OF TY_FINAL,

MATNR TYPE MATNR,


OJTXP TYPE OJTXP,
POSNR TYPE SPOSN,
MENGE TYPE KMPMG,
MEINS TYPE KMPME,
STUFE TYPE HISTU,
TTIDX TYPE TTIDX,
END OF TY_FINAL.
DATA : WA TYPE TY_FINAL.
DATA : STB LIKE STPOX OCCURS 0 WITH HEADER LINE.
DATA: GT_OUTTAB TYPE STANDARD TABLE OF TY_FINAL.
DATA: GR_TREE TYPE REF TO CL_SALV_TREE.
CONSTANTS: GC_TRUE TYPE SAP_BOOL VALUE 'X'.
*----------------------------------------------------------------------*
* SELECTION-SCREEN *
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME.
PARAMETERS: P_MATNR TYPE MATNR, "Mateial
P_WERKS TYPE WERKS_D, "Plant
P_MUL TYPE C.
"Multiple explode
SELECTION-SCREEN END OF BLOCK B1.
*----------------------------------------------------------------------*
* START-OF-SELECTION
*----------------------------------------------------------------------*
START-OF-SELECTION.
*----------------------------------------------------------------------*
* END-OF-SELECTION
*----------------------------------------------------------------------*
END-OF-SELECTION.
PERFORM DISPLAY_FULLSCREEN.
*&---------------------------------------------------------------------*
*&
Form display_fullscreen
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
FORM DISPLAY_FULLSCREEN .

*... 1 create an ALV tree


TRY.
CL_SALV_TREE=>FACTORY(
IMPORTING
R_SALV_TREE = GR_TREE
CHANGING
T_TABLE
= GT_OUTTAB ).
CATCH CX_SALV_NO_NEW_DATA_ALLOWED CX_SALV_ERROR.
EXIT.
ENDTRY.
PERFORM CREATE_TREE.
*... 3 Functions
*... 3.1 activate ALV generic Functions
*... 3.2 include own functions by setting own status
* GR_TREE->SET_SCREEN_STATUS(
* PFSTATUS
= 'SALV_STANDARD'
* REPORT
= SY-REPID
* SET_FUNCTIONS = GR_TREE->C_FUNCTIONS_ALL ).

*... set the columns technical


DATA: LR_COLUMNS TYPE REF TO CL_SALV_COLUMNS_TREE.
LR_COLUMNS = GR_TREE->GET_COLUMNS( ).
LR_COLUMNS->SET_OPTIMIZE( GC_TRUE ).
* PERFORM SET_COLUMNS_TECHNICAL USING LR_COLUMNS.
*... 4 display the table
GR_TREE->DISPLAY( ).
ENDFORM.
" display_fullscreen
*&---------------------------------------------------------------------*
*&
Form create_tree
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM CREATE_TREE .

PERFORM BUILD_HEADER.
PERFORM SELECT_DATA.
ENDFORM.

" build_tree

*&---------------------------------------------------------------------*
*&
Form build_header
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM BUILD_HEADER .
*... 2.1 build the hierarchy header
DATA: SETTINGS TYPE REF TO CL_SALV_TREE_SETTINGS.
SETTINGS = GR_TREE->GET_TREE_SETTINGS( ).
SETTINGS->SET_HIERARCHY_HEADER( TEXT-HD1 ).
SETTINGS->SET_HIERARCHY_TOOLTIP( TEXT-HT1 ).
SETTINGS->SET_HIERARCHY_SIZE( 30 ).
DATA: TITLE TYPE SALV_DE_TREE_TEXT.
TITLE = SY-TITLE.
SETTINGS->SET_HEADER( TITLE ).
ENDFORM.
" build_header
*&---------------------------------------------------------------------*
*&
Form select_data
*&---------------------------------------------------------------------*
* 2 to display the data, you first have to select it in some table
*----------------------------------------------------------------------*
FORM SELECT_DATA.
*... 2.2 select the data from the data table into a local internal
*table
DATA: LT_OUTTAB LIKE GT_OUTTAB.

CALL FUNCTION 'CS_BOM_EXPL_MAT_V2'


EXPORTING
CAPID
= 'BEST'
DATUV
= SY-DATUM
EHNDL
= '1'

MKTLS
= 'X'
MEHRS
= P_MUL
MMORY
= '1'
MTNRV
= P_MATNR
STLAL
= '1'
WERKS
= P_WERKS
TABLES
STB
= STB
* MATCAT
=
EXCEPTIONS
ALT_NOT_FOUND
=1
CALL_INVALID
=2
MATERIAL_NOT_FOUND
=3
MISSING_AUTHORIZATION
=4
NO_BOM_FOUND
=5
NO_PLANT_DATA
=6
NO_SUITABLE_BOM_FOUND
=7
CONVERSION_ERROR
=8
OTHERS
= 9.

LOOP AT STB.
WA-MATNR = STB-IDNRK.
WA-OJTXP = STB-OJTXP.
WA-POSNR = STB-POSNR.
WA-MENGE = STB-MENGE.
WA-MEINS = STB-MEINS.
WA-STUFE = STB-STUFE.
WA-TTIDX = STB-TTIDX.
APPEND WA TO LT_OUTTAB.
CLEAR WA.
ENDLOOP.

PERFORM SUPPLY_DATA USING LT_OUTTAB.


ENDFORM.
" select_data
*&---------------------------------------------------------------------*
*&
Form supply_data
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->P_LT_OUTTAB text
*----------------------------------------------------------------------*
FORM SUPPLY_DATA USING LT_OUTTAB TYPE STANDARD TABLE.

*... 2.3 supply the data to ALV, building the hierarchy


DATA: LS_DATA TYPE TY_FINAL.
DATA: L_CARRID_KEY TYPE LVC_NKEY,
L_CONNID_KEY TYPE LVC_NKEY,
L_LAST_KEY TYPE LVC_NKEY,
L_KEY TYPE LVC_NKEY.
DATA : BEGIN OF NKEY OCCURS 0,
NOD LIKE LS_DATA-STUFE,
KEY TYPE LVC_NKEY,
TTIDX TYPE TTIDX,
END OF NKEY.
LOOP AT LT_OUTTAB INTO LS_DATA.
IF LS_DATA-STUFE = '1'.
REFRESH NKEY.
PERFORM ADD_COMPLETE_LINE USING LS_DATA
''
CHANGING L_KEY.
CONTINUE.
ENDIF.
NKEY-NOD = LS_DATA-STUFE. NKEY-KEY = L_KEY.
NKEY-TTIDX = LS_DATA-TTIDX.
APPEND NKEY.
IF LS_DATA-STUFE < SY-TABIX.
CLEAR NKEY.
READ TABLE NKEY WITH KEY NOD = LS_DATA-STUFE
TTIDX = LS_DATA-TTIDX.
L_KEY = NKEY-KEY.
ENDIF.
PERFORM ADD_COMPLETE_LINE USING LS_DATA
L_KEY
CHANGING L_KEY.

ENDLOOP.
ENDFORM.

" supply_data

*&---------------------------------------------------------------------*
*&
Form add_complete_line

*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->P_LS_DATA text
*
-->P_L_CONNID_KEY text
*
<--P_L_LAST_KEY text
*----------------------------------------------------------------------*
FORM ADD_COMPLETE_LINE USING P_LS_DATA TYPE TY_FINAL
P_L_CONNID_KEY
CHANGING P_L_LAST_KEY.
DATA: NODES TYPE REF TO CL_SALV_NODES,
NODE TYPE REF TO CL_SALV_NODE.
NODES = GR_TREE->GET_NODES( ).
TRY.
NODE = NODES->ADD_NODE( RELATED_NODE = P_L_CONNID_KEY
DATA_ROW = P_LS_DATA
RELATIONSHIP = CL_GUI_COLUMN_TREE=>RELAT_LAST_CHILD
).
P_L_LAST_KEY = NODE->GET_KEY( ).
CATCH CX_SALV_MSG.
ENDTRY.
ENDFORM.
" add_complete_line
*&---------------------------------------------------------------------*
*&
Form set_columns_technical
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
FORM SET_COLUMNS_TECHNICAL USING IR_COLUMNS TYPE REF TO
CL_SALV_COLUMNS_TREE.
* those columns which should not be seen by the user at all are set
*technical
DATA: LR_COLUMN TYPE REF TO CL_SALV_COLUMN.
TRY.
LR_COLUMN = IR_COLUMNS->GET_COLUMN( 'MATNR' ).
LR_COLUMN->SET_LONG_TEXT( 'Material' ).
*
LR_COLUMN->SET_TECHNICAL( IF_SALV_C_BOOL_SAP=>TRUE ).
CATCH CX_SALV_NOT_FOUND.
"#EC NO_HANDLER
ENDTRY.

TRY.
LR_COLUMN = IR_COLUMNS->GET_COLUMN( 'OJTXP' ).
LR_COLUMN->SET_LONG_TEXT( 'Material' ).
*
LR_COLUMN->SET_TECHNICAL( IF_SALV_C_BOOL_SAP=>TRUE ).
CATCH CX_SALV_NOT_FOUND.
"#EC NO_HANDLER
ENDTRY.
TRY.
LR_COLUMN = IR_COLUMNS->GET_COLUMN( 'POSNR' ).
LR_COLUMN->SET_LONG_TEXT( 'Material' ).
*
LR_COLUMN->SET_TECHNICAL( IF_SALV_C_BOOL_SAP=>TRUE ).
CATCH CX_SALV_NOT_FOUND.
"#EC NO_HANDLER
ENDTRY.
TRY.
LR_COLUMN = IR_COLUMNS->GET_COLUMN( 'MENGE' ).
LR_COLUMN->SET_LONG_TEXT( 'Material' ).
*
LR_COLUMN->SET_TECHNICAL( IF_SALV_C_BOOL_SAP=>TRUE ).
CATCH CX_SALV_NOT_FOUND.
"#EC NO_HANDLER
ENDTRY.
TRY.
LR_COLUMN = IR_COLUMNS->GET_COLUMN( 'MEINS' ).
LR_COLUMN->SET_LONG_TEXT( 'Material' ).
*
LR_COLUMN->SET_TECHNICAL( IF_SALV_C_BOOL_SAP=>TRUE ).
CATCH CX_SALV_NOT_FOUND.
"#EC NO_HANDLER
ENDTRY.

ENDFORM.

" set_columns_technical