BAPI_ACC_DOCUMENT_POST 增强

使用 BAPI_ACC_DOCUMENT_POST 的时候,如果有些字段在 Tables 参数中没有,比如,现在大家都用 Reason code 来作为现金流量表的实现方案。但 BAPI_ACC_DOCUMENT_POSTaccountgl 参数是没有原因代码这个字段的。这种情况下,如何能在导入凭证的时候,包括这个字段呢?

简介

  • 定义一个结构,包括 POSNR (类型为 POSNR_ACC ) 和 RSTGR (原因代码) 两个字段。POSNR 这个字段必须有,因为 BAPI_ACC_DOCUMENT_POST 包含多个 tables 参数,需要行项目号来关联。比如第一行 #1 的总账科目、金额、extension2 等。

  • SE19 实现 BADI 增强 ACC_DOCUMENT,这个增强是用来将 BAPI_ACC_DOCUMENT_POST 参数表EXTENSION2 传入系统表 ACCIT。只需要实现 change 方法

  • BAPI_ACC_DOCUMENT_POST 中,启用 extension2 参数,将扩展字段传入

定义结构

事务码 SE11 定义一个结构 zext2,包括 POSNR (类型为 POSNR_ACC ) 和 RSTGR (原因代码) 两个字段。

BAPI_ACC_DOCUMENT_POST 增强_第1张图片
image

通过 BADI 将扩展字段传入系统表

SE19,使用 Classical BADI,BADI name 为 ACC_DOCUMENT,点击 Create 按钮。

BAPI_ACC_DOCUMENT_POST 增强_第2张图片
image

系统弹出如下对话框,输入 implementation name:

BAPI_ACC_DOCUMENT_POST 增强_第3张图片
image

点击 确认后,进入如下界面。下面有一个 Filter values,添加一行,选择 BKPFF,表示这个 BADI 实现只适用于会计凭证直接输入。

BAPI_ACC_DOCUMENT_POST 增强_第4张图片
image

点击激活按钮,系统提示 migrate 到 New Badi。并弹出如下对话框

BAPI_ACC_DOCUMENT_POST 增强_第5张图片
image

我们不选择已有的 Enhancement Implementation,自己新建一个。Composite Enhancement Implementation 留空。

BAPI_ACC_DOCUMENT_POST 增强_第6张图片
image

确认后,系统提示 Activate 成功。点击界面上的 Interface 按钮,可以看到,这个 BADI 的实现有两个方法,其中 CHANGE 方法就是我们需要实现的方法。

这个方法可以从 Example implementation class 拷贝,完全不用改动。

  method if_ex_acc_document~change.

    data: wa_extension   type bapiparex,
          ext_value(960) type c,
          wa_accit       type accit,
          l_ref          type ref to data.

    field-symbols:  type any,
                    type any.

    sort c_extension2 by structure.

    loop at c_extension2 into wa_extension.
      at new structure.
        create data l_ref type (wa_extension-structure).
        assign l_ref->* to .
      endat.
      concatenate wa_extension-valuepart1 wa_extension-valuepart2
                  wa_extension-valuepart3 wa_extension-valuepart4
             into ext_value.
      move ext_value to .
      assign component 'POSNR' of structure  to .
      read table c_accit with key posnr = 
            into wa_accit.
      if sy-subrc is initial.
        move-corresponding  to wa_accit.
        modify c_accit from wa_accit index sy-tabix.
      endif.
    endloop.

  endmethod.

过账函数包含扩展字段

BAPI_ACC_DOCUMENT_POST 使用 extension2 参数接收传入的自定义结构

data:  l_extname type string value 'ZEXT2'.

data: extension2 type standard table of bapiparex with header line,
      gs_zext2   like zext2.

form populate_extension2.
  " first line
  clear gs_zext2.
  gs_zext2-posnr = '1'.
  gs_zext2-rstgr = 'A01'.

  clear extension2.
  extension2-structure = l_extname.
  extension2-valuepart1 = gs_zext2.
  append extension2.
  
  " second line
  clear gs_zext2.
  gs_zext2-posnr = '2'.
  gs_zext2-rstgr = 'A01'.

  clear extension2.
  extension2-structure = l_extname.
  extension2-valuepart1 = gs_zext2.
  append extension2.
endform.

在 BAPI 中使用 extension2 参数:

call function 'BAPI_ACC_DOCUMENT_POST'
exporting
    documentheader = docheader
tables
    accountgl      = accountgl
    currencyamount = currencyamount
    extension2     = extension2
    return         = return.

完整代码

最后给出完整代码

report zbapi_acc_doc_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

" Extension2
data: extension2 type standard table of bapiparex with header line,
      gs_zext2   like zext2.

" Populate required values
data: l_cocd    type bukrs value 'Z900',
      l_curr    type bapiaccr09-currency value 'CNY',
      l_doctype type bapiache09-doc_type value 'SA',
      l_extname type string value 'ZEXT2'.

start-of-selection.
  perform populate_doc_header.
  perform populate_gl_accounts.
  perform populate_currency_amt.
  perform populate_extension2.
  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 populate_extension2.
  clear gs_zext2.
  gs_zext2-posnr = '1'.
  gs_zext2-rstgr = 'A01'.

  clear extension2.
  extension2-structure = l_extname.
  extension2-valuepart1 = gs_zext2.
  append extension2.

  clear gs_zext2.
  gs_zext2-posnr = '2'.
  gs_zext2-rstgr = 'A01'.

  clear extension2.
  extension2-structure = l_extname.
  extension2-valuepart1 = gs_zext2.
  append extension2.
endform.

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
      extension2     = extension2
      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
      extension2     = extension2
      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.

你可能感兴趣的:(BAPI_ACC_DOCUMENT_POST 增强)