创建会计凭证:BAPI_ACC_DOCUMENT_POST 增强字段

23 篇文章 0 订阅
创建会计凭证:BAPI_ACC_DOCUMENT_POST 增强字段

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

一、简介
  1. 首先定义一个包括POSNR(类型为POSNR_ACC) 和**RSTGR(原因代码)**两个字段。POSNR这个字段必须有,因为在BAPI_ACC_DOCUMENT_POST中会包含多个tables参数,所以需要使用行项目号来关联。比如第一行#1的总账科目、金额、extensions等。
  2. SE19实现BADI增强ACC_DOCUMENT,这个增强是用来将BAPI_ACC_DOCUMENT_POST参数表EXTENSION2传入系统表ACCIT。只需要实现change方法
  3. 在BAPI_ACC_DOCUMENT_POST 中,启用 extension2 参数,将扩展字段传入
二、定义结构
  1. 事务码SE11定义一个结构ZSACC,包括POSNR (类型为 POSNR_ACC ) 和 RSTGR (原因代码) 两个字段。

    在这里插入图片描述

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

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

      在这里插入图片描述

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

      外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    • 点击确认后,进入如下界面,下面有一个Filter Values,添加一行,选择BKPFF,表示这个BADI实现只适合用于会计凭证直接输入。(根据需要添加对应的选项即可)

      在这里插入图片描述

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

      !

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

      在这里插入图片描述

    • 确认后,系统提示 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: <l_struc> type any,
                         <l_field> 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 <l_struc>.
            endat.
            concatenate wa_extension-valuepart1 wa_extension-valuepart2
                        wa_extension-valuepart3 wa_extension-valuepart4
                   into ext_value.
            move ext_value to <l_struc>.
            assign component 'POSNR' of structure <l_struc> to <l_field>.
            read table c_accit with key posnr = <l_field>
                  into wa_accit.
            if sy-subrc is initial.
              move-corresponding <l_struc> to wa_accit.
              modify c_accit from wa_accit index sy-tabix.
            endif.
          endloop.
      
        endmethod.
      
    • 增强实现后只要在编写程序中导入数据时写入对应的参数,案例完整代码如下

三、完整代码
report zdemo001.

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.

四、其他

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值