abap 实现F-47过账增强

abap 实现F-47过账增强

  • 需求说明
    前台操作事务代码F-47,产生的是单边凭证,此凭证在epic中可以运行出发票建议,现在要使用ABAP程序实现同样的单边凭证的效果,如图所:
    abap 实现F-47过账增强_第1张图片

  • 英文参考说明及网址
    abap 实现F-47过账增强_第2张图片
    参考网址:https://blogs.sap.com/2013/03/27/f-47-downpayment-request-using-bapiaccdocumentpost/

  • 增强BADi
    badi_acc_document

  • 实现步骤
    1.创建增强BADi,如下图所示,BADi名称自己按规范命名
    abap 实现F-47过账增强_第3张图片

    2.编写增强代码,代码位置:IF_EX_ACC_DOCUMENT~CHANGE(示例代码环境:S/4)
    abap 实现F-47过账增强_第4张图片

完整参考代码

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.
  FIELD-SYMBOLS :   TYPE accit.

  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 .
    "判断是否是epic过账传输的增强结构
    IF wa_extension-structure = 'ZTMS_EPIC_EXTENSION'.
      ASSIGN COMPONENT 'POSNR' OF STRUCTURE  TO .
      "得到凭证行
      READ TABLE c_accit ASSIGNING  WITH KEY posnr = .
      IF sy-subrc = 0.
        "凭证状态
        -bstat = 'S'.

        "特别总帐科目的分配号=流动项目
        ASSIGN COMPONENT 'ZFITM' OF STRUCTURE  TO .
        -hzuon = .

        "目标特别总帐标志
        ASSIGN COMPONENT 'ZUMSK' OF STRUCTURE  TO .
        -zumsk = .
*        -zumsk = 'A'.
*        wa_accit-xmwst = 'X'.
        "过账事务代码
        ASSIGN COMPONENT 'TCODE' OF STRUCTURE  TO .
        -xref2 = .
      ENDIF.
      "业务事务
      c_acchd-glvor = 'RFST'.
      "过账事务代码
      ASSIGN COMPONENT 'TCODE' OF STRUCTURE  TO .
      c_acchd-tcode = .
*------------------------------------------------------*
    ENDIF.
  ENDLOOP.
ENDMETHOD.

生成凭证的function

FUNCTION ztm_epic_006.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(IV_ONLY_CHECK) TYPE  CHAR1 DEFAULT 'X'
*"     VALUE(IS_POST_STRUCT) TYPE  ZTMS_EPIC_006
*"  EXPORTING
*"     VALUE(EV_BELNR) TYPE  BELNR_D
*"     VALUE(EV_BUDAT) TYPE  BUDAT
*"  TABLES
*"      ET_MESSAGE STRUCTURE  ZTMS_EPIC_003 OPTIONAL
*"----------------------------------------------------------------------
  DATA:ls_message          TYPE ztms_epic_003.
  DATA:ls_documentheader   TYPE bapiache09.
  DATA:lt_accountgl        TYPE TABLE OF bapiacgl09 WITH HEADER LINE,
       lt_accountpayable   TYPE TABLE OF bapiacap09 WITH HEADER LINE,
       lt_currencyamount   TYPE TABLE OF bapiaccr09 WITH HEADER LINE,
       lt_return           TYPE TABLE OF bapiret2   WITH HEADER LINE.
  DATA:lt_extension        TYPE TABLE OF bapiparex  WITH HEADER LINE.
  DATA:ls_zexten           TYPE ztms_epic_extension.        "增强结构
  DATA:lv_zumsk            TYPE dzumsk.
  DATA:lv_zuonr            TYPE bseg-zuonr.                 "分配

  "清空字段
  CLEAR : ls_documentheader,lt_accountgl,lt_currencyamount,lt_return.
  CLEAR : lt_accountgl[],lt_currencyamount[],lt_return[],et_message[].

  "过账结构中没有数据,返回
  IF is_post_struct IS INITIAL.
    RETURN.
  ENDIF.

  "凭证抬头字段
  ls_documentheader-header_txt  = is_post_struct-bktxt.  "抬头文本
  ls_documentheader-comp_code   = is_post_struct-bukrs.  "公司代码
  ls_documentheader-doc_date    = is_post_struct-zreqd. "凭证日期
  ls_documentheader-pstng_date  = is_post_struct-budat.  "过账日期
  ls_documentheader-username    = sy-uname.       "用户
  ls_documentheader-doc_type    = 'SA'.           "凭证类型
  ls_documentheader-obj_type    = 'BKPFF'.       "参考过程

  "供应商行
  lt_accountpayable-itemno_acc   = '0000000010'.  "行项目编号
  lt_accountpayable-vendor_no    = is_post_struct-lifnr. "供应商编码
  lt_accountpayable-tax_code     = 'J0'.          "税码
  CONDENSE is_post_struct-zaprn.
  lv_zuonr = is_post_struct-zaprn.
  IF lv_zuonr IS INITIAL.
    lv_zuonr = TEXT-010.
  ENDIF.

  lt_accountpayable-alloc_nmbr   = lv_zuonr.          "分配
  lt_accountpayable-item_text    = is_post_struct-zunit.          "部门
*  lt_accountpayable-comp_code    = is_post_struct-bukrs.
  lt_accountpayable-sp_gl_ind    = 'F'.           "特殊总分类帐标志
*  lv_zumsk = is_post_struct-zumsk."目标特别总帐标志

  IF is_post_struct-zptyp = 'A'.        "预付
    lv_zumsk = 'A'.                "目标特别总帐标志
  ELSEIF is_post_struct-zptyp = 'O'.   "应付
    lv_zumsk = 'Z'.                "目标特别总帐标志
  ENDIF.

*  lt_accountpayable-pymt_meth    = '1'.                  "付款方式
  lt_accountpayable-bline_date   = is_post_struct-zfbdt. "到期日,资金申请
  lt_accountpayable-partner_bk   = is_post_struct-bvtyp. "对方开户行类型
*  lt_accountpayable-ref_key_1    = is_post_struct-zhtbh. "合同编号
  APPEND lt_accountpayable.
  CLEAR lt_accountpayable.

  "金额行
  lt_currencyamount-itemno_acc = '0000000010'.  "行项目编号
  lt_currencyamount-amt_doccur = 0 - is_post_struct-zpram. "金额
  lt_currencyamount-currency   = is_post_struct-waer1.     "货币码
*  lt_currencyamount-amt_doccur = 0 - is_post_struct-dmbtr. "金额
*  lt_currencyamount-currency   = is_post_struct-waer2. "货币码
  APPEND lt_currencyamount .
  CLEAR lt_currencyamount.

  "EXTENSION2扩展字段增强部分
  ls_zexten-posnr = '0000000010'.        "凭证行项目
  ls_zexten-zfitm = is_post_struct-zfitm."流动性项目
  ls_zexten-zumsk = lv_zumsk.            "目标特别总帐标志
  ls_zexten-tcode = is_post_struct-tcode."事务代码

  lt_extension-structure  = 'ZTMS_EPIC_EXTENSION'.
  lt_extension-valuepart1 = ls_zexten.
  APPEND lt_extension.

  CLEAR : lt_return.
  "调用凭证检查
  CALL FUNCTION 'BAPI_ACC_DOCUMENT_CHECK'
    EXPORTING
      documentheader          = ls_documentheader
    TABLES
*      accountgl               = lt_accountgl
      accountpayable          = lt_accountpayable
      currencyamount          = lt_currencyamount
      extension2              = lt_extension
      return                  = lt_return
    .
  CLEAR : et_message,et_message[].
  "返回检查结果
  LOOP AT lt_return WHERE type = 'E' OR type = 'A'.
    ls_message-msgty = lt_return-type.
    ls_message-index = is_post_struct-index.
    CONCATENATE TEXT-003 lt_return-parameter
           INTO ls_message-msgv1 SEPARATED BY ':' .
    CONCATENATE lt_return-message_v1 lt_return-message_v2
                lt_return-message_v1 lt_return-message_v2
           INTO ls_message-msgv2.
    ls_message-msgv3 = lt_return-message.
    APPEND ls_message TO et_message.
    CLEAR : lt_return,ls_message.
  ENDLOOP.

  IF iv_only_check = 'X'.
    "只检查过账
    RETURN.
  ELSE.
    "需要过账,并且检查没有错误,则过账
    IF et_message[] IS INITIAL.
      CLEAR : lt_return[],lt_return.
      "调用凭证过账
      CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
        EXPORTING
          documentheader          = ls_documentheader
        TABLES
*          accountgl               = lt_accountgl
          accountpayable          = lt_accountpayable
          currencyamount          = lt_currencyamount
          extension2              = lt_extension
          return                  = lt_return
        .
      CLEAR : et_message[],et_message.
      "返回检查结果
      LOOP AT lt_return WHERE type = 'E' OR type = 'A'.
        ls_message-msgty = lt_return-type.
        ls_message-index = is_post_struct-index.
        CONCATENATE TEXT-003 lt_return-parameter
               INTO ls_message-msgv1 SEPARATED BY ':' .
        CONCATENATE lt_return-message_v1 lt_return-message_v2
                    lt_return-message_v1 lt_return-message_v2
               INTO ls_message-msgv2.
        ls_message-msgv3 = lt_return-message.
        APPEND ls_message TO et_message.
        CLEAR : lt_return,ls_message.
      ENDLOOP.

      IF et_message[] IS INITIAL.
        "提交
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait          = 'X'  .
        READ TABLE lt_return INDEX 1.
        ev_belnr = lt_return-message_v2+0(10).  "凭证号
        ev_budat = is_post_struct-zreqd.        "过账日期
      ELSE.
        "回滚
        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK' .
      ENDIF.
    ENDIF.
  ENDIF.
ENDFUNCTION.

你可能感兴趣的:(sap,abap)