创建采购发票:要求跟销售发票一致。
FORM fr_miro CHANGING f_fs TYPE ty_out .
DATA: ls_headerdata LIKE bapi_incinv_create_header,
lt_itemdata LIKE TABLE OF bapi_incinv_create_item,
ls_itemdata LIKE bapi_incinv_create_item,
lt_return LIKE TABLE OF bapiret2,
ls_return LIKE bapiret2,
lv_invoicedocnumber LIKE bapi_incinv_fld-inv_doc_no.
DATA: lv_gross TYPE bapi_rmwwr.
DATA: lv_xsje TYPE bapi_rmwwr.
TYPES: BEGIN OF ty_ekpo,
ebeln TYPE ekko-ebeln,
bukrs TYPE ekko-bukrs,
waers TYPE ekko-waers,
ebelp TYPE ekpo-ebelp,
netpr TYPE ekpo-netpr,
menge TYPE ekpo-menge,
meins TYPE ekpo-meins,
mblnr TYPE mseg-mblnr,
zeile TYPE mseg-zeile,
mjahr TYPE mseg-mjahr,
bwart TYPE mseg-bwart,
belnr TYPE re_belnr,
msg TYPE string, "销售发票
retpo TYPE ekpo-retpo,
status TYPE c LENGTH 4,
END OF ty_ekpo.
DATA: lt_ekpo TYPE TABLE OF ty_ekpo,
ls_ekpo TYPE ty_ekpo,
ls_ekpotmp TYPE ty_ekpo.
DATA:lt_vbeln TYPE TABLE OF ty_out WITH HEADER LINE.
DATA: lv_index TYPE i,
lv_mseg TYPE string.
DATA: l_vbeln TYPE vbeln,
l_lfpos TYPE lfpos.
DATA:xs_mwsbk TYPE vbrk-mwsbk,
xs_mwsbk_h TYPE vbrk-mwsbk.
DATA: lt_tax TYPE TABLE OF bapi_incinv_create_tax,
ls_tax TYPE bapi_incinv_create_tax.
"校验发票金额。20241029
"跑太快了取不到数
"400毫秒
DO 10000000 TIMES.
ENDDO.
DO 10000 TIMES.
SELECT SINGLE netwr , mwsbk INTO @DATA(l_xsje) FROM vbrk WHERE vbeln = @f_fs-vbeln_vf.
IF sy-subrc = 0.
EXIT.
ENDIF.
ENDDO.
lv_xsje = l_xsje-netwr + l_xsje-mwsbk.
CLEAR lt_ekpo.
SELECT
a~ebeln
a~ebelp
a~mblnr
a~mjahr
c~bukrs
c~waers
b~netpr
b~meins
b~menge
d~retpo
INTO CORRESPONDING FIELDS OF TABLE lt_ekpo
FROM ztsto_04 AS a
LEFT JOIN ztsto_03 AS b ON a~vgbel = b~vgbel
LEFT JOIN ekko AS c ON c~ebeln = a~ebeln
LEFT JOIN ekpo AS d ON d~ebeln = a~ebeln AND d~ebelp = a~ebelp
WHERE a~vbeln_d = f_fs-vbeln_d.
CLEAR: l_lfpos, xs_mwsbk,lv_gross.
LOOP AT lt_ekpo ASSIGNING FIELD-SYMBOL(<fs_fh>) .
lv_index = lv_index + 1.
l_lfpos = l_lfpos + 2.
ls_itemdata-invoice_doc_item = lv_index.
ls_itemdata-po_number = <fs_fh>-ebeln.
ls_itemdata-po_item = <fs_fh>-ebelp.
ls_itemdata-item_amount = <fs_fh>-menge * f_fs-kbetr.
CALL FUNCTION 'ROUND'
EXPORTING
decimals = 2
input = ls_itemdata-item_amount
sign = 'X'
IMPORTING
output = ls_itemdata-item_amount
EXCEPTIONS
input_invalid = 1
overflow = 2
type_invalid = 3
OTHERS = 4.
IF sy-subrc <> 0.
-
Implement suitable error handling here
ENDIF.
ls_itemdata-quantity = <fs_fh>-menge. ls_itemdata-po_unit = <fs_fh>-meins.. ls_itemdata-tax_code = 'J2'. ls_itemdata-ref_doc = <fs_fh>-mblnr."参考凭证项目 ls_itemdata-ref_doc_year = <fs_fh>-mjahr. ls_itemdata-ref_doc_it = l_lfpos." 参考凭证项目 APPEND ls_itemdata TO lt_itemdata. "总税额 xs_mwsbk = xs_mwsbk + ls_itemdata-item_amount * 13 / 100.. AT END OF ebeln.
*&header
CLEAR: ls_headerdata.
IF <fs_fh>-retpo = 'X'."退货
ls_headerdata-invoice_ind = ''.
ls_headerdata-return_posting = 'H'.
ELSE.
ls_headerdata-invoice_ind = 'X'.
ENDIF.
ls_headerdata-invoice_ind = 'X'.
ls_headerdata-doc_date = sy-datum.
ls_headerdata-pstng_date = sy-datum.
ls_headerdata-comp_code = <fs_fh>-bukrs..
ls_headerdata-currency = <fs_fh>-waers.
IF ls_headerdata-comp_code = '2000'.
ls_headerdata-bus_area = '2080'.
ELSE.
ls_headerdata-bus_area = '2200'.
ENDIF.
ls_headerdata-bline_date = sy-datum. "基准日期
ls_headerdata-deliv_posting = 'S'.
ls_headerdata-pmnttrms = '0001'.
*ls_headerdata-item_text = 'text'.
"税
CALL FUNCTION 'ROUND'
EXPORTING
decimals = 2
input = xs_mwsbk
sign = 'X'
IMPORTING
output = xs_mwsbk
EXCEPTIONS
input_invalid = 1
overflow = 2
type_invalid = 3
OTHERS = 4.
IF sy-subrc <> 0.
-
Implement suitable error handling here
ENDIF.
"税 ls_tax-tax_code = 'J2'. ls_tax-tax_amount = xs_mwsbk. ". APPEND ls_tax TO lt_tax. CLEAR ls_tax. "校验税额 采购总税 - 销售税 IF xs_mwsbk - l_xsje-mwsbk > 0. "税 LOOP AT lt_tax ASSIGNING FIELD-SYMBOL(<tax>). <tax>-tax_amount = <tax>-tax_amount - ( xs_mwsbk - l_xsje-mwsbk ). ENDLOOP. "金额 READ TABLE lt_itemdata ASSIGNING FIELD-SYMBOL(<lt_data>) INDEX 1. <lt_data>-item_amount = <lt_data>-item_amount + ( xs_mwsbk - l_xsje-mwsbk ). ELSEIF xs_mwsbk - l_xsje-mwsbk < 0. "税 LOOP AT lt_tax ASSIGNING FIELD-SYMBOL(<tax2>). <tax2>-tax_amount = <tax2>-tax_amount + ( xs_mwsbk - l_xsje-mwsbk ). ENDLOOP. "金额 READ TABLE lt_itemdata ASSIGNING FIELD-SYMBOL(<lt_data2>) INDEX 1. <lt_data2>-item_amount = <lt_data2>-item_amount - ( xs_mwsbk - l_xsje-mwsbk ). ENDIF. ls_headerdata-calc_tax_ind = ''. "自动税
ls_headerdata-gross_amount = l_xsje-mwsbk.
ls_headerdata-gross_amount = lv_xsje.
ls_headerdata-ref_doc_no = '123'."参考要啥
CALL FUNCTION 'BAPI_INCOMINGINVOICE_CREATE'
EXPORTING
headerdata = ls_headerdata
IMPORTING
invoicedocnumber = lv_invoicedocnumber
TABLES
taxdata = lt_tax
itemdata = lt_itemdata
return = lt_return.
IF lv_invoicedocnumber IS NOT INITIAL .
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
<fs_fh>-msg = '采购开票完成'.
<fs_fh>-status = icon_green_light.
f_fs-msg = '开票完成'.
f_fs-status = icon_green_light.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = lv_invoicedocnumber
IMPORTING
output = l_vbeln.
f_fs-belnr = l_vbeln.
UPDATE ztsto_04 SET vbeln_vf = f_fs-vbeln_vf kbetr = f_fs-kbetr belnr = f_fs-belnr s_msg = f_fs-msg s_status = f_fs-status
WHERE vbeln_d = f_fs-vbeln_d.
IF sy-subrc = 0.
COMMIT WORK.
ENDIF.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
IMPORTING
RETURN =
.
f_fs-msg = '开票失败'.
f_fs-status = icon_red_light.
LOOP AT lt_return INTO ls_return WHERE type = 'E' OR type = 'A' OR type = 'X'.
f_fs-msg = f_fs-msg && ',' && ls_return-message.
ENDLOOP.
"失败冲销掉销售发票20241021。。。
PERFORM f_xsfpcx CHANGING f_fs.
ENDIF.
CLEAR:ls_ekpo,lv_gross,lt_itemdata,lv_index,lt_tax,xs_mwsbk.
ENDAT.
CLEAR:ls_ekpo,ls_itemdata,ls_ekpotmp.
ENDLOOP.
ENDFORM.