SAP ABAP 财务FB01通过BAPI创建凭证

该文章详细介绍了如何通过调用SAP的BAPI_BAPI_ACC_DOCUMENT_POST来创建会计凭证,包括设置凭证头信息、处理行项目、以及调用BAPI进行记账的过程。代码示例展示了如何处理不同科目的金额和检查原币与本位币的一致性。
摘要由CSDN通过智能技术生成


前言

使用BDC在FB01创建凭证可能会遇到各种错误 不能保证错误都报出 并且不能动态获取必输字段
现更换为调用BAPI创建

BAPI名称:

BAPI_ACC_DOCUMENT_POST


一、范例代码

示例

FORM frm_acc_document_post USING    iv_pos TYPE char02 is_head TYPE type_fi_doc_head
                                     iv_type TYPE char01 iv_kunnr TYPE kunnr
                                     it_item LIKE item[]
                            CHANGING cv_belnr
                                     cv_status
                                     cv_message
                                     ct_log LIKE gt_log.

  DATA: lt_ztslot_fee_int    TYPE TABLE OF ztslot_fee_int,
        ls_ztslot_fee_int    TYPE ztslot_fee_int,
        ls_item              TYPE type_fi_doc_item,
        lv_status            TYPE zestatus,
        lv_itemno            TYPE posnr_acc,
        lv_idx               TYPE int4,
        lv_idx_all           TYPE int4,
        lv_bukrs             TYPE bukrs,
        ls_header            TYPE bapiache09,
        lt_criteria          TYPE TABLE OF bapiackec9,
        lt_accountreceivable TYPE TABLE OF bapiacar09,
        lt_accountpayable    TYPE TABLE OF bapiacap09,
        lt_currencyamount    TYPE TABLE OF bapiaccr09,
        lt_accountgl         TYPE TABLE OF bapiacgl09,
        lt_return            TYPE TABLE OF bapiret2,
        lt_extension2        TYPE TABLE OF bapiparex,
        lv_obj_type          TYPE bapiache09-obj_type,
        lv_obj_key           TYPE bapiache09-obj_key,
        lv_obj_sys           TYPE bapiache09-obj_sys,
        lv_zdwrbtr           TYPE zdwrbtr,
        lv_zddmbtr           TYPE zdwrbtr.


*  生成HEADER
  ls_header-comp_code  = is_head-bukrs.
  ls_header-doc_date   = sy-datum.
  ls_header-pstng_date = is_head-budat.
  ls_header-doc_type   = is_head-blart.
  ls_header-username   = sy-uname.

  CALL FUNCTION 'OWN_LOGICAL_SYSTEM_GET'
    IMPORTING
      own_logical_system = ls_header-obj_sys.
  ls_header-obj_type   = 'BKPFF'.
  ls_header-obj_key    = '$'.
  ls_header-bus_act    = 'RFBU'.

  ls_header-header_txt = is_head-bktxt.
  ls_header-ref_doc_no = is_head-xblnr.

  lv_idx_all = lines( it_item[] ).

*  行项目
  LOOP AT it_item INTO ls_item.

    lv_idx = sy-tabix.
    IF ls_item-bschl EQ '50'.
      ls_item-wrbtr = ls_item-wrbtr * -1.
      ls_item-dmbtr = ls_item-dmbtr * -1.
    ENDIF.
*    09/19用
    lv_zdwrbtr = lv_zdwrbtr + ls_item-wrbtr.
    lv_zddmbtr = lv_zddmbtr + ls_item-dmbtr.

    lv_itemno = lv_itemno + 1.
*           总账科目  4050
    PERFORM frm_build_gl_account TABLES lt_accountgl lt_extension2 lt_criteria USING ls_item lv_itemno.
    PERFORM frm_build_amount_item_e TABLES  lt_currencyamount USING ls_item lv_itemno is_head-waers.

    IF lv_idx EQ lv_idx_all.

      IF ( lv_zdwrbtr LT 0 AND lv_zddmbtr LT 0 ) OR ( lv_zdwrbtr GT 0 AND lv_zddmbtr GT 0 ).

*      汇总   0919
        lv_itemno = lv_itemno + 1.
        ls_item-wrbtr = lv_zdwrbtr * -1.
        ls_item-dmbtr = lv_zddmbtr * -1.

        lv_bukrs = iv_kunnr."iv_kunnr+6(4).

*      只记客户
        PERFORM frm_build_accountreceivable_e TABLES  lt_accountreceivable lt_extension2
                                              USING ls_item lv_itemno lv_bukrs iv_type.

        PERFORM frm_build_amount_item_e TABLES lt_currencyamount USING ls_item lv_itemno is_head-waers.

      ELSE.
        cv_status = 'E'.
        cv_message = '原币与本位币符号不一致!'.
      ENDIF.
    ENDIF.

  ENDLOOP.

  CHECK lt_accountreceivable IS NOT INITIAL AND cv_status NE 'E'.

*  记账
  CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
    EXPORTING
      documentheader    = ls_header
    IMPORTING
      obj_type          = lv_obj_type
      obj_key           = lv_obj_key
      obj_sys           = lv_obj_sys
    TABLES
      accountgl         = lt_accountgl
      accountreceivable = lt_accountreceivable
      accountpayable    = lt_accountpayable
      currencyamount    = lt_currencyamount
      criteria          = lt_criteria
      return            = lt_return
      extension2        = lt_extension2.

  CLEAR:cv_message.
  LOOP AT lt_return INTO DATA(ls_return) WHERE type = 'A' OR type = 'E'.
    cv_message = cv_message && ls_return-message.
    cv_status = 'E'.

    IF iv_type EQ '3'.
      lv_status = '0'.
    ELSE.
      lv_status = 'A'.
    ENDIF.
  ENDLOOP.

  IF cv_status NE 'E'.

    cv_status = 'S'.
    READ TABLE lt_return INTO ls_return WITH KEY type = 'S' .
    IF sy-subrc = 0.
*      ls_log-type = 'S'.
      cv_belnr = lv_obj_key(10).
*      ls_log-gjahr = lv_obj_key+14(4).

      IF iv_type EQ '3'.
        lv_status = '1'.
      ELSE.
        lv_status = 'S'.
      ENDIF.

    ENDIF.

  ENDIF.

FORM

FORM frm_build_gl_account  TABLES pt_accountgl STRUCTURE bapiacgl09
                                    pt_extension2 STRUCTURE bapiparex
                                    pt_criteria STRUCTURE bapiackec9
                           USING is_item TYPE type_fi_doc_item
                                 pv_itemno TYPE posnr_acc.

  DATA:ls_extfields TYPE zacc_document,
       lc_bschl_50  TYPE bschl VALUE '50',
       lc_bschl_40  TYPE bschl VALUE '40'.

  APPEND INITIAL LINE TO pt_accountgl ASSIGNING FIELD-SYMBOL(<fs_accountgl>).
  <fs_accountgl>-gl_account = is_item-newko.
  <fs_accountgl>-itemno_acc = pv_itemno.
  <fs_accountgl>-alloc_nmbr = is_item-zuonr.
  <fs_accountgl>-item_text = is_item-sgtxt.
  <fs_accountgl>-acct_type = 'S'.
  <fs_accountgl>-ref_key_1 = is_item-xref1.
  <fs_accountgl>-ref_key_2 = is_item-xref2.
  <fs_accountgl>-ref_key_3 = is_item-xref3.
  <fs_accountgl>-material = is_item-matnr.
  <fs_accountgl>-quantity = is_item-menge.
  <fs_accountgl>-base_uom = is_item-meins.
  <fs_accountgl>-cmmt_item = is_item-fipos.
  <fs_accountgl>-costcenter = is_item-kostl.
  <fs_accountgl>-trade_id = is_item-vbund.
  <fs_accountgl>-value_date = is_item-valut.

  APPEND INITIAL LINE TO pt_extension2 ASSIGNING FIELD-SYMBOL(<fs_ext>).
  <fs_ext>-structure = 'ZACC_DOCUMENT'.
*  IF is_item-dmbtr LT 0.
*    ls_extfields-bschl = lc_bschl_50.
*  ELSEIF is_item-dmbtr GE 0.
*    ls_extfields-bschl = lc_bschl_40.
*  ENDIF.
  ls_extfields-bschl = is_item-bschl.
  ls_extfields-posnr = pv_itemno.
  <fs_ext>-valuepart1 = ls_extfields.

*  其他信息
*  CLEAR pt_criteria.
  pt_criteria-itemno_acc = pv_itemno.
  pt_criteria-fieldname = 'WWD02'.
  pt_criteria-character = is_item-wwd02.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD03'.
  pt_criteria-character = is_item-wwd03.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD05'.
  pt_criteria-character = is_item-wwd05.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD06'.
  pt_criteria-character = is_item-wwd06.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD09'.
  pt_criteria-character = is_item-wwd09.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD13'.
  pt_criteria-character = is_item-wwd13.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD15'.
  pt_criteria-character = is_item-wwd15.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD18'.
  pt_criteria-character = is_item-wwd18.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD19'.
  pt_criteria-character = is_item-wwd19.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD20'.
  pt_criteria-character = is_item-wwd20.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD21'.
  pt_criteria-character = is_item-wwd21.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD22'.
  pt_criteria-character = is_item-wwd22.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD23'.
  pt_criteria-character = is_item-wwd23.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD24'.
  pt_criteria-character = is_item-wwd24.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD25'.
  pt_criteria-character = is_item-wwd25.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD26'.
  pt_criteria-character = is_item-wwd26.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD27'.
  pt_criteria-character = is_item-wwd27.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD28'.
  pt_criteria-character = is_item-wwd28.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD29'.
  pt_criteria-character = is_item-wwd29.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD32'.
  pt_criteria-character = is_item-wwd32.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD33'.
  pt_criteria-character = is_item-wwd33.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD34'.
  pt_criteria-character = is_item-wwd34.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD35'.
  pt_criteria-character = is_item-wwd35.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD36'.
  pt_criteria-character = is_item-wwd36.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD37'.
  pt_criteria-character = is_item-wwd37.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD38'.
  pt_criteria-character = is_item-wwd38.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD39'.
  pt_criteria-character = is_item-wwd39.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD40'.
  pt_criteria-character = is_item-wwd40.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD41'.
  pt_criteria-character = is_item-wwd41.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD42'.
  pt_criteria-character = is_item-wwd42.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD43'.
  pt_criteria-character = is_item-wwd43.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD44'.
  pt_criteria-character = is_item-wwd44.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD45'.
  pt_criteria-character = is_item-wwd45.
  APPEND pt_criteria.
  pt_criteria-fieldname = 'WWD46'.
  pt_criteria-character = is_item-wwd46.
  APPEND pt_criteria.

ENDFORM.

FORM frm_build_amount_item_e  TABLES   pt_currencyamount STRUCTURE bapiaccr09
USING is_item TYPE type_fi_doc_item
      pv_itemno TYPE posnr_acc
      iv_waers TYPE waers.

  IF iv_waers <> is_item-curr.

    APPEND INITIAL LINE TO pt_currencyamount ASSIGNING FIELD-SYMBOL(<fs_amount>).
    <fs_amount>-itemno_acc = pv_itemno.
    <fs_amount>-curr_type = '00'.
    <fs_amount>-currency = is_item-curr.
    <fs_amount>-amt_doccur = is_item-wrbtr.

    APPEND INITIAL LINE TO pt_currencyamount ASSIGNING <fs_amount>.
    <fs_amount>-itemno_acc = pv_itemno.
    <fs_amount>-amt_doccur =  is_item-dmbtr.
    <fs_amount>-curr_type = '10'.
    <fs_amount>-currency = iv_waers.

  ELSE.

    APPEND INITIAL LINE TO pt_currencyamount ASSIGNING <fs_amount>.
    <fs_amount>-itemno_acc = pv_itemno.
    <fs_amount>-curr_type = '00'.
    <fs_amount>-currency = iv_waers.
    <fs_amount>-amt_doccur = is_item-dmbtr.

  ENDIF.

ENDFORM.

FORM frm_build_accountreceivable_e  TABLES   pt_accountreceivable STRUCTURE bapiacar09
                                             pt_extension2 STRUCTURE bapiparex
                                    USING    is_item TYPE type_fi_doc_item
                                             pv_itemno TYPE posnr_acc
                                             iv_bukrs TYPE bukrs
                                             iv_type TYPE char01.

  CONSTANTS:
    lc_bschl_19 TYPE bseg-bschl VALUE '19',
    lc_bschl_09 TYPE bseg-bschl VALUE '09'.

  DATA:ls_extfields TYPE zacc_document,
       lv_kunnr     TYPE kunnr.


  APPEND INITIAL LINE TO pt_accountreceivable ASSIGNING FIELD-SYMBOL(<fs_accountreceivable>).
  <fs_accountreceivable>-itemno_acc = pv_itemno.
*  配置表获取客户
  SELECT SINGLE kunnr INTO <fs_accountreceivable>-customer
    FROM z_post_pz
    WHERE bukrs EQ iv_bukrs.
  <fs_accountreceivable>-alloc_nmbr = is_item-zuonr.
*  <fs_accountreceivable>-sp_gl_ind = 'O'."is_item-newum. "特别总账
  IF iv_type EQ 3.
    <fs_accountreceivable>-sp_gl_ind = 'U'.
  ELSE.
    <fs_accountreceivable>-sp_gl_ind = 'O'.
  ENDIF.
  <fs_accountreceivable>-item_text = is_item-sgtxt.
  IF <fs_accountreceivable>-alloc_nmbr IS INITIAL.
    <fs_accountreceivable>-alloc_nmbr = iv_bukrs.
  ENDIF.
  IF <fs_accountreceivable>-item_text IS INITIAL.
    <fs_accountreceivable>-item_text = iv_bukrs.
  ENDIF.

  <fs_accountreceivable>-pmnttrms       = is_item-zterm.
  <fs_accountreceivable>-bline_date     = is_item-zfbdt.
  <fs_accountreceivable>-pymt_meth      = is_item-zlsch.
  <fs_accountreceivable>-pmnt_block     = is_item-zlspr.
  <fs_accountreceivable>-paymt_ref      = is_item-kidno.
  <fs_accountreceivable>-ref_key_1      = is_item-xref1.
  <fs_accountreceivable>-ref_key_2      = is_item-xref2.
  <fs_accountreceivable>-ref_key_3      = is_item-xref3.


  DATA: l_kunnr LIKE kna1-kunnr.

  SELECT SINGLE kunnr INTO lv_kunnr
    FROM z_post_pz
    WHERE bukrs EQ iv_bukrs.

  SELECT SINGLE knrze INTO l_kunnr
    FROM knb1 WHERE kunnr = lv_kunnr
    AND bukrs = iv_bukrs.
  IF l_kunnr = ''.
    l_kunnr = is_item-newko.
  ENDIF.
  SELECT SINGLE land1 INTO <fs_accountreceivable>-supcountry
  FROM kna1 WHERE kunnr = l_kunnr.

  APPEND INITIAL LINE TO pt_extension2 ASSIGNING FIELD-SYMBOL(<fs_ext>).
  <fs_ext>-structure = 'ZACC_DOCUMENT'.
  IF is_item-dmbtr GE 0.
    ls_extfields-bschl = lc_bschl_09.
  ELSE.
    ls_extfields-bschl = lc_bschl_19.
  ENDIF.

  ls_extfields-posnr = pv_itemno.
  <fs_ext>-valuepart1 = ls_extfields.

ENDFORM.

总结

注意不要漏填参数就行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ggreekn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值