TYPES:
abap_bool TYPE c 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 '-'.
CONSTANTS: cv_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'.
DATA: gv_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.
*perform pack_delivery using '' ''.
" 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 e398(00) WITH TEXT-017.
ENDIF.
DATA: lt_hu LIKE STANDARD TABLE OF hum_rehang_hu,
ls_hu LIKE LINE OF lt_hu,
lv_pksta TYPE vbup-pksta.
DATA: lt_vekp TYPE STANDARD TABLE OF vekp,
lt_vepo TYPE STANDARD TABLE OF vepo,
lv_exidv TYPE exidv.
DATA: ls_vbkok LIKE vbkok,
lt_hus LIKE STANDARD TABLE OF vekpvb,
ls_hus LIKE LINE OF lt_hus.
" Handling-Unit Confirmation: Header Data
DATA: lt_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 > 0 ).
" 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 0 ) 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.
CLEAR: ls_prot.
READ TABLE lt_prot INDEX 1 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 vbtypl
CHANGING ps_hu TYPE hum_rehang_hu.
DATA: lt_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 ls_headerproposal-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 1 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