业务说明:函数table中IS_ITEMS里的参数为预付凭证,也就是清账的凭证,importing中IS_INPUT时被清的凭证
FUNCTION zfb05_post.
*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" REFERENCE(IS_INPUT) TYPE ZSFI_034
*" REFERENCE(BUKRS) TYPE BUKRS DEFAULT '1000'
*" REFERENCE(BLART) TYPE BLART DEFAULT 'AB'
*" REFERENCE(BLDAT) TYPE BLDAT DEFAULT SY-DATUM
*" REFERENCE(BUDAT) TYPE BUDAT DEFAULT SY-DATUM
*" REFERENCE(MONAT) TYPE MONAT DEFAULT SY-DATUM+4(2)
*" REFERENCE(WAERS) TYPE WAERS DEFAULT 'CNY'
*" EXPORTING
*" REFERENCE(ES_TYPE) TYPE BAPI_MTYPE
*" REFERENCE(ES_MSG) TYPE BAPI_MSG
*" REFERENCE(ES_BELNR) TYPE BELNR_D
*" REFERENCE(ES_BUKRS) TYPE BUKRS
*" TABLES
*" IS_ITEMS STRUCTURE ZSFI_034
*"----------------------------------------------------------------------DATA : lv_sumwa TYPE wrbtr,lv_cywrb TYPE wrbtr,lv_belnr TYPE char30,lv_index TYPE char3,lv_msg TYPE string,lv_mode TYPE rfpdo-allgazmd." ftclear 是清账的数据的表参数DATA:gt_ftclear TYPE STANDARD TABLE OF ftclear,gs_ftclear LIKE LINE OF gt_ftclear." ftpost 是过账的抬头和行项目的表参数DATA:gt_ftpost TYPE STANDARD TABLE OF ftpost,gs_ftpost LIKE LINE OF gt_ftpost.DATA:gt_blntab LIKE TABLE OF blntab WITH HEADER LINE,gt_fttax LIKE TABLE OF fttax WITH HEADER LINE.DATA : lt_ztfi020 TYPE TABLE OF ztfi020,ls_ztfi020 TYPE ztfi020.
*&---ftpost 结构宏DEFINE populate_ftpost.CLEAR gs_ftpost.gs_ftpost-stype = &1. "记录类型 K:HEADER P:ITEMSgs_ftpost-count = &2. "凭证计数器gs_ftpost-fnam = &3. "BDC 字段名gs_ftpost-fval = &4. "BDC 字段值IF gs_ftpost-fnam = 'BSEG-WRBTR' .CONDENSE gs_ftpost-fval NO-GAPS .ENDIF .APPEND gs_ftpost TO gt_ftpost.END-OF-DEFINITION.*&---ftclear 结构宏DEFINE populate_ftclear.CLEAR gs_ftclear.gs_ftclear-agkoa = &1. "科目类型gs_ftclear-agkon = &2. "工作清单账号关键字gs_ftclear-agbuk = &3. "公司代码gs_ftclear-selfd = &4. "索引凭证关键字gs_ftclear-selvon = &5. "选择未清项目的输入字段gs_ftclear-agums = &6. "用于选择的特殊总账标识符gs_ftclear-xnops = COND #( WHEN &6 IS INITIAL THEN abap_true ).APPEND gs_ftclear TO gt_ftclear.END-OF-DEFINITION.*&---校验数据准确性DEFINE field_check .IF &1 < &2 .es_msg = es_msg && '预付凭证:' && &3 && '冲付金额不能大于' && &1 .ENDIF .IF &4 > is_input-wrbtr .es_msg = es_msg && '预付凭证总金额大于发票金额' .ENDIF .END-OF-DEFINITION.LOOP AT is_items INTO DATA(ls_line) .lv_sumwa = lv_sumwa + ls_line-zyfje .field_check : ls_line-wrbtr ls_line-zyfje ls_line-belnr lv_sumwa .CLEAR : ls_line .ENDLOOP .IF es_msg IS INITIAL ."发票会计凭证传入BDC抬头CLEAR : lv_belnr ,lv_cywrb.lv_belnr = is_input-belnr && is_input-gjahr && is_input-buzei .populate_ftclear 'K' is_input-lifnr is_input-bukrs 'BELNR' lv_belnr is_input-umskz.populate_ftpost 'K' 1 'BKPF-BUKRS' bukrs. " company codepopulate_ftpost 'K' 1 'BKPF-BLART' blart. " document typepopulate_ftpost 'K' 1 'BKPF-BLDAT' bldat. " posting datepopulate_ftpost 'K' 1 'BKPF-BUDAT' budat.populate_ftpost 'K' 1 'BKPF-MONAT' monat.populate_ftpost 'K' 1 'BKPF-WAERS' waers. " currency"判断发票会计凭证存不存在差异行IF is_input-wrbtr <> lv_sumwa ."预付金额与这次发票金额不能抵消lv_cywrb = is_input-wrbtr - lv_sumwa .lv_index = lv_index + 1 .populate_ftpost 'P' lv_index 'BSEG-SGTXT' is_input-belnr. "文本populate_ftpost 'P' lv_index 'RF05A-NEWKO' is_input-lifnr. "供应商编码(科目)populate_ftpost 'P' lv_index 'RF05A-NEWBS' is_input-bschl. "过账码populate_ftpost 'P' lv_index 'BSEG-WRBTR' lv_cywrb. "金额populate_ftpost 'P' lv_index 'BSEG-HKONT' is_input-hkont. "总账科目populate_ftpost 'P' lv_index 'RF05A-NEWUM' is_input-umskz. "特别总账标识ENDIF .LOOP AT is_items INTO ls_line ."预付凭证传入BDC抬头CLEAR : lv_belnr ,lv_cywrb.lv_belnr = ls_line-belnr && ls_line-gjahr && ls_line-buzei .populate_ftclear 'K' ls_line-lifnr ls_line-bukrs 'BELNR' lv_belnr ls_line-umskz."判断预付凭证要不要拆行IF ls_line-wrbtr <> ls_line-zyfje .lv_cywrb = ls_line-wrbtr - ls_line-zyfje .lv_index = lv_index + 1 .populate_ftpost 'P' lv_index 'BSEG-SGTXT' is_input-belnr. "文本populate_ftpost 'P' lv_index 'RF05A-NEWKO' ls_line-lifnr. "供应商编码(科目)populate_ftpost 'P' lv_index 'RF05A-NEWBS' ls_line-bschl. "过账码populate_ftpost 'P' lv_index 'BSEG-WRBTR' lv_cywrb. "金额populate_ftpost 'P' lv_index 'BSEG-HKONT' ls_line-hkont. "总账科目populate_ftpost 'P' lv_index 'RF05A-NEWUM' ls_line-umskz. "特别总账标识SELECT SINGLE * FROM ztfi020 INTO ls_ztfi020WHERE bukrs = bukrsAND gjahr = ls_line-gjahrAND belnr = ls_line-belnrAND buzei = ls_line-buzei .IF sy-subrc = 0 .CLEAR : ls_ztfi020-bukrs ,ls_ztfi020-belnr .ls_ztfi020-buzei = lv_index .APPEND ls_ztfi020 TO lt_ztfi020 .ENDIF .ENDIF .CLEAR : ls_line .ENDLOOP .IF gt_ftclear IS NOT INITIAL .lv_mode = 'N'.CALL FUNCTION 'POSTING_INTERFACE_START'EXPORTINGi_client = sy-mandti_function = 'C'i_mode = lv_modei_keep = 'X'i_update = 'S'i_user = sy-unameEXCEPTIONSclient_incorrect = 1function_invalid = 2group_name_missing = 3mode_invalid = 4update_invalid = 5user_invalid = 6.IF sy-subrc <> 0.MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgnoWITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.ENDIF.CALL FUNCTION 'POSTING_INTERFACE_CLEARING'EXPORTINGi_auglv = 'UMBUCHNG' "转账并清账i_tcode = 'FB05'i_sgfunct = 'C'IMPORTINGe_msgid = sy-msgide_msgno = sy-msgnoe_msgty = sy-msgtye_msgv1 = sy-msgv1e_msgv2 = sy-msgv2e_msgv3 = sy-msgv3e_msgv4 = sy-msgv4TABLESt_blntab = gt_blntabt_ftclear = gt_ftcleart_ftpost = gt_ftpostt_fttax = gt_fttaxEXCEPTIONSclearing_procedure_invalid = 1clearing_procedure_missing = 2table_t041a_empty = 3transaction_code_invalid = 4amount_format_error = 5too_many_line_items = 6company_code_invalid = 7screen_not_found = 8no_authorization = 9OTHERS = 10."获取消息文本MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgnoWITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 INTO lv_msg.IF sy-msgid = 'F5' AND sy-msgty = 'S' AND sy-msgno = '312' .es_msg = lv_msg .es_type = 'S' .es_belnr = sy-msgv1 .es_bukrs = sy-msgv2 .IF lt_ztfi020 IS NOT INITIAL .DO 10 TIMES.SELECT * FROM bseg INTO TABLE @DATA(lt_bseg) WHERE belnr = @es_belnr AND bukrs = @es_bukrs .IF sy-subrc = 0 .LOOP AT lt_ztfi020 ASSIGNING FIELD-SYMBOL(<fs_ztfi020>).READ TABLE lt_bseg INTO DATA(ls_bseg) WITH KEY buzei = <fs_ztfi020>-buzei .IF sy-subrc = 0 .<fs_ztfi020>-belnr = ls_bseg-belnr .<fs_ztfi020>-bukrs = ls_bseg-bukrs .<fs_ztfi020>-zqzbs = '' .CLEAR : ls_bseg .ENDIF .ENDLOOP .IF sy-subrc = 0 .MODIFY ztfi020 FROM TABLE lt_ztfi020 .ENDIF .EXIT .ELSE .WAIT UP TO '0.5' SECONDS .ENDIF .ENDDO.ENDIF .ELSE .es_msg = lv_msg .es_type = 'E' .ENDIF .CALL FUNCTION 'POSTING_INTERFACE_END'EXCEPTIONSsession_not_processable = 1OTHERS = 2.IF sy-subrc <> 0.MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgnoWITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.ENDIF.es_msg = lv_msg .es_type = sy-msgty .ENDIF .ELSE .es_type = 'E' .ENDIF .ENDFUNCTION.