发票校验BAPI BAPI_INCOMINGINVOICE_CREATE demo
LOOP AT lt_mseg.
SELECT SINGLE * FROM ekpo
WHERE ebeln = lt_mseg-ebeln AND ebelp = lt_mseg-ebelp.
SELECT SINGLE waers INTO waers FROM ekko
WHERE ebeln = lt_mseg-ebeln.
IF ekpo-repos = ‘X’.
READ TABLE it_item_invoice WITH KEY po_number = lt_mseg-ebeln
po_item = lt_mseg-ebelp
ref_doc_it = lt_mseg-zeile.
IF sy-subrc = 0.
item_amount = lt_mseg-wrbtr.
it_item_invoice-item_amount = it_item_invoice-item_amount + item_amount. "凭证货币金额
it_item_invoice-quantity = it_item_invoice-quantity + lt_mseg-menge. "数量
MODIFY it_item_invoice INDEX sy-tabix.
header_amount = header_amount + item_amount.
ELSE.
num = num + 1.
it_item_invoice-invoice_doc_item = num. "发票凭证中的凭证项目
it_item_invoice-po_number = lt_mseg-ebeln . "订单号
it_item_invoice-po_item = lt_mseg-ebelp. "项目编号
item_amount = lt_mseg-wrbtr.
it_item_invoice-item_amount = item_amount. "凭证货币金额
it_item_invoice-quantity = lt_mseg-menge. "数量
it_item_invoice-tax_code = ekpo-mwskz. "税码
it_item_invoice-po_unit = ekpo-meins. "单位
it_item_invoice-ref_doc = lt_mseg-mblnr.
it_item_invoice-ref_doc_it = lt_mseg-zeile.
it_item_invoice-ref_doc_year = lt_mseg-mjahr.
" 取税码
IF ekpo-mwskz <> ''.
REFRESH l_ftaxp.CLEAR l_ftaxp.CLEAR p_kbetr.
CALL FUNCTION 'GET_TAX_PERCENTAGE'
EXPORTING
aland = 'CN'
datab = sy-datum
mwskz = ekpo-mwskz
txjcd = 'TAXCN'
TABLES
t_ftaxp = l_ftaxp.
LOOP AT l_ftaxp WHERE kschl = 'MWVS'.
p_kbetr = l_ftaxp-kbetr / 10.
ENDLOOP.
header_amount1 = header_amount1 + ( it_item_invoice-item_amount + it_item_invoice-item_amount * p_kbetr / 100 ).
ELSE.
header_amount1 = header_amount1 + item_amount.
ENDIF.
APPEND it_item_invoice.
ENDIF.
ENDIF.
ENDLOOP.
header_amount = header_amount1.
SELECT SINGLE waers bukrs
INTO (it_header_invoice-currency,it_header_invoice-comp_code) "货币代码,公司代码
FROM ekko WHERE ebeln = l_ebeln .
IF p_date IS NOT INITIAL.
it_header_invoice-pstng_date = p_date."过账日期
ELSE.
it_header_invoice-pstng_date = v_wadat_ist.
ENDIF.
IF p_date IS NOT INITIAL.
it_header_invoice-doc_date = p_date. "凭证中的凭证日期
ELSE.
it_header_invoice-doc_date = sy-datum. "凭证中的凭证日期
ENDIF.
it_header_invoice-bline_date = it_header_invoice-pstng_date. "基线日期
it_header_invoice-ref_doc_no = p_vbeln_in. "参照
it_header_invoice-header_txt = ''. "抬头文本
it_header_invoice-gross_amount = header_amount. "凭证货币的总发票金额
it_header_invoice-calc_tax_ind = 'X'.
it_header_invoice-invoice_ind = 'X'. "记账标识
it_header_invoice-deliv_posting = 'S'.
SET UPDATE TASK LOCAL.
CALL FUNCTION 'BAPI_INCOMINGINVOICE_CREATE'
EXPORTING
headerdata = it_header_invoice
IMPORTING
invoicedocnumber = invoicedocnumber
TABLES
itemdata = it_item_invoice
return = it_return_invoice.
IF invoicedocnumber IS NOT INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
v_message = '采购发票检验成功'.
v_subrc = 'S'.