ABAP:ME01货源清单批导

BAPI:ME_INITIALIZE_SOURCE_LIST,ME_DIRECT_INPUT_SOURCE_LIST

*&---------------------------------------------------------------------*
*& Report ZMMU015
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zmmu015.

TABLES:sscrfields.
*&---------------------------------------------------------------------*
*&全局数据
*&---------------------------------------------------------------------*
TYPES:BEGIN OF ty_cols_rows,
        begin_col TYPE i,
        begin_row TYPE i,
        end_col   TYPE i,
        end_row   TYPE i,
      END OF ty_cols_rows.

DATA:gv_text TYPE string,
     functxt TYPE smp_dyntxt.
DATA:gt_data LIKE TABLE OF alsmex_tabline.
DATA:gs_cols_rows TYPE ty_cols_rows.

DATA:gs_fieldcat TYPE lvc_s_fcat,              "字段工作区
     gt_fieldcat TYPE lvc_t_fcat.

DATA:gs_layout TYPE lvc_s_layo.

DATA:BEGIN OF gs_alv,
       sel    TYPE char1,
       werks  TYPE eord-werks,
       matnr  TYPE eord-matnr,
       lifnr  TYPE eord-lifnr,
       ekorg  TYPE eord-ekorg,
       meins  TYPE eord-meins,
       flifn  TYPE eord-flifn,
       notkz  TYPE eord-notkz,
       autet  TYPE eord-autet,
       vdatu  TYPE eord-vdatu,
       bdatu  TYPE eord-bdatu,
       status TYPE icon-id,
       text   TYPE char255,
     END OF gs_alv,

     gt_alv LIKE TABLE OF gs_alv.
*&---------------------------------------------------------------------*
*&选择屏幕
*&---------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK bk1 WITH FRAME TITLE TEXT-001.
  PARAMETERS:p_file TYPE rlgrap-filename.
SELECTION-SCREEN END OF BLOCK bk1.

SELECTION-SCREEN FUNCTION KEY 1.

*&---------------------------------------------------------------------*
*&初始化
*&---------------------------------------------------------------------*
INITIALIZATION.
  PERFORM frm_init_botton.
*&---------------------------------------------------------------------*
*&AT SELECTION-SCREEN ON VALUE-REQUEST
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
****文件上传路径搜索帮助
  PERFORM f4_p_file.
*&---------------------------------------------------------------------*
*&AT SELECTION-SCREEN.
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN.
  "模板下载
  CASE sscrfields-ucomm..
    WHEN 'FC01'.
      PERFORM frm_download_file.
  ENDCASE.

*&---------------------------------------------------------------------*
*&START-OF-SELECTION.
*&---------------------------------------------------------------------*
START-OF-SELECTION.
  "数据上传
  PERFORM frm_upload_file.
  "数据处理
  PERFORM frm_process_data.
*&---------------------------------------------------------------------*
*&END-OF-SELECTION.
*&---------------------------------------------------------------------*
END-OF-SELECTION.
  PERFORM frm_pre_fieldcat.   "设置字段
  PERFORM frm_set_layout.     "设置布局
  PERFORM frm_show_alv.
*&---------------------------------------------------------------------*
*&      Form  F4_P_FILE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f4_p_file .
  DATA:lt_tab TYPE filetable,
       lv_qty TYPE i, "返回的文件数量
       ls_wa  TYPE string.

  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
      window_title     = '选择文件' "窗口标题
      default_filename = '' "默认文件名
      multiselection   = '' "是否多选,X=一次性可多选,空=单选
      file_filter      = 'Excel表格| *.xls;*.xlsx;*.xlsm'
    CHANGING
      file_table       = lt_tab  "返回的文件结果
      rc               = lv_qty.  "返回的文件个数

  LOOP AT lt_tab INTO ls_wa.
    p_file = ls_wa."文件选择框内 = 返回的所选文件路径
  ENDLOOP.
  CALL METHOD cl_gui_cfw=>flush.
ENDFORM.                    " F4_P_FILE
*&---------------------------------------------------------------------*
*& Form frm_init_botton
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_init_botton .
  CLEAR functxt.
  functxt-icon_id   = icon_export. "图标,可以通过ICON查找
  functxt-icon_text = '模板下载'."描述
  sscrfields-functxt_01 = functxt.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_download_file
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_download_file .
  DATA:lv_init_path TYPE string,
       lv_title     TYPE string,
       lv_path      TYPE string,
       lv_fpath     TYPE string,
       lv_file      TYPE rlgrap-filename.

  DATA:ls_wdatb LIKE wwwdatatab,
       lv_subrc TYPE sy-subrc,
       lv_msg   TYPE string.

  DATA:lv_objid TYPE w3objid,
       lv_fname TYPE string.


  lv_objid = 'ZMMU015'.
  lv_fname = '货源清单批导'.


  "获取桌面路径
  CALL METHOD cl_gui_frontend_services=>get_desktop_directory
    CHANGING
      desktop_directory    = lv_init_path
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.

  "保存文件
  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      window_title         = lv_title
      default_extension    = 'xlsx'
      default_file_name    = lv_fname
      initial_directory    = lv_init_path
      file_filter          = 'EXCEL文件(*.xlsx)||全部文件(*.*)||'
      prompt_on_overwrite  = 'X'
    CHANGING
      filename             = lv_fname
      path                 = lv_path
      fullpath             = lv_fpath
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ELSE.
    CLEAR:lv_file.
    lv_file = lv_fpath.
    IF lv_file IS NOT INITIAL.
      "判断文件模板是否存在
      SELECT SINGLE
             relid
             objid
        FROM wwwdata
        INTO CORRESPONDING FIELDS OF ls_wdatb
       WHERE srtf2 = 0
         AND relid = 'MI'
         AND objid = lv_objid.

      IF ls_wdatb IS INITIAL.
        MESSAGE TEXT-e03 TYPE 'E'.
      ELSE.

        CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
          EXPORTING
            key         = ls_wdatb
            destination = lv_file
          IMPORTING
            rc          = lv_subrc.
        IF lv_subrc <> 0.
          MESSAGE TEXT-e02 TYPE 'E'.
        ELSE.
          CLEAR:lv_msg.
          CONCATENATE '模板下载到本地文件' lv_file INTO lv_msg.
          MESSAGE lv_msg TYPE 'S'.
        ENDIF.
      ENDIF.
    ELSE.
      MESSAGE TEXT-e01 TYPE 'E' .
    ENDIF.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_upload_file
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_upload_file .

  IF p_file IS INITIAL.
    MESSAGE '文件路径不能为空' TYPE 'E'.
  ENDIF.

  CLEAR:gs_cols_rows.
  gs_cols_rows-begin_col = '1'.
  gs_cols_rows-begin_row = '2'.
  gs_cols_rows-end_row = '99999'.
  gs_cols_rows-end_col = '10'.
  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                = p_file
      i_begin_col             = gs_cols_rows-begin_col
      i_begin_row             = gs_cols_rows-begin_row
      i_end_col               = gs_cols_rows-end_col
      i_end_row               = gs_cols_rows-end_row
    TABLES
      intern                  = gt_data
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      OTHERS                  = 3.

  IF gt_data IS INITIAL.
    MESSAGE '模板中无数据,请确认' TYPE 'S' DISPLAY LIKE 'E'.
    LEAVE LIST-PROCESSING.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_process_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_process_data .

  DATA:BEGIN OF ls_excle,
         werks TYPE eord-werks,
         matnr TYPE eord-matnr,
         lifnr TYPE eord-lifnr,
         ekorg TYPE eord-ekorg,
         meins TYPE eord-meins,
         flifn TYPE eord-flifn,
         notkz TYPE eord-notkz,
         autet TYPE eord-autet,
         vdatu TYPE eord-vdatu,
         bdatu TYPE eord-bdatu,
       END OF ls_excle,

       lt_excle LIKE TABLE OF ls_excle.

  DATA:gv_struc TYPE string,
       gv_table TYPE string.



  FIELD-SYMBOLS: <fv_field>.
  LOOP AT gt_data INTO DATA(ls_data).

    ASSIGN COMPONENT ls_data-col OF STRUCTURE ls_excle TO <fv_field>.

    <fv_field> = ls_data-value.

    "物料增加前导零
    IF ls_data-col = 2.
      CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
        EXPORTING
          input  = <fv_field>
        IMPORTING
          output = <fv_field>.
    ENDIF.

    "供应商增加前导零
    IF ls_data-col = 3.
      <fv_field> = |{ <fv_field> ALPHA = IN }|.
    ENDIF.

    AT END OF row.
      APPEND ls_excle TO lt_excle.
      CLEAR:ls_excle.
    ENDAT.
    CLEAR:ls_data.
  ENDLOOP.

  SORT lt_excle BY werks matnr.

  LOOP AT lt_excle INTO ls_excle.

    MOVE-CORRESPONDING ls_excle TO gs_alv.

    APPEND gs_alv TO gt_alv.
    CLEAR:ls_excle,gs_alv.
  ENDLOOP.


ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_pre_fieldcat
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_pre_fieldcat .
  CLEAR gs_fieldcat.
  REFRESH gt_fieldcat.

* "===手动赋值fieldcat===
  DEFINE set_fieldcat.
    CLEAR: gs_fieldcat.
    gs_fieldcat-fieldname = &1 .
    gs_fieldcat-coltext = &2.
    gs_fieldcat-edit = &3.
    gs_fieldcat-ref_table = &4.
    gs_fieldcat-ref_field = &5.
    gs_fieldcat-icon = &6.
    gs_fieldcat-checkbox = &7.
    gs_fieldcat-convexit = &8.
    gs_fieldcat-lowercase = 'X'.

    APPEND gs_fieldcat TO gt_fieldcat .
  END-OF-DEFINITION.

*  set_fieldcat 'SEL' '选择' 'X' '' '' '' '' ''."列/名称/可编辑/参考表/参考字段/图标/复选框/例程转换
  set_fieldcat 'WERKS' '工厂代码' '' '' '' '' '' ''."列/名称/可编辑/参考表/参考字段/图标/复选框/例程转换
  set_fieldcat 'MATNR' 'SAP物料编码' '' '' '' '' '' 'MATN1'."列/名称/可编辑/参考表/参考字段/图标/复选框/例程转换
  set_fieldcat 'LIFNR' '供应商编码' '' '' '' '' '' 'ALPHA'."列/名称/可编辑/参考表/参考字段/图标/复选框/例程转换
  set_fieldcat 'EKORG' '采购组织' '' '' '' '' '' ''."列/名称/可编辑/参考表/参考字段/图标/复选框/例程转换
  set_fieldcat 'MEINS' '订单单位' '' '' '' '' '' 'CUNIT'."列/名称/可编辑/参考表/参考字段/图标/复选框/例程转换
  set_fieldcat 'FLIFN' '固定货源' '' '' '' '' 'X' ''."列/名称/可编辑/参考表/参考字段/图标/复选框/例程转换
  set_fieldcat 'NOTKZ' '冻结货源' '' '' '' '' 'X' ''."列/名称/可编辑/参考表/参考字段/图标/复选框/例程转换
  set_fieldcat 'AUTET' 'MRP' '' '' '' '' '' ''."列/名称/可编辑/参考表/参考字段/图标/复选框/例程转换
  set_fieldcat 'VDATU' '有效时间从' '' '' '' '' '' ''."列/名称/可编辑/参考表/参考字段/图标/复选框/例程转换
  set_fieldcat 'BDATU' '有效时间到' '' '' '' '' '' ''."列/名称/可编辑/参考表/参考字段/图标/复选框/例程转换
  set_fieldcat 'STATUS' '状态' '' '' '' 'X' '' ''."列/名称/可编辑/参考表/参考字段/图标/复选框/例程转换
  set_fieldcat 'TEXT' '消息' '' '' '' '' '' ''."列/名称/可编辑/参考表/参考字段/图标/复选框/例程转换
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_set_layout
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_set_layout .
  gs_layout-cwidth_opt = 'X'.
  gs_layout-box_fname = 'SEL'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_show_alv
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_show_alv .
  FIELD-SYMBOLS <fs_table> TYPE table .

  ASSIGN gt_alv TO <fs_table> .



  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program       = sy-cprog
      i_callback_pf_status_set = 'FRM_SUB_STATUS_SET'
      i_callback_user_command  = 'FRM_SUB_USER_COMMAND'
      is_layout_lvc            = gs_layout
      it_fieldcat_lvc          = gt_fieldcat
      i_save                   = 'A'
    TABLES
      t_outtab                 = <fs_table>
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_SUB_STATUS_SET
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM frm_sub_status_set USING rt_extab TYPE slis_t_extab.
  SET PF-STATUS 'STATUS'.
  SET TITLEBAR 'TITLE'.
ENDFORM.                    "SUB_STATUS_SET

*&---------------------------------------------------------------------*
*&      Form  FRM_SUB_USER_COMMAND
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM frm_sub_user_command USING s_ucomm TYPE sy-ucomm
                                re_selfield TYPE slis_selfield.

  re_selfield-col_stable = 'X'.
  re_selfield-row_stable = 'X'.
  re_selfield-refresh = 'X'.
  CASE s_ucomm.
    WHEN 'ZSAVE'.
      PERFORM frm_save_data."数据保存
  ENDCASE.
ENDFORM.                    "SUB_USER_COMMAND
*&---------------------------------------------------------------------*
*& Form frm_save_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_save_data .

  DATA:lt_eordu LIKE TABLE OF eordu WITH HEADER LINE.
  LOOP AT gt_alv ASSIGNING FIELD-SYMBOL(<fs_alv>) WHERE sel EQ 'X'
                                                    AND status NE icon_green_light.

    AT NEW matnr.
      CLEAR:lt_eordu[],lt_eordu.
    ENDAT.

    MOVE-CORRESPONDING <fs_alv> TO lt_eordu.

    lt_eordu-erdat = sy-datum.
    lt_eordu-ernam = sy-uname.
    lt_eordu-kz = 'I'.
    APPEND lt_eordu.

    AT END OF matnr.
      CALL FUNCTION 'ME_INITIALIZE_SOURCE_LIST'.

      CALL FUNCTION 'ME_DIRECT_INPUT_SOURCE_LIST'
        EXPORTING
          i_matnr          = lt_eordu-matnr
          i_werks          = lt_eordu-werks
        TABLES
          t_eord           = lt_eordu[]
        EXCEPTIONS
          plant_missing    = 1
          material_missing = 2
          OTHERS           = 3.
      IF sy-subrc NE 0.
        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
        <fs_alv>-status = icon_red_light.
        CONCATENATE sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 INTO <fs_alv>-text SEPARATED BY ','.
      ELSE.
        CALL FUNCTION 'ME_POST_SOURCE_LIST_NEW'
          EXPORTING
            i_matnr = lt_eordu-matnr.

        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait = 'X'.
        <fs_alv>-status = icon_green_light.
        <fs_alv>-text = '保存成功'.
      ENDIF.
    ENDAT.
  ENDLOOP.
  IF sy-subrc NE 0.
    MESSAGE '请选中未保存成功的行进行操作,请确认' TYPE 'E'.
  ENDIF.
ENDFORM.

 

posted @ 2023-09-06 08:06  阿胖的阿多  阅读(239)  评论(0编辑  收藏  举报