使用 BAPI 导入凭证,通过 BAPI BAPI_ACC_DOCUMENT_POST
, 可以导入 G/L, 应收账款、应付账款等。如果导入只包含总账科目的会计凭证,也可以用函数 BAPI_ACC_GL_POSTING_POST
。
基本使用方法
导入凭证,无非先将数据从文件导入到内表,然后进行校验。校验通过则调用 BAPI 生成会计凭证。为便于理解,直接硬编码,演示 BAPI 的使用要点。
假设我们要生成一张最简单的会计凭证:
DR: 现金 100
CR: 银行存款 100
我们来看看如何编写。首先给出完整代码:
report zbapi_ac_document_post_test.
data:
docheader like bapiache09, " structure of document header
accountgl like bapiacgl09 occurs 0 with header line, " internal table for glaccounts
currencyamount like bapiaccr09 occurs 0 with header line, " internal table for currency
return like bapiret2 occurs 0 with header line. " internal table for return
* Populate required values
data: l_cocd type bukrs value 'Z900', " company code
l_curr type bapiaccr09-currency value 'CNY',
l_doctype type bapiache09-doc_type value 'SA'.
start-of-selection.
perform populate_doc_header.
perform populate_gl_accounts.
perform populate_currency_amt.
perform generate_fi_document.
form populate_doc_header.
clear docheader.
docheader-username = sy-uname.
docheader-header_txt = 'Test FI doc using BAPI'.
docheader-comp_code = l_cocd. " company code
docheader-doc_date = sy-datum.
docheader-pstng_date = sy-datum.
docheader-doc_type = l_doctype.
endform.
form populate_gl_accounts.
clear accountgl.
accountgl-itemno_acc = '1'.
accountgl-gl_account = '0010010100'.
accountgl-comp_code = l_cocd.
accountgl-pstng_date = sy-datum.
accountgl-doc_type = l_doctype.
accountgl-item_text = '银行取现'.
append accountgl.
clear accountgl.
accountgl-itemno_acc = '2'.
accountgl-gl_account = '0010020100'.
accountgl-comp_code = l_cocd.
accountgl-pstng_date = sy-datum.
accountgl-value_date = sy-datum.
accountgl-doc_type = l_doctype.
accountgl-item_text = '银行取现'.
append accountgl.
endform.
form populate_currency_amt.
clear currencyamount.
currencyamount-itemno_acc = '1'.
currencyamount-currency = l_curr.
currencyamount-amt_doccur = '100.00'.
append currencyamount.
clear currencyamount.
currencyamount-itemno_acc = '2'.
currencyamount-currency = l_curr.
currencyamount-amt_doccur = '-100.00'.
append currencyamount.
endform.
form generate_fi_document.
call function 'BAPI_ACC_DOCUMENT_POST'
exporting
documentheader = docheader
tables
accountgl = accountgl
currencyamount = currencyamount
return = return.
if sy-subrc is initial.
call function 'BAPI_TRANSACTION_COMMIT'
exporting
wait = 'X'.
endif.
if sy-subrc is initial.
write 'Successful'.
endif.
endform.
使用要点说明
-
不需要 posting key,根据科目和金额的正负自动确定。
-
在填充 document header 的时候,不要填充
OBJ_KEY
,OBJ_TYPE
和OBJ_SYS
,由函数来填充。 -
一般将下面三个函数配合使用:
- 先用
BAPI_ACC_DOCUMENT_CHECK
进行检查。如果没有错误,sy-subrc <> 0
- 调用
BAPI_ACC_DOCUMENT_POST
进行过账。这个函数会占用凭证号码。 - 如果 POST 函数的
sy-subrc = 0
,调用函数BAPI_TRANSACTION_COMMIT
提交修改。
- 先用
-
本示例创建的会计凭证是本位币,没有汇率。
凭证过账检查
银行科目需要输入 value date。假设我们注释掉银行存款行的 value date,此时会计凭证时不能过账的,我们使用 BAPI_ACC_DOCUMENT_CHECK
来检查,读取函数 return 返回值获取信息:
report zacc_doc_docment_post_test2.
* 相同部分省略
* ...
form generate_fi_document.
data: has_error type c,
message_line type string.
has_error = space.
call function 'BAPI_ACC_DOCUMENT_CHECK'
exporting
documentheader = docheader
tables
accountgl = accountgl
currencyamount = currencyamount
return = return.
loop at return.
if return-type = 'E'.
has_error = 'X'.
exit.
endif.
endloop.
if has_error = 'X'.
loop at return.
concatenate return-id return-number ': ' return-message into message_line.
write: / message_line.
clear return.
endloop.
endif.
check has_error = space.
clear return[].
call function 'BAPI_ACC_DOCUMENT_POST'
exporting
documentheader = docheader
tables
accountgl = accountgl
currencyamount = currencyamount
return = return.
if sy-subrc is initial.
call function 'BAPI_TRANSACTION_COMMIT'
exporting
wait = 'X'.
" write messages
loop at return.
concatenate return-id return-number ': ' return-message into message_line.
write: / message_line.
clear return.
endloop.
endif.
if sy-subrc is initial.
write: / 'Successful'.
endif.
endform.
网友评论