首页 > 其他分享 >ABAP-F-02记账模块化

ABAP-F-02记账模块化

时间:2022-10-28 13:55:50浏览次数:63  
标签:02 wa lt item ABAP 记账 ls TYPE accountgl

FUNCTION zfi_fm_01.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(LS_HEADER) TYPE  ZSFB01_SRM
*"  EXPORTING
*"     REFERENCE(ZTYPE) TYPE  CHAR1
*"     REFERENCE(ZMSG) TYPE  BAPI_MSG
*"     REFERENCE(BELNR) TYPE  BELNR_D
*"  TABLES
*"      LT_ITEM STRUCTURE  ZSFB01_SRM_ITEM
*"----------------------------------------------------------------------
  DATA: lr_jie TYPE RANGE OF tbsl-bschl WITH HEADER LINE,
        lr_dai TYPE RANGE OF tbsl-bschl WITH HEADER LINE.

  DATA: g_waers TYPE waers.

  TYPES: BEGIN OF ty_bschl,
           bschl TYPE tbsl-bschl,
           shkzg TYPE tbsl-bschl,
         END OF ty_bschl.

  DATA: lt_bschl TYPE TABLE OF ty_bschl,
        ls_bschl TYPE ty_bschl.

  DATA: wa_documentheader    TYPE          bapiache09,
        wa_customercpd       TYPE          bapiacpa09,
        it_accountgl         TYPE TABLE OF bapiacgl09,
        wa_accountgl         TYPE          bapiacgl09,
        it_accountreceivable TYPE TABLE OF bapiacar09,
        wa_accountreceivable TYPE          bapiacar09,
        it_accountpayable    TYPE TABLE OF bapiacap09,
        wa_accountpayable    TYPE          bapiacap09,
        it_currency_amount   TYPE TABLE OF bapiaccr09,
        wa_currency_amount   TYPE          bapiaccr09,
        it_account_tax       TYPE TABLE OF bapiactx09,
        wa_account_tax       TYPE          bapiactx09,
        it_return            TYPE TABLE OF bapiret2,
        wa_return            TYPE          bapiret2,
        it_zexten            TYPE TABLE OF zsfi_acc_document3,
        wa_zexten            TYPE          zsfi_acc_document3,
        it_zexten_yf         TYPE TABLE OF zsms_epic_extension,
        wa_zexten_yf         TYPE          zsms_epic_extension,
        it_extension2        TYPE TABLE OF bapiparex,
        wa_extension2        TYPE          bapiparex,
****STA*******************************************************************************
*FI_OA费用报销单接口_新增扩展字段车号_20210915
        ls_extension2        TYPE          bapiparex,
        lt_zexten_car        TYPE TABLE OF zsfi_acc_document4,
        ls_zexten_car        TYPE          zsfi_acc_document4.
****END*******************************************************************************

  DATA: gv_objtype TYPE bapiacrev-obj_type,
        gv_objkey  TYPE bapiacrev-obj_key,
        gv_objsys  TYPE bapiacrev-obj_sys.

  DATA: gv_itemno_acc TYPE posnr_acc.

  DATA: l_msg TYPE char255.

*获取本位币
  SELECT SINGLE waers INTO g_waers FROM t001 WHERE bukrs = ls_header-bukrs. "根据公司代码获取本位币

  SELECT
    bschl
    shkzg
    FROM tbsl
    INTO CORRESPONDING FIELDS OF TABLE  lt_bschl.

  LOOP AT lt_bschl INTO ls_bschl.
    IF ls_bschl-shkzg = 'S'.
      CLEAR lr_jie.
      lr_jie-sign = 'I'.
      lr_jie-option = 'EQ'.
      lr_jie-low = ls_bschl-bschl.
      APPEND lr_jie.
    ELSE.
      CLEAR lr_dai.
      lr_dai-sign = 'I'.
      lr_dai-option = 'EQ'.
      lr_dai-low = ls_bschl-bschl.
      APPEND lr_dai.
    ENDIF.
  ENDLOOP.

  CLEAR wa_documentheader.
  wa_documentheader-username   = ls_header-usnam.
  wa_documentheader-comp_code  = ls_header-bukrs.
  wa_documentheader-doc_date   = ls_header-bldat.
  wa_documentheader-pstng_date = ls_header-budat.
  wa_documentheader-doc_type   = ls_header-blart.
  wa_documentheader-header_txt = ls_header-bktxt.
  wa_documentheader-ref_doc_no = ls_header-xblnr.
  IF ls_header-zyf = 'X'.
    wa_documentheader-obj_type    = 'BKPFF'.
    wa_documentheader-bus_act = 'RFST'.
  ENDIF.

  REFRESH: it_accountgl,
             it_accountreceivable,
             it_accountpayable,
             it_currency_amount,
             it_account_tax,
             it_extension2,
             it_return.

*&&----------------行项目
  LOOP AT lt_item.

    gv_itemno_acc = gv_itemno_acc + 1.

    "根据记账码区分总账、应收、应付行
    CASE lt_item-bschl.
      WHEN '40' OR '50' OR '80' OR '81' OR '82' OR '83' OR '84'
                OR '85' OR '86' OR '90' OR '91' OR '92' OR '93'
                OR '94' OR '95' OR '96' OR '70' OR '75'. "总账
        CLEAR wa_accountgl.

        wa_accountgl-itemno_acc = gv_itemno_acc.
        wa_accountgl-vendor_no  = lt_item-lifnr.      " 供应商编码
        wa_accountgl-customer   = lt_item-kunnr.      " 客户编码
        wa_accountgl-gl_account = lt_item-hkont.      " 会计科目
        wa_accountgl-asval_date = lt_item-bzdat.      " 资产价值日
        wa_accountgl-costcenter = lt_item-kostl.      " 成本中心
        wa_accountgl-profit_ctr = lt_item-prctr.      " 利润中心
        wa_accountgl-orderid    = lt_item-aufnr.      " 内部订单号
        wa_accountgl-alloc_nmbr = lt_item-zuonr.      " 分配
        wa_accountgl-item_text  = lt_item-sgtxt.      " 行项目文本
        wa_accountgl-bus_area   = lt_item-gsber.      " 业务范围
        wa_accountgl-value_date = lt_item-valut.      " 起息日
        wa_accountgl-plant      = lt_item-werks.      " 工厂
        wa_accountgl-po_number  = lt_item-ebeln.      " 采购凭证
        wa_accountgl-po_item    = lt_item-ebelp.      " 采购订单行号
        wa_accountgl-tax_code  = lt_item-mwskz.       " 税码
        wa_accountgl-ref_key_1 = lt_item-xref1.
        wa_accountgl-ref_key_2 = lt_item-xref2.
        wa_accountgl-ref_key_3 = lt_item-xref3.
        wa_accountgl-wbs_element = lt_item-posid.      "WBS元素
        wa_accountgl-network   = lt_item-nplnr.       "网络
        APPEND wa_accountgl TO it_accountgl.
      WHEN '01' OR '02' OR '03' OR '04' OR '05' OR '06' OR '07'
                OR '08' OR '09' OR '11' OR '12' OR '13' OR '14'
                OR '15' OR '16' OR '17' OR '18' OR '19'. "应收
        CLEAR wa_accountreceivable.
        wa_accountreceivable-itemno_acc = gv_itemno_acc.
        wa_accountreceivable-customer   = lt_item-kunnr.       " 客户编码
        wa_accountreceivable-gl_account = lt_item-hkont.       " 会计科目
        wa_accountreceivable-comp_code  = ls_header-bukrs.     " 公司代码
        wa_accountreceivable-bus_area   = lt_item-gsber.       " 业务范围
        wa_accountreceivable-item_text  = lt_item-sgtxt.       " 行项目文本
        wa_accountreceivable-alloc_nmbr = lt_item-zuonr.       " 分配
        wa_accountreceivable-sp_gl_ind  = lt_item-zumsk.       " 特别总账标识
        wa_accountreceivable-pymt_amt   = lt_item-pyamt.       " 支付金额
        wa_accountreceivable-profit_ctr = lt_item-prctr.       " 利润中心
        wa_accountreceivable-bline_date = lt_item-zfbdt.       " 付款基准日期/到期日
        wa_accountreceivable-ref_key_1  = lt_item-xref1.
        wa_accountreceivable-ref_key_2  = lt_item-xref2.
        wa_accountreceivable-ref_key_3  = lt_item-xref3.
        wa_accountreceivable-pmnt_block = lt_item-zlspr.

        wa_accountreceivable-pymt_meth = 'P'. "农行-对公支付               "付款方式
        IF ls_header-bukrs EQ '1000' OR ls_header-bukrs EQ '3000' OR ls_header-bukrs EQ '4000'.
          wa_accountreceivable-pymt_meth = 'A'."工行-对公支付
        ENDIF.
***增加贷方附加数据 add by dongdong at 20210508 start
        DATA(lv_len) = strlen( lt_item-bankn ) .
        IF lv_len <= 18."收款账户未超过18位
          SELECT SINGLE
            bvtyp INTO wa_accountreceivable-partner_bk
            FROM knbk
            WHERE kunnr = lt_item-kunnr
            AND   banks = 'CN'
            AND   bankn = lt_item-bankn.
        ELSE.
          DATA(lv_dif) = lv_len - 18.
          SELECT SINGLE
            bvtyp INTO wa_accountreceivable-partner_bk
            FROM knbk
            WHERE kunnr = lt_item-kunnr
            AND   banks = 'CN'
            AND   bankn = lt_item-bankn+0(18)
            AND   bkref = lt_item-bankn+18(lv_dif).
        ENDIF.

        APPEND wa_accountreceivable TO it_accountreceivable.
      WHEN '21' OR '22' OR '24' OR '25' OR '26' OR '27' OR '28' OR '29'
                OR '31' OR '32' OR '34' OR '35' OR '36' OR '37' OR '38'
                OR '39'. "应付·
        CLEAR wa_accountpayable.
        wa_accountpayable-itemno_acc = gv_itemno_acc.
        wa_accountpayable-vendor_no  = lt_item-lifnr.       " 供应商编码
        wa_accountpayable-gl_account = lt_item-hkont.       " 会计科目
        wa_accountpayable-comp_code  = ls_header-bukrs.     " 公司代码
        wa_accountpayable-bus_area   = lt_item-gsber.       " 业务范围
        wa_accountpayable-item_text  = lt_item-sgtxt.       " 行项目文本
        wa_accountpayable-alloc_nmbr = lt_item-zuonr.       " 分配
        DATA(long_txt) = lt_item-htnum. "OA传进来的合同号存入到行项目长文本

        IF ls_header-zyf = 'X'.
          wa_accountpayable-sp_gl_ind = 'F'.
          "wa_accountpayable-pymt_meth = lt_item-pymt_meth.
          wa_accountpayable-pymt_meth = 'P'. "农行-对公支付               "付款方式
          IF ls_header-bukrs EQ '1000' OR ls_header-bukrs EQ '3000' OR ls_header-bukrs EQ '4000'.
            wa_accountpayable-pymt_meth = 'A'."工行-对公支付
          ENDIF.
***增加贷方附加数据 add by dongdong at 20210508 start
          lv_len = strlen( lt_item-bankn ) .
          IF lv_len <= 18."收款账户未超过18位
            SELECT SINGLE
              bvtyp INTO wa_accountpayable-partner_bk
              FROM lfbk
              WHERE lifnr = lt_item-lifnr
              AND   banks = 'CN'
              AND   bankn = lt_item-bankn.
          ELSE.
            lv_dif = lv_len - 18.
            SELECT SINGLE
              bvtyp INTO wa_accountpayable-partner_bk
              FROM lfbk
              WHERE lifnr = lt_item-lifnr
              AND   banks = 'CN'
              AND   bankn = lt_item-bankn+0(18)
              AND   bkref = lt_item-bankn+18(lv_dif).
          ENDIF.

          SPLIT lt_item-bankp AT '|' INTO  wa_accountpayable-bank_id  wa_accountpayable-housebankacctid.


*          wa_accountpayable-bank_id = 'GS002'.
*          wa_accountpayable-housebankacctid = 'GS002'.
***增加贷方附加数据 add by dongdong at 20210508 end
        ELSE.
          wa_accountpayable-sp_gl_ind  = lt_item-zumsk.     " 特别总账标识
        ENDIF.
        wa_accountpayable-pymt_amt   = lt_item-pyamt.       " 支付金额
        wa_accountpayable-profit_ctr = lt_item-prctr.       " 利润中心
        wa_accountpayable-bline_date = lt_item-zfbdt.       " 付款基准日期/到期日

        wa_accountpayable-ref_key_1  = lt_item-xref1.
        wa_accountpayable-ref_key_2  = lt_item-xref2.
        wa_accountpayable-ref_key_3  = lt_item-xref3.
        wa_accountpayable-pmnt_block = lt_item-zlspr.
        APPEND wa_accountpayable TO it_accountpayable.




    ENDCASE.

    IF ls_header-zyf = ''.
      CLEAR wa_zexten.
      wa_zexten-posnr = gv_itemno_acc.
      wa_zexten-bschl = lt_item-bschl.
      wa_zexten-rstgr = lt_item-rstgr.
      wa_zexten-xnegp = lt_item-xnegp.
      wa_zexten-numpg = ls_header-numpg.
      wa_zexten-xmwst = lt_item-xmwst.

      CLEAR wa_extension2.
      wa_extension2-structure  = 'ZSFI_ACC_DOCUMENT3'.
      wa_extension2-valuepart1 = wa_zexten.
      APPEND wa_extension2 TO it_extension2.
      APPEND wa_zexten TO it_zexten.
    ELSE.
      wa_zexten_yf-posnr = gv_itemno_acc.        "凭证行项目
*      IF LT_ITEM-ZUMSK = 'A'.       "预付
*        WA_ZEXTEN_YF-ZUMSK = 'A'.      "目标特别总帐标志
*      ELSEIF LT_ITEM-ZUMSK = 'R'.   "应付
*        WA_ZEXTEN_YF-ZUMSK = 'R'.      "目标特别总帐标志
*      ENDIF.
      wa_zexten_yf-zumsk = lt_item-zumsk.
      wa_extension2-structure  = 'ZSMS_EPIC_EXTENSION'.
      wa_extension2-valuepart1 = wa_zexten_yf.
      APPEND wa_extension2 TO it_extension2.
      APPEND wa_zexten_yf TO it_zexten_yf.
    ENDIF.

****STA*******************************************************************************
*FI_OA费用报销单接口_新增扩展字段车号_20210915
    IF lt_item-zzcar IS NOT INITIAL.
      CLEAR lt_zexten_car[].
      CLEAR ls_zexten_car.
      ls_zexten_car-posnr = gv_itemno_acc.
      ls_zexten_car-zzcar = lt_item-zzcar.

      CLEAR ls_extension2.
      ls_extension2-structure  = 'ZSFI_ACC_DOCUMENT4'.
      ls_extension2-valuepart1 = ls_zexten_car.
      APPEND ls_extension2 TO it_extension2.
      APPEND ls_zexten_car TO lt_zexten_car.
    ENDIF.
****END*******************************************************************************

    IF ls_header-waers = g_waers. "货币是本位币 只需生成货币行项目信息

      CLEAR wa_currency_amount.
      wa_currency_amount-itemno_acc = gv_itemno_acc.
      wa_currency_amount-currency   = g_waers.
      IF lt_item-bschl IN lr_jie.
        wa_currency_amount-amt_doccur =  lt_item-wrbtr.
      ELSEIF lt_item-bschl IN lr_dai.
        wa_currency_amount-amt_doccur = 0 - lt_item-wrbtr.
      ENDIF.
      APPEND wa_currency_amount TO it_currency_amount.

    ELSE. "货币不是本位币 生成货币行项目信息以及对应本位币行项目信息

      CLEAR wa_currency_amount.
      wa_currency_amount-itemno_acc = gv_itemno_acc.
      wa_currency_amount-currency   = ls_header-waers.
      wa_currency_amount-curr_type  = '00'.
*      WA_CURRENCY_AMOUNT-EXCH_RATE  = I_KURSF.
      IF lt_item-bschl IN lr_jie.
        wa_currency_amount-amt_doccur = lt_item-wrbtr.
      ELSEIF lt_item-bschl IN lr_dai.
        wa_currency_amount-amt_doccur = 0 - lt_item-wrbtr.
      ENDIF.
      APPEND wa_currency_amount TO it_currency_amount.

      IF lt_item-dmbtr IS NOT INITIAL.
        CLEAR wa_currency_amount.
        wa_currency_amount-itemno_acc = gv_itemno_acc.
        wa_currency_amount-currency = g_waers.
        wa_currency_amount-curr_type = '10'.
        IF lt_item-bschl IN lr_jie.
          wa_currency_amount-amt_doccur = lt_item-dmbtr.
        ELSEIF lt_item-bschl IN lr_dai.
          wa_currency_amount-amt_doccur = 0 - lt_item-dmbtr.
        ENDIF.
        APPEND wa_currency_amount TO it_currency_amount.
      ENDIF.
    ENDIF.

  ENDLOOP.

  CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
    EXPORTING
      documentheader    = wa_documentheader
      customercpd       = wa_customercpd
    IMPORTING
      obj_type          = gv_objtype
      obj_key           = gv_objkey
      obj_sys           = gv_objsys
    TABLES
      accountgl         = it_accountgl
      accountreceivable = it_accountreceivable
      accountpayable    = it_accountpayable
      accounttax        = it_account_tax
      currencyamount    = it_currency_amount
      return            = it_return
      extension2        = it_extension2.

  LOOP AT it_return INTO wa_return WHERE type = 'E' OR type = 'A'.
    ztype = 'E'.
    CALL FUNCTION 'MESSAGE_TEXT_BUILD'
      EXPORTING
        msgid               = wa_return-id
        msgnr               = wa_return-number
        msgv1               = wa_return-message_v1
        msgv2               = wa_return-message_v2
        msgv3               = wa_return-message_v3
        msgv4               = wa_return-message_v4
      IMPORTING
        message_text_output = l_msg.

    CONCATENATE zmsg l_msg '!' INTO zmsg.
  ENDLOOP.

  IF ztype = 'E' .
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.

    ztype = 'S'.
    CONCATENATE '凭证' gv_objkey+0(10) '已过账到公司代码' gv_objkey+10(4) '中' INTO zmsg.
    belnr = gv_objkey+0(10).
**保存行项目长文本 start at 20210603 17:17

*    DATA: gs_header TYPE thead .
*    DATA: gt_ltxts TYPE STANDARD TABLE OF tline .
*    DATA: lw_ltxt  TYPE tline .
*
*
*    CLEAR gt_ltxts[] .
*    CLEAR lw_ltxt .
**
*    "  LW_LTXT-TDFORMAT = '1'.
*    lw_ltxt-tdline = long_txt.   " 长文本
*    APPEND lw_ltxt TO gt_ltxts .
*
*    CLEAR gs_header.
*    gs_header-tdobject = 'DOC_ITEM' .   "文本对象
*    gs_header-tdid      = '0001'.   "文本ID
*    gs_header-tdspras   = sy-langu.  "语言
*    gs_header-tdname    = gv_objkey+10(4) && gv_objkey+0(10) && gv_objkey+14(4) && '001'.  "输入参数
*
*
*    CALL FUNCTION 'SAVE_TEXT'
*      EXPORTING
*        client          = sy-mandt
*        header          = gs_header
*        savemode_direct = 'X'
*      TABLES
*        lines           = gt_ltxts.
*    IF sy-subrc <> 0.
*      ROLLBACK WORK .
*    ELSE.
*      COMMIT WORK AND WAIT .
*    ENDIF.

    DATA:lt_ht TYPE TABLE OF ztps002,
         ls_ht TYPE ztps002.

    ls_ht-belnr = gv_objkey+0(10).
    ls_ht-gjahr = gv_objkey+14(4).
    ls_ht-htnum = long_txt.
    APPEND ls_ht TO lt_ht.

    MODIFY ztps002 FROM TABLE lt_ht.
    IF sy-subrc EQ 0.
      COMMIT WORK.
    ELSE.
      ROLLBACK WORK.
    ENDIF.

**保存行项目长文本 end at 20210603 17:17
  ENDIF.

ENDFUNCTION.

抬头结构

 

 

行结构

 

 

 

 

 

标签:02,wa,lt,item,ABAP,记账,ls,TYPE,accountgl
From: https://www.cnblogs.com/eagle-dtq/p/16835827.html

相关文章