预制凭证及取消凭证

预制凭证及取消凭证(带增强)

事务代码:
FBV1:创建预制凭证
FBV2:修改预制凭证
FBV3:查看预制凭证
FBV0:预制凭证过账/冲销
FB08:已过账凭证冲销
SE19:实现 BADI 增强 ACC_DOCUMENT
BAPI:BAPI_ACC_DOCUMENT_POST

预制凭证

输入事务代码FBV0,输入数据,回车
预制凭证及取消凭证_第1张图片
下一行明细预制凭证及取消凭证_第2张图片
保存生成凭证号。
预制凭证过账:FBV0
输入公司代码、凭证号、财年回车,点击保存预制凭证及取消凭证_第3张图片

增强部分

SE11创建增强结构
预制凭证及取消凭证_第4张图片
SE19创建BADI实施预制凭证及取消凭证_第5张图片
选择 BKPFF,表示这个 BADI 实现只适用于会计凭证直接输入,并激活预制凭证及取消凭证_第6张图片
新建,保存激活成功预制凭证及取消凭证_第7张图片
CHANGE是我们需要的方法预制凭证及取消凭证_第8张图片
拷贝这段代码(不需要修改)

  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.

BAPI的使用

  LOOP AT LT_ZFPAMT_HEAD ASSIGNING FIELD-SYMBOL(<FS_HEAD>) .
    LV_ZFPSE = <FS_HEAD>-ZFPSE.
    LV_ZFPHS = <FS_HEAD>-ZFPHS.
    LV_BUKRS = <FS_HEAD>-BUKRS.
    AT FIRST.
      LS_DOCUMENTHEADER-DOC_DATE      = <FS_HEAD>-BUDAT_JH.                   "凭证日期
      LS_DOCUMENTHEADER-PSTNG_DATE    = -BUDAT_JH.                   "过账日期
      LS_DOCUMENTHEADER-TRANS_DATE    = <FS_HEAD>-BUDAT_JH.                   "换算日期
      LS_DOCUMENTHEADER-COMP_CODE     = -BUKRS.                      "公司代码
      LS_DOCUMENTHEADER-DOC_TYPE      = 'SA'.                                 "凭证类型
      LS_DOCUMENTHEADER-FISC_YEAR     = LS_DOCUMENTHEADER-PSTNG_DATE+0(4).    "财年
      LS_DOCUMENTHEADER-FIS_PERIOD    = LS_DOCUMENTHEADER-PSTNG_DATE+4(2).  "期间
      LS_DOCUMENTHEADER-USERNAME      = SY-UNAME.                             "用户名
      LS_DOCUMENTHEADER-DOC_STATUS    = '2'.                                   "凭证状态
      LS_DOCUMENTHEADER-REF_DOC_NO    = SY-DATUM .                            "参考凭证
    ENDAT.
    LOOP AT LT_ZFPAMT ASSIGNING FIELD-SYMBOL(<FS_ITEM>) WHERE ZFPSM = <FS_HEAD>-ZFPSM.
      LV_WAERS = <FS_ITEM>-WAERS.
      LV_LIFNR = <FS_ITEM>-LIFNR_JS.
      IF LS_DOCUMENTHEADER-HEADER_TXT IS INITIAL.
        LS_DOCUMENTHEADER-HEADER_TXT = <FS_ITEM>-NAME1_JS && <FS_HEAD>-BUDAT_JH+0(4) && <FS_HEAD>-BUDAT_JH+4(2) && '包装费补差发票'.
        LV_TEXT = LS_DOCUMENTHEADER-HEADER_TXT.
      ENDIF.
      LV_ITEMNO = LV_ITEMNO + 1.
      LS_ACCOUNTGL-ITEMNO_ACC = LV_ITEMNO.                        "行项目
      LS_ACCOUNTGL-GL_ACCOUNT = '8027000001'.                  "统驭科目
      LS_ACCOUNTGL-COSTCENTER = <FS_ITEM>-KOSTL.                  "成本中心
      LS_ACCOUNTGL-TAX_CODE   = -ZFPSM.                  "发票税码
      LS_ACCOUNTGL-COMP_CODE  = <FS_HEAD>-BUKRS.                  "公司代码
      LS_ACCOUNTGL-ITEM_TEXT  =  LS_DOCUMENTHEADER-HEADER_TXT.    "行项目文本
      APPEND LS_ACCOUNTGL TO LT_ACCOUNTGL.

*金额
      LS_CURRENCYAMOUNT-ITEMNO_ACC  = LV_ITEMNO.                   "行项目
      LS_CURRENCYAMOUNT-CURRENCY    = -WAERS.             "货币码
      LS_CURRENCYAMOUNT-AMT_DOCCUR  = <FS_ITEM>-ZFPAMT1 .          "
      APPEND LS_CURRENCYAMOUNT TO LT_CURRENCYAMOUNT.
      CLEAR LS_CURRENCYAMOUNT.

      LS_ITEMEXT-POSNR = LV_ITEMNO.
      IF -ZFPAMT1 > 0.
        LS_ITEMEXT-BSCHL = '40'.
      ELSE.
        LS_ITEMEXT-BSCHL = '50'.
      ENDIF.
      IF -ZFPAMT1 < 0.
        LS_ITEMEXT-XNEGP = 'X'.
      ELSE.
        LS_ITEMEXT-XNEGP = ''.
      ENDIF.
      LS_EXTENSION2-STRUCTURE   = 'ZDOC_EXTEN'.
      LS_EXTENSION2-VALUEPART1 = LS_ITEMEXT.
      APPEND LS_EXTENSION2 TO LT_EXTENSION2.
      CLEAR:LS_EXTENSION2,LS_ITEMEXT.

      SUM_ZFPAMT1 = SUM_ZFPAMT1 + -ZFPAMT1.
      CLEAR:LS_ACCOUNTGL,LS_CURRENCYAMOUNT.
    ENDLOOP.
  ENDLOOP.

  LOOP AT LT_ZFPNM_HEAD ASSIGNING .
    LV_ITEMNO = LV_ITEMNO + 1.
    LS_ACCOUNTGL-ITEMNO_ACC = LV_ITEMNO .
    LS_ACCOUNTGL-GL_ACCOUNT = '2171010400'.
    LS_ACCOUNTGL-COMP_CODE  = -BUKRS.
    LS_ACCOUNTGL-ALLOC_NMBR = -ZFPNM.
    APPEND LS_ACCOUNTGL TO LT_ACCOUNTGL.

    LS_ITEMEXT-POSNR = LV_ITEMNO.
    IF -ZFPSE_SUM > 0.
      LS_ITEMEXT-BSCHL = '40'.
    ELSE.
      LS_ITEMEXT-BSCHL = '50'.
    ENDIF.
    LS_ITEMEXT-XNEGP = ''.

    LS_EXTENSION2-STRUCTURE   = 'ZDOC_EXTEN'.
    LS_EXTENSION2-VALUEPART1 = LS_ITEMEXT.
    APPEND LS_EXTENSION2 TO LT_EXTENSION2.
    CLEAR:LS_ITEMEXT, LS_EXTENSION2 .
    LS_CURRENCYAMOUNT-ITEMNO_ACC = LV_ITEMNO .
    LS_CURRENCYAMOUNT-AMT_DOCCUR = -ZFPSE_SUM.
    LS_CURRENCYAMOUNT-CURRENCY = -WAERS.
    APPEND LS_CURRENCYAMOUNT TO LT_CURRENCYAMOUNT.
    CLEAR:LS_CURRENCYAMOUNT.
  ENDLOOP.
*应付凭证行项目
  LV_ITEMNO = LV_ITEMNO + 1.
  LS_ACCOUNTPAYABLE-ITEMNO_ACC = LV_ITEMNO .
  LS_ACCOUNTPAYABLE-BLINE_DATE = P_BUDAT .
  LS_ACCOUNTPAYABLE-VENDOR_NO = LV_LIFNR.
  LS_ACCOUNTPAYABLE-ITEM_TEXT = LV_TEXT.
  LS_ACCOUNTPAYABLE-COMP_CODE = LV_BUKRS.
  SELECT SINGLE AKONT
    INTO LS_ACCOUNTPAYABLE-GL_ACCOUNT
    FROM LFB1
   WHERE LIFNR = LV_LIFNR
     AND BUKRS = LV_BUKRS.
  APPEND LS_ACCOUNTPAYABLE TO LT_ACCOUNTPAYABLE.

  LS_CURRENCYAMOUNT-ITEMNO_ACC = LV_ITEMNO.
  LS_CURRENCYAMOUNT-AMT_DOCCUR = LV_ZFPHS * ( -1 ).
  LS_CURRENCYAMOUNT-CURRENCY = LV_WAERS.
  APPEND LS_CURRENCYAMOUNT TO LT_CURRENCYAMOUNT.

  "记账编码
  LS_ITEMEXT-POSNR = LV_ITEMNO.
  IF LS_CURRENCYAMOUNT-AMT_DOCCUR > 0.
    LS_ITEMEXT-BSCHL = '21'.
  ELSE.
    LS_ITEMEXT-BSCHL = '31'.
  ENDIF.
  LS_EXTENSION2-STRUCTURE   = 'ZDOC_EXTEN'.
  LS_EXTENSION2-VALUEPART1 = LS_ITEMEXT.
  APPEND LS_EXTENSION2 TO LT_EXTENSION2.
  CLEAR:LS_ITEMEXT, LS_EXTENSION2 .
      CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
      EXPORTING
        DOCUMENTHEADER = LS_DOCUMENTHEADER
      IMPORTING
        OBJ_KEY        = LV_OBJ_KEY
      TABLES
        ACCOUNTGL      = LT_ACCOUNTGL
        ACCOUNTPAYABLE = LT_ACCOUNTPAYABLE
        CURRENCYAMOUNT = LT_CURRENCYAMOUNT
        RETURN         = LT_RETURN
        EXTENSION2     = LT_EXTENSION2.
    READ TABLE LT_RETURN INTO LS_RETURN WITH KEY TYPE = 'E'.
    IF SY-SUBRC NE 0.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          WAIT = 'X'.
    ELSE.
      LOOP AT  LT_RETURN INTO LS_RETURN WHERE TYPE = 'E' OR TYPE = 'A'.
        LV_MSG = LV_MSG && LS_RETURN-MESSAGE.
        CLEAR:LS_RETURN.
      ENDLOOP.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK' .
     ENDIF.

凭证预览效果图
预制凭证及取消凭证_第9张图片

FBV0冲销(未过账)

预制凭证及取消凭证_第10张图片
预制凭证及取消凭证_第11张图片

FB08冲销(已过帐)

预制凭证及取消凭证_第12张图片

参考链接:BAPI_ACC_DOCUMENT_POST 增强
后记:写这些东西只是为了让给自己做个记录。如果你看到了这篇博文希望能对你有那么一点帮助,关于里面的注解只是我自己的理解,不正确的地方,请指出。

你可能感兴趣的:(BAPI,其他)