*&---------------------------------------------------------------------*
*& Report ZMMR127
*&---------------------------------------------------------------------*
*& Author : NathanSun
*& Create Date : 2019-05-27
*& Program Type : Report
*& Description : 更新物料主数据
*&-------------------------------------------------------------------
REPORT ZMMR127.
TABLES:SSCRFIELDS.
TYPE-POOLS:SLIS.
"定义ALV输出所需变量
DATA: S_LAYOUT TYPE SLIS_LAYOUT_ALV,
GT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
DATA: LS_STYLE TYPE LVC_S_STYL,
LT_STYLE TYPE TABLE OF LVC_S_STYL.
DATA RETURN LIKE BAPIRET2 OCCURS 0 WITH HEADER LINE.
DATA: GT_EXDATA TYPE ALSMEX_TABLINE OCCURS 0 WITH HEADER LINE.
DATA: IW_CELLCOLOR TYPE LVC_T_SCOL WITH HEADER LINE.
DATA L_FILENAME TYPE STRING .
DATA L_MUBAN TYPE STRING .
DATA GV_FULLPATH TYPE STRING .
DATA GV_PATH TYPE STRING .
DATA GV_NAME TYPE STRING .
DATA:MESSAGE TYPE BAPI_MSG .
DATA:SUBRC LIKE SYST-SUBRC .
DATA:FLAG TYPE C .
DATA:KSTBM TYPE KSTBM .
DATA:MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.
TYPES: BEGIN OF GTS_DATA,
MATNR TYPE BDCDATA-FVAL, "物料
WERKS TYPE BDCDATA-FVAL, "工厂
MMSTA TYPE BDCDATA-FVAL, "工厂特定的物料状态
NCOST TYPE BDCDATA-FVAL, "无成本核算
ZPLP1 TYPE BDCDATA-FVAL, "计划价格 1
ZPLD1 TYPE BDCDATA-FVAL, "计划价格日期 1
ZPLP2 TYPE BDCDATA-FVAL, "计划价格 2
ZPLD2 TYPE BDCDATA-FVAL, "计划价格日期 2
ZPLP3 TYPE BDCDATA-FVAL, "计划价格 3
ZPLD3 TYPE BDCDATA-FVAL, "计划价格日期 3
TYPBO TYPE CHAR4, "消息图标
MESSAGE TYPE BAPI_MSG, "报错信息
STYLE TYPE LVC_T_STYL, "控制不可编辑
END OF GTS_DATA.
DATA: FLAG1 TYPE C.
DATA: GT_DATA TYPE TABLE OF GTS_DATA WITH HEADER LINE.
DATA: GT_DATA_A TYPE TABLE OF GTS_DATA WITH HEADER LINE.
SELECTION-SCREEN: FUNCTION KEY 1.
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME.
PARAMETERS: P_FILE LIKE RLGRAP-FILENAME .
SELECTION-SCREEN END OF BLOCK B1.
IF P_FILE IS INITIAL .
MESSAGE '没有选择文件!' TYPE 'E'.
ENDIF.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
MASK = ',Excel Files,*.xls,All Files,*.*.'(101)
TITLE = '选择文件'(100)
IMPORTING
FILENAME = P_FILE
EXCEPTIONS
INV_WINSYS = 1
NO_BATCH = 2
SELECTION_CANCEL = 3
SELECTION_ERROR = 4
OTHERS = 5.
IF SY-SUBRC <> 0 AND SY-SUBRC <> 3.
MESSAGE '选择文件出错!' TYPE 'E'.
ENDIF.
*&---------------------------------------------------------------------*
*& Form FOM_GETDATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FOM_GETDATA .
CLEAR GT_EXDATA[].
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE' "读取excel文件中的内容
EXPORTING
FILENAME = P_FILE
I_BEGIN_COL = '1'
I_BEGIN_ROW = '2'
I_END_COL = '50'
I_END_ROW = '5000'
TABLES
INTERN = GT_EXDATA[].
SORT GT_EXDATA BY ROW COL VALUE.
**---整理到内表数据
CLEAR GT_DATA[] .
LOOP AT GT_EXDATA.
CLEAR FLAG .
AT END OF ROW .
FLAG = 'X'.
ENDAT .
CASE GT_EXDATA-COL.
WHEN 1.
GT_DATA-MATNR = GT_EXDATA-VALUE.
WHEN 2.
GT_DATA-WERKS = GT_EXDATA-VALUE.
WHEN 3.
GT_DATA-MMSTA = GT_EXDATA-VALUE.
WHEN 4.
GT_DATA-NCOST = GT_EXDATA-VALUE.
WHEN 5.
GT_DATA-ZPLP1 = GT_EXDATA-VALUE.
WHEN 6.
GT_DATA-ZPLD1 = GT_EXDATA-VALUE.
WHEN 7.
GT_DATA-ZPLP2 = GT_EXDATA-VALUE.
WHEN 8.
GT_DATA-ZPLD2 = GT_EXDATA-VALUE.
WHEN 9.
GT_DATA-ZPLP3 = GT_EXDATA-VALUE.
WHEN 10.
GT_DATA-ZPLD3 = GT_EXDATA-VALUE.
ENDCASE.
IF FLAG IS NOT INITIAL.
APPEND GT_DATA."append要加在最后一列.
CLEAR GT_DATA.
ENDIF.
ENDLOOP.
ENDFORM.
FORM FOM_UPDATA .
*字段RCTMS-MWERT(5)不是一个输入字段
LOOP AT GT_DATA.
CLEAR SUBRC .
CLEAR MESSAGE .
CLEAR MESSTAB[] .
CALL FUNCTION 'ZF_MM02_03'
EXPORTING
CTU = 'X'
MODE = 'N'
UPDATE = 'L'
NODATA = '/'
MATNR_001 = GT_DATA-MATNR
KZSEL_01_002 = 'X'
KZSEL_02_003 = 'X'
WERKS_004 = GT_DATA-WERKS
NCOST_005 = GT_DATA-NCOST
MMSTA_006 = GT_DATA-MMSTA
ZPLP1_007 = GT_DATA-ZPLP1
ZPLD1_008 = GT_DATA-ZPLD1
ZPLP2_009 = GT_DATA-ZPLP2
ZPLD2_010 = GT_DATA-ZPLD2
ZPLP3_011 = GT_DATA-ZPLP3
ZPLD3_012 = GT_DATA-ZPLD3
IMPORTING
SUBRC = SUBRC
MESSAGE = MESSAGE
TABLES
MESSTAB = MESSTAB[].
.
IF MESSAGE IS NOT INITIAL .
GT_DATA-TYPBO = '@0A@'.
CLEAR LT_STYLE[] .
LS_STYLE-FIELDNAME = 'TYPBO'.
LS_STYLE-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_BUTTON.
APPEND LS_STYLE TO LT_STYLE.
GT_DATA-STYLE = LT_STYLE.
GT_DATA-MESSAGE = MESSAGE .
ELSE.
GT_DATA-TYPBO = '@08@'.
CLEAR LT_STYLE[] .
LS_STYLE-FIELDNAME = 'TYPBO'.
LS_STYLE-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_BUTTON.
APPEND LS_STYLE TO LT_STYLE.
GT_DATA-STYLE = LT_STYLE.
LOOP AT MESSTAB WHERE MSGV1 IS NOT INITIAL.
GT_DATA-MESSAGE = MESSTAB-MSGV1 .
CLEAR MESSTAB .
IF GT_DATA-MESSAGE IS NOT INITIAL.
EXIT .
ENDIF.
ENDLOOP.
* GT_DATA-MESSAGE =
ENDIF.
MODIFY GT_DATA .
CLEAR GT_DATA.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DISPLAY
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_DISPLAY .
CLEAR S_LAYOUT.
S_LAYOUT-ZEBRA = 'X'.
S_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
S_LAYOUT-COLTAB_FIELDNAME = 'CELLCOLOR'. "单元格颜色内表字段
* s_layout-box_fieldname = 'BOX'.
PERFORM FRM_FILL_FIELD .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
IS_LAYOUT = S_LAYOUT
IT_FIELDCAT = GT_FIELDCAT
I_CALLBACK_PROGRAM = SY-REPID
I_CALLBACK_PF_STATUS_SET = 'PF_STATUS_SET'
* i_callback_html_top_of_page = 'HTML_TOP_OF_PAGE'
I_CALLBACK_USER_COMMAND = 'USER_COMMAND'
TABLES
T_OUTTAB = GT_DATA[].
ENDFORM.
FORM FRM_FILL_FIELD .
DATA: WA_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
DEFINE FILL_FIELD.
WA_FIELDCAT-FIELDNAME = &1.
WA_FIELDCAT-SELTEXT_L = &2.
* IF WA_FIELDCAT-FIELDNAME = 'MSLJH' OR WA_FIELDCAT-FIELDNAME = 'WCBJH'.
* WA_FIELDCAT-EMPHASIZE = 'C600'. "设置字段的颜色
* ENDIF.
*
* IF WA_FIELDCAT-FIELDNAME = 'MSLMB' OR WA_FIELDCAT-FIELDNAME = 'WCBMB'.
* WA_FIELDCAT-EMPHASIZE = 'C710'. "设置字段的颜色
* ENDIF.l
IF WA_FIELDCAT-FIELDNAME = 'TYPBO'
OR WA_FIELDCAT-FIELDNAME = 'TYPAI'
.
WA_FIELDCAT-ICON = 'X'.
WA_FIELDCAT-OUTPUTLEN = '13'.
ENDIF.
IF WA_FIELDCAT-FIELDNAME = 'PLNBEZ' OR WA_FIELDCAT-FIELDNAME = 'MATNR' .
WA_FIELDCAT-REF_TABNAME = 'MARA'.
WA_FIELDCAT-REF_FIELDNAME = 'MATNR'.
ENDIF.
IF WA_FIELDCAT-FIELDNAME = 'ARKTX' OR WA_FIELDCAT-FIELDNAME = 'WAKTX' .
WA_FIELDCAT-REF_TABNAME = 'VBAP'.
WA_FIELDCAT-REF_FIELDNAME = 'ARKTX'.
ENDIF.
IF WA_FIELDCAT-FIELDNAME = 'AUFNR' .
WA_FIELDCAT-REF_TABNAME = 'AUFK'.
WA_FIELDCAT-REF_FIELDNAME = 'AUFNR'.
ENDIF.
APPEND WA_FIELDCAT TO GT_FIELDCAT.
CLEAR WA_FIELDCAT.
END-OF-DEFINITION.
FILL_FIELD :
'MATNR' '物料',
'WERKS' '工厂',
'MMSTA' '工厂特定的物料状态',
'NCOST' '无成本核算',
'ZPLP1' '计划价格 1',
'ZPLD1' '计划价格日期 1',
'ZPLP2' '计划价格 2',
'ZPLD2' '计划价格日期 2',
'ZPLP3' '计划价格 3',
'ZPLD3' '计划价格日期 3',
'TYPBO' '是否成功',
'MESSAGE' '信息'.
ENDFORM.
FORM PF_STATUS_SET USING RT_EXTAB TYPE SLIS_T_EXTAB.
SET PF-STATUS 'STANDARD_FULLSCREEN'.
ENDFORM.
FORM USER_COMMAND USING I_UCOMM LIKE SY-UCOMM
IS_SELFIELD TYPE SLIS_SELFIELD.
DATA: REF TYPE REF TO CL_GUI_ALV_GRID.
DATA : LV_STABLE TYPE LVC_S_STBL. "刷新稳定性
RANGES: L_WERKS FOR T001W-WERKS.
RANGES: L_MATNR FOR MARA-MATNR.
DATA:L_BUKRS TYPE T001K-BUKRS.
LV_STABLE-ROW = '1'.
LV_STABLE-COL = '1'.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
E_GRID = REF.
CALL METHOD REF->CHECK_CHANGED_DATA.
CASE I_UCOMM.
WHEN 'GO'."表示双击
WHEN 'POST_T'.
ENDCASE.
CALL METHOD REF->REFRESH_TABLE_DISPLAY
EXPORTING
IS_STABLE = LV_STABLE.
ENDFORM.
*--------------------------------------------------------------------*
* 获取下载到本地的路径
*--------------------------------------------------------------------*
FORM FRM_GET_FULLPATH CHANGING PV_FULLPATH TYPE STRING
PV_PATH TYPE STRING
PV_NAME TYPE STRING.
DATA: LV_INIT_PATH TYPE STRING,
LV_INIT_FNAME TYPE STRING,
LV_PATH TYPE STRING,
LV_FILENAME TYPE STRING,
LV_FULLPATH TYPE STRING.
*&---初始名称(输出的文件名称)
* concatenate 'Material_Doc_' SY-DATUM '.xslx' into L_INIT_FNAME.
LV_INIT_FNAME = L_FILENAME.
* 获取桌面路径
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 = '指定保存文件名'
* default_extension = 'DOC'
DEFAULT_FILE_NAME = LV_INIT_FNAME
FILE_FILTER = CL_GUI_FRONTEND_SERVICES=>FILETYPE_EXCEL
* FILE_FILTER = CL_GUI_FRONTEND_SERVICES=>FILETYPE_WORD
INITIAL_DIRECTORY = LV_INIT_PATH
PROMPT_ON_OVERWRITE = 'X'
CHANGING
FILENAME = LV_FILENAME
PATH = LV_PATH
FULLPATH = LV_FULLPATH
* USER_ACTION =
* FILE_ENCODING =
EXCEPTIONS
CNTL_ERROR = 1
ERROR_NO_GUI = 2
NOT_SUPPORTED_BY_GUI = 3
OTHERS = 4.
IF SY-SUBRC = 0.
PV_FULLPATH = LV_FULLPATH.
PV_PATH = LV_PATH.
ENDIF.
ENDFORM.
*--------------------------------------------------------------------*
* 下载文件
*--------------------------------------------------------------------*
FORM FRM_DOWN USING PR_FILENAME.
DATA: LV_OBJDATA LIKE WWWDATATAB,
LV_MIME LIKE W3MIME,
LV_DESTINATION LIKE RLGRAP-FILENAME,
LV_OBJNAM TYPE STRING,
LV_RC LIKE SY-SUBRC,
LV_ERRTXT TYPE STRING.
DATA: LV_FILENAME TYPE STRING,
LV_RESULT,
LV_SUBRC TYPE SY-SUBRC.
DATA: LV_OBJID TYPE WWWDATATAB-OBJID .
LV_OBJID = L_MUBAN. "上传的模版名称
*&---查找文件是否存在。
SELECT SINGLE RELID OBJID
FROM WWWDATA
INTO CORRESPONDING FIELDS OF LV_OBJDATA
WHERE SRTF2 = 0
AND RELID = 'MI'
AND OBJID = LV_OBJID.
*&---判断模版不存在则报错
IF SY-SUBRC NE 0 OR LV_OBJDATA-OBJID EQ SPACE.
CONCATENATE '模板文件:' LV_OBJID '不存在,请用TCODE:SMW0进行加载'
INTO LV_ERRTXT.
MESSAGE LV_ERRTXT TYPE 'E'.
ENDIF.
LV_FILENAME = PR_FILENAME.
"判断本地地址是否已经存在此文件。
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_EXIST
EXPORTING
FILE = LV_FILENAME
RECEIVING
RESULT = LV_RESULT
EXCEPTIONS
CNTL_ERROR = 1
ERROR_NO_GUI = 2
WRONG_PARAMETER = 3
NOT_SUPPORTED_BY_GUI = 4
OTHERS = 5.
IF SY-SUBRC <> 0.
CASE SY-SUBRC.
WHEN 1.
WHEN 2.
WHEN OTHERS.
ENDCASE.
ENDIF.
IF LV_RESULT EQ 'X'. "如果存在则删除原始文件,重新覆盖
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_DELETE
EXPORTING
FILENAME = LV_FILENAME
CHANGING
RC = LV_SUBRC
EXCEPTIONS
FILE_DELETE_FAILED = 1
CNTL_ERROR = 2
ERROR_NO_GUI = 3
FILE_NOT_FOUND = 4
ACCESS_DENIED = 5
UNKNOWN_ERROR = 6
NOT_SUPPORTED_BY_GUI = 7
WRONG_PARAMETER = 8
OTHERS = 9.
IF SY-SUBRC <> 0.
CASE SY-SUBRC.
WHEN 1.
WHEN 2.
WHEN OTHERS.
ENDCASE.
ENDIF.
IF LV_SUBRC <> 0. "如果删除失败,则报错。
CONCATENATE '同名EXCEL文件已打开' '请关闭该EXCEL后重试。'
INTO LV_ERRTXT.
MESSAGE LV_ERRTXT TYPE 'E'.
ENDIF.
ENDIF.
LV_DESTINATION = PR_FILENAME.
*&---下载模版。
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
EXPORTING
KEY = LV_OBJDATA
DESTINATION = LV_DESTINATION
IMPORTING
RC = LV_RC.
IF LV_RC NE 0.
CONCATENATE '模板文件' '下载失败' INTO LV_ERRTXT.
MESSAGE LV_ERRTXT TYPE 'E'.
ENDIF.
ENDFORM.
INITIALIZATION.
SSCRFIELDS-FUNCTXT_01 = '@49@ 模板下载'.
AT SELECTION-SCREEN .
CASE SSCRFIELDS-UCOMM.
WHEN 'FC01'.
L_FILENAME = '更新物料主数据导入模板.xls'.
L_MUBAN = 'ZMMR127'.
*&---下载模板
PERFORM FRM_GET_FULLPATH CHANGING GV_FULLPATH GV_PATH GV_NAME.
*&---路径为空则退出
IF GV_FULLPATH IS INITIAL.
MESSAGE '用户取消操作' TYPE 'S'.
RETURN.
ENDIF.
PERFORM FRM_DOWN USING GV_FULLPATH.
WHEN OTHERS.
ENDCASE.
START-OF-SELECTION.
PERFORM FOM_GETDATA.
PERFORM FOM_UPDATA.
PERFORM FRM_DISPLAY.
MM-更新物料主数据(开发笔记)
最新推荐文章于 2022-05-20 08:50:18 发布