SD--批量刪除訂單

JackWu發表於2015-06-15

在sap應用中常常會需要批量刪除一些錯誤錄入的單據,為此開發了一個小程式。該程式為了安全,程式做了一下控制

1、限制使用者只能刪除自己的訂單,不能刪除別人輸入的訂單,如果需要修改一下查詢條件;

2、系統預設為“測試執行”,方便使用者在刪除訂單前要核實一下訂單,確認不要誤操作;

程式程式碼如下,供大家參考!

REPORT  zsde0099.
TYPE-POOLS: slis.
*----------------------------------------------------------------------*
*  data Declarations
*----------------------------------------------------------------------*
TABLES: vbak.        " Sales Document: Header Data
*----------------------------------------------------------------------*
* table control Output Declarations
*----------------------------------------------------------------------*
TYPES: BEGIN OF tc_0100,
        sel(1),                      " Check box
        vbeln  LIKE vbak-vbeln,      " Sales Order Number
        erdat  LIKE vbak-erdat,      " Sales order creation date
        ernam  LIKE vbak-ernam,
        kunnr  LIKE likp-kunag,      " Sold-to Party
        name1  LIKE kna1-name1,      " Sold-to Company Name
        text(200),
      END OF tc_0100.
DATA: wa_tc_0100 TYPE tc_0100,
      it_tc_0100 TYPE tc_0100 OCCURS 0 WITH HEADER LINE.

DATA g_fieldcat TYPE slis_t_fieldcat_alv.

*----------------------------------------------------------------------*
* Selection Screen
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK params1 WITH FRAME TITLE text-001.
PARAMETER:  s_vkorg LIKE vbak-vkorg DEFAULT '6001' OBLIGATORY MODIF ID s1.  " " Sales Org.
SELECT-OPTIONS:
            s_vtweg FOR vbak-vtweg OBLIGATORY,     " Distribution Channel
            s_spart FOR vbak-spart OBLIGATORY DEFAULT '10',    " Division
            s_auart FOR vbak-auart OBLIGATORY,        " Order type
            s_erdat FOR vbak-erdat OBLIGATORY, " Sales order creation date
*            s_ERNAM FOR vbak-ERNAM OBLIGATORY , " Sales order creation date
            s_kunnr FOR vbak-kunnr ,        " Sold-to party
            s_vbeln FOR vbak-vbeln .        " Sales Order
PARAMETER  p_test AS CHECKBOX DEFAULT 'X'.

SELECTION-SCREEN END OF BLOCK params1.
***********************************************************************
*INITIALIZATION.
***********************************************************************
INITIALIZATION.
  s_auart-sign = 'I'.
  s_auart-option = 'EQ'.
  s_auart-low = 'ZC01'.
  APPEND s_auart.
  s_auart-low = 'ZC03'.
  APPEND s_auart.
  s_auart-low = 'ZC06'.
  APPEND s_auart.
  s_auart-low = 'ZC20'.
  APPEND s_auart.
  s_vtweg-sign   = 'I'.
  s_vtweg-option = 'EQ'.
  s_vtweg-low    = '10'.
  APPEND s_vtweg.
  s_vtweg-low    = '20'.
  APPEND s_vtweg.
  s_vtweg-low    = '30'.
  APPEND s_vtweg.
  s_vtweg-low    = '40'.
  APPEND s_vtweg.
*----------------------------------------------------------------------*
* Selection Events Processing
*----------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT.
  PERFORM screen_check.

AT SELECTION-SCREEN.
  CLEAR:   it_tc_0100, wa_tc_0100.
  REFRESH: it_tc_0100.

START-OF-SELECTION.
  PERFORM extract_data.

END-OF-SELECTION.
  PERFORM change_sales_orders.
  PERFORM display_mes.

*&---------------------------------------------------------------------*
*&      Form  SCREEN_CHECK
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM screen_check.
  DATA: l_line TYPE i .
  DESCRIBE TABLE s_erdat LINES l_line.
  IF l_line EQ 0.
    s_erdat-sign   = 'I'.
    s_erdat-option = 'BT'.
    s_erdat-high   = sy-datum.
    s_erdat-low    = sy-datum.
    APPEND s_erdat.
  ENDIF.
  DESCRIBE TABLE s_vtweg LINES l_line.
  IF l_line EQ 0.
  ENDIF.
ENDFORM.                    "SCREEN_CHECK
*&---------------------------------------------------------------------*
*&      Form  extract_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM extract_data.
  DATA: lt_it1 LIKE it_tc_0100 OCCURS 0 WITH HEADER LINE,
        l_chk(1).                                          " for exclude incomplete SO

  SELECT a~vbeln a~erdat a~ernam a~kunnr  INTO CORRESPONDING FIELDS OF TABLE lt_it1
  FROM vbak AS a
  WHERE a~vbeln IN s_vbeln
    AND a~auart IN s_auart
    AND a~vkorg EQ s_vkorg
    AND a~vtweg IN s_vtweg
    AND a~spart IN s_spart
    AND a~erdat IN s_erdat
    AND a~ernam = sy-uname
    AND a~kunnr IN s_kunnr.

  LOOP AT lt_it1.
* get sold-to party company name
    PERFORM get_customer_company_name USING lt_it1-kunnr CHANGING lt_it1-name1.
    MODIFY lt_it1.
  ENDLOOP.
  it_tc_0100[] = lt_it1[].
ENDFORM.                    "extract_data
*&---------------------------------------------------------------------*
*&      Form  GET_CUSTOMER_COMPANY_NAME
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->F_KUNNR    text
*      -->F_NAME     text
*----------------------------------------------------------------------*
FORM get_customer_company_name USING f_kunnr CHANGING f_name.
  CLEAR f_name.
  SELECT SINGLE name1 INTO f_name
  FROM kna1
  WHERE kunnr = f_kunnr.
ENDFORM.                    "GET_CUSTOMER_COMPANY_NAME
*&---------------------------------------------------------------------*
*&      Form  change_sales_orders
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM change_sales_orders.
  DATA: lt_order_header_in  LIKE bapisdh1,
        lt_order_header_inx LIKE bapisdh1x,
        lt_return           LIKE bapiret2 OCCURS 0 WITH HEADER LINE,
        lt_order_item_in    LIKE bapisditm OCCURS 0 WITH HEADER LINE,
        lt_order_item_inx   LIKE bapisditmx OCCURS 0 WITH HEADER LINE,
        cp_eind(1).

  SORT it_tc_0100 BY vbeln.
  DELETE ADJACENT DUPLICATES FROM it_tc_0100.

  LOOP AT it_tc_0100.
    CLEAR:   lt_order_header_inx,lt_order_item_in, lt_order_item_inx, lt_return,cp_eind.
    REFRESH: lt_order_item_in, lt_order_item_inx, lt_return.
    lt_order_header_inx-updateflag = 'D'.
    IF p_test NE 'X'.
      CALL FUNCTION 'BAPI_SALESORDER_CHANGE'
        EXPORTING
          salesdocument    = it_tc_0100-vbeln
*         ORDER_HEADER_IN  = LT_ORDER_HEADER_IN
          order_header_inx = lt_order_header_inx
        TABLES
          return           = lt_return.
* 處理錯誤訊息:通過判斷訊息的型別,來判斷BAPI是否成功
      LOOP AT lt_return .
        IF lt_return-type EQ 'E' OR
            lt_return-type = 'A' OR
            lt_return = 'X'.
          cp_eind = 'X'.  "失敗
          it_tc_0100-text = lt_return-message.
        ENDIF.
      ENDLOOP.
      IF cp_eind NE 'X'.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait = 'X'.
      ELSE.
        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      ENDIF.
    ENDIF.
    IF  cp_eind NE 'X' AND sy-subrc = 0.
      IF p_test = 'X'.
        it_tc_0100-text = 'Test successfully.'.

      ELSE.
        it_tc_0100-text = 'Delete successfully.'.
      ENDIF.
    ELSE.
      CONCATENATE  it_tc_0100-text '---' 'Delete error.' into it_tc_0100-text.
    ENDIF.
    MODIFY it_tc_0100.
  ENDLOOP.
ENDFORM.                    "change_sales_orders


*&---------------------------------------------------------------------*
*&      Form  init_fieldedi
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_FIELDTAB  text
*----------------------------------------------------------------------*
FORM init_fieldedi USING p_p_fieldtab TYPE slis_t_fieldcat_alv.
  DATA: l_fieldcat TYPE slis_fieldcat_alv.
  REFRESH  p_p_fieldtab.
  CLEAR l_fieldcat.
  l_fieldcat-fieldname  = 'VBELN'.
  l_fieldcat-seltext_m = 'ORDER NUMBER'.
  l_fieldcat-outputlen = 10.
  APPEND l_fieldcat TO p_p_fieldtab.
  CLEAR l_fieldcat.
  l_fieldcat-fieldname  = 'ERDAT'.
  l_fieldcat-seltext_m = 'Create Date'.
  l_fieldcat-ref_fieldname = 'ERDAT'.
  l_fieldcat-ref_tabname = 'VBAK'.
  l_fieldcat-outputlen = 10.
  APPEND l_fieldcat TO p_p_fieldtab.
  CLEAR l_fieldcat.
  l_fieldcat-fieldname  = 'ERNAM'.
  l_fieldcat-seltext_m = 'Input Person'.
  l_fieldcat-ref_fieldname = 'ERDAT'.
  l_fieldcat-ref_tabname = 'VBAK'.
  l_fieldcat-outputlen = 10.
  APPEND l_fieldcat TO p_p_fieldtab.
  CLEAR l_fieldcat.
  l_fieldcat-fieldname  = 'KUNNR'.
  l_fieldcat-seltext_m = 'Sold Code'.
  l_fieldcat-ref_fieldname = 'KUNNR'.
  l_fieldcat-ref_tabname = 'VBAK'.
  l_fieldcat-outputlen = 10.
  APPEND l_fieldcat TO p_p_fieldtab.

  CLEAR l_fieldcat.
  l_fieldcat-fieldname  = 'NAME1'.
  l_fieldcat-seltext_m = 'Sold Name1'.
  l_fieldcat-outputlen = 35.
  APPEND l_fieldcat TO p_p_fieldtab.

  CLEAR l_fieldcat.
  l_fieldcat-fieldname  = 'TEXT'.
  l_fieldcat-seltext_m = 'Message'.
  l_fieldcat-outputlen = 120.
  APPEND l_fieldcat TO p_p_fieldtab.

ENDFORM.                               " INITIALIZE_FIELDEDI
*&---------------------------------------------------------------------*
*&      Form  display_mes
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM display_mes .
  PERFORM init_fieldedi USING g_fieldcat[].
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = sy-repid
      it_fieldcat        = g_fieldcat[]
      i_save             = 'A'
      i_default          = 'A'
    TABLES
      t_outtab           = it_tc_0100.
ENDFORM.                    " display_mes

 

相關文章