Hello experts
,
When I create a Goods Receipt with reference to Purchase Order using BAPI_GOODSMVT_CREATE, the following error message appears.
No goods receipt possible for purchase order <PO number> <line item number>.
But the GR is getting created manually using Transaction MIGO for the same PO number.
Here is the code:
SELECT deliverydocument, “vbeln
deliverydocumentitem, “posnr
plant “werks
FROM i_deliverydocumentitem “lips
INTO TABLE @DATA(dlv_item)
WHERE deliverydocument IN @inb_delv ” OR deliverydocument IN @delv_num )
AND plant IN @Store.
IF sy–subrc = 0.
SELECT deliverydocument, “vbeln
supplier “lifnr
FROM i_deliverydocument “likp
INTO TABLE @DATA(dlv_hdr)
FOR ALL ENTRIES IN @dlv_item
WHERE deliverydocument = @dlv_item–deliverydocument
AND deliverydate IN @s_date_tmp
AND sddocumentcategory = ‘7’.
IF sy–subrc = 0.
SELECT businesspartner, “partner
bpidentificationtype, “type
bpidentificationnumber “idnumber
FROM i_bupaidentification “but0id
INTO TABLE @DATA(bp_id)
FOR ALL ENTRIES IN @dlv_hdr
WHERE businesspartner = @dlv_hdr–supplier
AND bpidentificationtype = ‘GLREG’.
IF sy–subrc = 0.
SELECT low
FROM ztle_constant
INTO TABLE @DATA(legacy_vendor)
WHERE name = ‘ZS4_LEGACY_VENDOR’.
IF sy–subrc = 0 AND legacy_vendor IS NOT INITIAL.
LOOP AT bp_id ASSIGNING FIELD–SYMBOL(<lfs_bp_id>).
* READ TABLE legacy_vendor WITH KEY low = <lfs_bp_id>-bpidentificationnumber TRANSPORTING NO FIELDS.
* IF sy-subrc = 0.
IF line_exists( legacy_vendor[ low = <lfs_bp_id>–bpidentificationnumber ] ).
IF NOT dlv_hdr IS INITIAL.
DELETE dlv_hdr WHERE supplier = <lfs_bp_id>–businesspartner.
ELSE.
EXIT.
ENDIF.
ENDIF.
ENDLOOP.
ENDIF.
IF NOT dlv_hdr IS INITIAL.
SELECT deliverydocument, “vbeln
deliverydocumentitem, “posnr
material, “matnr
plant, “werks
actualdeliveryquantity, “lfimg
baseunit, “Meins
referencesddocument, “vgbel
referencesddocumentitem, “vgpos
goodsmovementstatus “wbsta
FROM i_deliverydocumentitem “lips
INTO TABLE @DATA(dlv_item_final)
FOR ALL ENTRIES IN @dlv_hdr
WHERE deliverydocument = @dlv_hdr–deliverydocument
AND ( goodsmovementstatus = ‘A’
OR goodsmovementstatus = ‘B’).
IF sy–subrc = 0.
SORT dlv_item_final BY referencesddocument referencesddocumentitem.
SELECT b~purchasingdocument, “ebeln
b~purchasingdocumentitem, “ebelp
b~supplierconfirmationcontrolkey, “bstae
a~purchasingdocumenttype “bsart
FROM i_purchasingdocumentitem AS b “ekpo
INNER JOIN i_purchasingdocument AS a “ekko
ON b~purchasingdocument = a~purchasingdocument
FOR ALL ENTRIES IN @dlv_item_final
WHERE b~purchasingdocument = @dlv_item_final–referencesddocument
AND b~purchasingdocumentitem = @dlv_item_final–referencesddocumentitem+1(5)
AND b~supplierconfirmationcontrolkey IN ( ‘Z002’ , ‘Z003’ )
AND a~purchasingdocumenttype = ‘ZDNB’
INTO TABLE @DATA(dsp_po).
IF sy–subrc = 0.
SORT dsp_po BY purchasingdocument purchasingdocumentitem.
LOOP AT dlv_item_final ASSIGNING FIELD–SYMBOL(<lfs_item>).
DATA(index) = sy–tabix.
* READ TABLE dsp_po WITH KEY purchasingdocument = <lfs_item>-referencesddocument
* purchasingdocumentitem = <lfs_item>-referencesddocumentitem+1(5)
* BINARY SEARCH
* TRANSPORTING NO FIELDS.
* IF sy-subrc NE 0.
IF NOT line_exists( dsp_po[ purchasingdocument = <lfs_item>–referencesddocument
purchasingdocumentitem = <lfs_item>–referencesddocumentitem+1(5) ] ).
DELETE dlv_item_final INDEX index.
ENDIF.
ENDLOOP.
ENDIF.
ELSE.
MESSAGE ‘GR is already done for this delivery’(007) TYPE ‘I’.
LEAVE LIST-PROCESSING.
ENDIF.
CLEAR: goodsmvt_item.
SORT dlv_hdr BY deliverydocument.
SORT dlv_item_final BY deliverydocument deliverydocumentitem.
LOOP AT dlv_hdr ASSIGNING FIELD–SYMBOL(<lfs_dlv_hdr>).
READ TABLE dlv_item_final ASSIGNING FIELD–SYMBOL(<lfs_dlv_item>) WITH KEY deliverydocument = <lfs_dlv_hdr>–deliverydocument
BINARY SEARCH.
IF sy–subrc = 0.
DATA(item_indx) = sy–tabix.
goodsmvt_header–pstng_date = sy–datum.
goodsmvt_header–ref_doc_no = <lfs_dlv_item>–referencesddocument.
goodsmvt_code–gm_code = gm_code_gr.
LOOP AT dlv_item_final ASSIGNING <lfs_item> FROM item_indx.
IF <lfs_item>–deliverydocument NE <lfs_dlv_hdr>–deliverydocument.
EXIT.
ENDIF.
APPEND VALUE #( material = <lfs_item>–material
plant = <lfs_item>–plant
no_more_gr = abap_true
mvt_ind = movement_indicator
move_type = movement_type_gr
entry_qnt = <lfs_item>–actualdeliveryquantity
entry_uom = <lfs_item>–baseunit
po_number = <lfs_item>–referencesddocument
po_item = <lfs_item>–referencesddocumentitem+1(5)
ref_doc = <lfs_item>–referencesddocument
ref_doc_it = <lfs_item>–referencesddocumentitem+1(5)
deliv_numb = <lfs_item>–deliverydocument
deliv_item = <lfs_item>–deliverydocumentitem ) TO goodsmvt_item.
ENDLOOP.
IF goodsmvt_item IS NOT INITIAL.
CALL FUNCTION ‘BAPI_GOODSMVT_CREATE’
EXPORTING
goodsmvt_header = goodsmvt_header
goodsmvt_code = goodsmvt_code
IMPORTING
materialdocument = materialdocument
matdocumentyear = matdocumentyear
TABLES
goodsmvt_item = goodsmvt_item
return = return.
READ TABLE return WITH KEY type = ‘E’ TRANSPORTING NO FIELDS.
IF sy–subrc = 0.
CALL FUNCTION ‘BAPI_TRANSACTION_ROLLBACK’.
ELSE.
CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’
EXPORTING
wait = abap_true.
ENDIF.
ENDIF.
ENDIF.
ENDLOOP.
ENDIF.
ENDIF.
ENDIF.
ENDIF.