BAPI_ACC_DOCUMENT_POST 基本用法

使用 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_TYPEOBJ_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.
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,342评论 0 3
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,145评论 1 32
  • ▲GL主数据——会计科目 在SAP系统中,每一个总账科目(G/L Account)都有两个层次, 首先必须在cha...
    TaoMin阅读 7,160评论 0 4
  • 本博客转自:「作者:若愚链接:https://zhuanlan.zhihu.com/p/22361337来源:知乎...
    韩宝亿阅读 2,798评论 0 3
  • 一说到情人节,我就想起了小山。 小山想结束自己的单身,于是就和他心仪的女神表白了,念了整整一页的情书,结局很明显,...
    乔某人阅读 299评论 0 2