首页 > 其他分享 >财务FB05清账bapi

财务FB05清账bapi

时间:2022-09-24 23:22:39浏览次数:40  
标签:bapi populate ftpost 清账 sy lv FB05 wrbtr ls

  TYPES: BEGIN OF ts_key_bseg,
           bukrs TYPE bseg-bukrs,
           belnr TYPE bseg-belnr,
           gjahr TYPE bseg-gjahr,
           buzei TYPE bseg-buzei,
         END OF  ts_key_bseg.
  DATA: ls_key_bseg TYPE ts_key_bseg.
  DATA: lt_bsid_key TYPE TABLE OF ts_key_bseg WITH HEADER LINE.

  DATA: it_blntab  TYPE TABLE OF blntab WITH HEADER LINE,
        it_ftclear TYPE TABLE OF ftclear WITH HEADER LINE,
        it_ftpost  TYPE TABLE OF ftpost WITH HEADER LINE,
        it_fttax   TYPE TABLE OF fttax WITH HEADER LINE.

  DATA: lt_bsad_clearing TYPE TABLE OF bsad WITH HEADER LINE. "客户未清项表.

  DATA: lv_ftclear_agbuk TYPE bukrs. "清账公司代码.
  DATA: lv_mode TYPE rfpdo-allgazmd.
  DATA: lv_type TYPE c.
  DATA: lv_count TYPE count_pi.
  DATA: LV_selvon TYPE ftclear-selvon.
  DATA: BEGIN OF lt_txt OCCURS 0,
          sgtxt TYPE bseg-sgtxt,
        END OF lt_txt.

  DEFINE populate_ftclear.
    it_ftclear-agkoa  = &1.       "科目类型,based on account type, Vendor or Customer for Out Going / Incoming Payments.
    it_ftclear-agbuk  = &2.       "公司代码.
    it_ftclear-agkon  = &3.       "科目.
    it_ftclear-xnops  = 'X'.      "标准未清项目.
    it_ftclear-agums  = 'VW'.      "特别总账未清项目.
*    it_ftclear-selfd  = 'XBLNR'.   "凭证索引中的字段名(使用该字段搜索用来做对冲的借贷方).
    it_ftclear-selfd  = 'BELNR'.   "凭证索引中的字段名(使用该字段搜索用来做对冲的借贷方).
    it_ftclear-selvon = &4.   "Input Field for Search Criterion for Selecting Open Items.
    APPEND it_ftclear.
  END-OF-DEFINITION.

  DEFINE populate_ftpost.
    it_ftpost-stype = &1.   "K为header,P为item.
    it_ftpost-count = &2.   "凭证抬头或行项目的计数器(记帐界面).
    it_ftpost-fnam  = &3.   "BDC 字段名.
    it_ftpost-fval  = &4.   "BDC 字段值.

    DESCRIBE FIELD &4 TYPE lv_type.
    IF lv_type EQ 'P'.
      CLEAR it_ftpost-fval.
      WRITE &4 TO it_ftpost-fval.
    CONDENSE it_ftpost-fnam NO-GAPS.
    CONDENSE it_ftpost-fval NO-GAPS.
    ENDIF.
        IF lv_type EQ 'D'.
          CLEAR it_ftpost-fval.
      WRITE &4 TO it_ftpost-fval.
     CONDENSE it_ftpost-fval NO-GAPS.
      ENDIF.
    IF NOT ( lv_type EQ 'D' AND &4 IS INITIAL ).
    APPEND it_ftpost.
    ENDIF.
  END-OF-DEFINITION.

  LOOP AT lt_tab_2 ASSIGNING <ls_it_tab_1>.
    CLEAR lv_iferror_flg.

    CLEAR: lv_text.
    CLEAR ls_kunnrt.
    READ TABLE lt_kunnrt INTO ls_kunnrt WITH KEY kunnr = <ls_it_tab_1>-kunnr
    BINARY SEARCH.
    IF sy-subrc EQ 0.
      lv_text = |{ <ls_it_tab_1>-budat(4) }年{ <ls_it_tab_1>-budat+4(2) }月{ <ls_it_tab_1>-budat+6(2) }日收{ ls_kunnrt-name1 }客户货款|.
    ENDIF.
    CONDENSE lv_text NO-GAPS.
    lv_mode = 'N'.
    CALL FUNCTION 'POSTING_INTERFACE_START'
      EXPORTING
        i_client           = sy-mandt
        i_function         = 'C'
        i_mode             = lv_mode
        i_keep             = 'X'
        i_update           = 'S'
        i_user             = sy-uname
      EXCEPTIONS
        client_incorrect   = 1
        function_invalid   = 2
        group_name_missing = 3
        mode_invalid       = 4
        update_invalid     = 5
        user_invalid       = 6
        OTHERS             = 7.

    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.

    IF <ls_it_tab_1>-zhkfs EQ 'A'.
      populate_ftpost 'K' 1 'BKPF-BUKRS' <ls_it_tab_1>-bukrs. " company code
      populate_ftpost 'K' 1 'BKPF-BLART' 'DZ'.
      populate_ftpost 'K' 1 'BKPF-BLDAT' <ls_it_tab_1>-budat. " posting date
      populate_ftpost 'K' 1 'BKPF-BUDAT' <ls_it_tab_1>-budat.
      populate_ftpost 'K' 1 'BKPF-WAERS' <ls_it_tab_1>-waers.    " currency

      populate_ftpost 'P' 1 'RF05A-NEWBS' '40'.
      <ls_it_tab_1>-hkont = |{ <ls_it_tab_1>-hkont ALPHA = IN }|.
      populate_ftpost 'P' 1 'RF05A-NEWKO' <ls_it_tab_1>-hkont.
      populate_ftpost 'P' 1 'COBL-PRCTR' <ls_it_tab_1>-prctr.
      populate_ftpost 'P' 1 'BSEG-RSTGR' <ls_it_tab_1>-rstgr.
*      populate_ftpost 'P' 1 'BSEG-SGTXT' <ls_it_tab_1>-sgtxt.
      populate_ftpost 'P' 1 'BSEG-SGTXT' lv_text.
      CLEAR lv_xref3.
      lv_xref3 = 'CRM' && <ls_it_tab_1>-zcrm.
      populate_ftpost 'P' 1 'BSEG-XREF3' lv_xref3.

      CLEAR lv_wrbtr_diff.
      lv_wrbtr_diff =
      <ls_it_tab_1>-wrbtr - <ls_it_tab_1>-wrbtr1.
      populate_ftpost 'P' 1 'BSEG-WRBTR' lv_wrbtr_diff.

      populate_ftpost 'P' 2 'RF05A-NEWBS' '40'.
      CLEAR lv_hkont.
      lv_hkont = |{ '66030300' ALPHA = IN }|.
      populate_ftpost 'P' 2 'RF05A-NEWKO' lv_hkont.
      CLEAR lv_wrbtr.
      lv_wrbtr = <ls_it_tab_1>-wrbtr1.
      populate_ftpost 'P' 2 'BSEG-WRBTR' lv_wrbtr.
*      populate_ftpost 'P' 2 'BSEG-SGTXT' <ls_it_tab_1>-sgtxt.
      populate_ftpost 'P' 2 'BSEG-SGTXT' lv_text.
      lv_count = 2.

    ELSEIF <ls_it_tab_1>-zhkfs EQ 'V' OR <ls_it_tab_1>-zhkfs EQ 'W' .

      populate_ftpost 'K' 1 'BKPF-BUKRS' <ls_it_tab_1>-bukrs. " company code
      populate_ftpost 'K' 1 'BKPF-BLART' 'DZ'.
      populate_ftpost 'K' 1 'BKPF-BLDAT' <ls_it_tab_1>-budat. " posting date
      populate_ftpost 'K' 1 'BKPF-BUDAT' <ls_it_tab_1>-budat.
      populate_ftpost 'K' 1 'BKPF-WAERS' <ls_it_tab_1>-waers.    " currency

      populate_ftpost 'P' 1 'RF05A-NEWBS' '09'.
      CLEAR lv_kunnr.
      lv_kunnr = |{ <ls_it_tab_1>-kunnr ALPHA = IN }|.
      populate_ftpost 'P' 1 'RF05A-NEWKO' lv_kunnr.
      populate_ftpost 'P' 1 'RF05A-NEWUM' <ls_it_tab_1>-zhkfs.
      populate_ftpost 'P' 1 'BSED-WLZBP' <ls_it_tab_1>-wlzbp.
      populate_ftpost 'P' 1 'BSED-WBZOG' <ls_it_tab_1>-wbzog.

      SELECT SINGLE butxt
      INTO lv_butxt
      FROM t001
      WHERE bukrs EQ <ls_it_tab_1>-bukrs.
      populate_ftpost 'P' 1 'BSED-WNAME' lv_butxt.

      populate_ftpost 'P' 1 'BSED-WDATE' <ls_it_tab_1>-wdate.
      populate_ftpost 'P' 1 'BSEG-ZFBDT' <ls_it_tab_1>-zfbdt.
*      populate_ftpost 'P' 1 'COBL-PRCTR' <ls_it_tab_1>-prctr.
      populate_ftpost 'P' 1 'BSEG-KKBER' <ls_it_tab_1>-kkber.
      CLEAR lv_wrbtr.
      lv_wrbtr = <ls_it_tab_1>-wrbtr.
      populate_ftpost 'P' 1 'BSEG-WRBTR' lv_wrbtr.
      populate_ftpost 'P' 1 'BSEG-SGTXT' lv_text.
      populate_ftpost 'P' 1 'BSEG-ZUONR' '库存'.
      IF sy-sysid EQ 'S4D'.
        populate_ftpost 'P' 1 'BSED-BOENO' <ls_it_tab_1>-zhkfs.
      ENDIF.
      populate_ftpost 'P' 1 'BSEG-RSTGR' <ls_it_tab_1>-rstgr.

      lv_count = 1.

    ELSE.
      populate_ftpost 'K' 1 'BKPF-BUKRS' <ls_it_tab_1>-bukrs. " company code
      populate_ftpost 'K' 1 'BKPF-BLART' 'DZ'.
      populate_ftpost 'K' 1 'BKPF-BLDAT' <ls_it_tab_1>-budat. " posting date
      populate_ftpost 'K' 1 'BKPF-BUDAT' <ls_it_tab_1>-budat.
      populate_ftpost 'K' 1 'BKPF-WAERS' <ls_it_tab_1>-waers.    " currency

      CLEAR lv_count.

    ENDIF.

***************************************
    "FTPOST插入差异金额的行项目
    CLEAR lt_bsid_key[].
    LOOP AT it_tab_2 WHERE zcrm EQ <ls_it_tab_1>-zcrm.
      lv_kunnr = |{ <ls_it_tab_1>-kunnr ALPHA = IN }|.

      CLEAR ls_key_bseg.
      ls_key_bseg-belnr = |{ it_tab_2-belnr ALPHA = IN }|.

      ls_key_bseg-bukrs = <ls_it_tab_1>-bukrs.
      ls_key_bseg-buzei = it_tab_2-buzei.
      ls_key_bseg-gjahr = it_tab_2-budat(4).

      CLEAR LV_selvon.
      CONCATENATE ls_key_bseg-belnr ls_key_bseg-gjahr ls_key_bseg-buzei INTO LV_selvon.

      populate_ftclear 'D' <ls_it_tab_1>-bukrs lv_kunnr LV_selvon.


      lt_bsid_key = ls_key_bseg.
      APPEND lt_bsid_key.
      CLEAR lt_bsid_key.
    ENDLOOP.
    IF sy-subrc NE 0.
      lv_iferror_flg = 'X'.
    ENDIF.



    IF lt_bsid_key[] IS NOT INITIAL.
      SELECT bukrs, belnr, gjahr, buzei, bschl,shkzg,
      wrbtr, hkont,kunnr, zfbdt,zterm,kkber,sgtxt
      INTO TABLE @DATA(lt_bsid)
            FROM bsid
            FOR ALL ENTRIES IN @lt_bsid_key
            WHERE belnr = @lt_bsid_key-belnr
            AND gjahr = @lt_bsid_key-gjahr
            AND bukrs = @lt_bsid_key-bukrs
            AND buzei = @lt_bsid_key-buzei
            %_HINTS HDB '&prefer_in_itab_opt 1&&prefer_fix_blocking -1&'.
      IF sy-subrc NE 0.
        lv_iferror_flg = 'X'.
      ENDIF.
    ENDIF.

    IF lv_iferror_flg IS NOT INITIAL.
      CLEAR ls_tab.
      ls_tab-type = 'E'.
      ls_tab-message = '传入的待核销凭证未在BSID取到有效数据,请检查'.
      ls_tab-zcrm = <ls_it_tab_1>-zcrm.
      APPEND ls_tab TO et_tab.
      CONTINUE.
    ENDIF.

    CLEAR lv_wrbtr_total.
    LOOP AT lt_bsid ASSIGNING FIELD-SYMBOL(<ls_bsid>) .
      IF <ls_bsid>-shkzg EQ 'S'.
        <ls_bsid>-wrbtr = abs( <ls_bsid>-wrbtr ).
      ELSE.
        <ls_bsid>-wrbtr = abs( <ls_bsid>-wrbtr ) * ( -1 ).
      ENDIF.
      lv_wrbtr_total = lv_wrbtr_total + <ls_bsid>-wrbtr.
    ENDLOOP.

    CLEAR lv_sgtxt.
    CLEAR lt_txt[].
    MOVE-CORRESPONDING lt_bsid TO lt_txt[].
    SORT lt_txt[] BY sgtxt.
    DELETE ADJACENT DUPLICATES FROM lt_txt[] COMPARING sgtxt.
    LOOP AT lt_txt.
      IF lv_sgtxt IS INITIAL.
        lv_sgtxt = lt_txt-sgtxt.
      ELSE.
        lv_sgtxt = lv_sgtxt && ',' && lt_txt-sgtxt.
      ENDIF.
    ENDLOOP.
    IF <ls_it_tab_1>-zhkfs NE 'Z'.
      SORT lt_bsid BY wrbtr DESCENDING.
      READ TABLE lt_bsid INTO DATA(ls_bsid) INDEX 1.
      CLEAR lv_wrbtr_diff.
      lv_wrbtr_diff = lv_wrbtr_total  - <ls_it_tab_1>-wrbtr.

      IF lv_wrbtr_diff NE 0.
        lv_count = lv_count + 1.
        populate_ftpost 'P' lv_count 'RF05A-NEWBS' '06'.
        populate_ftpost 'P' lv_count 'RF05A-NEWKO' ls_bsid-kunnr.
        populate_ftpost 'P' lv_count 'BSEG-XNEGP' 'X'.

        populate_ftpost 'P' lv_count 'BSEG-WRBTR' lv_wrbtr_diff.
        populate_ftpost 'P' lv_count 'BSEG-ZFBDT' ls_bsid-zfbdt.
        populate_ftpost 'P' lv_count 'BSEG-ZTERM' ls_bsid-zterm.
        populate_ftpost 'P' lv_count 'BSEG-KKBER' ls_bsid-kkber.

        populate_ftpost 'P' lv_count 'BSEG-SGTXT' lv_sgtxt.
      ENDIF.

    ELSE.

      IF lv_wrbtr_total LE 0.
        SORT lt_bsid BY wrbtr .
        READ TABLE lt_bsid INTO ls_bsid INDEX 1.
        IF lv_wrbtr NE 0.
          CLEAR lv_wrbtr.
          lv_wrbtr = abs( lv_wrbtr_total ).

          lv_count = lv_count + 1.
          populate_ftpost 'P' lv_count 'RF05A-NEWBS' '15'.
          populate_ftpost 'P' lv_count 'RF05A-NEWKO' ls_bsid-kunnr.
          populate_ftpost 'P' lv_count 'BSEG-XNEGP' ' '.

          populate_ftpost 'P' lv_count 'BSEG-WRBTR' lv_wrbtr.
          populate_ftpost 'P' lv_count 'BSEG-ZFBDT' ls_bsid-zfbdt.
          populate_ftpost 'P' lv_count 'BSEG-KKBER' ls_bsid-kkber.

          populate_ftpost 'P' lv_count 'BSEG-SGTXT' lv_sgtxt.
        ENDIF.
      ELSE.

        SORT lt_bsid BY wrbtr DESCENDING.
        READ TABLE lt_bsid INTO ls_bsid INDEX 1.

        lv_count = lv_count + 1.
        populate_ftpost 'P' lv_count 'RF05A-NEWBS' '06'.
        populate_ftpost 'P' lv_count 'RF05A-NEWKO' ls_bsid-kunnr.
        populate_ftpost 'P' lv_count 'BSEG-XNEGP' 'X'.
        CLEAR lv_wrbtr.
        lv_wrbtr = abs( lv_wrbtr_total ).
        populate_ftpost 'P' lv_count 'BSEG-WRBTR' lv_wrbtr.
        populate_ftpost 'P' lv_count 'BSEG-ZFBDT' ls_bsid-zfbdt.
        populate_ftpost 'P' lv_count 'BSEG-ZTERM' ls_bsid-zterm.
        populate_ftpost 'P' lv_count 'BSEG-KKBER' ls_bsid-kkber.

        populate_ftpost 'P' lv_count 'BSEG-SGTXT' lv_sgtxt.
      ENDIF.

    ENDIF.
***************************************

    CALL FUNCTION 'POSTING_INTERFACE_CLEARING' "Post with clearing (FB05) using internal posting interface
      EXPORTING
        i_auglv                    = 'AUSGZAHL'                " t041a-auglv   Clearing Transaction
        i_tcode                    = 'FB05'              " sy-tcode      Transaction code
        i_sgfunct                  = 'C'                 " rfipi-sgfunct  Different FUNCT function
      IMPORTING
        e_msgid                    = sy-msgid              " sy-msgid      Message ID (only for Call Trans. ..Using)
        e_msgno                    = sy-msgno              " sy-msgno      Message number (only for Call Trans. ..Using)
        e_msgty                    = sy-msgty              " sy-msgty      Message category (only for Call Trans. ..Using)
        e_msgv1                    = sy-msgv1              " sy-msgv1      Message variable 1 (only for Call Trans. ..Using)
        e_msgv2                    = sy-msgv2              " sy-msgv2      Message variable 2 (only for Call Trans. ..Using)
        e_msgv3                    = sy-msgv3              " sy-msgv3      Message variable 3 (only for Call Trans. ..Using)
        e_msgv4                    = sy-msgv4              " sy-msgv4      Message variable 4 (only for Call Trans. ..Using)
        e_subrc                    = lv_subrc
      TABLES
        t_blntab                   = it_blntab              " blntab        Table of the document numbers (only for Call Trans ..)
        t_ftclear                  = it_ftclear              " ftclear       Clearing data
        t_ftpost                   = it_ftpost              " ftpost        Document header and item data
        t_fttax                    = it_fttax              " fttax         Taxes
      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
        OTHERS                     = 10.
    IF ( sy-subrc <> 0 OR lv_subrc <> 0 ).
      CALL FUNCTION 'POSTING_INTERFACE_END'. "POSTING INTERFACE CLOSE

      CLEAR ls_tab.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 INTO ls_tab-message .
      ls_tab-type = 'E'.
      ls_tab-zcrm = <ls_it_tab_1>-zcrm.
      APPEND ls_tab TO et_tab.

    ELSE.

      CALL FUNCTION 'POSTING_INTERFACE_END'
        EXCEPTIONS
          session_not_processable = 1
          OTHERS                  = 2.
      IF sy-subrc <> 0.
        CLEAR ls_tab.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 INTO ls_tab-message.
        ls_tab-type = 'E'.
        ls_tab-zcrm = <ls_it_tab_1>-zcrm.
        APPEND ls_tab TO et_tab.

      ELSE.
        CLEAR ls_tab.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 INTO ls_tab-message.
        ls_tab-type = 'S'.
        ls_tab-zcrm = <ls_it_tab_1>-zcrm.
        ls_tab-belnr = sy-msgv1.
        APPEND ls_tab TO et_tab.

      ENDIF.
    ENDIF.

    CLEAR:
    it_blntab ,it_blntab[],
    it_ftclear,it_ftclear[],
    it_ftpost ,it_ftpost[],
    it_fttax  ,it_fttax[].

  ENDLOOP.

 

标签:bapi,populate,ftpost,清账,sy,lv,FB05,wrbtr,ls
From: https://www.cnblogs.com/yy-abaper/p/16726954.html

相关文章