Tuesday, 7 November 2017

Simulate Shipment Cost (VT02N) – Using Function Module

I am here with yet another unsolved mystery, yup! You better believe me! As the subject is as clean as a whistle, this blog post is all about Simulating Shipment Cost through Function Module. Before I proceed, let’s have a clear understanding what actually I am trying to do and what exactly we will be going to achieve!

So, here is the scenario. You have the Shipment created and you see the button on VT02N to simulate cost. Well, when you do it through T-code, it is as easy as anything but the reality is as different as cheese and chalk! Oh yes, I mean it. It’s not easy when it comes to implement via Function Module.

Recently, I have encountered the same problem but somehow I managed to pull that off. I thought I should be sharing it to those who are still stuck with this and automating the Shipment Cost has undone them!

I will go step by step to implement the solution but first let’s see what is the Simulate Shipment Cost button and where it resides in Shipment (VT02N). Have a look the beautiful Shipment Created in the System. Have a look at the highlighted button, this is when you click and simulate the shipment cost.

SAP ABAP Development, SAP ABAP Tutorials and Materials, SAP ABAP Guides

Now let’s click it and Simulate it.

SAP ABAP Development, SAP ABAP Tutorials and Materials, SAP ABAP Guides

You see, bingo! Cost is simulated in split a second. Now let’s try using a Function Module. You need to follow the steps mentioned below:

1. Shipment should be created
2. Make use of FM: RV_SHIPMENT_VIEW
3. Move the returned tables to a particular Deep Structure (Don’t worry, will elaborate)
4. Finally, simulate the Shipment Cost using: SD_SCD_SIMULATE_FREIGHT_COSTS

Now we are ready to Simulate the Shipment Cost by following the above steps! So, why waste time? 

Let’s do it.

Let’s do the global declarations.

Data: f_vttkvb                    TYPE vttkvb,
      f_tvtk                      TYPE tvtk,
      f_tvtkt                     TYPE tvtkt,
      f_ttds                      TYPE ttds,
      f_ttdst                     TYPE ttdst,
      f_vbpla                     TYPE vbpla,
      e_delivery_missing          TYPE rv56a-selkz,
      e_dsd_display               TYPE rv56a-selkz,
      f_vttp                      TYPE STANDARD TABLE OF vttpvb,
      f_trlk                      TYPE STANDARD TABLE OF vtrlk,
      f_trlp                      TYPE STANDARD TABLE OF vtrlp,
      f_vtts                      TYPE STANDARD TABLE OF vttsvb,
      f_vtsp                      TYPE STANDARD TABLE OF vtspvb,
      f_vbpa                      TYPE STANDARD TABLE OF vbpavb,
      f_vbadr                     TYPE STANDARD TABLE OF sadrvb,
      f_vtfa                      TYPE STANDARD TABLE OF vtfavb,
      f_vbplk                     TYPE STANDARD TABLE OF vbplk,
      f_vbplp                     TYPE STANDARD TABLE OF vbplp,
      ls_vbplk                    TYPE vbplk,
      ls_vbplp                    TYPE vbplp,
      f_vbpls                     TYPE STANDARD TABLE OF vbpls,
      f_yvtfa                     TYPE STANDARD TABLE OF vtfavb,
      lt_shipments                TYPE v54a0_refobj_tab,
      ls_shipments                TYPE v54a0_refobj,
      i_scd_sim                   TYPE i VALUE 2,
      lt_freight_costs            TYPE v54a0_scdd_tab,
      ls_freight_costs            TYPE v54a0_scdd,
      e_errors_occured            TYPE c,
      e_warnings_occured          TYPE c,
      e_created_freight_costs     TYPE i.

Now get the shipment data by passing Shipment Number.

  CALL FUNCTION 'RV_SHIPMENT_VIEW'
    EXPORTING
      shipment_number             = p_shipment_number
      option_tvtk                 = abap_true
      option_ttds                 = abap_true
      language                    = sy-langu
      option_items                = abap_true
      option_minimized_item_data  = abap_false
      option_sales_orders         = abap_false
      option_export_data          = abap_false
      option_stawn_read           = abap_false
      option_segments             = abap_true
      option_partners             = abap_true
      option_messages             = abap_true
      option_packages             = abap_true
      option_flow                 = abap_true
      option_delivery_lock        = abap_false
      option_authority_check      = abap_false
      activity                    = 'A' "Display
      option_no_refresh           = abap_true
      option_ignore_missing_deliv = abap_true
      i_filter_type               = 'F' "Transportation Relevance
    IMPORTING
      f_vttkvb                    = f_vttkvb
      f_tvtk                      = f_tvtk
      f_tvtkt                     = f_tvtkt
      f_ttds                      = f_ttds
      f_ttdst                     = f_ttdst
      e_delivery_missing          = e_delivery_missing
    TABLES
      f_vttp                      = f_vttp
      f_trlk                      = f_trlk
      f_trlp                      = f_trlp
      f_vtts                      = f_vtts
      f_vtsp                      = f_vtsp
      f_vbpa                      = f_vbpa
      f_vbadr                     = f_vbadr
      f_vbplk                     = f_vbplk
      f_vbplp                     = f_vbplp
      f_vbpls                     = f_vbpls
    EXCEPTIONS
      not_found                   = 1
      no_authority                = 2
      delivery_missing            = 3
      delivery_lock               = 4
      OTHERS                      = 5.
  IF sy-subrc <> 0.
* Implement Suitable Exceptions
  ENDIF.

Great, we have got the Shipment data, just give it a final go then! Pass all these Internal Tables and Structures to Simulate Cost FM.

  ls_shipments-vttkf   = f_vttkvb.
  ls_shipments-vttp[]  = f_vttp[].
  ls_shipments-vtrlk[] = f_trlk[].
  ls_shipments-vtrlp[] = f_trlp[].
  ls_shipments-vtrlp_c[] = f_trlp[].
  ls_shipments-vtrlp_s[] = f_trlp[].
  ls_shipments-vttsf[] = f_vtts[].
  ls_shipments-vtsp[]  = f_vtsp[].
  ls_shipments-vbpa[]  = f_vbpa[].
  ls_shipments-vbplk[] = f_vbplk[].
  ls_shipments-vbplp[] = f_vbplp[].
  ls_shipments-vbadr[] = f_vbadr[].
  ls_shipments-tvtk    = f_tvtk.
  ls_shipments-ttds    = f_ttds.
  APPEND ls_shipments TO lt_shipments.
  CLEAR: ls_shipments.

Brilliant, now lets Simulate the cost! Just pass LT_SHIPMENTS to the following FM:

  CALL FUNCTION 'SD_SCD_SIMULATE_FREIGHT_COSTS'
    EXPORTING
      i_shipments             = lt_shipments
      i_scd_sim               = 3
      i_run                   = 0
    IMPORTING
      e_freight_costs         = lt_freight_costs
      e_errors_occured        = e_errors_occured
      e_warnings_occured      = e_warnings_occured
      e_created_freight_costs = e_created_freight_costs.

  IF NOT lt_freight_costs[] IS INITIAL.

    LOOP AT lt_freight_costs INTO ls_freight_costs.
      LOOP AT ls_freight_costs-x-item INTO ls_item.
        LOOP AT ls_item-komv INTO ls_komv.
          APPEND ls_komv TO lt_komv.
          CLEAR: ls_komv.
        ENDLOOP.
      ENDLOOP.
    ENDLOOP.

  ELSE.

    p_error_message = 'Cost could not be Simulated'.

  ENDIF.

LT_FRIEGHT_COSTS will be returned and again it’s a deep structure, Simulated cost will be sitting in LT_FRIEGHT_COSTS-X-ITEM-KOMV.

SAP ABAP Development, SAP ABAP Tutorials and Materials, SAP ABAP Guides

SAP ABAP Development, SAP ABAP Tutorials and Materials, SAP ABAP Guides

SAP ABAP Development, SAP ABAP Tutorials and Materials, SAP ABAP Guides

KOMV-KBETR with currency is your simulated cost, and KOMV-KWERT is your converted simulated cost.

No comments:

Post a Comment