ABAP:采购订单审批及采购订单反审批BAPI

采购订单审批及反审批:

  DATA:lt_zsdt008 LIKE TABLE OF zsdt008,
        ls_zsdt008 LIKE LINE OF lt_zsdt008,
        lt_zsdt027 TYPE TABLE OF zsdt027,
        ls_zsdt027 TYPE zsdt027,
        lt_zsdt028 TYPE TABLE OF zsdt028,
        ls_zsdt028 TYPE zsdt028,
        ls_zsdt001 TYPE zsdt001,
        ls_zrfct002 TYPE zrfct002,
        lt_ekko TYPE TABLE OF ekko,
        ls_ekko TYPE ekko.
  CLEAR:lt_zsdt028,ls_zsdt028.
  SELECT SINGLE * INTO ls_zsdt028 FROM zsdt028 WHERE zrfc_logid = i_id." AND bsart = 'PO'.
  IF sy-subrc <> 0.
    e_code = 'E'.
    e_msg  = '无可执行数据'.
    RETURN.
  ENDIF.

  CLEAR:lt_ekko.
  SELECT *
  INTO TABLE lt_ekko
  FROM ekko
  WHERE ebeln = ls_zsdt028-vbeln.

  READ TABLE lt_ekko INTO ls_ekko INDEX 1.
  IF sy-subrc <> 0.
    e_code = 'E'.
    e_msg  = '无可执行数据'.
    RETURN.
  ENDIF.

  PERFORM frm_release_po USING ls_ekko-ebeln
                               '' "反审
                               'X'"审批
                         CHANGING e_code e_msg e_flow_end.

*  MESSAGE '审批成功' TYPE 'I'.
  IF e_code = 'E'.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
  ELSE.
    CALL METHOD zcl_rfc=>get_source
      EXPORTING
        i_id       = i_id
        i_canum    = i_canum
      IMPORTING
        e_zrfct002 = ls_zrfct002.
*      RECEIVING
*        R_SOURCE   = LS_SOURCE.

    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.

    CLEAR:ls_zsdt008.
    ls_zsdt008-canum      = ls_zrfct002-canum.
    ls_zsdt008-objtype    = ls_zrfct002-objtype_save.
    ls_zsdt008-objkey     = ls_ekko-ebeln.
    ls_zsdt008-zrfcid     = ls_zrfct002-zrfcid.
    ls_zsdt008-zrfc_logid = i_id.
    APPEND ls_zsdt008 TO lt_zsdt008.

    MODIFY zsdt008 FROM TABLE lt_zsdt008.

    e_code = 'S'.
    CONCATENATE 'PO:' ls_ekko-ebeln '审批成功' INTO e_msg.
    DATA:ls_zrfct005 TYPE zrfct005.
    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_ekko-ebeln.
    APPEND ls_zrfct005 TO et_return.
  ENDIF.

FORM frm_release_po  USING  uv_ebeln TYPE ekko-ebeln
                            uv_status10
                            uv_status20
                     CHANGING cv_code TYPE msgty
                              cv_msg  TYPE msgtxt
                              cv_flow_end TYPE char01.
  DATA:ls_ekko TYPE ekko.
  DATA:ls_t16fs TYPE t16fs.

  DATA:l_level TYPE n,                                                  "当前订单已审批通过级别数
     l_last_char TYPE n,                                                 "当前订单最后审批通过级别
     l_pre_field TYPE string VALUE 'FRGC',              "所有审批组对应字段相同部分
     l_fieldname TYPE string.  "由l_pre_field + l_last_char组成对应审批组字段

  DATA:lv_index TYPE n.

  FIELD-SYMBOLS:<fs_field> TYPE t16fs-frgc1,      "当前审批组值
                <fs_t16fs> TYPE t16fs.

  IF uv_status20 IS NOT INITIAL.  "审批
    SELECT SINGLE * INTO ls_ekko FROM ekko WHERE ebeln = uv_ebeln.
    SELECT SINGLE * INTO ls_t16fs FROM t16fs WHERE frggr = ls_ekko-frggr AND frgsx = ls_ekko-frgsx.      "审批策略
    IF sy-subrc <> 0.
      cv_code = 'E'.
      cv_msg = '审批策略不存在'.
      RETURN.
    ENDIF.

    CLEAR:l_level,
          l_last_char,
          l_fieldname.



    l_last_char = STRLEN( ls_ekko-frgzu ). ""当前已审批级数
    DO  8 TIMES.
      UNASSIGN:<fs_field>.
      lv_index = sy-index.
      CONCATENATE l_pre_field lv_index INTO l_fieldname.
      ASSIGN COMPONENT l_fieldname OF STRUCTURE ls_t16fs TO <fs_field>.
      IF  <fs_field> IS INITIAL.
        l_level = sy-index - l_last_char - 1."需要审批的次数
        EXIT.
      ENDIF.
    ENDDO.

    CLEAR:l_last_char.
    DO l_level TIMES.
      WAIT UP TO '0.5' SECONDS.
      l_last_char = l_last_char + 1.
      CONCATENATE l_pre_field l_last_char INTO l_fieldname.
      ASSIGN COMPONENT l_fieldname OF STRUCTURE ls_t16fs TO <fs_field>.
      CALL FUNCTION 'BAPI_PO_RELEASE'
        EXPORTING
          purchaseorder                = uv_ebeln
          po_rel_code                  = <fs_field>
*             USE_EXCEPTIONS               = 'X'
*             NO_COMMIT                    = ' '
*           IMPORTING
*             REL_STATUS_NEW               =
*             REL_INDICATOR_NEW            =
*             RET_CODE                     =
*           TABLES
*             RETURN                       =
*           EXCEPTIONS
*             AUTHORITY_CHECK_FAIL         = 1
*             DOCUMENT_NOT_FOUND           = 2
*             ENQUEUE_FAIL                 = 3
*             PREREQUISITE_FAIL            = 4
*             RELEASE_ALREADY_POSTED       = 5
*             RESPONSIBILITY_FAIL          = 6
*             OTHERS                       = 7
          .
      IF sy-subrc <> 0.
        cv_code = 'E'.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
                INTO cv_msg.
        RETURN.
      ENDIF.
    ENDDO.

  ELSEIF uv_status10 IS NOT INITIAL. "反审
    SELECT SINGLE * INTO ls_ekko FROM ekko WHERE ebeln = uv_ebeln.
    SELECT SINGLE * INTO ls_t16fs FROM t16fs WHERE frggr = ls_ekko-frggr AND frgsx = ls_ekko-frgsx.      "审批策略
    IF sy-subrc <> 0.
      cv_code = 'E'.
      cv_msg = '审批策略不存在'.
      RETURN.
    ENDIF.

    IF ls_ekko-frgzu IS INITIAL.    ""还未审批,不需要撤销审批
      RETURN.
    ENDIF.
    CALL FUNCTION 'BAPI_PO_RESET_RELEASE'     ""撤销审批只需要撤销第一级审批即可
      EXPORTING
        purchaseorder                  = uv_ebeln
        po_rel_code                    = ls_t16fs-frgc1
*       USE_EXCEPTIONS                 = 'X'
*     IMPORTING
*       REL_STATUS_NEW                 =
*       REL_INDICATOR_NEW              =
*     TABLES
*       RETURN                         =
*     EXCEPTIONS
*       AUTHORITY_CHECK_FAIL           = 1
*       DOCUMENT_NOT_FOUND             = 2
*       ENQUEUE_FAIL                   = 3
*       PREREQUISITE_FAIL              = 4
*       RELEASE_ALREADY_POSTED         = 5
*       RESPONSIBILITY_FAIL            = 6
*       NO_RELEASE_ALREADY             = 7
*       NO_NEW_RELEASE_INDICATOR       = 8
*       OTHERS                         = 9
              .
    IF sy-subrc <> 0.
      cv_code = 'E'.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
              WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
              INTO cv_msg.
      RETURN.
    ENDIF.

  ENDIF.

ENDFORM.    

 

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