ABAP:销售发票冲销BAPI及VF02过账BDC

销售发票冲销BAPI及VF02过账BDC

FUNCTION zsdfu019.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  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:lt_zsdt027   TYPE TABLE OF zsdt027,
       ls_zsdt027   LIKE LINE OF lt_zsdt027,
       ls_zsdt026   TYPE zsdt026,
       lt_zsdt026   TYPE TABLE OF zsdt026,
       ls_zsdt017   TYPE zsdt017,
       ls_zsdt017_1 TYPE zsdt017,
       lt_zsdt017   TYPE TABLE OF zsdt017.
  DATA:ls_zrfct002 TYPE zrfct002.
  DATA:ls_source TYPE TABLE OF zrfcs001.
  DATA:ls_zrfct005 TYPE zrfct005.
  DATA:BEGIN OF ls_objkey,
         objkey TYPE zsdt008-objkey,
       END OF ls_objkey.
  DATA lt_objkey LIKE TABLE OF ls_objkey.
  DATA:BEGIN OF ls_zrfc_logid,
         zrfc_logid TYPE zsdt017-zrfc_logid,
         objkey     TYPE zsdt008-objkey,
       END OF ls_zrfc_logid.
  DATA lt_zrfc_logid LIKE TABLE OF ls_zrfc_logid.
  DATA:lv_fkdat TYPE vbrk-fkdat,
       lv_stgrd TYPE stgrd.

  DATA:message   TYPE c LENGTH 200.
  DATA:mtext   TYPE c LENGTH 200.

  "获取源表数据
  SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_zsdt027
    FROM zsdt027
   WHERE zrfc_logid = i_id.

  IF lt_zsdt027 IS INITIAL.
    e_code = 'E'.
    e_msg  = '无可执行数据'.
    RETURN.
  ENDIF.

  LOOP AT lt_zsdt027 INTO ls_zsdt027.
    ls_objkey-objkey = ls_zsdt027-vbeln.
    lv_fkdat = ls_zsdt027-fkdat.
    APPEND ls_objkey TO lt_objkey.
    ls_zsdt027-zposnr = ''.
    MODIFY lt_zsdt027 FROM ls_zsdt027.
  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_zsdt026-zzrfcid,ls_zsdt026-zcanum)
     FROM zsdt026
    WHERE zrfcid = ls_zrfct002-zrfcid
      AND canum = i_canum.

  "根据开票流程步骤找到发票数据
  SELECT zrfc_logid objkey
    INTO TABLE lt_zrfc_logid
    FROM zsdt017
     FOR ALL ENTRIES IN lt_objkey
   WHERE objkey = lt_objkey-objkey
     AND zrfcid = ls_zsdt026-zzrfcid.

  IF lt_zrfc_logid IS INITIAL.
    e_code = 'E'.
    e_msg  = '无可执行数据'.
    RETURN.
  ENDIF.

  SORT lt_zrfc_logid BY zrfc_logid.
  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_zsdt026-zcanum.

  SORT lt_zsdt017 BY zrfc_logid.
  SORT lt_zrfc_logid BY objkey.
  "销售发票冲销结构定义
  DATA:lt_return TYPE TABLE OF bapireturn1.
  DATA:lt_success TYPE TABLE OF bapivbrksuccess.
  DATA:ls_return TYPE bapireturn1.
  DATA:ls_success TYPE bapivbrksuccess.
  DATA:lv_billingdate TYPE bapivbrk-bill_date.
  DATA:lv_vbeln_old TYPE bill_doc.
  CLEAR:ls_zsdt017_1.
  READ TABLE lt_zsdt017 INTO ls_zsdt017_1 INDEX 1.
  CLEAR:lt_return[],lt_success[],lv_billingdate,lv_vbeln_old.

  lv_vbeln_old = ls_zsdt017_1-objkey.
  lv_billingdate = lv_fkdat.
  "判断发票是否产生会计凭证
  DATA:lv_rfbsk TYPE vbrk-rfbsk.
  CLEAR:lv_rfbsk.
  SELECT SINGLE rfbsk INTO lv_rfbsk FROM vbrk WHERE vbeln = ls_zsdt017_1-objkey.
  CALL FUNCTION 'BAPI_BILLINGDOC_CANCEL1'
    EXPORTING
      billingdocument = lv_vbeln_old
      billingdate     = lv_billingdate
    TABLES
      return          = lt_return
      success         = lt_success.
  READ TABLE lt_return INTO ls_return WITH KEY type = 'E'.
  IF sy-subrc = 0.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    e_code = 'E'.
    e_msg = ls_return-message.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.
    WAIT UP TO '2' SECONDS.
    READ TABLE lt_success INTO ls_success WITH KEY ref_doc = lv_vbeln_old.
    IF sy-subrc = 0.

      IF lv_rfbsk = 'C'. "冲销之后过账
        CLEAR:messtab,bdcdata[].
        PERFORM bdc_dynpro      USING 'SAPMV60A' '0101'.
        PERFORM bdc_field       USING 'BDC_CURSOR'
                                      'VBRK-VBELN'.
        PERFORM bdc_field       USING 'BDC_OKCODE'
                                      '/00'.
        PERFORM bdc_field       USING 'VBRK-VBELN'
                                      ls_success-bill_doc.
        PERFORM bdc_dynpro      USING 'SAPMV60A' '0104'.
        PERFORM bdc_field       USING 'BDC_CURSOR'
                                      'VBRK-FKART'.
        PERFORM bdc_field       USING 'BDC_OKCODE'
                                      '=FKFR'.
        PERFORM bdc_dynpro      USING 'SAPMV60A' '0104'.
        PERFORM bdc_field       USING 'BDC_CURSOR'
                                      'VBRK-FKART'.
        PERFORM bdc_field       USING 'BDC_OKCODE'
                                      '=SICH'.
        gwa_ctu-defsize = 'X'.
        gwa_ctu-dismode = 'N'.
        gwa_ctu-updmode = 'S'.
        CALL TRANSACTION 'VF02' USING bdcdata OPTIONS FROM gwa_ctu MESSAGES  INTO messtab.
        LOOP AT messtab WHERE msgtyp = 'E' OR msgtyp = 'X' OR msgtyp = 'A'.
          MESSAGE ID messtab-msgid TYPE messtab-msgtyp NUMBER messtab-msgnr
              INTO mtext
              WITH messtab-msgv1 messtab-msgv2 messtab-msgv3 messtab-msgv4.
          CONCATENATE message mtext INTO message SEPARATED BY  '/'.
        ENDLOOP.
        IF  sy-subrc <> '0'.
        ELSE.
          e_code = 'E'.
          e_msg = message.
        ENDIF.
      ENDIF.

      CLEAR:ls_zsdt017.
      SELECT SINGLE belnr INTO ls_zsdt017-belnr FROM bkpf WHERE awkey = ls_success-bill_doc.
      ls_zsdt017-zrfc_logid = i_id.
      ls_zsdt017-canum      = ls_zrfct002-canum.
      ls_zsdt017-objtype    = ls_zrfct002-objtype_save.
      ls_zsdt017-objkey     = ls_success-bill_doc.
      ls_zsdt017-zrfcid     = ls_zrfct002-zrfcid.
*      ls_zsdt017-belnr = ls_success-bill_doc.
      ls_zsdt017-zzrfc_logid = ls_zsdt017_1-zrfc_logid.
      ls_zsdt017-zcanum = ls_zsdt017_1-canum.
      ls_zsdt017-zobjkey = ls_zsdt017_1-objkey.
      ls_zsdt017-zobjtype = ls_zsdt017_1-objtype.
      APPEND ls_zsdt017 TO lt_zsdt017.

      CLEAR:ls_zsdt017.
      SELECT SINGLE * INTO ls_zsdt017 FROM zsdt017 WHERE zrfc_logid = ls_zsdt017_1-zrfc_logid
        AND canum = ls_zsdt017_1-canum AND objkey = ls_zsdt017_1-objkey AND objtype = ls_zsdt017_1-objtype.
      ls_zsdt017-fksto = 'X'.
      APPEND ls_zsdt017 TO lt_zsdt017.
      MODIFY zsdt017 FROM TABLE lt_zsdt017.

      e_code = 'S'.
      CONCATENATE 'BL:' ls_success-bill_doc '冲销成功' INTO e_msg.
      CLEAR:ls_zrfct005.
      ls_zrfct005-status = '53'.
      ls_zrfct005-msgty = e_code.
      ls_zrfct005-msgid = '00'.
      ls_zrfct005-msgno = '001'.
      ls_zrfct005-msgv1 = e_msg.
      ls_zrfct005-msgv4 = ls_success-bill_doc.
      APPEND ls_zrfct005 TO et_return.
    ELSE.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      e_code = 'E'.
      e_msg = '冲销失败'.
    ENDIF.
  ENDIF.
ENDFUNCTION.

 

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