Wednesday, March 19, 2014

How to pack Delivery x HUs by ABAP?

Once I had to face the following problem, how to programmatically  pack all the goods of some delivery including creation of handling units. I have spent quite considerable time finding working solution. You know there is a certain set of various function modules which leads to the solution, but there is big number of blind ways. Even when you pick up proper FMs, you have to use them in correct order and provide correct input parameters. Finally we have decided to pack every delivery item/position into each new HU, not to pack them all into one HU. Below there is offered a solution working in our case. I hope it could save time to someone else. If you would give me your feedback, it would be very nice.

I already tested following FMs as BAPI_HU_CREATE, BAPI_HU_CHANGE_HEADER, BAPI_HU_PACK, SD_DELIVERY_UPDATE_PACKING, WS_DELIVERY_UPDATE, HU_PACKING_AND_UNPACKING.

I discovered that even the transaction VL02N internally calls FM HU_PACKING_AND_UNPACKING. Usage of BAPI_HU_CREATE and BAPI_HU_PACK with itempropsal is Ok unless you do not need to act with delivery. In such case you need to involve some of those FMs as SD_DELIVERY_UPDATE_PACKING or  WS_DELIVERY_UPDATE or HU_PACKING_AND_UNPACKING. But HU with already filled item proposals causes doubled items inside the every HU after usage FMs working with the delivery. Later on I came to the folowing solution, ommit every item proposals and let the job on FM WS_DELIVERY_UPDATE with suitable VERPO (VEPO) itab given as parameter.

The following code should be commented enough to follow the main idea.

Some data of top include

TYPES:
  abap_bool   TYPE LENGTH 1,
  gtty_result TYPE STANDARD TABLE OF bapiret2.

* constants for abap_bool
CONSTANTS:
  abap_true      TYPE abap_bool VALUE 'X',
  abap_false     TYPE abap_bool VALUE ' ',
  abap_undefined TYPE abap_bool VALUE '-'.

CONSTANTScv_vbtyp_notification LIKE likp-vbtyp VALUE '7',
           cv_in_delivery   LIKE verko-object VALUE '03'" inbound
           cv_out_delivery LIKE verko-object VALUE '01'" outbound
           cv_velin LIKE vepo-velin VALUE '1',
           cv_pksta_packed TYPE pksta VALUE 'C'.

DATAgv_vbuk  TYPE vbuk" vbuk-pkstk = Overall packing status of all items
      gs_likp  TYPE likp,
      gv_shipment_already_created TYPE abap_bool VALUE abap_false,
      gv_pack_mat TYPE mara-matnr.


The main routine

*&---------------------------------------------------------------------*
*&      Form  pack_delivery
*&---------------------------------------------------------------------*
*       Pack whole delivery = all positions
*----------------------------------------------------------------------*
*      -->PS_LIKP      contains vbeln, vbtyp
*      -->PV_PACK_MAT  packaging material
*----------------------------------------------------------------------*
FORM pack_delivery
    USING    ps_likp     TYPE likp " contains vbeln, vbtyp
             pv_pack_mat TYPE mara-matnr.

  DATA:  lt_lips TYPE STANDARD TABLE OF lips.

  " add leading zeros
  UNPACK ps_likp-vbeln TO ps_likp-vbeln.

  IF ps_likp-vbeln IS NOT INITIAL.
    SELECT " posnr werks lgort meins lfimg matnr charg ...
      FROM lips
      INTO CORRESPONDING FIELDS OF TABLE lt_lips
      WHERE vbeln ps_likp-vbeln.
  ENDIF.
  IF lt_lips IS INITIAL.
    MESSAGE e000 WITH text-017.
  ENDIF.

  DATAlt_hu    LIKE STANDARD TABLE OF hum_rehang_hu,
        ls_hu    LIKE LINE OF lt_hu,
        lv_pksta TYPE vbup-pksta.

  DATAlt_vekp  TYPE STANDARD TABLE OF vekp,
        lt_vepo  TYPE STANDARD TABLE OF vepo,
        lv_exidv TYPE exidv.

  DATAls_vbkok LIKE vbkok,
        lt_hus   LIKE STANDARD TABLE OF vekpvb,
        ls_hus   LIKE LINE OF lt_hus.

  " Handling-Unit Confirmation: Header Data
  DATAlt_verko  LIKE STANDARD TABLE OF verko,
        ls_verko  TYPE verko,
        " Handling Unit Confirmation: Content Data
        lt_verpo  LIKE STANDARD TABLE OF verpo,
        ls_verpo  TYPE verpo,
        lt_prot   LIKE STANDARD TABLE OF prott,
        ls_prot   TYPE prott.

  FIELD-SYMBOLS<fs_vekp> LIKE vekp,
                 <fs_vepo> LIKE vepo,
                 <fs_lips> LIKE lips.

  " Pack each non empty delivery position into its own new HU
  LOOP AT lt_lips ASSIGNING <fs_lips>.
    IF <fs_lips>-lfimg IS NOT INITIAL AND <fs_lips>-lfimg > ).

      " Filter already packed items
      CLEAR lv_pksta.
      SELECT SINGLE pksta
        FROM vbup
        INTO lv_pksta
        WHERE vbeln <fs_lips>-vbeln
          AND
              posnr <fs_lips>-posnr.
      IF NOT sy-subrc EQ AND lv_pksta NE cv_pksta_packed ).
        CONTINUE.
      ENDIF.

      " Create HU and assign it to the delivery (no items are yet created)
      CLEAR ls_hu.
      PERFORM get_assigned_hu
      USING     <fs_lips>
                pv_pack_mat
                ps_likp-vbtyp
      CHANGING  ls_hu.

      " Get data needed for delivery packing
      IF ls_hu IS NOT INITIAL.
        APPEND ls_hu TO lt_hu.

        " FM HU_CREATE alredy made record in VEKP table
        SELECT FROM vekp
          INTO TABLE lt_vekp
          WHERE venum ls_hu-venum.

        LOOP AT lt_vekp ASSIGNING <fs_vekp>.
          CLEAR ls_verko.
          MOVE-CORRESPONDING <fs_vekp> TO ls_verko.
          ls_verko-ernam sy-uname.
          ls_verko-object cv_out_delivery.   " Outbound Delivery
          ls_verko-objkey ps_likp-vbeln.     " Object to Which the HU is Assigned
          ls_verko-exidv <fs_vekp>-exidv.
          APPEND ls_verko TO lt_verko.
        ENDLOOP.

        " Prepare item row (later stored in VEPO)
        " in our case 1x HU = 1 x item row = no loop needed

        " new HU
        ls_verpo-exidv_ob ls_hu-top_hu_external.
        ls_verpo-venum    =  ls_hu-venum.
        " other lips fields
        MOVE-CORRESPONDING <fs_lips> TO ls_verpo.
        " quantity + units
        ls_verpo-tmeng =  <fs_lips>-lfimg.
        ls_verpo-vrkme =  <fs_lips>-meins.
        APPEND ls_verpo TO lt_verpo.

      ENDIF.
    ENDIF.
  ENDLOOP.

  IF lt_verko IS NOT INITIAL AND lt_verpo IS NOT INITIAL ).

    ls_vbkok-vbeln    <fs_lips>-vbeln.
    ls_vbkok-vbeln_vl <fs_lips>-vbeln.
    ls_vbkok-vbtyp_vl ps_likp-vbtyp.

    " Save Handling unit data, pack, update delivery
    CALL FUNCTION 'WS_DELIVERY_UPDATE'
      EXPORTING
        vbkok_wa      ls_vbkok
        synchron      abap_true
        commit        abap_true
        delivery      <fs_lips>-vbeln
        nicht_sperren space
      TABLES
        verko_tab     lt_verko
        verpo_tab     lt_verpo
        prot          lt_prot.

    IF NOT lt_prot[] IS INITIAL.
      CLEARls_prot.
      READ TABLE lt_prot INDEX INTO ls_prot.
      MESSAGE ID ls_prot-msgid
             TYPE ls_prot-msgty
             NUMBER ls_prot-msgno
             WITH ls_prot-msgv1 ls_prot-msgv2
                  ls_prot-msgv3 ls_prot-msgv4.
    ENDIF.
  ENDIF.

ENDFORM.                    " PACK_DELIVERY


*&---------------------------------------------------------------------*
*&      Form  GET_ASSIGNED_HU
*&---------------------------------------------------------------------*
*       Create empty HU and assign it to the delivery
*----------------------------------------------------------------------*
*      -->PV_VBELN     text
*      -->PV_PACK_MAT  text
*      -->PV_VBTYP     text
*----------------------------------------------------------------------*
FORM get_assigned_hu
    USING    ps_lips     TYPE lips       " delivery position data
             pv_pack_mat TYPE mara-matnr
             pv_vbtyp    TYPE vbtyp
    CHANGING ps_hu       TYPE hum_rehang_hu.

  DATAlt_return LIKE STANDARD TABLE OF bapiret2,
        ls_return LIKE LINE OF lt_return,
        lv_hukey TYPE  bapihukey-hu_exid,
        ls_huheader  TYPE  bapihuheader,
        ls_headerproposal TYPE  bapihuhdrproposal,
        lt_itemproposal TYPE STANDARD TABLE OF bapihuitmproposal,
        ls_itemproposal LIKE LINE OF lt_itemproposal.

  " Subprocess 1: Create HU

  " HU header proposal
  ls_headerproposal-plant    ps_lips-werks.
  MOVE ps_lips-lgort TO ls_headerproposal-stge_loc" storage location
  UNPACK pv_pack_mat TO pv_pack_mat.
  ls_headerproposal-pack_mat pv_pack_mat.

  " creates record in VEKP (with item proposal given also in VEPO)
  CALL FUNCTION 'BAPI_HU_CREATE'
    EXPORTING
      headerproposal ls_headerproposal
    IMPORTING
      huheader       ls_huheader
      hukey          lv_hukey
    TABLES
      itemsproposal  lt_itemproposal
      return         lt_return.

  IF lt_return[] IS INITIAL.
    CLEAR ls_return.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait   'X'
      IMPORTING
        return ls_return.
  ELSE.
    PERFORM get_bapi_error USING lt_return.
  ENDIF.

  " Wait to avoid lock in production environment
  WAIT UP TO SECONDS.

  " Set connection to the outbound delivery type (pack_mat_object)
  MOVE cv_out_delivery TO ls_huheader-pack_mat_object.
  MOVE ps_lips-vbeln   TO ls_huheader-pack_mat_obj_key.

  REFRESH lt_return.
  CALL FUNCTION 'BAPI_HU_CHANGE_HEADER'
    EXPORTING
      hukey     ls_huheader-hu_exid
      huchanged ls_huheader
    IMPORTING
      huheader  ls_huheader
    TABLES
      return    lt_return.
  IF lt_return[] IS INITIAL.
    CLEAR ls_return.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait   'X'
      IMPORTING
        return ls_return.
  ELSE.
    PERFORM get_bapi_error USING lt_return.
  ENDIF.

  CLEAR ps_hu.
  " note: hu_exid + hu_id are stored in VEKP - Handling Unit - Header Table
  ps_hu-top_hu_external ls_huheader-hu_exid" External Handling Unit Identification of new HU
  ps_hu-top_hu_internal ls_huheader-hu_id.   " Internal Handling Unit Number of new HU
  ps_hu-venum ls_huheader-hu_id.             " Internal Handling Unit Number of new HU
  ps_hu-rfbel ps_lips-vbeln.
  ps_hu-rfpos ps_lips-posnr.

ENDFORM.                    " GET_ASSIGNED_HU

*&---------------------------------------------------------------------*
*&      Form  GET_BAPI_ERROR
*&---------------------------------------------------------------------*
*       Checks errors in result table
*----------------------------------------------------------------------*
*      -->P_LT_RETURN  text
*----------------------------------------------------------------------*
FORM get_bapi_error
  USING pt_return TYPE gtty_result.

  FIELD-SYMBOLS<fs_return> LIKE bapiret2.

  LOOP AT pt_return ASSIGNING <fs_return>.
    IF <fs_return>-type 'E' OR <fs_return>-type 'A'.
      MESSAGE ID <fs_return>-id
              TYPE <fs_return>-type
              NUMBER <fs_return>-number
              WITH <fs_return>-message_v1 <fs_return>-message_v2
                   <fs_return>-message_v3 <fs_return>-message_v4.
      LEAVE SCREEN.
    ENDIF.
  ENDLOOP.

ENDFORM.                    " GET_BAPI_ERROR








8 comments:

  1. Dear Karel, thanks for that nice documentation helped me a lot.
    Did you ever try to delete a HU created that way?
    When I use the delete function in VL02n (or any other way to delete assigned HUs) the HU record in VEKP remains with Status 0020. I would like to avoid this. Do you habe any idea?
    Regards and thanks Michael

    ReplyDelete
  2. Dear Michael,
    Thanks for feedback. I am not able to help without deeper understanding your situation. In my case every problematically created HU was possible to delete via VL02N without any problem. Via ABAP I would use BAPI_HU_DELETE.

    ReplyDelete
  3. Hi Karel,

    I wonder if you are still monitoring this thread? I am not able to get the material packed using WS_DELIVERY_UPDATE.

    Many thanks,
    Phil

    ReplyDelete
    Replies
    1. Hi Phil,

      sorry for my delay answer. But it’s really hard to give any advice without a touch with your system. For successful packing you need not only proper coding, but also already set right statuses before run of WS_DELIVERY_UPDATE. E.g. I mean confirmed transfer orders, properly set packaging instructions. So it's not only matter of coding.

      Karel

      Delete
  4. Thanks a lot mate ! Close to 3 years and this helped me today !

    ReplyDelete
  5. Thanks a lot Karel.you made by job easy in packing the materials and linking it to delivery.
    Regards,
    Varma.

    ReplyDelete
  6. Hi Karel. Thanks for the post .i am able to pack the item into handling unit linked to delivery now.
    but the delivery items doesn't get update with the packing material as line item.please suggest me if there is any way i can update the packing material as kine item in delivery change through WS_DELIVERY_UPDATE.currently it throws an exception the item doesn't exist.

    ReplyDelete
  7. Just a note. A colleague of mine found, FM SD_DELIVERY_UPDATE_PACKING has better performance comparing to WS_DELIVERY_UPDATE.

    ReplyDelete