ABAP:MR8M采购发票冲销BAPI
MR8M采购发票冲销BAPI及删除预制发票BDC
FUNCTION ZSDFU021. *"---------------------------------------------------------------------- *"*"本地接口: *" IMPORTING *" VALUE(I_ID) TYPE ZRFC_LOGID OPTIONAL *" VALUE(I_CANUM) TYPE CANUM OPTIONAL *" EXPORTING *" VALUE(E_CODE) TYPE MSGTY *" VALUE(E_MSG) TYPE MSGTXT *" VALUE(E_FLOW_END) TYPE CHAR01 *" TABLES *" ET_RETURN STRUCTURE ZRFCT005 OPTIONAL *"---------------------------------------------------------------------- DATA:lt_zsdt028 TYPE TABLE OF zsdt028, ls_zsdt028 LIKE LINE OF lt_zsdt028, ls_zsdt026 TYPE zsdt026, lt_zsdt026 TYPE TABLE OF zsdt026, ls_zsdt017 TYPE zsdt017, ls_zsdt017_1 TYPE zsdt017, lt_zsdt017 TYPE TABLE OF zsdt017. DATA:ls_zrfct002 TYPE zrfct002. DATA:ls_zrfct005 TYPE zrfct005. DATA:BEGIN OF ls_objkey, objkey TYPE zsdt008-objkey, END OF ls_objkey. DATA lt_objkey LIKE TABLE OF ls_objkey. DATA:BEGIN OF ls_zrfc_logid, zrfc_logid TYPE zsdt017-zrfc_logid, objkey TYPE zsdt008-objkey, END OF ls_zrfc_logid. DATA lt_zrfc_logid LIKE TABLE OF ls_zrfc_logid. DATA:lv_fkdat TYPE vbrk-fkdat, lv_stgrd TYPE stgrd. "获取源表数据 SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_zsdt028 FROM zsdt028 WHERE zrfc_logid = i_id. IF lt_zsdt028 IS INITIAL. e_code = 'E'. e_msg = '无可执行数据'. RETURN. ENDIF. LOOP AT lt_zsdt028 INTO ls_zsdt028. ls_objkey-objkey = ls_zsdt028-belnr. lv_fkdat = ls_zsdt028-budat. lv_stgrd = ls_zsdt028-stgrd. APPEND ls_objkey TO lt_objkey. ls_zsdt028-zposnr = ''. MODIFY lt_zsdt028 FROM ls_zsdt028. ENDLOOP. DELETE ADJACENT DUPLICATES FROM lt_objkey COMPARING ALL FIELDS. CALL METHOD zcl_rfc=>get_source EXPORTING i_id = i_id i_canum = i_canum IMPORTING e_zrfct002 = ls_zrfct002. "获取开票流程步骤 SELECT SINGLE zzrfcid zcanum INTO (ls_zsdt026-zzrfcid,ls_zsdt026-zcanum) FROM zsdt026 WHERE zrfcid = ls_zrfct002-zrfcid AND canum = i_canum. "根据开票流程步骤找到发票数据 SELECT zrfc_logid objkey INTO TABLE lt_zrfc_logid FROM zsdt017 FOR ALL ENTRIES IN lt_objkey WHERE objkey = lt_objkey-objkey AND zrfcid = ls_zsdt026-zzrfcid. IF lt_zrfc_logid IS INITIAL. e_code = 'E'. e_msg = '无可执行数据'. RETURN. ENDIF. SORT lt_zrfc_logid BY zrfc_logid. SELECT * INTO TABLE lt_zsdt017"开票采购订单 FROM zsdt017 FOR ALL ENTRIES IN lt_zrfc_logid WHERE zrfc_logid = lt_zrfc_logid-zrfc_logid AND canum = ls_zsdt026-zcanum. SORT lt_zsdt017 BY zrfc_logid. SORT lt_zrfc_logid BY objkey. READ TABLE lt_zsdt017 INTO ls_zsdt017_1 INDEX 1. DATA lv_invoicedocnumber_reversal LIKE bapi_incinv_fld-inv_doc_no. "采购发票 DATA:lt_return LIKE TABLE OF bapiret2, ls_return TYPE bapiret2. DATA:lv_gjahr TYPE rbkp-gjahr. CLEAR:lv_gjahr. lv_gjahr = ls_zsdt017_1-gjahr. * SELECT SINGLE gjahr INTO lv_gjahr FROM rbkp WHERE belnr = ls_zsdt017_1-objkey. "判断发票是否产生会计凭证 DATA:lv_rbstat TYPE rbkp-rbstat. CLEAR:lv_rbstat. SELECT SINGLE rbstat INTO lv_rbstat FROM rbkp WHERE belnr = ls_zsdt017_1-objkey AND gjahr = lv_gjahr. lv_invoicedocnumber_reversal = ls_zsdt017_1-objkey. IF lv_rbstat = '5'." CALL FUNCTION 'BAPI_INCOMINGINVOICE_CANCEL' EXPORTING invoicedocnumber = lv_invoicedocnumber_reversal fiscalyear = lv_gjahr reasonreversal = lv_stgrd postingdate = lv_fkdat IMPORTING invoicedocnumber_reversal = lv_invoicedocnumber_reversal * FISCALYEAR_REVERSAL = TABLES return = lt_return. . READ TABLE lt_return INTO ls_return WITH KEY type = 'E'. IF sy-subrc = 0. CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'. e_code = 'E'. e_msg = ls_return-message. ELSE. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = 'X'. "更新冲销数据 CLEAR:ls_zsdt017. DATA:lv_awkey TYPE bkpf-awkey. lv_awkey = lv_invoicedocnumber_reversal && lv_gjahr. SELECT SINGLE belnr FROM bkpf INTO ls_zsdt017-belnr WHERE awkey = lv_awkey. ls_zsdt017-zrfc_logid = i_id. ls_zsdt017-canum = ls_zrfct002-canum. ls_zsdt017-objtype = ls_zrfct002-objtype_save. ls_zsdt017-objkey = lv_invoicedocnumber_reversal. ls_zsdt017-zrfcid = ls_zrfct002-zrfcid. ls_zsdt017-fksto = ''. * ls_zsdt017-belnr = lv_invoicedocnumber_reversal. ls_zsdt017-zzrfc_logid = ls_zsdt017_1-zrfc_logid. ls_zsdt017-zcanum = ls_zsdt017_1-canum. ls_zsdt017-zobjkey = ls_zsdt017_1-objkey. ls_zsdt017-zobjtype = ls_zsdt017_1-objtype. APPEND ls_zsdt017 TO lt_zsdt017. "更新原凭证数据 CLEAR:ls_zsdt017. SELECT SINGLE * INTO ls_zsdt017 FROM zsdt017 WHERE zrfc_logid = ls_zsdt017_1-zrfc_logid AND canum = ls_zsdt017_1-canum AND objkey = ls_zsdt017_1-objkey AND objtype = ls_zsdt017_1-objtype. ls_zsdt017-fksto = 'X'. APPEND ls_zsdt017 TO lt_zsdt017. MODIFY zsdt017 FROM TABLE lt_zsdt017. e_code = 'S'. CONCATENATE 'IN:' lv_invoicedocnumber_reversal '冲销成功' INTO e_msg. CLEAR:ls_zrfct005. ls_zrfct005-status = '53'. ls_zrfct005-msgty = e_code. ls_zrfct005-msgid = '00'. ls_zrfct005-msgno = '001'. ls_zrfct005-msgv1 = e_msg. ls_zrfct005-msgv4 = lv_invoicedocnumber_reversal. APPEND ls_zrfct005 TO et_return. ENDIF. ELSEIF lv_rbstat = 'A'."预制发票删除 CLEAR:messtab,bdcdata[]. PERFORM bdc_dynpro USING 'SAPLMR1M' '0300'. PERFORM bdc_field USING 'BDC_CURSOR' 'RBKPV-BELNR'. PERFORM bdc_field USING 'BDC_OKCODE' '=RBAN'. PERFORM bdc_field USING 'RBKPV-BELNR' ls_zsdt017_1-objkey. PERFORM bdc_field USING 'RBKPV-GJAHR' ''. PERFORM bdc_field USING 'UF05A-STGRD' '01'. PERFORM bdc_dynpro USING 'SAPLMR1M' '6000'. PERFORM bdc_field USING 'BDC_OKCODE' '/EPPCH'. PERFORM bdc_dynpro USING 'SAPLMR1M' '6000'. PERFORM bdc_field USING 'BDC_OKCODE' '/EDELE'. gwa_ctu-defsize = 'X'. gwa_ctu-dismode = 'N'. gwa_ctu-updmode = 'S'. CALL TRANSACTION 'MR8M' USING bdcdata OPTIONS FROM gwa_ctu MESSAGES INTO messtab. e_code = 'S'. CONCATENATE '预制IN:' ls_zsdt017_1-objkey '删除成功' INTO e_msg. CLEAR:ls_zrfct005. ls_zrfct005-status = '53'. ls_zrfct005-msgty = e_code. ls_zrfct005-msgid = '00'. ls_zrfct005-msgno = '001'. ls_zrfct005-msgv1 = e_msg. ls_zrfct005-msgv4 = lv_invoicedocnumber_reversal. APPEND ls_zrfct005 TO et_return. ENDIF. ENDFUNCTION.