BAPI_ACC_DOCUMENT_POST 简单理解过账BAPI使用

业务场景

甲方是一家从事房屋租赁的公司,它的主营业务就是从各大租户手里收租子。月底了,小明发了工资美滋滋,钱到手没多久房东就催租子了,房租每月100,水电100, 税费22,合计222.小明如约在手机APP上向房东支付了当月的房租,这边钱到账后,甲方会计收到银行的转账通知,这个时候需要录入一笔凭证 。


借:其他应收款-公司往来款  100元    水电

        其他应收款-公司往来款  100元  房租

        其他应收款-公司往来款  22元       应缴税费

贷:主营业务收入-租赁收入  222元 

        在FB01中,我们手工建立的会计凭证如下:

BAPI_ACC_DOCUMENT_POST 简单理解过账BAPI使用_第1张图片          


  BAPI代码如下:

  lt_accountgl[]:             表示贷方,即甲方的应收(总账科目)

 lt_accountreceivable: 表示借方,即客户的应付   (一般科目即为客户编号)

lt_currencyamount:      借方与贷方的金额,需要用ITEM_NO进行关联。(注意前台与后台的区别,金额有正有负,正负相抵)

  CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
    EXPORTING
      documentheader    = ls_documentheader
    IMPORTING
*     OBJ_TYPE          =
      obj_key           = lv_key
*     OBJ_SYS           =
    TABLES
      accountgl         = lt_accountgl[]
      accountreceivable = lt_accountreceivable[]
      accountpayable    = lt_accountpayable[]
      currencyamount    = lt_currencyamount[]
      return            = lt_return[]
      extension2        = lt_ext2[].

    详细代码:
 

FUNCTION zf_fi_054_accdoc_pos.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     REFERENCE(IS_HEAD) TYPE  ZFIT_038_HEAD01
*"  EXPORTING
*"     REFERENCE(OS_KEY) TYPE  ZFIS_054_RETURN
*"  TABLES
*"      IT_ITEM STRUCTURE  ZFIT_038_ITEM01
*"      IT_RETURN STRUCTURE  ZFIS_038_RETURN
*"----------------------------------------------------------------------
  DATA:ls_documentheader    TYPE bapiache09,
       lt_accountgl         TYPE STANDARD TABLE OF bapiacgl09,
       lt_accountpayable    TYPE STANDARD TABLE OF bapiacap09,
       lt_accountreceivable TYPE STANDARD TABLE OF bapiacar09,
       lt_currencyamount    TYPE STANDARD TABLE OF bapiaccr09,
       lt_ext2              TYPE STANDARD TABLE OF bapiparex WITH HEADER LINE,
       lt_order_info        TYPE STANDARD TABLE OF zfit_order_info,
       lv_itemno_acc        TYPE posnr_acc,
       lt_return            TYPE STANDARD TABLE OF bapiret2,
       ls_return            TYPE bapiret2,
       ls_no_acc_rule       TYPE zfit_no_acc_rule,
       lv_error_message     TYPE char255,
       ls_retmsg            TYPE zfis_retmsg,
       lv_koart             TYPE tbsl-koart,
       lv_kunnr             TYPE kna1-kunnr,
       lv_lifnr             TYPE lfa1-lifnr,
       lv_order_no          TYPE zfit_order_info-order_no,
       lv_key               TYPE bapiache09-obj_key,
       ls_item              TYPE zfit_038_item01.

  CONSTANTS:cn_bus_act  TYPE glvor VALUE 'RFBU',
            cn_status_s TYPE char4 VALUE 'S000',
            cn_status_e TYPE char4 VALUE 'E0M1',
            cn_type_s   TYPE c VALUE 'S',
            cn_type_e   TYPE c VALUE 'E',
            cn_koart_k  TYPE c VALUE 'K',
            cn_koart_d  TYPE c VALUE 'D',
            cn_koart_s  TYPE c VALUE 'S',
            cn_shkzg_h  TYPE c VALUE 'H',
            cn_shkzg_s  TYPE c VALUE 'S',
            cn_blart_z5 TYPE bkpf-blart VALUE 'Z5',
            cn_bschl_50 TYPE bschl VALUE '50',
            cn_bschl_40 TYPE bschl VALUE '40'.
  DATA  ls_accountreceivable TYPE bapiacar09.
  DATA: l_item TYPE char10 VALUE '0000000000'.
  DATA: ls_currencyamount    TYPE LINE OF bapiaccr09_tab.
  DATA: l_wrbtr_sum TYPE bapidoccur,
        l_tax_sum   TYPE bapidoccur.
  DATA: ls_itemext TYPE zfis_enhbapiaccdoc_001.
*->填充总账信息
  DATA ls_accountgl TYPE bapiacgl09.
* 分配字段写入“收付款单号+行项目号”
* 抬头信息
  ls_documentheader-doc_date   = is_head-budat.
  ls_documentheader-pstng_date = is_head-budat.
  ls_documentheader-doc_type   = 'DR'.
  ls_documentheader-comp_code  = is_head-bukrs.
  ls_documentheader-header_txt = is_head-bktxt.
  ls_documentheader-bus_act    = cn_bus_act. "'RFBU'.
  ls_documentheader-username   = sy-uname.

  LOOP AT it_item INTO ls_item.

    ADD 10 TO l_item.
    ls_accountreceivable-itemno_acc = l_item.
    ls_accountreceivable-customer   = ls_item-kunnr."客户
    ls_accountreceivable-bline_date = is_head-budat.
    ls_accountreceivable-gl_account = ls_item-hkont."总账科目
    ls_accountreceivable-item_text  = ls_item-sgtxt."项目文本
    APPEND ls_accountreceivable TO lt_accountreceivable.
    CLEAR  ls_accountreceivable.
    ls_currencyamount-itemno_acc = l_item.
    ls_currencyamount-currency   = is_head-waers.
    ls_currencyamount-amt_doccur = ls_item-wrbtr.
    APPEND ls_currencyamount TO lt_currencyamount.
    CLEAR:ls_currencyamount.
    ADD ls_item-zhsje TO l_wrbtr_sum.
    ADD ls_item-mwsts TO l_tax_sum.
    "----------- extension2 --------------------
    "原因码
    ls_itemext-buzei       = l_item.
    ls_itemext-bschl       = '01'.
    IF ls_itemext IS NOT INITIAL .
      lt_ext2-structure = 'ZFIS_ENHBAPIACCDOC_001'.
      lt_ext2-valuepart1 = ls_itemext          .
      APPEND  lt_ext2.
      CLEAR   lt_ext2.
      CLEAR   ls_itemext   .
    ENDIF.
  ENDLOOP.

  "税合计
  ADD 10 TO l_item.
  ls_accountreceivable-itemno_acc = l_item.
  ls_accountreceivable-customer   = ls_item-kunnr."客户
  ls_accountreceivable-bline_date = is_head-budat.
  ls_accountreceivable-gl_account = ls_item-hkont."总账科目
  ls_accountreceivable-item_text  = TEXT-004.
  ls_accountgl-profit_ctr         = '0000600002'.
  APPEND ls_accountreceivable TO lt_accountreceivable.
  CLEAR  ls_accountreceivable.
  ls_currencyamount-itemno_acc = l_item.
  ls_currencyamount-currency   = is_head-waers.
  ls_currencyamount-amt_doccur = l_tax_sum.
  APPEND ls_currencyamount TO lt_currencyamount.

  "----------- extension2 --------------------
  "原因码
    ls_itemext-buzei       = l_item.
    ls_itemext-bschl       = '01'.
    IF ls_itemext IS NOT INITIAL .
      lt_ext2-structure = 'ZFIS_ENHBAPIACCDOC_001'.
      lt_ext2-valuepart1 = ls_itemext          .
      APPEND  lt_ext2.
      CLEAR   lt_ext2.
      CLEAR   ls_itemext   .
    ENDIF.
  "行项目检查
  ADD 10 TO l_item.
  ls_accountgl-itemno_acc = l_item.
  ls_accountgl-gl_account = '6001030000'.  "会计科目在配置表中
  ls_accountgl-comp_code  = is_head-bukrs.
  ls_accountgl-fis_period = is_head-monat.
  ls_accountgl-fisc_year  = is_head-gjahr.
  ls_accountgl-pstng_date = is_head-budat.
  APPEND ls_accountgl TO lt_accountgl.

  "行项目金额检查
  ls_currencyamount-itemno_acc = l_item.
  ls_currencyamount-currency   = is_head-waers.
  ls_currencyamount-amt_doccur = 0 - l_wrbtr_sum.
  APPEND ls_currencyamount TO lt_currencyamount.
  CLEAR: ls_accountgl,ls_currencyamount.

  "----------- extension2 --------------------
  "原因码
    ls_itemext-buzei       = l_item.
    ls_itemext-bschl       = '50'.
    IF ls_itemext IS NOT INITIAL .
      lt_ext2-structure = 'ZFIS_ENHBAPIACCDOC_001'.
      lt_ext2-valuepart1 = ls_itemext          .
      APPEND  lt_ext2.
      CLEAR   lt_ext2.
      CLEAR   ls_itemext   .
    ENDIF.

  CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
    EXPORTING
      documentheader    = ls_documentheader
    IMPORTING
*     OBJ_TYPE          =
      obj_key           = lv_key
*     OBJ_SYS           =
    TABLES
      accountgl         = lt_accountgl[]
      accountreceivable = lt_accountreceivable[]
      accountpayable    = lt_accountpayable[]
      currencyamount    = lt_currencyamount[]
      return            = lt_return[]
      extension2        = lt_ext2[].

  READ TABLE lt_return INTO ls_return WITH KEY type = 'E'.

  IF sy-subrc NE 0 .
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.
    os_key-belnr   = lv_key+0(10).
    os_key-bukrs   = lv_key+10(4).
    os_key-gjahr   = lv_key+14(4).
    os_key-type    = 'S'.
    os_key-message = TEXT-002.
  ELSE.
    os_key-type = 'E'.
    LOOP AT lt_return INTO ls_return WHERE type = 'E'.
      IF os_key-message IS INITIAL.
        os_key-message = ls_return-message.
      ELSE.
        CONCATENATE os_key-message '/' ls_return-message INTO os_key-message.
      ENDIF.
    ENDLOOP.
  ENDIF.

  CLEAR:l_item,
        l_wrbtr_sum,
        lt_return[],
        lt_accountgl[],
        lt_accountreceivable[],
        lt_accountpayable[],
        lt_currencyamount[],
        lt_return[],
        lt_ext2[].

ENDFUNCTION.

 

你可能感兴趣的:(『SAP』ABAP-BAPI)