
DATA: gt_final1 TYPE TABLE OF ty_final,
go_tree TYPE REF TO cl_salv_tree.
* Create ALV tree
TRY.
cl_salv_tree=>factory(
IMPORTING
r_salv_tree = go_tree
CHANGING
t_table = gt_final1 ).
CATCH cx_salv_error.
LEAVE LIST-PROCESSING.
ENDTRY.
DATA: lo_settings TYPE REF TO cl_salv_tree_settings.
lo_settings = go_tree->get_tree_settings( ).
lo_settings->set_hierarchy_header( ‘Ship-to’ ).
lo_settings->set_hierarchy_tooltip( ‘Ship-to’ ).
lo_settings->set_header( ‘Order Combination Report’ ).
DATA: lo_events TYPE REF TO cl_salv_events_tree.
lo_events = go_tree->get_event( ).
SET HANDLER go_create->on_link_click FOR lo_events.
LOOP AT gt_final ASSIGNING <ls_final>.
* Check if there is a change in Ship-to or Payment type
* in which case a new folder needs to be created
IF <ls_final>-kunwe NE ls_prev-kunwe
OR <ls_final>-zpaym_type NE ls_prev-zpaym_type.
* Get the Ship-to text
CLEAR lv_text.
CONCATENATE ‘Ship to’
<ls_final>-kunwe
<ls_final>-name1
INTO lv_text SEPARATED BY space.
* Here we get the key to the Ship-to node, we will use it for Orders
* belonging to this Ship-to
* Create new folder for Ship-to
lv_kunwe_key = go_create->add_tree_node( iv_text = lv_text ).
ENDIF.
* Keep changing the flag to get alternate background color
* Changing colors as Order no. changes
IF ls_prev-vbeln NE <ls_final>-vbeln.
IF lv_set_style IS INITIAL.
lv_set_style = abap_true.
ELSE.
lv_set_style = abap_false.
ENDIF.
ENDIF.
* Add new item in the folder
* Add the node with the relevant data
* Here we give the key to the Ship-to node, this way the Order
* becomes the child of the Ship-to node
go_create->add_tree_node( iv_nkey = lv_kunwe_key
is_data = <ls_final>
iv_set_type = if_salv_c_item_type=>checkbox
iv_set_style = lv_set_style ).
* Store the current line as "previous" for further processing
ls_prev = <ls_final>.
ENDLOOP.
METHOD add_tree_node.
*---------------------------------------------------------
* Add a node to the ALV tree
*---------------------------------------------------------
DATA: lo_nodes TYPE REF TO cl_salv_nodes,
lo_node TYPE REF TO cl_salv_node,
lo_item TYPE REF TO cl_salv_item,
lo_item_delivery TYPE REF TO cl_salv_item.
lo_nodes = go_tree->get_nodes( ).
* We use the standard method to add the node
TRY.
lo_node = lo_nodes->add_node(
related_node = iv_nkey
relationship = if_salv_c_node_relation=>last_child
data_row = is_data
collapsed_icon = '@5F@' "space
expanded_icon = '@5F@' "space
text = iv_text ).
CATCH cx_salv_msg.
LEAVE LIST-PROCESSING.
ENDTRY.
* We provided text only for Ship-to node.
* The below code will set the text of the node (viz. Ship-to 12345 ABC Org.)
* Set the text for the node
IF iv_text IS NOT INITIAL.
lo_node->set_text( iv_text ).
ENDIF.
* In order to distinguish between Orders, we set different background colors.
* The below code sets blue color as background
IF iv_set_style IS NOT INITIAL.
lo_node->set_row_style( if_salv_c_tree_style=>emphasized_b ).
ENDIF.
lo_item = lo_node->get_hierarchy_item( ).
IF iv_nkey IS INITIAL.
* For the Ship-to node, we take the Delivery column and set it to Button
TRY.
lo_item_delivery = lo_node->get_item( 'DELIVERY' ).
CATCH cx_salv_msg.
EXIT.
ENDTRY.
lo_item_delivery->set_type( if_salv_c_item_type=>button ).
lo_item_delivery->set_value( ‘Delivery Creation’ ).
ELSE.
* For the order nodes, we get the entire row and Set it as editable checkbox
lo_item->set_type( if_salv_c_item_type=>checkbox ).
lo_item->set_editable( abap_true ).
* Set the data of the node
lo_node->set_data_row( is_data ).
lo_nodes->expand_all( ).
ENDIF.
* Return the node key
rv_nkey = lo_node->get_key( ).
ENDMETHOD. "add_tree_node
METHOD on_link_click.
*---------------------------------------------------------
* Method when the user clicks on Link or button
*---------------------------------------------------------
DATA: lo_nodes TYPE REF TO cl_salv_nodes,
lo_node TYPE REF TO cl_salv_node.
lo_nodes = go_tree->get_nodes( ).
* Get the parent node
TRY.
lo_node = lo_nodes->get_node( node_key ).
CATCH cx_salv_msg.
EXIT.
ENDTRY.
go_create->create_delivery( lo_node ).
ENDMETHOD. "on_link_click
METHOD create_delivery.
*---------------------------------------------------------
* Create the delivery from the selected Orders
*---------------------------------------------------------
DATA: lt_nodes TYPE salv_t_nodes,
lo_item TYPE REF TO cl_salv_item,
lo_data TYPE REF TO data,
lv_delivery TYPE vbeln_vl,
lv_number TYPE vbnum,
lt_sales_orders TYPE STANDARD TABLE OF bapidlvreftosalesorder,
lt_created_items TYPE STANDARD TABLE OF bapidlvitemcreated,
lt_return TYPE STANDARD TABLE OF bapiret2.
FIELD-SYMBOLS: <ls_nodes> TYPE salv_s_nodes,
<ls_data> TYPE ty_final,
<ls_orders> TYPE bapidlvreftosalesorder,
<ls_items> TYPE bapidlvitemcreated.
* Get the entire subtree
TRY.
lt_nodes = io_node->get_subtree( ).
CATCH cx_salv_msg.
EXIT.
ENDTRY.
* Here we will loop at all the nodes, find the ones that were selected
* and pass those orders to BAPI for Delivery creation
LOOP AT lt_nodes ASSIGNING <ls_nodes>.
* Get the item details
lo_item = <ls_nodes>-node->get_hierarchy_item( ).
* Get the node data if it is checked
IF lo_item->is_enabled( ) = abap_true
AND lo_item->is_checked( ) = abap_true.
lo_data = <ls_nodes>-node->get_data_row( ).
ASSIGN lo_data->* TO <ls_data>.
IF sy-subrc EQ 0.
* Get the sales order and item
APPEND INITIAL LINE TO lt_sales_orders ASSIGNING <ls_orders>.
<ls_orders>-ref_doc = <ls_data>-vbeln.
<ls_orders>-ref_item = <ls_data>-posnr.
ENDIF.
ENDIF.
ENDLOOP.
* Call the BAPI for Delivery Creation
CALL FUNCTION 'BAPI_OUTB_DELIVERY_CREATE_SLS'
IMPORTING
delivery = lv_delivery
num_deliveries = lv_number
TABLES
sales_order_items = lt_sales_orders
created_items = lt_created_items
return = lt_return.
SORT lt_created_items BY ref_doc ref_item.
IF lv_delivery IS NOT INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ENDIF.
* Next, loop at the nodes and display the delivery
LOOP AT lt_nodes ASSIGNING <ls_nodes>.
lo_item = <ls_nodes>-node->get_hierarchy_item( ).
IF lo_item->is_checked( ) = abap_true.
lo_item->set_checked( abap_false ).
lo_data = <ls_nodes>-node->get_data_row( ).
ASSIGN lo_data->* TO <ls_data>.
IF sy-subrc EQ 0.
* Set the delivery
READ TABLE lt_created_items ASSIGNING <ls_items>
WITH KEY ref_doc = <ls_data>-vbeln
ref_item = <ls_data>-posnr
BINARY SEARCH.
IF sy-subrc EQ 0.
<ls_data>-delivery = <ls_items>-deliv_numb.
<ls_nodes>-node->set_data_row( <ls_data> ).
ENDIF.
ENDIF.
* Disable the checkbox if the delivery is created,
IF <ls_data>-delivery IS NOT INITIAL.
lo_item->set_enabled( abap_false ).
lo_item->set_icon( '@0V\QOK@' ). "OK
ENDIF.
ENDIF.
ENDLOOP.
ENDMETHOD. "create_delivery
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
2 | |
2 | |
2 | |
2 | |
2 | |
2 | |
2 | |
2 | |
2 |