MIR7采购发票预制BAPI
FUNCTION zsdfu006.
*"----------------------------------------------------------------------
*"*"本地接口:
*" 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_zsdt013 TYPE TABLE OF zsdt013,
ls_zsdt013 LIKE LINE OF lt_zsdt013,
ls_zsdt016 TYPE zsdt016,
lt_zsdt016 TYPE TABLE OF zsdt016,
lt_zsdt018 TYPE TABLE OF zsdt018,
ls_zsdt018 LIKE LINE OF lt_zsdt018,
lt_zsdt017 TYPE TABLE OF zsdt017,
ls_zsdt017 LIKE LINE OF lt_zsdt017,
lt_zsdt008 TYPE TABLE OF zsdt008,
ls_zsdt008 LIKE LINE OF lt_zsdt008.
DATA:ls_zrfct002 TYPE zrfct002.
DATA:ls_creatordatain TYPE bapicreatordata,
lt_billing TYPE STANDARD TABLE OF bapivbrk,
ls_billing TYPE bapivbrk.
DATA: wa_headerdata LIKE bapi_incinv_create_header,
gt_itemdata LIKE TABLE OF bapi_incinv_create_item,
wa_itemdata LIKE bapi_incinv_create_item,
gt_taxdata LIKE TABLE OF bapi_incinv_create_tax,
wa_taxdata LIKE bapi_incinv_create_tax.
DATA:lv_name2 TYPE lfa1-name2,
lv_sortl TYPE lfa1-sortl,
lv_ebeln TYPE ekko-ebeln,
lv_waers TYPE ekko-waers,
lv_bukrs TYPE ekko-bukrs,
lv_werks TYPE ekpo-werks,
header_txt TYPE bktxt,
rblgp TYPE rblgp,
l_flg(1) TYPE c,
lv_vbeln TYPE zsdt013-objkey,
lv_canum TYPE canum,
lv_fkart TYPE vbrk-fkart,
lv_zfkart TYPE vbrk-fkart,
lv_dmbtr TYPE mseg-dmbtr.
DATA ls_ekpo TYPE ekpo.
DATA lt_lips TYPE TABLE OF lips.
DATA lt_mseg TYPE TABLE OF mseg WITH HEADER LINE.
SELECT *
INTO CORRESPONDING FIELDS OF TABLE lt_zsdt018
FROM zsdt018
WHERE zrfc_logid = i_id.
IF lt_zsdt018 IS INITIAL.
e_code = 'E'.
e_msg = '无可执行数据'.
RETURN.
ENDIF.
SORT lt_zsdt018 BY mblnr mjahr zeile ebeln ebelp.
CALL METHOD zcl_rfc=>get_source
EXPORTING
i_id = i_id
i_canum = i_canum
IMPORTING
e_zrfct002 = ls_zrfct002.
LOOP AT lt_zsdt018 INTO ls_zsdt018.
wa_headerdata-invoice_ind = 'X'.
wa_headerdata-doc_date = ls_zsdt018-bldat.
wa_headerdata-pstng_date = ls_zsdt018-budat.
wa_headerdata-bline_date = ls_zsdt018-bldat ."pzfbdt. "基线日期/付款计算的起始日期
wa_headerdata-ref_doc_no = ls_zsdt018-mblnr.
lv_ebeln = ls_zsdt018-ebeln.
wa_headerdata-calc_tax_ind = 'X'.
lv_dmbtr = lv_dmbtr + ls_zsdt018-brtwr."ls_ekpo-brtwr / ls_ekpo-menge * lt_mseg-menge."含税
wa_itemdata-item_amount = ls_zsdt018-brtwr.
wa_itemdata-po_number = ls_zsdt018-ebeln.
wa_itemdata-po_item = ls_zsdt018-ebelp .
SELECT SINGLE *
FROM ekbe
INTO @DATA(ls_ekbe)
WHERE ebeln = @ls_zsdt018-ebeln
AND ebelp = @ls_zsdt018-ebelp
AND gjahr = @ls_zsdt018-mjahr
AND belnr = @ls_zsdt018-mblnr
AND buzei = @ls_zsdt018-zeile.
SELECT SINGLE bprme
FROM ekpo
INTO wa_itemdata-po_pr_uom
WHERE ebeln = ls_zsdt018-ebeln
AND ebelp = ls_zsdt018-ebelp.
wa_itemdata-po_pr_qnt = ls_ekbe-bpmng.
wa_itemdata-ref_doc = ls_zsdt018-mblnr .
wa_itemdata-ref_doc_year = ls_zsdt018-mjahr .
wa_itemdata-ref_doc_it = ls_zsdt018-zeile .
wa_itemdata-quantity = ls_zsdt018-menge .
wa_itemdata-po_unit = ls_zsdt018-meins .
rblgp = rblgp + 1.
wa_itemdata-invoice_doc_item = rblgp .
APPEND wa_itemdata TO gt_itemdata.
wa_headerdata-comp_code = ls_zsdt018-bukrs.
wa_headerdata-currency = ls_zsdt018-waers.
wa_headerdata-gross_amount = lv_dmbtr.
AT END OF ebeln.
ls_zsdt017-canum = i_canum.
ls_zsdt017-objtype = ls_zrfct002-objtype_save.
ls_zsdt017-zrfcid = ls_zrfct002-zrfcid.
ls_zsdt017-zrfc_logid = i_id.
SELECT SINGLE zrfc_logid canum INTO (ls_zsdt017-zzrfc_logid,ls_zsdt017-zcanum) FROM zsdt013
WHERE zobjkey = lv_ebeln AND mblnr = wa_headerdata-ref_doc_no.
ls_zsdt017-zobjkey = ls_zsdt018-ebeln.
ls_zsdt017-zobjtype = 'PO'.
APPEND ls_zsdt017 TO lt_zsdt017.
ENDAT.
ENDLOOP.
PERFORM create_in TABLES et_return gt_itemdata lt_zsdt017 USING i_id i_canum wa_headerdata CHANGING e_code e_msg e_flow_end.
REFRESH:gt_itemdata,lt_zsdt017.
CLEAR:lv_dmbtr.
ENDFUNCTION.
FORM create_in TABLES p_et_return STRUCTURE zrfct005
gt_itemdata STRUCTURE bapi_incinv_create_item
p_lt_zsdt017 STRUCTURE zsdt017
USING p_i_id TYPE zrfc_logid
p_i_canum TYPE canum
wa_headerdata TYPE bapi_incinv_create_header
CHANGING p_e_code
p_e_msg
p_e_flow_end.
DATA: wa_itemdata LIKE bapi_incinv_create_item,
gt_materialdata LIKE TABLE OF bapi_incinv_create_material,
wa_materialdata LIKE bapi_incinv_create_material,
gt_taxdata LIKE TABLE OF bapi_incinv_create_tax,
wa_taxdata LIKE bapi_incinv_create_tax,
gt_return LIKE TABLE OF bapiret2,
wa_return LIKE bapiret2,
g_belnr LIKE bapi_incinv_fld-inv_doc_no,
g_gjahr LIKE bapi_incinv_fld-fisc_year.
DATA:ls_zrfct005 TYPE zrfct005.
DATA:message TYPE c LENGTH 200.
DATA ls_zsdt017 TYPE zsdt017.
CALL FUNCTION 'BAPI_INCOMINGINVOICE_PARK'
EXPORTING
headerdata = wa_headerdata
IMPORTING
invoicedocnumber = g_belnr
fiscalyear = g_gjahr
TABLES
itemdata = gt_itemdata
taxdata = gt_taxdata
return = gt_return.
IF g_belnr IS NOT INITIAL .
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
LOOP AT p_lt_zsdt017 INTO ls_zsdt017.
ls_zsdt017-objkey = g_belnr."发票凭证
ls_zsdt017-gjahr = g_gjahr.
ls_zsdt017-objtype = 'IN'.
MODIFY p_lt_zsdt017 FROM ls_zsdt017.
ENDLOOP.
MODIFY zsdt017 FROM TABLE p_lt_zsdt017.
p_e_code = 'S'.
CONCATENATE 'IN:' g_belnr '创建成功' INTO p_e_msg.
CLEAR:ls_zrfct005.
ls_zrfct005-status = '53'.
ls_zrfct005-msgty = p_e_code.
ls_zrfct005-msgid = '00'.
ls_zrfct005-msgno = '001'.
ls_zrfct005-msgv1 = p_e_msg.
ls_zrfct005-msgv4 = g_belnr.
APPEND ls_zrfct005 TO p_et_return.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
LOOP AT gt_return INTO wa_return WHERE type = 'E' OR type = 'A'.
CONCATENATE message wa_return-message INTO message.
ENDLOOP.
p_e_code = 'E'.
p_e_msg = message.
ENDIF.
ENDFORM.