sap fi清账函数POSTING_INTERFACE*的使用

sap提供了一套标准的清账函数

  • POSTING_INTERFACE_START
  • POSTING_INTERFACE_CLEARING
  • POSTING_INTERFACE_END

但是很多小伙伴不怎么会去使用,因为POSTING_INTERFACE_*这一套函数使用有限制,而且比普通录屏要复杂一些,也需要通过增强才能实现所有的清账函数都能使用。
(需要注意的是,清账函数可以使用的tcode包含了所有的清账tcode:fb05、f-04等等),如下代码:

  DATA e_msgid   TYPE sy-msgid.
  DATA e_msgno   TYPE sy-msgno.
  DATA e_msgty   TYPE sy-msgty.
  DATA e_msgv1   TYPE sy-msgv1.
  DATA e_msgv2   TYPE sy-msgv2.
  DATA e_msgv3   TYPE sy-msgv3.
  DATA e_msgv4   TYPE sy-msgv4.
  DATA e_subrc   TYPE sy-subrc.

  DATA t_blntab  TYPE STANDARD TABLE OF blntab.
  DATA t_ftclear TYPE STANDARD TABLE OF ftclear.
  DATA t_ftpost  TYPE STANDARD TABLE OF ftpost.
  DATA t_fttax   TYPE STANDARD TABLE OF fttax.

  DATA: p_mode     TYPE rfpdo-allgazmd VALUE 'N',
        ls_ftpost  TYPE ftpost,
        ls_ftclear TYPE ftclear,
        ls_blntab  TYPE blntab,
        lt_bdcdata TYPE TABLE OF bdcdata, " bdc
        ls_bdcdata TYPE bdcdata,
        gv_id      TYPE char20, "MEMORY ID
        lv_mitkz   TYPE mitkz,
        lv_wrbtr   TYPE string,
        lv_budat   TYPE string,
        lr_hkont   TYPE RANGE OF bseg-hkont.

* 定义宏
  DEFINE def_ftpost.
    ls_ftpost-stype = &1.
    ls_ftpost-count = &2.
    ls_ftpost-fnam  = &3.
    ls_ftpost-fval  = &4.
    APPEND ls_ftpost TO t_ftpost.
  END-OF-DEFINITION.

  DEFINE def_bdcdata.
    CLEAR ls_bdcdata.
    ls_bdcdata-program = &1.
    ls_bdcdata-dynpro = &2.
    ls_bdcdata-dynbegin = &3.
    ls_bdcdata-fnam = &4.
    ls_bdcdata-fval = &5.
    APPEND ls_bdcdata TO lt_bdcdata.
  END-OF-DEFINITION.

  "银行科目
  lr_hkont = VALUE #( sign = 'I' option = 'CP' ( low = '1001*' )
                                               ( low = '1002*' ) ).

  CLEAR:lt_bdcdata,gv_id.
  CONCATENATE sy-uname 'ADD_SCR' INTO gv_id.

  DATA:doc_line TYPE i,
       lv_kbetr TYPE string.

  REFRESH: t_blntab[],  t_ftclear[], t_ftpost[],  t_fttax[].

  DATA(lv_bktxt) = |{ ls_input-req-zzjptbm }|. " 抬头文本
  " Header
  def_ftpost: 'K' '001' 'BKPF-BUKRS' ls_input-req-bukrs,      " 公司代码
              'K' '001' 'BKPF-BUDAT' ls_input-req-zjyrq,      " 过账日期
              'K' '001' 'BKPF-BLDAT' ls_input-req-zjyrq,      " 记账日期
              'K' '001' 'BKPF-MONAT' ls_input-req-zjyrq+4(2). " 期间

*  CASE ls_input-req-zywlx.
*    WHEN '票据兑付'.
*      def_ftpost: 'K' '001' 'BKPF-BLART' 'KZ'.      " 凭证类型
*    WHEN OTHERS.
*      def_ftpost: 'K' '001' 'BKPF-BLART' 'DZ'.      " 凭证类型
*  ENDCASE.

  def_ftpost: 'K' '001' 'BKPF-BLART' 'ZD'.      " 凭证类型
  def_ftpost: 'K' '001' 'BKPF-WAERS' ls_input-req-waers,        " 货币
              'K' '001' 'BKPF-BKTXT' lv_bktxt.                  " 抬头文本

  " 会计凭证行项目 item1
  doc_line  = doc_line + 1.  
      def_ftpost : 'P' doc_line 'RF05A-NEWBS' '50', " posting key
                           'P' doc_line 'RF05A-NEWUM' ''.  
    lv_gl_account = '1001010001'.
    def_ftpost:'P' doc_line 'RF05A-NEWKO' '1001010001'.
      DATA(lv_item_text)     = |付{ ls_input-req-zdfhm }{ ls_input-req-zywlx }|." 文本
  def_ftpost :   'P' doc_line 'BSEG-SGTXT' lv_item_text.

  IF lv_gl_account IN lr_hkont.
    def_ftpost :'P' doc_line 'BSEG-RSTGR' ls_input-req-zyydm.  " 原因代码
  ENDIF.

  CLEAR:lv_kbetr.
  lv_kbetr = ls_input-req-zje.
  CONDENSE lv_wrbtr NO-GAPS.
  def_ftpost : 'P' doc_line 'BSEG-WRBTR' lv_kbetr.       " 金额

  " 部分清账界面
  def_bdcdata: 'SAPDF05X' '3100' 'X' '' '',
*  def_bdcdata: 'SAPMF05A' '0122' 'X' '' '',
               '' '' '' 'BDC_OKCODE' '=PART', "点击部分清账页签
               '' '' '' 'BDC_SUBSCR' 'SAPDF05X   6102PAGE',
               '' '' '' 'BDC_CURSOR' 'DF05B-PSSKT(01)',
               '' '' '' 'RF05A-ABPOS' '1'.

  " 查找清账行
  " 根据ZPJHM=BSED-WBANK取BSED-BELNR、BSED-GJAHR、BSED-BUZEI;再根据BSED取ACDOCA-KOART、ACDOCA-LIFNR、ACDOCA-KUNNR;去重后写入下面的数据
  SELECT a~*
    FROM bsed AS a
   INNER JOIN @ls_input-req-billdetails AS b
      ON a~wbank = b~zpjhm
    INTO TABLE @DATA(lt_bsed).
  SORT lt_bsed BY bukrs belnr gjahr buzei.
  DELETE ADJACENT DUPLICATES FROM lt_bsed COMPARING bukrs belnr gjahr buzei.
  IF lt_bsed IS NOT INITIAL.
    SELECT *
      FROM acdoca
      INTO TABLE @DATA(lt_acdoca)
       FOR ALL ENTRIES IN @lt_bsed
     WHERE rbukrs = @lt_bsed-bukrs
       AND belnr = @lt_bsed-belnr
       AND gjahr = @lt_bsed-gjahr
       AND buzei = @lt_bsed-buzei.
    SORT lt_acdoca BY rbukrs belnr gjahr buzei.
  ENDIF.

  SORT ls_input-req-billdetails BY zpjhm.
  LOOP AT lt_bsed INTO DATA(ls_bsed).

    READ TABLE lt_acdoca INTO DATA(ls_acdoca) WITH KEY rbukrs = ls_bsed-bukrs
                                                       belnr  = ls_bsed-belnr
                                                       gjahr  = ls_bsed-gjahr
                                                       buzei  = ls_bsed-buzei BINARY SEARCH.
    CHECK sy-subrc EQ 0.
    READ TABLE ls_input-req-billdetails INTO DATA(ls_billdetails) WITH KEY zpjhm = ls_bsed-wbank BINARY SEARCH.
    CHECK sy-subrc EQ 0.

    CLEAR:lv_mitkz.
    SELECT SINGLE mitkz INTO @lv_mitkz FROM skb1 WHERE bukrs = @ls_acdoca-rbukrs  AND saknr = @ls_acdoca-racct.
    CHECK sy-subrc = 0.

    CLEAR ls_ftclear.
    ls_ftclear-agkoa  = lv_mitkz.               " 账户类型
    ls_ftclear-agbuk  = ls_input-req-bukrs." 公司代码
    IF ls_acdoca-umskz IS  INITIAL.
      ls_ftclear-xnops  = 'X'.             " 标准未清项
    ELSE.
      ls_ftclear-agums  = ls_acdoca-umskz. " 特殊总帐标识符
    ENDIF.
    ls_ftclear-selfd  = 'BELNR'.           " 凭证索引中的字段名
    ls_ftclear-agkon  = ls_acdoca-lifnr.   " 供应商
    ls_ftclear-selvon = ls_acdoca-belnr && ls_acdoca-gjahr && ls_acdoca-buzei. "选择未清项目的搜索标准的输入字段
    APPEND ls_ftclear TO t_ftclear.

    def_bdcdata: 'SAPDF05X' '3100' 'X' '' '',
                  '' '' '' 'BDC_OKCODE' '/00',
                  '' '' '' 'BDC_OKCODE' '=OSU',               " 点击筛选按钮
                  '' '' '' 'BDC_SUBSCR' 'SAPDF05X   6104PAGE',
                  '' '' '' 'BDC_CURSOR' 'RF05A-ABPOS',
                  '' '' '' 'RF05A-ABPOS' '1'.

    " 选择凭证编号按钮
    def_bdcdata: 'SAPDF05X' '2000' 'X' '' '',
                  '' '' '' 'BDC_CURSOR' 'RF05A-XPOS1(01)',
                  '' '' '' 'BDC_OKCODE' '=GO',
                  '' '' '' 'RF05A-XPOS1(01)' '',
                  '' '' '' 'RF05A-XPOS1(04)' 'X'.

    " 填入凭证编号
    def_bdcdata: 'SAPDF05X' '0731' 'X' '' '',
                  '' '' '' 'BDC_CURSOR' 'RF05A-SEL01(01)',
                  '' '' '' 'BDC_OKCODE' '=GO',
                  '' '' '' 'RF05A-SEL01(01)' ls_acdoca-belnr .

    " 点击筛选按钮
    def_bdcdata: 'SAPDF05X' '3100' 'X' '' '',
                  '' '' '' 'BDC_OKCODE' '/00',
                  '' '' '' 'BDC_OKCODE' '=OSU',
                  '' '' '' 'BDC_SUBSCR' 'SAPDF05X   6104PAGE'.

    " 选择过账日期按钮
    def_bdcdata: 'SAPDF05X' '2000' 'X' '' '',
                  '' '' '' 'BDC_CURSOR' 'RF05A-XPOS1(01)',
                  '' '' '' 'BDC_OKCODE' '=GO',
                  '' '' '' 'RF05A-XPOS1(01)' '',
                  '' '' '' 'RF05A-XPOS1(05)' 'X'.
    CLEAR:lv_budat.
    lv_budat = ls_acdoca-budat.
    CONDENSE lv_budat NO-GAPS.
    " 填入凭证过账日期
    def_bdcdata: 'SAPDF05X' '0732' 'X' '' '',
                  '' '' '' 'BDC_OKCODE' '=GO',
                  '' '' '' 'RF05A-VONDT(01)' lv_budat .

    " 点击筛选按钮
    def_bdcdata: 'SAPDF05X' '3100' 'X' '' '',
                  '' '' '' 'BDC_OKCODE' '/00',
                  '' '' '' 'BDC_OKCODE' '=OSU',
                  '' '' '' 'BDC_SUBSCR' 'SAPDF05X   6104PAGE'.

    " 选择行按钮
    def_bdcdata: 'SAPDF05X' '2000' 'X' '' '',
                  '' '' '' 'BDC_CURSOR' 'RF05A-XPOS1(01)',
                  '' '' '' 'BDC_OKCODE' '=GO',
                  '' '' '' 'RF05A-XPOS1(01)' '',
                  '' '' '' 'RF05A-XPOS1(02)' 'X'.

    " 填入凭证行
    def_bdcdata: 'SAPDF05X' '0731' 'X' '' '',
                  '' '' '' 'BDC_CURSOR' 'RF05A-SEL01(01)',
                  '' '' '' 'BDC_OKCODE' '=GO',
                  '' '' '' 'RF05A-SEL01(01)' ls_acdoca-buzei.
    CLEAR:lv_wrbtr.
    lv_wrbtr = ls_billdetails-zpjje.
    CONDENSE lv_wrbtr NO-GAPS.

    " 填入金额
    def_bdcdata: 'SAPDF05X' '3100' 'X' '' '',
                 '' '' '' 'BDC_OKCODE' '/00',                " 回车
                 '' '' '' 'BDC_SUBSCR' 'SAPDF05X   6104PAGE',
                 '' '' '' 'RF05A-ABPOS' '1',
                 '' '' '' 'BDC_CURSOR' 'DF05B-PSZAH(01)',
                 '' '' '' 'DF05B-PSZAH(01)' lv_wrbtr.        " 金额

    def_bdcdata: 'SAPDF05X' '3100' 'X' '' '',
                 '' '' '' 'BDC_OKCODE' '=OSE',  " 返回上一层
                 '' '' '' 'BDC_SUBSCR' 'SAPDF05X   6104PAGE'.

    def_bdcdata: 'SAPDF05X' '3100' 'X' '' '',
                 '' '' '' 'BDC_OKCODE' '=OSE',  "返回上一层
                 '' '' '' 'BDC_SUBSCR' 'SAPDF05X   6104PAGE'.

    def_bdcdata: 'SAPDF05X' '3100' 'X' '' '',
                 '' '' '' 'BDC_OKCODE' '=OSE',  "返回上一层
                 '' '' '' 'BDC_SUBSCR' 'SAPDF05X   6104PAGE'.
  ENDLOOP.

  " 过账
  def_bdcdata: 'SAPDF05X' '3100' 'X' '' '',
         '' '' '' 'BDC_OKCODE' '=BU', "保存过账
         '' '' '' 'BDC_SUBSCR' 'SAPDF05X   6104PAGE',
         '' '' '' 'BDC_CURSOR' 'DF05B-PSZAH(01)',
         '' '' '' 'RF05A-ABPOS' '1'.
 
  CALL FUNCTION 'POSTING_INTERFACE_START'
    EXPORTING
      i_client           = sy-mandt
      i_function         = 'C'
      i_keep             = 'X'
      i_mode             = p_mode
      i_update           = 'S'
      i_user             = sy-uname
    EXCEPTIONS
      client_incorrect   = 1
      function_invalid   = 2
      group_name_missing = 3
      mode_invalid       = 4
      update_invalid     = 5
      OTHERS             = 6.
  IF sy-subrc <> 0.

    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 INTO DATA(lv_txt).
    DATA(lv_msg) = '创建失败:' && lv_txt.
    set_msg_ats003 '' 'E' lv_msg.
    RETURN.

  ELSE.

    IF lt_bdcdata IS NOT INITIAL.
      EXPORT lt_bdcdata FROM lt_bdcdata TO MEMORY ID gv_id.  "部分清账时补充屏幕数据
    ENDIF.
    REFRESH:t_blntab[].
    CALL FUNCTION 'POSTING_INTERFACE_CLEARING'
      EXPORTING
        i_auglv                    = 'UMBUCHNG'
        i_tcode                    = 'FB05' " 调用F-04
        i_sgfunct                  = 'C'
      IMPORTING
        e_msgid                    = e_msgid
        e_msgno                    = e_msgno
        e_msgty                    = e_msgty
        e_msgv1                    = e_msgv1
        e_msgv2                    = e_msgv2
        e_msgv3                    = e_msgv3
        e_msgv4                    = e_msgv4
        e_subrc                    = e_subrc
      TABLES
        t_blntab                   = t_blntab
        t_ftclear                  = t_ftclear
        t_ftpost                   = t_ftpost
        t_fttax                    = t_fttax
      EXCEPTIONS
        clearing_procedure_invalid = 1
        clearing_procedure_missing = 2
        table_t041a_empty          = 3
        transaction_code_invalid   = 4
        amount_format_error        = 5
        too_many_line_items        = 6
        company_code_invalid       = 7
        screen_not_found           = 8
        no_authorization           = 9.

    READ TABLE t_blntab INTO ls_blntab INDEX 1.
    IF sy-subrc <> 0.

      CALL FUNCTION 'FORMAT_MESSAGE'
        EXPORTING
          id        = e_msgid
          lang      = sy-langu
          no        = e_msgno
          v1        = e_msgv1
          v2        = e_msgv2
          v3        = e_msgv3
          v4        = e_msgv4
        IMPORTING
          msg       = lv_txt
        EXCEPTIONS
          not_found = 1
          OTHERS    = 2.
      lv_msg = '创建失败:' && lv_txt.
      set_msg_ats003 '' 'E' lv_msg.
      RETURN.

    ELSE.

      set_msg_ats003 ls_blntab-belnr 'S' '凭证创建成功'.

    ENDIF.

    CALL FUNCTION 'POSTING_INTERFACE_END'
      EXPORTING
        i_bdcimmed              = 'X'
      EXCEPTIONS
        session_not_processable = 1
        OTHERS                  = 2.

  ENDIF.

同时需要在程序LFIPIF00中做一个代码的隐式增强:

代码如下:

*{   INSERT         S4DK900285                                        1
     DATA:GV_ID TYPE CHAR20,
          LT_BDCDATA TYPE TABLE OF BDCDATA,
          LS_BDCDATA TYPE BDCDATA,
          LV_LINES TYPE I.

     CLEAR:LT_BDCDATA,GV_ID.
     CONCATENATE SY-UNAME 'ADD_SCR' INTO GV_ID.

    IMPORT LT_BDCDATA TO LT_BDCDATA FROM MEMORY ID GV_ID. "补充屏幕数据

    IF LT_BDCDATA IS NOT INITIAL.
      APPEND LINES OF LT_BDCDATA TO FT.

      LOOP AT  FT WHERE FVAL = '/11'.
         FT-FVAL = '=PA' .
       MODIFY FT.
      ENDLOOP.

     FREE MEMORY ID GV_ID.
    ENDIF.
*}   INSERT

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