***********************************************************************
* Title : ZPPR101 *
* Application : PP *
* Subject : 批量更改BOM组件 *
* Requested by : PP *
* Execution : any time *
* Ref no: : B221 *
* Author : rainy *
* Req Date : 20150701 *
***********************************************************************
* Production Order Variance Report *
***********************************************************************
***********************************************************************
*1.取数: Excel 上传数据并对数据bom 批量更改BOM组件
*2.输出: *
***********************************************************************
* MODIFICATIONS (latest entry at the top) *
* ------------------------------------------------------------------- *
* REL DATE NAME (COMPANY) DESCRIPTION TASK-NO *
* --- ---- ---- --------- ----------- ------- *
* *
***********************************************************************
REPORT zppr101 MESSAGE-ID zmm_001.
TABLES:marc,
stpo,
sscrfields..
DATA:BEGIN OF it_text,
matnr LIKE marc-matnr,
werks LIKE marc-werks,
idnrk LIKE stpo-idnrk,"原始组件
idnrk_n LIKE stpo-idnrk,"更改值
meins LIKE stpo-meins,"更改单位
text TYPE string,
zicon TYPE char4,
END OF it_text.
DATA:gt_text LIKE TABLE OF it_text WITH HEADER LINE,
wa_text LIKE gt_text.
*----------------------------------------------------------------------
* Data parameters for alv report use
*----------------------------------------------------------------------
DATA: g_program TYPE sy-repid,
gw_layout TYPE slis_layout_alv,
gt_fieldcat TYPE slis_t_fieldcat_alv,
wa_fieldcat TYPE slis_fieldcat_alv,
gt_event TYPE slis_t_event,
wa_event TYPE slis_alv_event.
DATA: g_grid TYPE REF TO cl_gui_alv_grid.
DATA l_len TYPE i.
************************************************************************
* Select Screen *
************************************************************************
*SELECTION-SCREEN BEGIN OF BLOCK block1 WITH FRAME TITLE text-001 . "‘请指定选择条件‘.
*"BOM 选择条件
*PARAMETERS p_plant LIKE csap_mbom-werks.
*PARAMETERS p_usage LIKE csap_mbom-stlan.
*PARAMETERS p_alt LIKE csap_mbom-stlal.
*PARAMETERS p_datuv LIKE csap_mbom-datuv .
*SELECTION-SCREEN END OF BLOCK block1.
SELECTION-SCREEN SKIP.
SELECTION-SCREEN BEGIN OF BLOCK block2 WITH FRAME TITLE t1. " ‘请指定本地EXCEL文件‘
PARAMETERS: p_name LIKE rlgrap-filename." OBLIGATORY.
SELECTION-SCREEN END OF BLOCK block2.
SELECTION-SCREEN FUNCTION KEY 1.
INITIALIZATION. " 初始化选择屏上的变量
t1 = ‘导入数据‘.
sscrfields-functxt_01 = ‘下载导入模板‘.
AT SELECTION-SCREEN.
*&下载模板
PERFORM frm_download_template.
*&读取文件
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_name.
PERFORM getdata USING p_name.
* CALL FUNCTION ‘WS_FILENAME_GET‘
* EXPORTING
* def_path = ‘C:\‘
* mask = ‘,DAT(*.DATA),*.DAT.‘
*
"MASK = ‘,Excel文件(*.XLS),*.XLS.‘
** MODE = ‘O‘
** TITLE = ‘ ‘
* IMPORTING
* filename = p_name
** RC =
* EXCEPTIONS
* inv_winsys = 1
* no_batch = 2
* selection_cancel = 3
* selection_error = 4
* OTHERS = 5.
*&---------------------------------------------------------------------*
*& 主程序
*&---------------------------------------------------------------------*
START-OF-SELECTION.
*&对工厂操作权限检查
* PERFORM plant_authority_check USING p_plant.
*&S1.读取EXCEL 数据
PERFORM frm_get_excel.
*&S2.更改BOM
PERFORM frm_maintain_bom.
*&S3.list
PERFORM frm_alv_grid.
*&---------------------------------------------------------------------*
*& Form PLANT_AUTHORITY_CHECK
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_PLANT text
*----------------------------------------------------------------------*
FORM plant_authority_check USING p_werks.
* AUTHORITY-CHECK OBJECT ‘C_STUE_WRK‘
* ID ‘ACTVT‘ FIELD ‘03‘
* ID ‘CSWRK‘ FIELD p_werks.
*
* IF sy-subrc <> 0.
* MESSAGE e001(00) WITH ‘你没有工厂 ‘ p_werks ‘ 的权限.‘.
* ENDIF.
ENDFORM. "PLANT_AUTHORITY_CHECK
*&---------------------------------------------------------------------*
*& Form FRM_DOWNLOAD_TEMPLATE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_download_template .
DATA: ls_key TYPE wwwdatatab,
lv_temp TYPE c,
lv_file TYPE string VALUE ‘\ZP101.xlsx‘,
lv_filepath TYPE rlgrap-filename,
lv_result TYPE abap_bool,
lv_path TYPE string,
lv_msg TYPE string.
DATA:l_rc TYPE sy-subrc.
TRANSLATE lv_msg TO UPPER CASE.
*REPLACE
* REPLACE [{first occurrence}|{all occurrences} OF] PATTERN
* IN [section_of] dobj WITH NEW
* [IN {character|BYTE} MODE]
* [replace_options].
CASE sscrfields-ucomm.
WHEN ‘FC01‘.
CALL METHOD cl_gui_frontend_services=>directory_browse
EXPORTING
window_title = ‘选择文件目录‘
* initial_folder = ‘C:\TEMP‘
CHANGING
selected_folder = lv_path
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.
CONCATENATE lv_path lv_file INTO lv_file.
CALL METHOD cl_gui_frontend_services=>file_exist
EXPORTING
file = lv_file
RECEIVING
result = lv_result
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
wrong_parameter = 3
not_supported_by_gui = 4
OTHERS = 5.
IF lv_result = ‘X‘.
CONCATENATE ‘模板‘ lv_file ‘已存在!‘ INTO lv_msg.
MESSAGE lv_msg TYPE ‘S‘.
STOP.
ELSE.
ls_key-relid = ‘MI‘.
ls_key-objid = ‘ZP101‘. "SMW0定义的对象名称
lv_filepath = lv_file.
CALL FUNCTION ‘DOWNLOAD_WEB_OBJECT‘
EXPORTING
key = ls_key
destination = lv_filepath
IMPORTING
rc = l_rc.
IF l_rc <> 0.
MESSAGE ‘下载模板失败!‘ TYPE ‘E‘.
ELSE.
MESSAGE s000 WITH ‘成功下载模板至目录:‘ lv_filepath.
ENDIF.
ENDIF.
ENDIF.
ENDCASE.
ENDFORM. " FRM_DOWNLOAD_TEMPLATE
*&---------------------------------------------------------------------*
*& Form FRM_GET_EXCEL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_get_excel .
DATA:BEGIN OF i_excel OCCURS 0.
INCLUDE STRUCTURE alsmex_tabline.
DATA:END OF i_excel.
DATA:l_answer(1) TYPE c,
l_culumn TYPE i,
l_pathname LIKE rlgrap-filename.
FIELD-SYMBOLS:<fs>.
DATA:it_raw TYPE truxs_t_text_data.
*&文件名赋值l_pathname.
MOVE p_name TO l_pathname.
CALL FUNCTION ‘TEXT_CONVERT_XLS_TO_SAP‘
EXPORTING
* I_FIELD_SEPERATOR =
i_line_header = ‘X‘
i_tab_raw_data = it_raw
i_filename = l_pathname
TABLES
i_tab_converted_data = gt_text.
* EXCEPTIONS
* CONVERSION_FAILED = 1
* OTHERS = 2
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.
IF gt_text[] IS INITIAL.
CALL FUNCTION ‘POPUP_TO_CONFIRM_WITH_MESSAGE‘
EXPORTING
* DEFAULTOPTION = ‘Y‘
diagnosetext1 = ‘No data in excel‘
* DIAGNOSETEXT2 = ‘ ‘
* DIAGNOSETEXT3 = ‘ ‘
textline1 = ‘No data in excel‘
* TEXTLINE2 = ‘ ‘
titel = ‘Confirm‘
* START_COLUMN = 25
* START_ROW = 6
* CANCEL_DISPLAY = ‘X‘
IMPORTING
answer = l_answer.
*&OUT
EXIT.
ENDIF.
ENDIF.
ENDFORM. " FRM_GET_EXCEL
*&---------------------------------------------------------------------*
*& Form FRM_MAINTAIN_BOM
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_maintain_bom .
DATA:lv_line TYPE string,
lv_false TYPE c.
DATA:lt_stpo TYPE STANDARD TABLE OF stpo_api02 WITH HEADER LINE.
DATA:
it_stpo TYPE TABLE OF stpo_api03,
is_stpo TYPE stpo_api03,
is_stko TYPE stko_api01,
lt_stko TYPE TABLE OF stko_api02,
ls_stpo TYPE stpo_api02,
ls_stko TYPE stko_api02,
l_prio TYPE alprf_bi,
l_datuv TYPE csap_mbom-datuv.
DATA:is_stko_add TYPE stko_api01,
is_stpo_add TYPE stpo_api02,
it_stpo_add TYPE TABLE OF stpo_api03.
LOOP AT gt_text.
*&L1.为物料号补0
CALL FUNCTION ‘CONVERSION_EXIT_MATN1_INPUT‘
EXPORTING
input = gt_text-matnr
IMPORTING
output = gt_text-matnr.
*&L2.组件号补0
CALL FUNCTION ‘CONVERSION_EXIT_MATN1_INPUT‘
EXPORTING
input = gt_text-idnrk
IMPORTING
output = gt_text-idnrk.
*&条目
lv_line = lv_line + 1.
CLEAR:lv_false.
*&L3.read BOM
CLEAR:lt_stpo,lt_stpo[],
lt_stko,
it_stpo,it_stpo[],
is_stko,
it_stpo_add,it_stpo_add[],
is_stko_add.
CLEAR:l_datuv.
*&读取bom原始数据
CALL FUNCTION ‘CSAP_MAT_BOM_READ‘
EXPORTING
material = gt_text-matnr
plant = gt_text-werks
bom_usage = ‘1‘
alternative = ‘ ‘
TABLES
t_stpo = lt_stpo
t_stko = lt_stko
EXCEPTIONS
error = 1
OTHERS = 2.
IF sy-subrc = 0.
READ TABLE lt_stko INTO ls_stko INDEX 1.
MOVE-CORRESPONDING ls_stko TO is_stko.
MOVE-CORRESPONDING ls_stko TO is_stko_add.
l_datuv = ls_stko-created_on.
LOOP AT lt_stpo INTO ls_stpo.
READ TABLE gt_text WITH KEY idnrk = ls_stpo-component. "werks = ls_stpo-plant.
IF sy-subrc <> 0.
DELETE lt_stpo.
CONTINUE.
ELSE.
* IF ls_stpo-ai_group IS INITIAL.
* DELETE lt_stpo.
* CONTINUE.
* ENDIF.
*&修改组件物料号+单位
* ls_stpo-component = gt_text-idnrk_n.
* ls_stpo-comp_unit = gt_text-meins.
ls_stpo-fldelete = ‘X‘.
is_stpo_add-component = gt_text-idnrk_n.
is_stpo_add-comp_unit = gt_text-meins.
ENDIF.
* IF sy-tabix = 1.
* l_prio = sy-tabix.
* ls_stpo-ai_prio = l_prio.
* ls_stpo-usage_prob = ‘100‘.
* ls_stpo-ai_strateg = ‘2‘.
* ELSE.
* l_prio = sy-tabix.
* ls_stpo-ai_prio = l_prio.
* ls_stpo-usage_prob = ‘0‘.
* ls_stpo-ai_strateg = ‘1‘.
* ENDIF.
MOVE-CORRESPONDING ls_stpo TO is_stpo.
APPEND is_stpo TO it_stpo.
MOVE-CORRESPONDING ls_stpo TO is_stpo_add.
APPEND is_stpo_add TO it_stpo_add.
ENDLOOP.
ENDIF.
*&1.删除
*& 先将组件项目 打上删除标记
CALL FUNCTION ‘CSAP_MAT_BOM_MAINTAIN‘
EXPORTING
material = gt_text-matnr
plant = gt_text-werks
bom_usage = ‘1‘
alternative = ‘ ‘
valid_from = l_datuv
i_stko = is_stko
fl_commit_and_wait = ‘X‘
* fl_new_item = ‘X‘
TABLES
t_stpo = it_stpo
EXCEPTIONS
error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid
TYPE sy-msgty
NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 INTO gt_text-text.
* CONCATENATE gt_text-werks ‘-‘ gt_text-matnr ‘bom组件更改失败!‘ INTO gt_text-text.
gt_text-zicon = ‘@[email protected]‘.
ELSE.
gt_text-text = ‘更改成功!‘.
gt_text-zicon = ‘@[email protected]‘.
*&2.新增
CALL FUNCTION ‘CSAP_MAT_BOM_MAINTAIN‘
EXPORTING
material = gt_text-matnr
plant = gt_text-werks
bom_usage = ‘1‘
alternative = ‘ ‘
valid_from = l_datuv
i_stko = is_stko_add
fl_commit_and_wait = ‘X‘
* fl_new_item = ‘X‘
TABLES
t_stpo = it_stpo_add
EXCEPTIONS
error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid
TYPE sy-msgty
NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 INTO gt_text-text.
* CONCATENATE gt_text-werks ‘-‘ gt_text-matnr ‘bom组件更改失败!‘ INTO gt_text-text.
gt_text-zicon = ‘@[email protected]‘.
ELSE.
gt_text-text = ‘更改成功!‘.
gt_text-zicon = ‘@[email protected]‘.
ENDIF.
ENDIF.
MODIFY gt_text.
ENDLOOP.
ENDFORM. " FRM_MAINTAIN_BOM
*&---------------------------------------------------------------------*
*& Form FRM_ALV_GRID
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_alv_grid .
DATA: l_nn TYPE i.
DEFINE add_fieldcat.
clear wa_fieldcat.
wa_fieldcat-fieldname = &1.
wa_fieldcat-seltext_l = &2.
wa_fieldcat-key = &3.
wa_fieldcat-col_pos = l_nn + 1.
wa_fieldcat-just = &4.
wa_fieldcat-outputlen = &5.
wa_fieldcat-fix_column = &6.
wa_fieldcat-no_zero = &7.
wa_fieldcat-edit = &8.
append wa_fieldcat to gt_fieldcat.
END-OF-DEFINITION.
CLEAR gt_fieldcat.
REFRESH gt_fieldcat.
add_fieldcat ‘ZICON‘ ‘指示灯‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘.
add_fieldcat ‘MATNR‘ ‘物料‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘.
add_fieldcat ‘WERKS‘ ‘工厂‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘.
add_fieldcat ‘IDNRK‘ ‘原始组件‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘.
add_fieldcat ‘IDNRK_N‘ ‘更改值‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘.
add_fieldcat ‘MEINS‘ ‘单位‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘.
add_fieldcat ‘TEXT‘ ‘消息‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘.
gw_layout-colwidth_optimize = ‘X‘.
* gw_layout-box_fieldname = ‘SEL‘.
*& 调用函数显示ALV列表
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY‘
EXPORTING
i_callback_program = g_program
i_save = ‘A‘
is_layout = gw_layout
it_fieldcat = gt_fieldcat
it_events = gt_event
TABLES
t_outtab = gt_text
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDFORM. " FRM_ALV_GRID
*&---------------------------------------------------------------------*
*& Form GETDATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_p_name text
*----------------------------------------------------------------------*
FORM getdata USING p_name.
* Data for open dialog
DATA: l_filetab TYPE filetable,
l_rc TYPE i.
CLEAR l_filetab.
REFRESH l_filetab.
* Open dialog
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
* WINDOW_TITLE = ‘SAP Custom - Open File‘
* DEFAULT_EXTENSION =
default_filename = ‘*.xls‘
* FILE_FILTER = ‘*.xls‘
initial_directory = ‘d:\‘
multiselection = ‘‘
CHANGING
file_table = l_filetab
rc = l_rc
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.
* Get file path
CHECK l_rc EQ 1.
READ TABLE l_filetab INDEX 1 INTO p_name.
ENDFORM. " GETDATA