SAP 批量维护货源清单

*&---------------------------------------------------------------------*
*& Report  ZMM_ME01_BATCH
*&---------------------------------------------------------------------*
*&PROGRAM NAME : <ZMM_ME01_BATCH>
*---------------------------------------------------------------------*
* VERSION:            V1.0
* DATE CREATED :      2019/08/28
* CREATED BY   :      XXXXXXXXXX
* TRANSPORT REQUEST:  XXXXXXXXXX
* DESCRIPTION  :      Batch Maintain Source List
* request raised by:  XXXXXXXXXX

REPORT zmm_me01_batch.

********************************
* INNITIALIZATION
********************************

TYPES : BEGIN OF ltype_infor,
          matnr        TYPE eord-matnr,
          werks        TYPE eord-werks,
          vdatu        TYPE eord-vdatu,
          bdatu        TYPE eord-bdatu,
          lifnr        TYPE eord-lifnr,
          ekorg        TYPE eord-ekorg,
          message_text TYPE string,
        END OF ltype_infor.

TYPES : BEGIN OF ltype_marc,
          matnr TYPE marc-matnr,
          werks TYPE marc-werks,
        END OF ltype_marc.

TYPES : BEGIN OF ltype_lfa1,
          lifnr TYPE lfa1-lifnr,
        END OF ltype_lfa1.

DATA : lt_upload TYPE TABLE OF alsmex_tabline,
       ls_upload LIKE LINE OF lt_upload.

DATA : lt_infor TYPE TABLE OF ltype_infor,
       ls_infor LIKE LINE OF lt_infor.

DATA : lt_eord LIKE eord OCCURS 0 WITH HEADER LINE,
       ls_eord LIKE LINE OF lt_eord.

DATA : gt_eordu TYPE STANDARD TABLE OF eordu,
       wa_eordu LIKE LINE OF gt_eordu.

DATA : gt_xeord TYPE STANDARD TABLE OF eordu,   "存放要修改的数据
       wa_xeord LIKE LINE OF gt_xeord.

DATA : gt_eord LIKE eord OCCURS 0 WITH HEADER LINE, "数据库中的未修改前的数据,
       gs_eord LIKE LINE OF gt_eord.

DATA : lt_marc TYPE TABLE OF ltype_marc.

DATA : lt_lfa1 TYPE TABLE OF ltype_lfa1.

DATA: g_update_flag TYPE char1 VALUE 'X'. "//should be updated

TYPE-POOLS: slis.
DATA : it_fieldcat TYPE slis_t_fieldcat_alv,
       it_layout   TYPE slis_layout_alv.
DATA : lv_repid LIKE sy-repid.
DATA : it_ls_fieldcat TYPE slis_fieldcat_alv.

********************************
* SELECTION-SCREEN
********************************

SELECTION-SCREEN BEGIN OF BLOCK blk01 WITH FRAME.

PARAMETERS : p_path LIKE ibipparms-path OBLIGATORY.

SELECTION-SCREEN END OF BLOCK blk01.

********************************
* AT SELECTION-SCREEN ON VALUE-REQUEST FOR xxxxx.
********************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path.
  PERFORM frm_get_filepath USING p_path.   "获取路径

*&---------------------------------------------------------------------*
*&      Form  FRM_GET_FILEPATH
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_PATH  text
*----------------------------------------------------------------------*
FORM frm_get_filepath USING f_path.

  CALL FUNCTION 'F4_FILENAME'
    IMPORTING
      file_name = p_path.

ENDFORM.
********************************
* START OF SELECTION
********************************
START-OF-SELECTION.

  PERFORM frm_upload_data.      "上传数据

  IF lt_infor IS NOT INITIAL.

    PERFORM frm_get_data.

    PERFORM frm_alv_display.

  ENDIF.

*&---------------------------------------------------------------------*
*&      Form  FRM_UPLOAD_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_upload_data.

  DATA : lc_fname TYPE rlgrap-filename.
  DATA : wa_upload LIKE LINE OF lt_upload.

  lc_fname = p_path.

  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                = lc_fname
      i_begin_col             = 1
      i_begin_row             = 2
      i_end_col               = 11
      i_end_row               = 65536
    TABLES
      intern                  = lt_upload
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      OTHERS                  = 3.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  IF lt_upload IS NOT INITIAL.

    CLEAR lt_infor[].
    CLEAR ls_upload.
    LOOP AT lt_upload INTO ls_upload.

      CASE ls_upload-col.
        WHEN 1. "matnr
          CONDENSE ls_upload-value.

          ls_infor-matnr = ls_upload-value.

          CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'   "前缀补零
            EXPORTING
              input  = ls_infor-matnr
            IMPORTING
              output = ls_infor-matnr.

          IF ls_infor-matnr IS INITIAL.
            ls_infor-message_text = 'Please input material'.
            g_update_flag = ''.
            CONTINUE.
          ENDIF.

          CLEAR wa_upload.    "工厂检查是否为空
          READ TABLE lt_upload INTO wa_upload WITH KEY row =  ls_upload-row col = 2.
          IF sy-subrc NE 0.

            IF ls_infor-message_text IS NOT INITIAL.
              CONCATENATE ls_infor-message_text 'Please input plant' INTO ls_infor-message_text SEPARATED BY '/'.
            ELSE.
              ls_infor-message_text = 'Please input plant'.
            ENDIF.
            g_update_flag = 'X'.
          ENDIF.

          CLEAR wa_upload.   "有效期自......开始
          READ TABLE lt_upload INTO wa_upload WITH KEY row =  ls_upload-row col = 3.
          IF sy-subrc NE 0.

            IF ls_infor-message_text IS NOT INITIAL.
              CONCATENATE ls_infor-message_text 'Please input begin date' INTO ls_infor-message_text SEPARATED BY '/'.
            ELSE.
              ls_infor-message_text = 'Please input begin date'.
            ENDIF.
            g_update_flag = 'X'.
          ENDIF.

          CLEAR wa_upload.  "有效期至......结束
          READ TABLE lt_upload INTO wa_upload WITH KEY row =  ls_upload-row col = 4.
          IF sy-subrc NE 0.

            IF ls_infor-message_text IS NOT INITIAL.
              CONCATENATE ls_infor-message_text 'Please input end date' INTO ls_infor-message_text SEPARATED BY '/'.
            ELSE.
              ls_infor-message_text = 'Please input end date'.
            ENDIF.
            g_update_flag = 'X'.
          ENDIF.

          CLEAR wa_upload.  "供应商代码
          READ TABLE lt_upload INTO wa_upload WITH KEY row =  ls_upload-row col = 5.
          IF sy-subrc NE 0.

            IF ls_infor-message_text IS NOT INITIAL.
              CONCATENATE ls_infor-message_text 'Please input customer no' INTO ls_infor-message_text SEPARATED BY '/'.
            ELSE.
              ls_infor-message_text = 'Please input customer no'.
            ENDIF.
            g_update_flag = 'X'.
          ENDIF.

          CLEAR wa_upload.  "采购组织
          READ TABLE lt_upload INTO wa_upload WITH KEY row =  ls_upload-row col = 6.
          IF sy-subrc NE 0.

            IF ls_infor-message_text IS NOT INITIAL.
              CONCATENATE ls_infor-message_text 'Please input plant' INTO ls_infor-message_text SEPARATED BY '/'.
            ELSE.
              ls_infor-message_text = 'Please input plant'.
            ENDIF.
            g_update_flag = 'X'.
          ENDIF.

        WHEN 2. "werks
          CONDENSE ls_upload-value.
          ls_infor-werks = ls_upload-value.

        WHEN 3. "vdatu
          CONDENSE ls_upload-value.
          ls_infor-vdatu = ls_upload-value.

        WHEN 4. "bdatu
          CONDENSE ls_upload-value.
          ls_infor-bdatu = ls_upload-value.

        WHEN 5. "lifnr
          CONDENSE ls_upload-value.
          ls_infor-lifnr = ls_upload-value.

          CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'   "前缀补零
            EXPORTING
              input  = ls_infor-lifnr
            IMPORTING
              output = ls_infor-lifnr.

        WHEN 6. "vkorg
          CONDENSE ls_upload-value.
          ls_infor-ekorg = ls_upload-value.

          APPEND ls_infor TO lt_infor.
          CLEAR ls_infor.

      ENDCASE.

    ENDLOOP.

  ENDIF.



ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_get_data .

  DATA l_err TYPE string.

  CLEAR lt_eord[].
  SELECT *
  FROM eord
  INTO CORRESPONDING FIELDS OF TABLE lt_eord
  FOR ALL ENTRIES IN lt_infor
  WHERE matnr = lt_infor-matnr
    AND werks = lt_infor-werks
    AND lifnr = lt_infor-lifnr
    AND ekorg = lt_infor-ekorg.

  SORT lt_eord BY matnr werks lifnr ekorg ASCENDING vdatu DESCENDING.

  CLEAR ls_infor.
  CLEAR wa_eordu.
  CLEAR gt_eordu[].
  LOOP AT lt_infor INTO ls_infor.

    SELECT
      matnr
      werks
      FROM marc
      INTO CORRESPONDING FIELDS OF TABLE lt_marc
      WHERE matnr = ls_infor-matnr
      AND   werks = ls_infor-werks.

    SELECT lifnr
       FROM lfa1
       INTO CORRESPONDING FIELDS OF TABLE lt_lfa1
       WHERE lifnr = ls_infor-lifnr.


    IF lt_marc IS INITIAL.    "判断工厂物料是否存在

      CONCATENATE ls_infor-message_text 'Material in plant not exists!' INTO ls_infor-message_text SEPARATED BY '/'.

      MODIFY lt_infor FROM ls_infor TRANSPORTING message_text.

      CONTINUE.

    ELSEIF lt_lfa1 IS INITIAL.  "判读供应商是否存在

      CONCATENATE ls_infor-message_text 'Vendor not exists!' INTO ls_infor-message_text SEPARATED BY '/'.

      MODIFY lt_infor FROM ls_infor TRANSPORTING message_text.

      CONTINUE.

    ENDIF.

    CLEAR:ls_eord,gt_eordu,gt_eordu[].
    READ TABLE lt_eord INTO ls_eord WITH KEY matnr = ls_infor-matnr werks = ls_infor-werks lifnr = ls_infor-lifnr ekorg = ls_infor-ekorg.
    IF ls_eord-lifnr <> ls_infor-lifnr. "创建货源清单.

      wa_eordu-matnr = ls_infor-matnr.
      wa_eordu-werks = ls_infor-werks.
      wa_eordu-lifnr = ls_infor-lifnr.
      wa_eordu-vdatu = ls_infor-vdatu.
      wa_eordu-bdatu = ls_infor-bdatu.
      wa_eordu-ekorg = ls_infor-ekorg.
      wa_eordu-flifn = ''.
      wa_eordu-kz = 'I' . "I for insert

      APPEND wa_eordu TO gt_eordu.

      WAIT UP TO '1' SECONDS.
      CALL FUNCTION 'ZRFC_ME01' STARTING NEW TASK 'Z01'
        EXPORTING
          i_matnr  = ls_infor-matnr
          i_werks  = ls_infor-werks
        TABLES
          gt_eordu = gt_eordu.

      IF sy-subrc = 0.
        CONCATENATE ls_infor-message_text 'Source List Created Succeed' INTO ls_infor-message_text SEPARATED BY '/'.
      ELSE.
        CONCATENATE ls_infor-message_text 'Source List Created Failed' INTO ls_infor-message_text SEPARATED BY '/'.
      ENDIF.
    ELSEIF ls_eord-lifnr = ls_infor-lifnr AND ls_eord-vdatu = ls_infor-vdatu AND ls_eord-bdatu = ls_infor-bdatu AND ls_eord-ekorg = ls_infor-ekorg.
      CONCATENATE ls_infor-message_text 'Source List Already Exists!' INTO ls_infor-message_text SEPARATED BY '/'.
    ELSE.
      "//修改货源清单
      IF sy-subrc IS INITIAL.

        MOVE-CORRESPONDING ls_eord TO gs_eord.
        APPEND gs_eord TO gt_eord.
        MOVE-CORRESPONDING ls_eord TO wa_xeord.
        wa_xeord-matnr = ls_infor-matnr.
        wa_xeord-werks = ls_infor-werks.
        wa_xeord-vdatu = ls_infor-vdatu.
        wa_xeord-bdatu = ls_infor-bdatu.
        wa_xeord-lifnr = ls_infor-lifnr.
        wa_xeord-ekorg = ls_infor-ekorg.
        wa_xeord-flifn = ''.

        APPEND wa_xeord TO gt_xeord.

        CALL FUNCTION 'ME_UPDATE_SOURCES_OF_SUPPLY'
          EXPORTING
            i_changedocument = 'X'
          TABLES
            xeord            = gt_xeord    "存放要修改的数据
            yeord            = gt_eord[].  "数据库中未修改前的数据
        IF sy-subrc = 0.
          COMMIT WORK AND WAIT.
          l_err = 'SUCCESS'.
        ELSE.
          ROLLBACK WORK.
          l_err = 'FAIL'.
        ENDIF.

        IF l_err NE 'FAIL'.
          CONCATENATE ls_infor-message_text 'Source List Changed Succeed' INTO ls_infor-message_text SEPARATED BY '/'.
        ELSE.
          CONCATENATE ls_infor-message_text 'Source List Changed Failed' INTO ls_infor-message_text SEPARATED BY '/'.
        ENDIF.
      ENDIF.
      CLEAR gt_xeord[].
      CLEAR gt_eord[].
    ENDIF.

    MODIFY lt_infor FROM ls_infor.
    CLEAR ls_infor.
    CLEAR ls_eord.
    CLEAR wa_eordu.
    CLEAR gt_eordu[].

  ENDLOOP.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_ALV_DISPLAY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_alv_display .

  lv_repid = sy-repid.
  it_layout-detail_popup = 'X'.
  it_layout-info_fieldname = 'LINECOLOR'.
  REFRESH :it_fieldcat[].
  PERFORM frm_fieldcat_init.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = lv_repid
      it_fieldcat        = it_fieldcat[]
      is_layout          = it_layout
      i_default          = 'X'
      i_save             = 'A'
*     i_grid_title       = result
    TABLES
      t_outtab           = lt_infor.

ENDFORM.                    " FRM_ALV_DISPLAY
*&---------------------------------------------------------------------*
*&      Form  FRM_FIELDCAT_INIT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM frm_fieldcat_init.
  DEFINE fieldcat.
    CLEAR  it_ls_fieldcat.
    it_ls_fieldcat-col_pos       =  1.
    it_ls_fieldcat-fieldname     = &1.
    it_ls_fieldcat-seltext_l     = &2.
    it_ls_fieldcat-outputlen     = &3.
    it_ls_fieldcat-key           = &4.

    it_ls_fieldcat-seltext_m = it_ls_fieldcat-seltext_l.
    it_ls_fieldcat-seltext_s = it_ls_fieldcat-seltext_l.

    APPEND it_ls_fieldcat TO it_fieldcat.
    CLEAR  it_ls_fieldcat.
  END-OF-DEFINITION.

  fieldcat          'MATNR'              'Material'              '18'          'X'  .
  fieldcat          'WERKS'              'Plant'                 '6'          'X'  .
  fieldcat          'VDATU'              'Valid From'            '10'          'X'  .
  fieldcat          'BDATU'              'Valid To'              '10'           ''  .
  fieldcat          'LIFNR'              'Vendor'                '12'           ''  .
  fieldcat          'EKORG'              'POrg'                  '6'          'X'  .
  fieldcat          'MESSAGE_TEXT'       'Message'               '40'          'X'  .
ENDFORM.

异步调用Function。
SAP 批量维护货源清单
SAP 批量维护货源清单
SAP 批量维护货源清单
Function Code
 

FUNCTION ZRFC_ME01.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(I_MATNR) LIKE  MARA-MATNR OPTIONAL
*"     VALUE(I_WERKS) LIKE  MARC-WERKS OPTIONAL
*"  TABLES
*"      GT_EORDU STRUCTURE  EORDU OPTIONAL
*"----------------------------------------------------------------------

 data l_err(10).

      CALL FUNCTION 'ME_DIRECT_INPUT_SOURCE_LIST'   "创建货源清单
        EXPORTING
          i_matnr          = i_matnr
          i_werks          = i_werks
        TABLES
          t_eord           = gt_eordu
        EXCEPTIONS
          plant_missing    = 1
          material_missing = 2
          error_message    = 4
          OTHERS           = 3.

      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
        l_err = 'FAIL'.
      ELSE.
        CALL FUNCTION 'ME_POST_SOURCE_LIST_NEW'
          EXPORTING
            i_matnr = i_matnr.
        IF sy-subrc <> 0.
          MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
          l_err = 'FAIL'.
        ENDIF.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait = 'X'.
        IF sy-subrc <> 0.
          MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
          l_err = 'FAIL'.
        ENDIF.

      ENDIF.



ENDFUNCTION.

 

上一篇:python_005_while循环,列表的高级操作,生成器的概念,字典


下一篇:自动审核