一、F-02 创建财务凭证-BDC 期初余额导入程序
程序:ZFIU_LDV_F_02_UPLOAD
创建F-02 凭证:
*&---------------------------------------------------------------------------------------------------- CALL TRANSACTION 'F-02'
USING it_bdcdata
MODE mode_f
UPDATE 'S'
MESSAGES INTO it_bdcmsge.
READ TABLE it_bdcmsge INDEX .
it_msg_tab-msgtyp = it_bdcmsge-msgtyp.
it_msg_tab-msgid = it_bdcmsge-msgid.
it_msg_tab-msgnr = it_bdcmsge-msgnr.
IF it_bdcmsge-msgid NE space.
CALL FUNCTION 'MESSAGE_TEXT_BUILD'
EXPORTING
msgid = it_bdcmsge-msgid
msgnr = it_bdcmsge-msgnr
msgv1 = it_bdcmsge-msgv1
msgv2 = it_bdcmsge-msgv2
msgv3 = it_bdcmsge-msgv3
msgv4 = it_bdcmsge-msgv4
IMPORTING
message_text_output = msg_text.
it_msg_tab-text = msg_text.
ELSE.
it_msg_tab-text = '======成功创建======'.
ENDIF.
* MOVE it_upload_data-col01 TO it_msg_tab-saknr.
APPEND it_msg_tab.
*&----------------------------------------------------------------------------------------------------
SAPMF05A | 100 | X | ||
0 | BDC_OKCODE | /00 | ||
0 | BKPF-BLDAT | 20170411 | ||
0 | BKPF-BLART | SA | ||
0 | BKPF-BUKRS | CV00 | ||
0 | BKPF-BUDAT | 20170411 | ||
0 | BKPF-MONAT | 4 | ||
0 | BKPF-WAERS | CNY | ||
0 | BKPF-XBLNR | 参照 | ||
0 | BKPF-BKTXT | 凭证抬头文本 | ||
0 | BKPF-KURSF | 1 | ||
0 | RF05A-NEWBS | 40 | ||
0 | RF05A-NEWKO | 6608990200 | ||
0 | RF05A-NEWUM | |||
SAPMF05A | 300 | X | ||
0 | BDC_OKCODE | /00 | ||
0 | BSEG-WRBTR | 30 | ||
0 | BSEG-ZUONR | |||
0 | BSEG-SGTXT | |||
0 | RF05A-NEWBS | |||
0 | RF05A-NEWKO | |||
SAPLKACB | 2 | X | ||
0 | BDC_OKCODE | #NAME? | ||
SAPMF05A | 300 | X | ||
0 | BDC_OKCODE | #NAME? | ||
0 | BSEG-WRBTR | |||
0 | BSEG-ZUONR | |||
0 | BSEG-SGTXT | |||
SAPLKACB | 2 | X | ||
0 | BDC_OKCODE | #NAME? |
二、BAPI_ACC_DOCUMENT_POST
创建供应商,客户,总账凭证,
*&---------------------------------------- FUNCTION zfifm_receipt_posting .
*"----------------------------------------------------------------------
*"*"Local interface:
*" EXPORTING
*" VALUE(ERRCODE) TYPE CHAR8
*" VALUE(ERRDESC) TYPE CHAR250
*" TABLES
*" IT_HEAD STRUCTURE ZSFI_DOC_HEAD OPTIONAL
*" IT_ITEM STRUCTURE ZSFI_DOC_ITEM OPTIONAL
*" OT_HEAD STRUCTURE ZSFI_DOC_HEAD_O OPTIONAL
*"----------------------------------------------------------------------
DATA: t_head TYPE STANDARD TABLE OF ztfi_doc_head WITH HEADER LINE,
t_item TYPE STANDARD TABLE OF ztfi_doc_item WITH HEADER LINE.
DATA: lt_item TYPE STANDARD TABLE OF ztfi_doc_item WITH HEADER LINE.
g_datum = sy-datum.
g_uzeit = sy-uzeit. *---- Mapping Data
PERFORM frm_mapping_data TABLES t_head
t_item
it_head
it_item. LOOP AT t_head.
idx_tabix = sy-tabix. CLEAR: lt_item , lt_item[].
LOOP AT t_item WHERE zsernr = t_head-zsernr.
MOVE-CORRESPONDING t_item TO lt_item.
APPEND lt_item.
ENDLOOP. *---- 数据检查
PERFORM frm_check_data1 TABLES lt_item USING t_head. IF t_head-proce <> ''.
MODIFY t_head INDEX idx_tabix.
CONTINUE.
ENDIF. *---- 创建会计凭证
PERFORM frm_create_fi_doc TABLES lt_item USING t_head. MODIFY t_head INDEX idx_tabix.
ENDLOOP. LOOP AT t_head.
MOVE-CORRESPONDING t_head TO ot_head.
IF ot_head-proce = 'W' AND ot_head-belnr <> ''.
ot_head-proce = 'S'.
ENDIF.
APPEND ot_head.
ENDLOOP. *---- 自定义表更新
MODIFY ztfi_doc_head FROM TABLE t_head.
MODIFY ztfi_doc_item FROM TABLE t_item.
COMMIT WORK AND WAIT. *---- 返回码
IF t_head[] IS NOT INITIAL.
READ TABLE t_head INDEX .
IF t_head-proce = 'S'.
errcode = 'S0000000'.
ELSE.
errcode = 'E0468001'.
errdesc = t_head-message.
ENDIF.
ENDIF. ENDFUNCTION. *&---------------------------------------------------------------------------------------------------- FORM frm_create_fi_doc TABLES lt_item STRUCTURE ztfi_doc_item
USING wa_head STRUCTURE ztfi_doc_head.
DATA: documentheader LIKE bapiache09.
DATA: customercpd LIKE bapiacpa09.
DATA: accountgl TYPE STANDARD TABLE OF bapiacgl09 WITH HEADER LINE,
accountreceivable TYPE STANDARD TABLE OF bapiacar09 WITH HEADER LINE,
accountpayable TYPE STANDARD TABLE OF bapiacap09 WITH HEADER LINE,
accounttax TYPE STANDARD TABLE OF bapiactx09 WITH HEADER LINE,
currencyamount TYPE STANDARD TABLE OF bapiaccr09 WITH HEADER LINE,
criteria TYPE STANDARD TABLE OF bapiackec9 WITH HEADER LINE,
lt_return TYPE STANDARD TABLE OF bapiret2 WITH HEADER LINE. DATA: obj_type LIKE bapiache09-obj_type,
obj_key LIKE bapiache09-obj_key,
obj_sys LIKE bapiache09-obj_sys. documentheader-username = sy-uname.
documentheader-doc_type = wa_head-blart.
documentheader-header_txt = wa_head-bktxt.
documentheader-comp_code = wa_head-bukrs.
documentheader-doc_date = wa_head-bldat.
documentheader-pstng_date = sy-datum."wa_head-budat.
documentheader-ref_doc_no = wa_head-xblnr. LOOP AT lt_item. documentheader-neg_postng = lt_item-xnegp. *---- 金额
CLEAR: currencyamount.
currencyamount-itemno_acc = lt_item-buzei.
currencyamount-currency = wa_head-waers.
currencyamount-amt_doccur = lt_item-wrbtr.
currencyamount-exch_rate = wa_head-kursf.
IF lt_item-shkzg ='H'." lt_item-bschl = '50' OR lt_item-bschl = '11' OR lt_item-bschl = '39' OR lt_item-bschl = '31'.
currencyamount-amt_doccur = - currencyamount-amt_doccur.
ENDIF.
APPEND currencyamount. CASE lt_item-flag.
WHEN 'D'.
*---- 客户
accountreceivable-itemno_acc = lt_item-buzei.
accountreceivable-customer = lt_item-hkont.
* ACCOUNTRECEIVABLE-gl_account = lt_item-hkont.
accountreceivable-item_text = lt_item-sgtxt.
accountreceivable-alloc_nmbr = lt_item-zuonr.
accountreceivable-bline_date = lt_item-zfbdt.
accountreceivable-sp_gl_ind = lt_item-umskz.
* ACCOUNTRECEIVABLE-costcenter = lt_item-kostl.
APPEND accountreceivable. *----- 一次性客户信息
SELECT SINGLE * FROM kna1 WHERE kunnr = lt_item-hkont.
IF kna1-xcpdk = 'X'.
customercpd-name = lt_item-sgtxt.
customercpd-city = lt_item-sgtxt.
customercpd-country = 'CN'.
customercpd-bank_ctry = 'CN'.
ENDIF.
WHEN 'K'.
*---- 供应商
accountpayable-itemno_acc = lt_item-buzei.
accountpayable-vendor_no = lt_item-hkont.
* ACCOUNTRECEIVABLE-gl_account = lt_item-hkont.
accountpayable-item_text = lt_item-sgtxt.
accountpayable-alloc_nmbr = lt_item-zuonr.
accountpayable-bline_date = lt_item-zfbdt.
accountpayable-sp_gl_ind = lt_item-umskz.
* accountpayable-costcenter = lt_item-kostl.
APPEND accountpayable. WHEN OTHERS.
*---- 总帐科目
accountgl-itemno_acc = lt_item-buzei.
accountgl-gl_account = lt_item-hkont.
accountgl-item_text = lt_item-sgtxt.
accountgl-alloc_nmbr = lt_item-zuonr.
accountgl-costcenter = lt_item-kostl.
APPEND accountgl. ENDCASE.
ENDLOOP. CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
EXPORTING
documentheader = documentheader
customercpd = customercpd
* CONTRACTHEADER = CONTRACTHEADER
IMPORTING
obj_type = obj_type
obj_key = obj_key
obj_sys = obj_sys
TABLES
accountgl = accountgl
accountreceivable = accountreceivable
accountpayable = accountpayable
accounttax = accounttax
currencyamount = currencyamount
* CRITERIA = CRITERIA
* VALUEFIELD = VALUEFIELD
* EXTENSION1 = EXTENSION1
return = lt_return
* PAYMENTCARD = PAYMENTCARD
* CONTRACTITEM = CONTRACTITEM
* EXTENSION2 = EXTENSION2
* REALESTATE = REALESTATE
* ACCOUNTWT = ACCOUNTWT
. READ TABLE lt_return WITH KEY type = 'E'.
IF sy-subrc = .
LOOP AT lt_return WHERE type = 'E'.
wa_head-proce = 'E'.
CONCATENATE wa_head-message lt_return-message INTO wa_head-message SEPARATED BY ' '.
ENDLOOP.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ELSE.
wa_head-belnr = obj_key+().
wa_head-gjahr = obj_key+().
wa_head-proce = 'S'.
wa_head-message = '凭证创建成功!'.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'. ENDIF.
ENDFORM. "FRM_CREATE_FI_DOC
三、SE37 函数创建凭证,测试使用
1.SE37初始界面,
2.菜单:FUNCTION MODULE-> TEST-> TEST sequences
3. BAPI_ACC_DOCUMENT_POST
+BAPI_TRANSACTION_COMMIT (提交数据)
*&------------------------------------------------------------------------------------------------
BAPI_TRANSACTION_COMMIT (测试)
*&------------------------------------------------------------------------------------------------
创建F-02 凭证 测试参数
- 运行第一个函数
得到 财务凭证:100000035
2.运行第二个函数,提交凭证,