调用BAPI审批/取消审批采购订单

data: gt_t16fs TYPE STANDARD TABLE OF t16fs, "批准策略

  "定义采购订单对应的审批信息
  BEGIN OF gt_ekko OCCURS 0,
    ebeln LIKE ekko-ebeln,                                "采购订单编号
    frggr LIKE ekko-frggr,                                   "审批组
    frgsx LIKE ekko-frgsx,                                  "审批策略
    frgzu LIKE ekko-frgzu,                                  "版本状态
    bsart LIKE ekko-bsart,                                 "采购凭证类型
    ekorg LIKE ekko-ekorg,                               "采购组织
  END   OF gt_ekko.

&———————————————————————
*& Form get_Approval_strategy
&———————————————————————

  •   取采购订单对应的审批组和审批策略
    
  •   再根据审批组和审批管理取每级对应的审批组
    

———————————————————————-
FORM get_approval_strategy.
"取采购订单对应审批组
SELECT ebeln "采购订单
frggr "审批组
frgsx "审批策略
frgzu "版本状态
bsart "采购凭证类型
ekorg "采购组织
INTO TABLE gt_ekko
FROM ekko

WHERE ebeln = l_ebeln.

"根据审批组取对应批准策略
SELECT *
INTO TABLE gt_t16fs
FROM t16fs
FOR ALL ENTRIES IN gt_ekko
WHERE frggr = gt_ekko-frggr "审批组

    AND frgsx = gt_ekko-frgsx.      "审批策略

ENDFORM. "get_Approval_strategy

&———————————————————————
*& Form PROCESS_po
&———————————————————————

  •   处理采购订单,包括取消审批和审批
    

———————————————————————-

  •  –>VALUE(FV_APPROVE)  处理标识,N:表示取消审批, Y:表示审批通过
    

———————————————————————-
FORM process_po USING value(fv_approve).
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组成对应审批组字段

FIELD-SYMBOLS: <fs_field> TYPE t16fs-frgc1. "当前审批组值
CLEAR: l_level,
l_last_char,
l_fieldname.

READ TABLE gt_ekko ASSIGNING <fs_ekko> WITH KEY ebeln = w_poheader-po_number.
IF sy-subrc EQ 0 AND <fs_ekko>-frgzu IS NOT INITIAL.
"根据采购订单的审批组、审批策略取对应的审批组
READ TABLE gt_t16fs ASSIGNING <fs_t16fs> WITH KEY frggr = <fs_ekko>-frggr
frgsx = <fs_ekko>-frgsx.

"计算审批通过级数
l_level = STRLEN( <fs_ekko>-frgzu ).
"取消采购订单,只取消第一级审批就可以,不需要逐级取消审批,如果逐级取消审批后造成采购订单被锁定错误
IF fv_approve = ‘N’.

  "组成字段名
  CONCATENATE l_pre_field '1' INTO l_fieldname.

  "通过指针获取批准代码
  ASSIGN COMPONENT l_fieldname OF STRUCTURE <fs_t16fs> TO <fs_field>.

  PERFORM approve_po USING <fs_ekko>-ebeln 'N' <fs_field>.

ENDIF.

"批准采购订单,由前往后逐级审批

IF fv_approve = 'Y'.
  DO l_level TIMES.
    l_last_char = sy-index.
    CONCATENATE l_pre_field l_last_char INTO l_fieldname.
    ASSIGN COMPONENT l_fieldname OF STRUCTURE <fs_t16fs> TO <fs_field>.
      PERFORM approve_po USING <fs_ekko>-ebeln 'Y' <fs_field>.
  ENDDO.

ENDIF.

ENDIF.

ENDFORM. "PROCESS_po

&———————————————————————
*& Form approve_po
&———————————————————————

  •   审批或取消采购订单审批
    

———————————————————————-

  •  –>VALUE(FV_EBELN)    采购订单编号
    
  •  –>VALUE(FV_APPROVE)  审批状态
    
  •  –>VALUE(FV_REL_COD)  审批代码
    

———————————————————————-
FORM approve_po USING value(fv_ebeln)
value(fv_approve)
value(fv_rel_cod).
CALL FUNCTION ‘Z_MM_APPROVE_PO’
EXPORTING
purchaseorder = fv_ebeln
approve = fv_approve
rel_cod = fv_rel_cod.

IF sy-subrc EQ 0.
WAIT UP TO 1 SECONDS.
ENDIF.

ENDFORM. "approve_po

FUNCTION z_mm_approve_po.
*"———————————————————————-
"“Update function module:
*”
"“Local interface:
*” IMPORTING
*" VALUE(PURCHASEORDER) TYPE BAPIMMPARA-PO_NUMBER
*" VALUE(APPROVE) TYPE CHAR1
*" VALUE(REL_COD) TYPE BAPIMMPARA-PO_REL_COD
*"———————————————————————-

IF approve = ‘Y’.
CALL FUNCTION ‘BAPI_PO_RELEASE’
EXPORTING
purchaseorder = purchaseorder
po_rel_code = rel_cod

  • USE_EXCEPTIONS               = 'X'
    
  • NO_COMMIT                    = ' '
    
  • IMPORTING

  • REL_STATUS_NEW               =
    
  • REL_INDICATOR_NEW            =
    
  • RET_CODE                     =
    
  • TABLES

  • RETURN                       = 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.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
    ELSEIF approve = ‘N’.
    CALL FUNCTION ‘BAPI_PO_RESET_RELEASE’
    EXPORTING
    purchaseorder = purchaseorder
    po_rel_code = rel_cod

  • USE_EXCEPTIONS                 = 'X'
    
  • IMPORTING

  • REL_STATUS_NEW                 =
    
  • REL_INDICATOR_NEW              =
    
  • TABLES

  • RETURN                         = 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.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.

    ENDIF.
    ENDFUNCTION.

转自:http://www.sapffj.com/?p=153

上一篇:【ABAP系列】SAP 获取工单和工序的状态


下一篇:洛谷 P2400 秘密文件【分治】