首页 > 其他分享 >BAPI_ACC_DOCUMENT_POST 基本用法

BAPI_ACC_DOCUMENT_POST 基本用法

时间:2023-01-06 10:36:08浏览次数:50  
标签:ACC BAPI return sy POST currencyamount docheader accountgl

使用 BAPI 导入凭证,通过 BAPI BAPI_ACC_DOCUMENT_POST, 可以导入 G/L, 应收账款、应付账款等。如果导入只包含总账科目的会计凭证,也可以用函数 BAPI_ACC_GL_POSTING_POST。

基本使用方法
导入凭证,无非先将数据从文件导入到内表,然后进行校验。校验通过则调用 BAPI 生成会计凭证。为便于理解,直接硬编码,演示 BAPI 的使用要点。

假设我们要生成一张最简单的会计凭证:

借: 现金 100
  贷: 银行存款 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.

————————————————
版权声明:本文为CSDN博主「stone0823」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/stone0823/article/details/96838529

标签:ACC,BAPI,return,sy,POST,currencyamount,docheader,accountgl
From: https://www.cnblogs.com/hua900822/p/17029705.html

相关文章