ABAP-笔记
METHOD OPERATE.
*&---发票校验
DATA:LI_HEAD TYPE BAPI_INCINV_CREATE_HEADER,
LI_ITEM TYPE BAPI_INCINV_CREATE_ITEM,
LT_ITEM TYPE STANDARD TABLE OF BAPI_INCINV_CREATE_ITEM,
LVC_TAX TYPE BAPI_INCINV_CREATE_TAX,
LVT_TAX TYPE STANDARD TABLE OF BAPI_INCINV_CREATE_TAX,
LT_RETURN TYPE STANDARD TABLE OF BAPIRET2,
LS_ACCOUNT TYPE BAPI_INCINV_CREATE_ACCOUNT,
LT_ACCOUNT TYPE STANDARD TABLE OF BAPI_INCINV_CREATE_ACCOUNT,
LS_GL TYPE BAPI_INCINV_CREATE_GL_ACCOUNT,
LT_GL TYPE STANDARD TABLE OF BAPI_INCINV_CREATE_GL_ACCOUNT.
DATA: INVOICEDOCNUMBER TYPE BAPI_INCINV_FLD-INV_DOC_NO,
FISCALYEAR TYPE BAPI_INCINV_FLD-FISC_YEAR.
DATA LS_ITEMS TYPE ZDT_INVOICEVERIFICATION_RESQ_I.
DATA: LV_MESSAGE TYPE BAPIRET2-MESSAGE.
DATA: LO_REASONCODE TYPE STGRD.
DATA: L_GJAHR TYPE GJAHR.
"日志
DATA: LS_POLOG TYPE ZPO_HEADER,
LT_POLOG TYPE TABLE OF ZPO_HEADER.
OUTPUT-ORDERID = INPUT-ORDERID.
CLEAR:LI_HEAD.
DATA(LT_ITEMS) = INPUT-ITEMS.
SORT LT_ITEMS BY MWSKZ_MRM.
LOOP AT INPUT-ITEMS ASSIGNING FIELD-SYMBOL(<FS_ITEMS>) .
CASE <FS_ITEMS>-ACTION.
WHEN 'A'."发票校验
LI_HEAD-INVOICE_IND = 'X'.
LI_HEAD-DOC_TYPE = 'RE'.
LI_HEAD-REF_DOC_NO = INPUT-XBLNR.
LI_HEAD-PSTNG_DATE = INPUT-BLDAT.
LI_HEAD-DOC_DATE = INPUT-BUDAT.
LI_HEAD-COMP_CODE = INPUT-BUKRS.
LI_HEAD-GROSS_AMOUNT = INPUT-RMWWR.
LI_HEAD-INV_TRAN = ''.
LI_HEAD-CURRENCY = 'CNY'.
IF INPUT-INV_TRAN = '1'.
LI_HEAD-INVOICE_IND = 'X'.
ELSE.
LI_HEAD-INVOICE_IND = ' '.
ENDIF.
SELECT SINGLE * FROM EKPO INTO @DATA(LS_EKPO) WHERE EBELN = @<FS_ITEMS>-EBELN.
IF SY-SUBRC = 0.
LI_ITEM-PO_UNIT = LS_EKPO-MEINS. "单位
ENDIF.
LI_ITEM-INVOICE_DOC_ITEM = SY-TABIX.
LI_ITEM-PO_NUMBER = <FS_ITEMS>-EBELN.
LI_ITEM-PO_ITEM = <FS_ITEMS>-EBELP.
LI_ITEM-TAX_CODE = <FS_ITEMS>-MWSKZ_MRM. "税码
LI_ITEM-ITEM_AMOUNT = <FS_ITEMS>-BAPIWRBTR. "不含税金额
LI_ITEM-QUANTITY = <FS_ITEMS>-MENGE. "数量
APPEND LI_ITEM TO LT_ITEM.
READ TABLE LT_ITEMS TRANSPORTING NO FIELDS WITH KEY MWSKZ_MRM = <FS_ITEMS>-MWSKZ_MRM BINARY SEARCH.
IF SY-SUBRC = 0.
LOOP AT LT_ITEMS ASSIGNING FIELD-SYMBOL(<LS_ITEMS>) WHERE MWSKZ_MRM = <FS_ITEMS>-MWSKZ_MRM .
LVC_TAX-TAX_CODE = <LS_ITEMS>-MWSKZ_MRM. "税码
LVC_TAX-TAX_BASE_AMOUNT = <LS_ITEMS>-BAPIWRBTR + LVC_TAX-TAX_BASE_AMOUNT.
LVC_TAX-ITEMNO_TAX = <LS_ITEMS>-EBELN.
LVC_TAX-TAX_AMOUNT = <LS_ITEMS>-WMWST + LVC_TAX-TAX_AMOUNT .
ENDLOOP.
APPEND LVC_TAX TO LVT_TAX.
CLEAR:LVC_TAX.
DELETE LT_ITEMS WHERE MWSKZ_MRM = <FS_ITEMS>-MWSKZ_MRM.
ENDIF.
CLEAR LI_ITEM.
WHEN 'B'."取消发票校验
SELECT SINGLE * FROM RBKP INTO @DATA(LO_RBKP) WHERE BELNR = @<FS_ITEMS>-BELNR AND GJAHR = @<FS_ITEMS>-GJAHR AND BUKRS = @INPUT-BUKRS.
IF LO_RBKP-GJAHR = SY-DATUM+0(4).
IF LO_RBKP-BUDAT+4(2) EQ SY-DATUM+4(2).
LO_REASONCODE = '01'."当前期间
ELSE.
LO_REASONCODE = '02'."关闭期间
ENDIF.
ENDIF.
L_GJAHR = <FS_ITEMS>-GJAHR.
CALL FUNCTION 'BAPI_INCOMINGINVOICE_CANCEL'
EXPORTING
INVOICEDOCNUMBER = <FS_ITEMS>-BELNR
FISCALYEAR = L_GJAHR
REASONREVERSAL = LO_REASONCODE
IMPORTING
INVOICEDOCNUMBER_REVERSAL = INVOICEDOCNUMBER
FISCALYEAR_REVERSAL = FISCALYEAR
TABLES
RETURN = LT_RETURN.
LOOP AT LT_RETURN INTO DATA(LS_RETURN) WHERE TYPE = 'A'
OR TYPE = 'E'
OR TYPE = 'X'.
OUTPUT-SUBRC = '1'.
OUTPUT-MESSAGE = LS_RETURN-MESSAGE && OUTPUT-MESSAGE.
ENDLOOP.
IF OUTPUT-SUBRC = '1'.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
OUTPUT-MESSAGE = '冲销失败:' && OUTPUT-MESSAGE.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
OUTPUT-SUBRC = '0'.
OUTPUT-MESSAGE = '冲销成功!'.
ENDIF.
WHEN OTHERS.
ENDCASE.
ENDLOOP.
CHECK LI_HEAD-COMP_CODE IS NOT INITIAL.
"发票校验BAPI
CALL FUNCTION 'BAPI_INCOMINGINVOICE_CREATE'
EXPORTING
HEADERDATA = LI_HEAD
* ADDRESSDATA =
IMPORTING
INVOICEDOCNUMBER = INVOICEDOCNUMBER
FISCALYEAR = FISCALYEAR
TABLES
ITEMDATA = LT_ITEM
TAXDATA = LVT_TAX[]
RETURN = LT_RETURN.
IF INVOICEDOCNUMBER IS NOT INITIAL .
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
OUTPUT-MBLNR = INVOICEDOCNUMBER.
OUTPUT-MJAHR = FISCALYEAR.
OUTPUT-SUBRC = '0'.
* SELECT BUZEI
* FROM RSEG
* INTO TABLE @DATA(LT_RSEG)
* WHERE BELNR = @INVOICEDOCNUMBER
* AND GJAHR = @FISCALYEAR.
* LOOP AT LT_RSEG INTO DATA(LS_RSEG).
* LS_ITEMS-ZEILE = LS_RSEG-BUZEI.
* APPEND LS_ITEMS TO OUTPUT-ITEMS.
* ENDLOOP.
DATA:LI_AWKEY TYPE BKPF-AWKEY.
LI_AWKEY = OUTPUT-MBLNR && OUTPUT-MJAHR.
SELECT SINGLE BELNR
FROM BKPF
INTO OUTPUT-BELNR
WHERE AWKEY = LI_AWKEY.
LS_POLOG-EBELN = OUTPUT-BELNR.
LS_POLOG-ORDERID = INPUT-ORDERID.
LS_POLOG-SUBRC = '0'.
APPEND LS_POLOG TO LT_POLOG.
CLEAR LS_POLOG.
MODIFY ZPO_HEADER FROM TABLE LT_POLOG.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
ELSE.
LOOP AT LT_RETURN INTO LS_RETURN WHERE TYPE = 'A'
OR TYPE = 'E'
OR TYPE = 'X'.
CALL FUNCTION 'FORMAT_MESSAGE'
EXPORTING
ID = LS_RETURN-ID
NO = LS_RETURN-LOG_MSG_NO
V1 = LS_RETURN-MESSAGE_V1
V2 = LS_RETURN-MESSAGE_V2
V3 = LS_RETURN-MESSAGE_V3
V4 = LS_RETURN-MESSAGE_V4
IMPORTING
MSG = LV_MESSAGE
EXCEPTIONS
NOT_FOUND = 1
OTHERS = 2.
IF LS_RETURN-MESSAGE IS NOT INITIAL.
LV_MESSAGE = LS_RETURN-MESSAGE.
ENDIF.
OUTPUT-SUBRC = '1'.
OUTPUT-MESSAGE = LV_MESSAGE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
RETURN.
ENDLOOP.
ENDIF.
ENDMETHOD.