根据SAP 提供的解决方案,创建一个程序zzmbstor 可以直接冲销ERP中凭证。
SAP Note 3350383
程序代码:
REPORT zzmbstor.
*********************************************************************
* Inconsistency between EWM and S/4/ERP
* Report to post in S/4/ERP only, without EWM and without *
* distributed delivery *
* *
* Report to cancel material documents coming from EWM, posted *
* without delivery, but in Trx MIGO, a material document *
* cancellation would create a delivery (distributed to EWM) *
* *
* *
* LF_BUDAT => Posting date for the reversal *
* LF_MJAHR => Posting year of the material document to reverse *
* LF_MBLNR => Goods issue document (with leading zeroes) *
* LF_TEST => When 'X' it is test mode only, otherwise GI will be *
* cancelled *
*********************************************************************
TABLES: mkpf, imkpf, emkpf, likp.
DATA: BEGIN OF imseg OCCURS 0.
INCLUDE STRUCTURE imseg.
DATA: END OF imseg.
DATA: BEGIN OF emseg OCCURS 0.
INCLUDE STRUCTURE emseg.
DATA: END OF emseg.
DATA: g_flag_answer(01) TYPE C.
PARAMETERS: lf_budat LIKE sy-datlo DEFAULT sy-datlo OBLIGATORY,
lf_mjahr LIKE mkpf-mjahr DEFAULT sy-datlo(4) OBLIGATORY,
lf_mblnr LIKE mkpf-mblnr OBLIGATORY,
lf_test TYPE C DEFAULT 'X'.
START-OF-SELECTION.
SELECT SINGLE * FROM mkpf WHERE mblnr = lf_mblnr
AND mjahr = lf_mjahr.
IF sy-subrc IS INITIAL.
IF NOT ( mkpf-tcode2(2) = 'MB' OR mkpf-tcode2(2) = 'VL' ) OR
mkpf-spe_logsys IS INITIAL OR
mkpf-spe_mdnum_ewm IS INITIAL.
WRITE: / 'Goods movement cannot be reverted with this report, use other transaction!'.
EXIT.
ENDIF.
IF mkpf-tcode2(2) = 'VL'.
mkpf-tcode2 = 'VL09'.
ENDIF.
CALL FUNCTION 'POPUP_TO_CONFIRM'
EXPORTING
TITLEBAR = 'Cancel document without EWM update'
text_question = 'Do you really want to proceed?'
text_button_1 = 'Go on'
icon_button_1 = 'ICON_OKAY'
text_button_2 = 'Go back'
icon_button_2 = 'ICON_CANCEL'
default_button = '2'
display_cancel_button = ' '
IMPORTING
answer = g_flag_answer
EXCEPTIONS
OTHERS = 1.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty
NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2
sy-msgv3 sy-msgv4.
ENDIF.
IF g_flag_answer = '2'.
* the user answered with "no" : stop
EXIT.
ENDIF.
CALL FUNCTION 'MB_CANCEL_GOODS_MOVEMENT'
EXPORTING
budat = lf_budat
mblnr = lf_mblnr
mjahr = lf_mjahr
tcode = mkpf-tcode2
bfwms = '2'
IMPORTING
emkpf = emkpf
TABLES
emseg = emseg
imseg = imseg
EXCEPTIONS
OTHERS = 1.
*... No error found, then it will be posted
IF emkpf-subrc EQ 1.
IF lf_test IS INITIAL.
CALL FUNCTION 'MB_POST_GOODS_MOVEMENT'
IMPORTING
emkpf = emkpf
EXCEPTIONS
OTHERS = 0.
IF emkpf-subrc = 0.
COMMIT WORK.
WRITE: / 'Goods movement was cancelled successfully with document:',
emkpf-mblnr.
ELSE.
ROLLBACK WORK.
WRITE: / 'Error in posting'.
WRITE: / emkpf-msgid,
emkpf-msgno,
emkpf-msgty,
emkpf-msgv1,
emkpf-msgv2,
emkpf-msgv3,
emkpf-msgv4.
ENDIF.
ELSE.
WRITE: / 'Testmode: Goods movement could be cancelled !'.
ENDIF.
ELSE.
*... otherwise write an error-log
LOOP AT emseg.
WRITE: /
emseg-msgid,
emseg-msgno,
emseg-msgty,
emseg-msgv1,
emseg-msgv2,
emseg-msgv3,
emseg-msgv4.
ENDLOOP.
WRITE: / emkpf-msgid,
emkpf-msgno,
emkpf-msgty,
emkpf-msgv1,
emkpf-msgv2,
emkpf-msgv3,
emkpf-msgv4.
ENDIF.
ELSE.
WRITE: / 'Material document to reverse does not exsist'.
ENDIF.
AT SELECTION-SCREEN.
* the user needs authorization for debugging
* authorization check can be replaced with custom check if required
AUTHORITY-CHECK OBJECT 'S_DEVELOP'
ID 'DEVCLASS' DUMMY
ID 'OBJTYPE' FIELD 'DEBUG'
ID 'OBJNAME' DUMMY
ID 'P_GROUP' DUMMY
ID 'ACTVT' FIELD '03'.
IF sy-subrc <> 0.
* You have no authorization for this trans./event
MESSAGE e858(m7).
ENDIF.
MESSAGE i895(m7)
WITH 'CAUTION: Only for double posted EWM documents!'.
MESSAGE w895(m7)
WITH 'Otherwise it causes discrepancies between ERP/EWM!'.