ABAP:MIRO采购预制发票过账BAPI

MIRO采购预制发票过账BAPI:

FUNCTION zsdfu011.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(I_ID) TYPE  ZRFC_LOGID OPTIONAL
*"     VALUE(I_CANUM) TYPE  CANUM OPTIONAL
*"  EXPORTING
*"     VALUE(E_CODE) TYPE  MSGTY
*"     VALUE(E_MSG) TYPE  MSGTXT
*"     VALUE(E_FLOW_END) TYPE  CHAR01
*"  TABLES
*"      ET_RETURN STRUCTURE  ZRFCT005 OPTIONAL
*"----------------------------------------------------------------------
  DATA:ls_zsdt021 TYPE zsdt021,
       lt_zsdt017 TYPE TABLE OF zsdt017,
       ls_zsdt017 LIKE LINE OF lt_zsdt017,
       lt_zsdt019 TYPE TABLE OF zsdt019,
       ls_zsdt019 LIKE LINE OF lt_zsdt019.
  DATA:BEGIN OF ls_objkey,
         objkey TYPE zsdt008-objkey.
  DATA:END OF ls_objkey.
  DATA lt_objkey LIKE TABLE OF ls_objkey.
  DATA:BEGIN OF ls_zrfc_logid,
         zrfc_logid TYPE zrfc_logid,
       END OF ls_zrfc_logid.
  DATA lt_zrfc_logid LIKE TABLE OF ls_zrfc_logid.
  DATA:ls_zrfct002 TYPE zrfct002.
  DATA:ls_creatordatain TYPE bapicreatordata,
       lt_billing       TYPE STANDARD TABLE OF bapivbrk,
       ls_billing       TYPE bapivbrk.

  SELECT *
    INTO CORRESPONDING FIELDS OF TABLE lt_zsdt019
     FROM zsdt019
     WHERE zrfc_logid = i_id.
  IF lt_zsdt019 IS INITIAL.
    e_code = 'E'.
    e_msg  = '无可执行数据'.
    RETURN.
  ENDIF.
  SORT lt_zsdt019 BY vbeln posnr.
  LOOP AT lt_zsdt019 INTO ls_zsdt019.
    ls_objkey-objkey = ls_zsdt019-vbeln.
    APPEND ls_objkey TO lt_objkey.
    ls_zsdt019-zposnr = ''.
    MODIFY lt_zsdt019 FROM ls_zsdt019.
  ENDLOOP.
  DELETE ADJACENT DUPLICATES FROM lt_objkey COMPARING ALL FIELDS.

  CALL METHOD zcl_rfc=>get_source
    EXPORTING
      i_id       = i_id
      i_canum    = i_canum
    IMPORTING
      e_zrfct002 = ls_zrfct002.
  SELECT SINGLE zzrfcid zcanum
    INTO (ls_zsdt021-zzrfcid,ls_zsdt021-zcanum)
    FROM zsdt021
    WHERE zrfcid = ls_zrfct002-zrfcid
      AND canum = i_canum.

  SELECT  zrfc_logid
    INTO TABLE lt_zrfc_logid
    FROM zsdt017
    FOR ALL ENTRIES IN lt_objkey
    WHERE objkey = lt_objkey-objkey
      AND zrfcid = ls_zsdt021-zzrfcid.
  IF lt_zrfc_logid IS INITIAL.
    e_code = 'E'.
    e_msg  = '无可执行数据'.
    RETURN.
  ENDIF.
  SELECT *
    INTO TABLE lt_zsdt017
    FROM zsdt017
    FOR ALL ENTRIES IN lt_zrfc_logid
    WHERE zrfc_logid = lt_zrfc_logid-zrfc_logid
      AND canum = ls_zsdt021-zcanum.
  SORT lt_zsdt017 BY objkey.
  DELETE ADJACENT DUPLICATES FROM lt_zsdt017 COMPARING objkey.
  LOOP AT lt_zsdt017 INTO ls_zsdt017.
    CLEAR:e_code,e_msg.
    PERFORM post_in TABLES et_return CHANGING e_code e_msg e_flow_end ls_zsdt017.
    IF e_code = 'S'.
      MODIFY lt_zsdt017 FROM ls_zsdt017.
    ENDIF.
  ENDLOOP.
  MODIFY zsdt017 FROM TABLE lt_zsdt017.
  REFRESH:lt_zsdt017.
ENDFUNCTION.

FORM post_in  TABLES   p_et_return STRUCTURE zrfct005
              CHANGING p_e_code
                       p_e_msg
                       p_e_flow_end
                       p_ls_zsdt017 TYPE zsdt017.
  DATA:message   TYPE c LENGTH 200.
*  DATA:lv_gjahr  TYPE rbkp-gjahr.
  DATA:lv_belnr  TYPE rbkp-belnr.
  DATA:lv_awkey  TYPE bkpf-awkey.
  DATA:ls_zrfct005 TYPE zrfct005.
  DATA ls_zsdt017 TYPE zsdt017.
  DATA:lt_return LIKE TABLE OF bapiret2 WITH HEADER LINE.
  lv_belnr = p_ls_zsdt017-objkey.
*  SELECT SINGLE gjahr
*    INTO lv_gjahr
*    FROM rbkp
*    WHERE belnr = lv_belnr.
  CALL FUNCTION 'BAPI_INCOMINGINVOICE_POST'
    EXPORTING
      invoicedocnumber = lv_belnr
      fiscalyear       = p_ls_zsdt017-gjahr
    TABLES
      return           = lt_return.
  CLEAR:message.
  LOOP AT lt_return WHERE type = 'A' OR type = 'E' .
    CONCATENATE message lt_return-message INTO message SEPARATED BY  '/'.
  ENDLOOP.
  IF  sy-subrc <> '0'.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.
    p_e_code = 'S'.
    CONCATENATE lv_belnr p_ls_zsdt017-gjahr INTO lv_awkey.
    CLEAR lv_belnr.
    SELECT SINGLE belnr
      INTO lv_belnr
      FROM bkpf
      WHERE awkey = lv_awkey.
    CONCATENATE '凭证:' lv_belnr '创建成功' INTO p_e_msg.
    CLEAR:ls_zrfct005.
    ls_zrfct005-status = '53'.
    ls_zrfct005-msgty = p_e_code.
    ls_zrfct005-msgid = '00'.
    ls_zrfct005-msgno = '001'.
    ls_zrfct005-msgv1 = p_e_msg.
    ls_zrfct005-msgv4 = lv_belnr.
    APPEND ls_zrfct005 TO p_et_return.
    p_ls_zsdt017-belnr = lv_belnr."会计凭证
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    p_e_code = 'E'.
    p_e_msg = message.
  ENDIF.
  CLEAR:lv_awkey,lv_belnr.
ENDFORM.   

 

posted @ 2022-12-19 11:11  阿胖的阿多  阅读(468)  评论(0编辑  收藏  举报