首页 > 代码库 > 创建 会计凭证

创建 会计凭证

FUNCTION zrfc_mm018.
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     VALUE(CALLNO) TYPE  ZCALLNO
*"     VALUE(ZFIS005) LIKE  ZFIS005 STRUCTURE  ZFIS005
*"  EXPORTING
*"     VALUE(BELNR) TYPE  BELNR_D
*"     VALUE(COMP_CODE) TYPE  BUKRS
*"     VALUE(GJAHR) TYPE  GJAHR
*"     VALUE(FLAG) LIKE  BAPIRET2-TYPE
*"     VALUE(MESSAGE) LIKE  BAPIRET2-MESSAGE
*"  TABLES
*"      ZFIS006 STRUCTURE  ZFIS006 OPTIONAL
*"      ZFIS007 STRUCTURE  ZFIS007 OPTIONAL
*"----------------------------------------------------------------------
DATA: ls_documentheader LIKE bapiache09,                   "凭证抬头数据
      lt_accountgl      LIKE TABLE OF bapiacgl09,          "总账科目项目
      lt_accountpayable LIKE TABLE OF bapiacap09,          "供应商科目项目
      lt_curren         LIKE TABLE OF bapiaccr09,          "金额项目
      lt_extension2        LIKE TABLE OF bapiparex,           "扩展字段项目
      lt_return         LIKE TABLE OF bapiret2,            "返回消息
      ls_return         LIKE bapiret2,
      lv_obj_type       LIKE bapiache09-obj_type,
      lv_obj_key        LIKE bapiache09-obj_key,
      lv_obj_sys        LIKE bapiache09-obj_sys,
      lt_zfis006        LIKE TABLE OF zfis006,
      lt_zfis007        LIKE TABLE OF zfis007,
      lv_bukrs          LIKE zfis005-bukrs,                "分公司
      lv_waers          LIKE zfis005-waers,                "货币码
      lv_message        LIKE bapiret2-message.

DATA: ls_zfis006        LIKE  zfis006,
      ls_zfis007        LIKE  zfis007,
      ls_accountgl      LIKE  bapiacgl09,
      ls_accountpayable LIKE  bapiacap09,
      ls_curren         LIKE  bapiaccr09,
      ls_extension2        LIKE  bapiparex,
      ls_zfis001        LIKE  zfis001.

*---预提凭证账期检查
*IF  zfis005-blart = ‘SA_W‘ OR zfis005-blart = ‘SA_R‘.
*PERFORM frm_check_post_date USING zfis005-blart zfis005-budat CHANGING flag message.
*ENDIF.
*CHECK flag IS INITIAL.

*---参数赋值
lt_zfis006  = zfis006[].
lt_zfis007  = zfis007[].
lv_waers    = zfis005-waers.
lv_bukrs    = zfis005-bukrs.

*---凭证抬头数据:
ls_documentheader-header_txt = zfis005-bktxt.              "抬头文本
ls_documentheader-comp_code  = zfis005-bukrs.              "公司代码
ls_documentheader-doc_date   = zfis005-bldat.              "凭证日期
ls_documentheader-pstng_date = zfis005-budat.              "过账日期
ls_documentheader-doc_type   = zfis005-blart.              "凭证类型
ls_documentheader-ref_doc_no = zfis005-xblnr.              "参考凭证编号
ls_documentheader-username   = sy-uname.                   "

*---凭证行项目:
*-----------------------------------------------------------------------------------
*                             借方项目
*-----------------------------------------------------------------------------------
LOOP AT lt_zfis006 INTO ls_zfis006.
*---总账科目项目
  ls_accountgl-itemno_acc    =  ls_zfis006-posnr.             "行项目编号
  ls_accountgl-gl_account    =  ls_zfis006-hkont.             "总账科目
  ls_accountgl-alloc_nmbr    =  ls_zfis006-zuonr.             "分配编号
  ls_accountgl-item_text     =  ls_zfis006-sgtxt.             "文本
  ls_accountgl-costcenter    =  ls_zfis006-kostl.             "成本中心
  ls_accountgl-value_date    =  ls_zfis006-valut.             "起息日
  ls_accountgl-ref_key_2     =  ls_zfis006-xref2.             "参考码2--预算号
  ls_accountgl-ref_key_3     =  ls_zfis006-xref3.             "参考码3--预算号
  APPEND ls_accountgl TO lt_accountgl.

*---金额项目
  ls_curren-itemno_acc       =  ls_zfis006-posnr.             "项目
  ls_curren-currency         =  lv_waers.                     "币种
  ls_curren-amt_doccur       =  ls_zfis006-bapiwrbtr.         "金额
  APPEND ls_curren TO lt_curren.

*---扩展字段项目
  ls_zfis001-posnr           =  ls_zfis006-posnr.
  ls_zfis001-bschl           =  ls_zfis006-bschl.             "借方记账码40
  ls_zfis001-rstgr           =  ls_zfis006-rstgr.             "付款原因代码
  ls_zfis001-shkzg           =  ls_zfis006-shkzg.             "借贷标识
  ls_zfis001-zumsk           =  ls_zfis006-zumsk.             "目标特别总帐标志
  ls_extension2-structure    = ZFIS001.
  ls_extension2-valuepart1   = ls_zfis001.
  APPEND ls_extension2 TO lt_extension2.
  CLEAR:ls_zfis006,ls_accountgl,ls_extension2,ls_zfis001,ls_curren.
ENDLOOP.

*-----------------------------------------------------------------------------------
*                             贷方项目(员工借款借贷都在供应商行项目上)
*-----------------------------------------------------------------------------------
*---供应商科目项目
LOOP AT  lt_zfis007 INTO ls_zfis007.

CALL FUNCTION CONVERSION_EXIT_ALPHA_INPUT
  EXPORTING
    input         = ls_zfis007-lifnr
 IMPORTING
   output        =  ls_zfis007-lifnr.

ls_accountpayable-itemno_acc = ls_zfis007-posnr.              "行项目编号
ls_accountpayable-vendor_no  = ls_zfis007-lifnr.              "员工供应商账号
ls_accountpayable-alloc_nmbr = ls_zfis007-zuonr.              "分配编号
ls_accountpayable-item_text  = ls_zfis007-sgtxt.              "文本
ls_accountpayable-comp_code  = lv_bukrs.                      "公司代码
ls_accountpayable-ref_key_2  = ls_zfis007-xref2.              "参考码2--预算号
ls_accountpayable-ref_key_3  = ls_zfis007-xref3.              "参考码3(预付款申请凭证)
ls_accountpayable-sp_gl_ind  = ls_zfis007-sp_gl_ind.          "特别总账标识(预付款申请凭证: F 原借款:I )
ls_accountpayable-bline_date = ls_zfis007-bline_date.         "到期日计算的基限日期(员工借款)
ls_accountpayable-tax_code   = ls_zfis007-mwskz.              "销售税代码(预付款申请凭证)
APPEND ls_accountpayable TO lt_accountpayable.

*---金额项目
ls_curren-itemno_acc         = ls_zfis007-posnr.
ls_curren-currency           = lv_waers.
IF zfis005-blart = KR.
  ls_curren-amt_doccur       = ls_zfis007-bapiwrbtr.
  AT LAST.
  ls_curren-amt_doccur       = ls_curren-amt_doccur * -1.
  ENDAT.
ELSE.
ls_curren-amt_doccur         = ls_zfis007-bapiwrbtr * -1.
ENDIF.
APPEND ls_curren TO lt_curren.

*---扩展字段项目
ls_zfis001-posnr             =  ls_zfis007-posnr.             "项目编号
ls_zfis001-bschl             =  ls_zfis007-bschl.             "借方记账码
ls_zfis001-rstgr             =  ls_zfis007-rstgr.             "付款原因代码
ls_zfis001-zumsk             =  ls_zfis007-zumsk.             "目标特别总帐标志.(预付款申请凭证 N )
ls_zfis001-zfbdt             =  ls_zfis007-zfbdt.             "用于到期日计算的基准日期(预付款申请凭证)
ls_zfis001-wmwst             =  ls_zfis007-wmwst.             "税额(预付款申请凭证)

IF zfis005-blart = KA.                                      "(预付款申请凭证类型)
    ls_zfis001-bstat         = S.                           "凭证状态 S(预付款申请凭证)
    ls_zfis001-glvor         = RFST.                        "交易(预付款申请凭证)
ENDIF.
ls_extension2-structure      = ZFIS001.
ls_extension2-valuepart1     = ls_zfis001.
APPEND ls_extension2 TO lt_extension2.
CLEAR:ls_zfis007,ls_accountgl,ls_extension2,ls_zfis001,ls_curren.
ENDLOOP.

*-----------------------------------------------------------------------------------
*                             调用BAPI生成凭证
*-----------------------------------------------------------------------------------
CALL FUNCTION BAPI_ACC_DOCUMENT_POST
  EXPORTING
    documentheader         = ls_documentheader
 IMPORTING
   obj_type                = lv_obj_type
   obj_key                 = lv_obj_key
   obj_sys                 = lv_obj_sys
  TABLES
   accountgl               = lt_accountgl
   accountpayable          = lt_accountpayable
   currencyamount          = lt_curren
   return                  = gt_return
   extension2              = lt_extension2.

  READ TABLE gt_return INTO gs_return WITH KEY type = E.
  IF sy-subrc NE 0.
    belnr = lv_obj_key+0(10).          "会计凭证
    comp_code = lv_obj_key+10(4).      "公司代码
    gjahr     = lv_obj_key+14(4).      "会计年度
    flag = S.
    CALL FUNCTION BAPI_TRANSACTION_COMMIT
      EXPORTING
        wait = X.
  ELSE.
    CALL FUNCTION BAPI_TRANSACTION_ROLLBACK.

    LOOP AT gt_return INTO gs_return WHERE type CA AEX.
        MESSAGE ID     gs_return-id
                TYPE   gs_return-type
                NUMBER gs_return-number
                WITH   gs_return-message_v1 gs_return-message_v2
                       gs_return-message_v3 gs_return-message_v4
                       INTO lv_message.
        CONCATENATE message lv_message INTO message SEPARATED BY .
    ENDLOOP.
    SHIFT message LEFT DELETING LEADING  .
    flag = E.
  ENDIF.

*-----------------------------------------------------------------------------------
*                             日志记录
*-----------------------------------------------------------------------------------
PERFORM frm_in_log USING     callno
                             flag
                             message
                             zfis005
                             lt_zfis006
                             lt_zfis007.
*-----------------------------------------------------------------------------------
*                             凭证记录
**-----------------------------------------------------------------------------------
IF flag = S.
PERFORM frm_insert_zmmt002 USING belnr comp_code gjahr zfis005-blart.
ENDIF.

*-----------------------------------------------------------------------------------
*                          做预提凭证的冲销
*-----------------------------------------------------------------------------------
IF zfis005-blart+0(2) = SA AND flag = S.
  PERFORM frm_rev_post USING lv_obj_type  lv_obj_key lv_obj_sys zfis005-budat.
ENDIF.

ENDFUNCTION.
FORM frm_in_log  USING   callno     TYPE zcallno
                          flag       LIKE bapiret2-type
                          message       LIKE    bapiret2-message
                          p_zfis005  LIKE zfis005
                          pt_zfis006 LIKE gt_zfis006
                          pt_zfis007 LIKE gt_zfis007.

  DATA: ls_zfis006 TYPE zfis006,
        ls_zfis007 TYPE zfis007,
        lt_in_log  TYPE TABLE OF zrfc_mm01in_log,
        ls_in_log  TYPE zrfc_mm01in_log,
        lv_datano  TYPE zdatano,
        bapiwrbtr(15),
        wmwst(15).

*---抬头项目
    lv_datano        = lv_datano + 1.
    bapiwrbtr        = ls_zfis006-bapiwrbtr.
    ls_in_log-name   = ZRFC_MM018.
    ls_in_log-cdate  = sy-datum.
    GET TIME.
    ls_in_log-ctime  = sy-uzeit.
    ls_in_log-callno = callno.
    ls_in_log-datano = lv_datano.
    ls_in_log-flag   = flag.
    ls_in_log-log    = message.
    CONCATENATE p_zfis005-bukrs  p_zfis005-blart   p_zfis005-bldat   p_zfis005-budat
                p_zfis005-bktxt  p_zfis005-waers   p_zfis005-xblnr
                INTO ls_in_log-content SEPARATED BY |.
    CONDENSE ls_in_log-content NO-GAPS.
    ls_in_log-length = STRLEN( ls_in_log-content ).
    APPEND ls_in_log TO lt_in_log.


*---总账科目项目
  LOOP AT pt_zfis006 INTO ls_zfis006.
    lv_datano        = lv_datano + 1.
    bapiwrbtr        = ls_zfis006-bapiwrbtr.
    ls_in_log-name   = ZRFC_MM018.
    ls_in_log-cdate  = sy-datum.
    GET TIME.
    ls_in_log-ctime  = sy-uzeit.
    ls_in_log-callno = callno.
    ls_in_log-datano = lv_datano.
    ls_in_log-flag   = flag.
    ls_in_log-log    = message.
    CONCATENATE ZFIS006         ls_zfis006-posnr  ls_zfis006-hkont  ls_zfis006-kostl  ls_zfis006-valut
                ls_zfis006-zuonr  ls_zfis006-sgtxt  bapiwrbtr         ls_zfis006-bschl  ls_zfis006-rstgr
                ls_zfis006-shkzg  ls_zfis006-zumsk  ls_zfis006-xref2  ls_zfis006-xref3
                INTO ls_in_log-content SEPARATED BY |.
    SHIFT message LEFT DELETING LEADING  .
    CONDENSE ls_in_log-content NO-GAPS.
    ls_in_log-length = STRLEN( ls_in_log-content ).
    APPEND ls_in_log TO lt_in_log.
    CLEAR bapiwrbtr.
  ENDLOOP.

*----供应商项目
  LOOP AT pt_zfis007 INTO ls_zfis007.
    lv_datano = lv_datano + 1.
    bapiwrbtr = ls_zfis007-bapiwrbtr.
    wmwst     = ls_zfis007-wmwst.
    ls_in_log-name   = ZRFC_MM018.
    ls_in_log-cdate  = sy-datum.
    GET TIME.
    ls_in_log-ctime  = sy-uzeit.
    ls_in_log-callno = callno.
    ls_in_log-datano = lv_datano.
    ls_in_log-flag   = flag.
    ls_in_log-log    = message.
    CONCATENATE ZFIS007            ls_zfis007-posnr  ls_zfis007-lifnr  ls_zfis007-bline_date  ls_zfis007-sp_gl_ind
                ls_zfis007-pmnttrms  ls_zfis007-zuonr  ls_zfis007-sgtxt  bapiwrbtr              ls_zfis007-bschl
                ls_zfis007-rstgr     ls_zfis007-shkzg  ls_zfis006-zumsk  ls_zfis007-xref2       ls_zfis007-xref3
                wmwst                ls_zfis007-zfbdt  ls_zfis007-mwskz
                INTO ls_in_log-content SEPARATED BY |.
    SHIFT message LEFT DELETING LEADING  .
    CONDENSE ls_in_log-content NO-GAPS.
    ls_in_log-length = STRLEN( ls_in_log-content ).
    APPEND ls_in_log TO lt_in_log.
    CLEAR: bapiwrbtr,wmwst.
  ENDLOOP.

IF lt_in_log IS NOT INITIAL.
     INSERT zrfc_mm01in_log FROM TABLE lt_in_log.
ENDIF.

ENDFORM.                    " FRM_IN_LOG
FORM frm_insert_zmmt002  USING    p_belnr      TYPE bseg-belnr
                                  p_bukrs      TYPE bseg-bukrs
                                  p_gjahr      TYPE bseg-gjahr
                                  p_blart      TYPE zfis005-blart.

DATA: lt_zmmt002 TYPE TABLE OF zmmt002,
      ls_zmmt002  TYPE zmmt002.

*---会计凭证信息
SELECT  bukrs belnr gjahr buzei zuonr sgtxt xref1 xref2 xref3
  INTO CORRESPONDING FIELDS OF TABLE lt_zmmt002
  FROM bseg
  WHERE bukrs  = p_bukrs
  AND   belnr  = p_belnr
  AND   gjahr  = p_gjahr.

  ls_zmmt002-blart = p_blart.  "凭证类型
  MODIFY lt_zmmt002 FROM  ls_zmmt002 TRANSPORTING blart WHERE blart IS INITIAL .

INSERT zmmt002 FROM TABLE lt_zmmt002.
  IF sy-subrc = 0.
     COMMIT WORK AND WAIT.
  ELSE.
     ROLLBACK WORK.
  ENDIF.

ENDFORM.                    " FRM_INSERT_ZMMT002
FORM frm_rev_post  USING    p_obj_type TYPE bapiache09-obj_type
                            p_obj_key  TYPE bapiache09-obj_key
                            p_obj_sys  TYPE bapiache09-obj_sys
                            p_budat    TYPE d.

DATA: l_datum1     TYPE d,
      fis_period   TYPE bapiacrev-fis_period,
      reason_rev   TYPE bapiacrev-reason_rev VALUE 04,
      ls_bkpf      TYPE bkpf,
      l_period     TYPE t009b-poper,
      l_reversal   TYPE bapiacrev,
      l_bus_act    TYPE bapiache09-bus_act,
      lt_return    TYPE TABLE OF bapiret2,
      ls_return    TYPE bapiret2.

DATA: l_obj_type   TYPE bapiache09-obj_type,
      l_obj_key    TYPE bapiache09-obj_key,
      l_obj_sys    TYPE bapiache09-obj_sys,
      belnr        TYPE bseg-belnr,
      comp_code    TYPE bseg-bukrs,
      gjahr        TYPE bseg-gjahr,
      flag            LIKE    bapiret2-type,
      message         LIKE    bapiret2-message,
      lv_message   LIKE bapiret2-message.

SELECT SINGLE * INTO ls_bkpf FROM bkpf
  WHERE awtyp = p_obj_type
   AND  awkey = p_obj_key.


*取得冲销凭证的账期
l_datum1 = p_budat.

*---得到当前过账日期的下一个月日期
CALL FUNCTION MONTH_PLUS_DETERMINE
  EXPORTING
    months        = 1
    olddate       = p_budat
 IMPORTING
   newdate        = l_datum1.

CONCATENATE l_datum1+0(6) 01 INTO l_datum1.

*---取下个月账期
CALL FUNCTION G_PERIOD_GET
 EXPORTING
   company                              = ls_bkpf-bukrs
   date                                 = l_datum1
   ledger                               = 0L
 IMPORTING
   period                               = l_period.

*-----冲销凭证
l_reversal-obj_type   = ls_bkpf-awtyp.
l_reversal-obj_key    = ls_bkpf-awkey.
l_reversal-obj_key_r  = ls_bkpf-awkey.
l_reversal-pstng_date = l_datum1.
l_reversal-fis_period = l_period.
l_reversal-comp_code  = ls_bkpf-bukrs.
l_reversal-reason_rev = reason_rev.
l_reversal-ac_doc_no  = ls_bkpf-belnr.
l_bus_act             = ls_bkpf-glvor.

CALL FUNCTION BAPI_ACC_DOCUMENT_REV_POST
  EXPORTING
    reversal       = l_reversal
    bus_act        = l_bus_act
 IMPORTING
   obj_type        = l_obj_type
   obj_key         = l_obj_key
   obj_sys         = l_obj_sys
  TABLES
    return         = lt_return.



  READ TABLE lt_return INTO ls_return WITH KEY type = E.
  IF sy-subrc NE 0.
    belnr     = l_obj_key+0(10).      "会计凭证
    comp_code = l_obj_key+10(4).      "公司代码
    gjahr     = l_obj_key+14(4).      "会计年度
    flag = S.
    CALL FUNCTION BAPI_TRANSACTION_COMMIT
      EXPORTING
        wait = X.
  ELSE.
    CALL FUNCTION BAPI_TRANSACTION_ROLLBACK.

    LOOP AT lt_return INTO ls_return WHERE type CA AEX.
        MESSAGE ID     ls_return-id
                TYPE   ls_return-type
                NUMBER ls_return-number
                WITH   ls_return-message_v1 ls_return-message_v2
                       ls_return-message_v3 ls_return-message_v4
                       INTO lv_message.
        CONCATENATE message lv_message INTO message SEPARATED BY .
    ENDLOOP.
    SHIFT message LEFT DELETING LEADING  .
    flag = E.
  ENDIF.


IF flag = S.
PERFORM frm_insert_zmmt002 USING belnr comp_code gjahr SA.
ENDIF.
ENDFORM.                    " FRM_REV_POST

 

创建 会计凭证