预制发票:BAPI_INCOMINGINVOICE_PARK
DATA: ls_header TYPE bapi_incinv_create_header,lt_itemdata TYPE TABLE OF bapi_incinv_create_item,ls_itemdata TYPE bapi_incinv_create_item,lt_taxdata TYPE TABLE OF bapi_incinv_create_tax,ls_taxdata TYPE bapi_incinv_create_tax,bapi_return_t TYPE TABLE OF bapiret2,bapi_return_s TYPE bapiret2,lv_belnr TYPE belnr_d,lv_gjahr TYPE gjahr,lv_rblgp TYPE rblgp,lv_meins LIKE ekpo-meins,lt_save TYPE TABLE OF znymmt00620,ls_save TYPE znymmt00620.CLEAR: ls_header, lt_itemdata, lt_taxdata, bapi_return_t,lv_rblgp,lv_meins,ls_save,lt_save,ls_tmp,lt_tmp.LOOP AT gt_alv ASSIGNING FIELD-SYMBOL(<fs_alv>).lv_rblgp = lv_rblgp + 10. * 如果是第一个行项目,填充表头IF lv_rblgp = '10'.ls_header-invoice_ind = 'X'.ls_header-doc_type = 'RE'.ls_header-doc_date = <fs_alv>-bldat.ls_header-ref_doc_no = <fs_alv>-xblnr."参考凭证号ls_header-pstng_date = <fs_alv>-budat.ls_header-comp_code = <fs_alv>-bukrs.ls_header-bline_date = <fs_alv>-zfbdt.* 读取供应商SELECT SINGLE lifnr INTO ls_header-diff_inv FROM ekko WHERE ebeln = <fs_alv>-ebeln. * 抬头文本IF <fs_alv>-ztext IS INITIAL.ls_header-header_txt = 'MIS发票预制'.ELSE.ls_header-header_txt = <fs_alv>-ztext.ENDIF. * 读取币别SELECT SINGLE waers INTO ls_header-currency FROM t001 WHERE bukrs = <fs_alv>-bukrs.ENDIF.* 总金额 * ls_header-gross_amount = ls_header-gross_amount + <fs_alv>-wrbtr.ls_header-gross_amount = <fs_alv>-wrbtr. * 行项目ls_itemdata-invoice_doc_item = lv_rblgp. "发票行项目号ls_itemdata-po_number = <fs_alv>-ebeln.ls_itemdata-po_item = <fs_alv>-ebelp.* ls_itemdata-item_amount = <fs_alv>-wrbtr1. "changed by gxsk_guoyp 20250106ls_itemdata-quantity = <fs_alv>-menge. * 单位SELECT SINGLE meins INTO ls_itemdata-po_unit FROM ekpo WHERE ebeln = <fs_alv>-ebeln AND ebelp = <fs_alv>-ebelp.ls_itemdata-po_unit_iso = ls_itemdata-po_unit.ls_itemdata-tax_code = <fs_alv>-mwskz1.APPEND ls_itemdata TO lt_itemdata.CLEAR: ls_itemdata. *&----- 税额ls_taxdata-tax_code = <fs_alv>-mwskz1.ls_taxdata-tax_amount = <fs_alv>-wmwst.APPEND ls_taxdata TO lt_taxdata.**& 处理税码 * DATA: ws_taxdata LIKE ls_taxdata. * SORT lt_taxdata BY tax_code. * CLEAR ws_taxdata. * LOOP AT lt_taxdata INTO ls_taxdata. * ws_taxdata-tax_code = ls_taxdata-tax_code. * ws_taxdata-tax_amount = ws_taxdata-tax_amount + ls_taxdata-tax_amount. * AT END OF tax_code. * MODIFY lt_taxdata FROM ws_taxdata TRANSPORTING tax_amount * WHERE tax_code = ws_taxdata-tax_code. * CLEAR ws_taxdata. * ENDAT. * ENDLOOP.DELETE ADJACENT DUPLICATES FROM lt_taxdata COMPARING tax_code.MOVE-CORRESPONDING <fs_alv> TO ls_save.ls_save-i_date = sy-datum.ls_save-i_uzeit = sy-uzeit.ls_save-uname = sy-uname.APPEND ls_save TO lt_save.AT END OF HBFLG. * 调用BAPI 生成预制凭证CLEAR:lv_belnr,lv_gjahr.CALL FUNCTION 'BAPI_INCOMINGINVOICE_PARK'EXPORTINGheaderdata = ls_header * ADDRESSDATA =IMPORTINGinvoicedocnumber = lv_belnrfiscalyear = lv_gjahrTABLESitemdata = lt_itemdata * ACCOUNTINGDATA = * GLACCOUNTDATA = * MATERIALDATA =taxdata = lt_taxdata * WITHTAXDATA = * VENDORITEMSPLITDATA =return = bapi_return_t * EXTENSIONIN = * TM_ITEMDATA = .READ TABLE bapi_return_t INTO bapi_return_s WITH KEY type = 'E'.IF sy-subrc = 0.CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.<fs_alv>-status = icon_red_light.LOOP AT bapi_return_t INTO bapi_return_s WHERE type = 'E'.CONCATENATE <fs_alv>-msg '/' bapi_return_s-message INTO <fs_alv>-msg.ENDLOOP.ELSE.<fs_alv>-status = icon_green_light.<fs_alv>-msg = '发票创建成功'.CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'EXPORTINGWAIT = 'X'.WAIT UP TO 1 SECONDS.<fs_alv>-belnr = lv_belnr.<fs_alv>-gjahr = lv_gjahr.ENDIF.MOVE-CORRESPONDING <fs_alv> TO ls_tmp.APPEND ls_tmp TO lt_tmp.CLEAR:ls_tmp,ls_header,ls_itemdata,lt_itemdata,ls_taxdata,lt_taxdata,bapi_return_t,lv_belnr,lv_rblgp.ENDAT.ENDLOOP.
发票过账:BAPI_INCOMINGINVOICE_POST
DATA:message TYPE c LENGTH 200.DATA:lv_awkey TYPE bkpf-awkey.DATA:lt_return LIKE TABLE OF bapiret2 WITH HEADER LINE.DATA:lt_save TYPE TABLE OF znymmt00620,ls_save TYPE znymmt00620.CLEAR:lt_tmp,ls_tmp.SORT gt_alv BY belnr.LOOP AT gt_alv ASSIGNING FIELD-SYMBOL(<fs_alv>) WHERE status = icon_green_light.AT END OF belnr.CALL FUNCTION 'BAPI_INCOMINGINVOICE_POST'EXPORTINGinvoicedocnumber = <fs_alv>-belnrfiscalyear = <fs_alv>-gjahrTABLESreturn = lt_return.CLEAR:message.LOOP AT lt_return WHERE type = 'A' OR type = 'E' .CONCATENATE message lt_return-message INTO message SEPARATED BY '/'.ENDLOOP.IF sy-subrc <> '0'.CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'EXPORTINGwait = 'X'.CLEAR lv_awkey.CONCATENATE <fs_alv>-belnr <fs_alv>-gjahr INTO lv_awkey.SELECT SINGLE belnrINTO @DATA(lv_belnr)FROM bkpfWHERE awkey = @lv_awkey.CONCATENATE '凭证:' lv_belnr '创建成功' INTO message.<fs_alv>-status = icon_green_light.<fs_alv>-msg = message.<fs_alv>-belnr1 = lv_belnr.ELSE.<fs_alv>-status = icon_red_light.<fs_alv>-msg = message.CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.ENDIF.