PP模块工作中心对应事务码: CR01:创建工作中心
CR02:修改工作中心
CR03:查看工作中心
批量创建工作中心BAPI如下:
创建:CRAP_WORKCENTER_CREATE
修改:CRAP_WORKCENTER_CHANGE
批量创建DEMO程序如下:
*&---------------------------------------------------------------------*
*& Report ZTEST_008
*&---------------------------------------------------------------------*
*&PP工作中心批量创建DEMO
*&---------------------------------------------------------------------*
REPORT ztest_008.
* 类型池
*----------------------------------------------------------------------*
TYPE-POOLS: slis,icon.
*----------------------------------------------------------------------*
* 数据库表声明
*----------------------------------------------------------------------*
TABLES: sscrfields. "选择屏幕上的字段
*----------------------------------------------------------------------*
* 类型声明
*----------------------------------------------------------------------*
TYPES:BEGIN OF ty_alv,
arbpl TYPE crhd-arbpl, "工作中心
ktext TYPE crtx-ktext, "工作中心描述
vgwts TYPE tc21-vgwts, "标准值码
kostl TYPE crco-kostl, "成本中心
icon TYPE icon_d, "红绿灯
message TYPE bapi_msg, "消息
END OF ty_alv.
*----------------------------------------------------------------------*
* 数据对象声明
*----------------------------------------------------------------------*
DATA: gt_out TYPE TABLE OF ty_alv, "Excel上传的数据
gs_out TYPE ty_alv.
"选择界面下载按钮参数
DATA: functxt TYPE smp_dyntxt.
*----------------------------------------------------------------------*
* ALV变量
*----------------------------------------------------------------------*
DATA: gs_layout TYPE lvc_s_layo, " ALV布局
gt_fieldcat TYPE TABLE OF lvc_s_fcat,
gs_fieldcat TYPE lvc_s_fcat.
*----------------------------------------------------------------------*
* FIELD-SYMBOLS声明
*----------------------------------------------------------------------*
FIELD-SYMBOLS: <fs_out> TYPE ty_alv.
*&---------------------------------------------------------------------*
*& Selection Screen/选择屏幕
*&---------------------------------------------------------------------*
*&------选择文件
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
PARAMETERS p_path LIKE rlgrap-filename MEMORY ID m.
SELECTION-SCREEN END OF BLOCK b1.
SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE TEXT-003.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN: COMMENT 1(79) warning1.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN: COMMENT 1(75) warning2.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN: COMMENT 1(75) warning3.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN: COMMENT 1(75) warning4.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN:END OF BLOCK b3.
*&---------------------------------------------------------------------*
*& INITIALIZATION/选择屏幕前初始化
*&---------------------------------------------------------------------*
INITIALIZATION.
SELECTION-SCREEN: FUNCTION KEY 1.""在屏幕定义功能码
"选择屏幕下载按钮
functxt-icon_id = icon_export.
functxt-icon_text = '模板下载'.
sscrfields-functxt_01 = functxt.
warning1 = '【1】请勿对下载模板的列顺序进行变更!'.
warning2 = '【2】请使用下载功能下载模版进行后续操作!'.
warning3 = '【3】请使用日期格式YYYYMMDD ! 例:20220131'.
warning4 = '【4】空白默认不修改该字段! 如需置空,请填写#'.
*&---------------------------------------------------------------------*
*& at selection-screen/选择屏幕开始
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path .
PERFORM frm_f4_filename.
AT SELECTION-SCREEN.
IF sscrfields-ucomm = 'FC01'.
DATA: lv_filename TYPE string,
lv_path TYPE string,
lv_fullpath TYPE string.
PERFORM frm_file_save USING '工作中心批量导入模版.xlsx'
CHANGING lv_filename
lv_path
lv_fullpath.
IF lv_fullpath IS NOT INITIAL.
PERFORM frm_download_template USING lv_fullpath .
ENDIF.
CLEAR sscrfields-ucomm .
ENDIF.
AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN .
IF screen-name = 'P_PATH'.
screen-required = '2'. "假必输
MODIFY SCREEN.
ENDIF.
ENDLOOP.
*&---------------------------------------------------------------------*
*& Start-of-selection/开始选择屏幕
*&---------------------------------------------------------------------*
START-OF-SELECTION.
IF p_path IS INITIAL.
MESSAGE s055(00) DISPLAY LIKE 'E'.
EXIT.
ENDIF.
PERFORM frm_upload_data.
IF gt_out IS INITIAL.
MESSAGE '无数据!' TYPE 'S' DISPLAY LIKE 'E'.
EXIT.
ENDIF.
PERFORM frm_check_data.
PERFORM frm_set_layout.
PERFORM frm_set_fieldcat.
PERFORM frm_display_alv.
*&---------------------------------------------------------------------*
*& Form set_pf_status
*&---------------------------------------------------------------------*
* 定义界面状态栏
*----------------------------------------------------------------------*
* -->RT_EXTAB text
*----------------------------------------------------------------------*
FORM set_pf_status USING rt_extab TYPE slis_t_extab.
SET PF-STATUS 'STATUS'. "自定义状态名称
ENDFORM. "set_pf_status
*&---------------------------------------------------------------------*
*& Form alv_user_command
*&---------------------------------------------------------------------*
* 响应状态触发事件
*----------------------------------------------------------------------*
* -->R_UCOMM text
* -->RS_SELFIELD text
*----------------------------------------------------------------------*
FORM alv_user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
DATA:ref_grid TYPE REF TO cl_gui_alv_grid,
ls_stbl TYPE lvc_s_stbl.
ls_stbl-row = 'X'.
ls_stbl-col = 'X'.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = ref_grid. "获取全局变量
CALL METHOD ref_grid->check_changed_data. "获取响应事件
rs_selfield-refresh = 'X'. "刷新界面
CASE r_ucomm.
WHEN 'ZCL'.
READ TABLE gt_out INTO gs_out WITH KEY icon = icon_red_light.
IF sy-subrc = 0.
CLEAR:gs_out.
MESSAGE '上载的数据中存在错误,请先处理错误再继续维护!' TYPE 'S' DISPLAY LIKE 'E'.
EXIT.
ENDIF.
READ TABLE gt_out INTO gs_out WITH KEY icon = icon_green_light.
IF sy-subrc = 0.
CLEAR:gs_out.
MESSAGE '不可重复处理!' TYPE 'S' DISPLAY LIKE 'E'.
EXIT.
ENDIF.
"创建工作中心
PERFORM frm_call_bapi USING ''.
WHEN OTHERS.
ENDCASE.
CALL METHOD ref_grid->check_changed_data. "获取响应事件
CALL METHOD ref_grid->refresh_table_display
EXPORTING
is_stable = ls_stbl
i_soft_refresh = 'X'
EXCEPTIONS
finished = 1
OTHERS = 2.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_CHECK_DATA
*&---------------------------------------------------------------------*
*& 数据校验
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_check_data .
* CLEAR:gv_error.
* LOOP AT gt_out ASSIGNING <fs_out>.
* IF <fs_out>-matnr IS INITIAL OR <fs_out>-werks IS INITIAL.
* <fs_out>-icon = icon_red_light.
* <fs_out>-message = '物料和工厂必填!'.
* gv_error = 'X'.
* ENDIF.
* CLEAR:<fs_out>.
* ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_F4_FILENAME
*&---------------------------------------------------------------------*
*& 文件搜索帮助
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_f4_filename .
CALL FUNCTION 'F4_FILENAME'
* EXPORTING
* PROGRAM_NAME = SYST-CPROG
* DYNPRO_NUMBER = SYST-DYNNR
* FIELD_NAME = ' '
IMPORTING
file_name = p_path.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_FILE_SAVE
*&---------------------------------------------------------------------*
*& 保存文件弹出窗口
*&---------------------------------------------------------------------*
*& --> P_
*& <-- LV_FILENAME
*& <-- LV_PATH
*& <-- LV_FULLPATH
*&---------------------------------------------------------------------*
FORM frm_file_save USING VALUE(p_text)
CHANGING lv_filename
lv_path
lv_fullpath.
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
* WINDOW_TITLE =
default_extension = 'XLSX'
default_file_name = p_text
* WITH_ENCODING =
* FILE_FILTER =
* INITIAL_DIRECTORY =
* 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
invalid_default_file_name = 4
OTHERS = 5.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DOWNLOAD_TEMPLATE
*&---------------------------------------------------------------------*
*& 下载模板
*&---------------------------------------------------------------------*
*& --> LV_FULLPATH
*&---------------------------------------------------------------------*
FORM frm_download_template USING lv_fullpath.
DATA:ls_key TYPE wwwdatatab,
lv_rc TYPE sy-subrc,
lv_destination LIKE rlgrap-filename.
lv_destination = lv_fullpath .
SELECT SINGLE relid
objid
INTO CORRESPONDING FIELDS OF ls_key
FROM wwwdata
WHERE srtf2 = 0
AND relid = 'MI'
AND objid = 'MM01'.
IF sy-subrc <> 0.
MESSAGE '模板文件 MM01 不存在!' TYPE 'S' DISPLAY LIKE 'E'.
EXIT.
ENDIF.
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
EXPORTING
key = ls_key
destination = lv_destination
IMPORTING
rc = lv_rc
* CHANGING
* TEMP =
.
IF lv_rc <> 0.
MESSAGE '模板文件 MM01 下载失败,请与相关人员联系!' TYPE 'S' DISPLAY LIKE 'E'.
EXIT.
ELSE.
MESSAGE '下载成功!' TYPE 'S'.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_UPLOAD_DATA
*&---------------------------------------------------------------------*
*& 上传Excel数据到内表
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_upload_data .
DATA: lt_exc_data TYPE zalsmex_tabline OCCURS 0 WITH HEADER LINE,
lv_index TYPE sy-tabix.
DATA: BEGIN OF lt_split OCCURS 0,
field TYPE c LENGTH 50,
END OF lt_split.
FIELD-SYMBOLS: <fs_value>.
CLEAR: lt_exc_data[],
gt_out,
gs_out.
CALL FUNCTION 'ZALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = p_path
i_begin_col = 1
i_begin_row = 8
i_end_col = 200
i_end_row = 50000
TABLES
intern = lt_exc_data
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
IF sy-subrc <> 0.
EXIT.
ENDIF.
LOOP AT lt_exc_data.
lv_index = lt_exc_data-col.
ASSIGN COMPONENT lv_index OF STRUCTURE gs_out TO <fs_value>.
MOVE lt_exc_data-value TO <fs_value>.
CLEAR lt_exc_data.
AT END OF row.
APPEND gs_out TO gt_out.
CLEAR gs_out.
ENDAT.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_CALL_BAPI
*&---------------------------------------------------------------------*
*& 创建BOM
*&---------------------------------------------------------------------*
*& --> P_
*&---------------------------------------------------------------------*
FORM frm_call_bapi USING lv_test.
"定义BAPI变量
DATA:ls_in_crhd_api01 TYPE crhd_api01,
ls_in_crhd_api02 TYPE crhd_api02,
ls_in_crhd_api03 TYPE crhd_api03,
lt_in_crco_api01 TYPE TABLE OF crco_api01,
ls_in_crco_api01 TYPE crco_api01.
"进度条变量
DATA:lt_data TYPE TABLE OF ty_alv,
lv_lines TYPE i, "总行数
lv_index TYPE i,
lv_msg TYPE string.
IF gt_out IS NOT INITIAL.
"获取标准值码对应关键值
SELECT vgwts ,
par01 ,
par02 ,
par03 ,
par04 ,
par05 ,
par06
FROM tc21
INTO TABLE @DATA(lt_tc21)
FOR ALL ENTRIES IN @gt_out
WHERE vgwts = @gt_out-vgwts.
SORT lt_tc21 BY vgwts.
"关键值对应单位
SELECT parid,
unit
FROM tc20
INTO TABLE @DATA(lt_tc20).
SORT lt_tc20 BY parid.
ENDIF.
lt_data = gt_out.
SORT lt_data BY arbpl.
DELETE ADJACENT DUPLICATES FROM lt_data COMPARING arbpl.
DESCRIBE TABLE lt_data LINES lv_lines.
CLEAR:lv_index.
LOOP AT lt_data INTO gs_out.
"进度条
lv_index = lv_index + 1.
lv_msg = 'BOM创建处理中...' && lv_index && '/' && lv_lines.
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
percentage = 25
text = lv_msg.
"抬头字段
CLEAR:ls_in_crhd_api01.
ls_in_crhd_api01-arbpl = gs_out-arbpl."工作中心
ls_in_crhd_api01-werks = '1200' ."工厂
ls_in_crhd_api01-verwe = '0002' ."工作中心类型
ls_in_crhd_api01-ktext = gs_out-ktext."工作中心描述
"基本信息
CLEAR:ls_in_crhd_api02.
ls_in_crhd_api02-planv = '003' ."用途
ls_in_crhd_api02-veran = 'FUZR' ."工作中心负责人
ls_in_crhd_api02-vgwts = gs_out-vgwts."标准值码
"默认值
CLEAR:ls_in_crhd_api03.
ls_in_crhd_api03-steus = 'ZKZM'."控制码
"成本核算
CLEAR:ls_in_crco_api01.
ls_in_crco_api01-kostl = gs_out-kostl. "成本中心
ls_in_crco_api01-begda = sy-datum."开始日期
ls_in_crco_api01-endda = '99991231'."结束日期
ls_in_crco_api01-kokrs = '1000'."控制范围
READ TABLE lt_tc21 INTO DATA(ls_tc21) WITH KEY vgwts = gs_out-vgwts BINARY SEARCH.
IF sy-subrc = 0.
IF ls_tc21-par01 IS NOT INITIAL.
ls_in_crco_api01-lstar1 = ls_tc21-par01."活动类型
ls_in_crco_api01-forml1 = ls_tc21-par01."公式码
READ TABLE lt_tc20 INTO DATA(ls_tc20) WITH KEY parid = ls_tc21-par01 BINARY SEARCH.
IF sy-subrc = 0.
ls_in_crhd_api03-vge01 = ls_tc20-unit."默认值 标准值单位
CLEAR:ls_tc20.
ENDIF.
ENDIF.
IF ls_tc21-par02 IS NOT INITIAL.
ls_in_crco_api01-lstar2 = ls_tc21-par02."活动类型
ls_in_crco_api01-forml2 = ls_tc21-par02."公式码
READ TABLE lt_tc20 INTO ls_tc20 WITH KEY parid = ls_tc21-par02 BINARY SEARCH.
IF sy-subrc = 0.
ls_in_crhd_api03-vge02 = ls_tc20-unit."默认值 标准值单位
CLEAR:ls_tc20.
ENDIF.
ENDIF.
IF ls_tc21-par03 IS NOT INITIAL.
ls_in_crco_api01-lstar3 = ls_tc21-par03."活动类型
ls_in_crco_api01-forml3 = ls_tc21-par03."公式码
READ TABLE lt_tc20 INTO ls_tc20 WITH KEY parid = ls_tc21-par03 BINARY SEARCH.
IF sy-subrc = 0.
ls_in_crhd_api03-vge03 = ls_tc20-unit."默认值 标准值单位
CLEAR:ls_tc20.
ENDIF.
ENDIF.
IF ls_tc21-par04 IS NOT INITIAL.
ls_in_crco_api01-lstar4 = ls_tc21-par04."活动类型
ls_in_crco_api01-forml4 = ls_tc21-par04."公式码
READ TABLE lt_tc20 INTO ls_tc20 WITH KEY parid = ls_tc21-par04 BINARY SEARCH.
IF sy-subrc = 0.
ls_in_crhd_api03-vge04 = ls_tc20-unit."默认值 标准值单位
CLEAR:ls_tc20.
ENDIF.
ENDIF.
IF ls_tc21-par05 IS NOT INITIAL.
ls_in_crco_api01-lstar5 = ls_tc21-par05."活动类型
ls_in_crco_api01-forml5 = ls_tc21-par05."公式码
READ TABLE lt_tc20 INTO ls_tc20 WITH KEY parid = ls_tc21-par05 BINARY SEARCH.
IF sy-subrc = 0.
ls_in_crhd_api03-vge05 = ls_tc20-unit."默认值 标准值单位
CLEAR:ls_tc20.
ENDIF.
ENDIF.
IF ls_tc21-par06 IS NOT INITIAL.
ls_in_crco_api01-lstar6 = ls_tc21-par06."活动类型
ls_in_crco_api01-forml6 = ls_tc21-par06."公式码
READ TABLE lt_tc20 INTO ls_tc20 WITH KEY parid = ls_tc21-par06 BINARY SEARCH.
IF sy-subrc = 0.
ls_in_crhd_api03-vge06 = ls_tc20-unit."默认值 标准值单位
CLEAR:ls_tc20.
ENDIF.
ENDIF.
CLEAR:ls_tc21.
ENDIF.
APPEND ls_in_crco_api01 TO lt_in_crco_api01 .
"调用BAPI
CALL FUNCTION 'CRAP_WORKCENTER_CREATE'
EXPORTING
in_crhd_api01 = ls_in_crhd_api01
in_crhd_api02 = ls_in_crhd_api02
in_crhd_api03 = ls_in_crhd_api03
* IN_CRHD_API05 =
* COMMENT =
test = ''
* IN_CRHD_API07 =
* IV_NO_COMMIT =
TABLES
* IN_KAPA_API01 =
* IN_KAPA_API02 =
* IN_CRHD_API04 =
in_crco_api01 = lt_in_crco_api01
* IN_KAZY_API01 =
* IN_KAPA_API04 =
.
IF sy-subrc <> 0.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
DATA:l_return(50) TYPE c.
CLEAR:l_return.
CALL FUNCTION 'MESSAGE_TEXT_BUILD'
EXPORTING
msgid = sy-msgid
msgnr = sy-msgno
msgv1 = sy-msgv1
msgv2 = sy-msgv2
msgv3 = sy-msgv3
msgv4 = sy-msgv4
IMPORTING
message_text_output = l_return.
LOOP AT gt_out ASSIGNING FIELD-SYMBOL(<fs_out4>) WHERE arbpl = gs_out-arbpl.
<fs_out4>-message = <fs_out4>-message && l_return.
<fs_out4>-icon = icon_red_light.
ENDLOOP.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
LOOP AT gt_out ASSIGNING <fs_out4> WHERE arbpl = gs_out-arbpl.
<fs_out4>-message = '创建成功!'.
<fs_out4>-icon = icon_green_light.
ENDLOOP.
ENDIF.
CLEAR:gs_out.
REFRESH:lt_in_crco_api01.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SET_LAYOUT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_set_layout .
CLEAR gs_layout.
gs_layout-sel_mode = 'D'. "设置行模式"
gs_layout-cwidth_opt = 'X'. "优化列宽设置"
gs_layout-zebra = 'X'. "设置斑马线"
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SET_FIELDCAT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_set_fieldcat .
PERFORM frm_get_fieldcat USING 'ICON ' ' 指示灯' ' ' ' ' ' '.
PERFORM frm_get_fieldcat USING 'MESSAGE' '消息 ' ' ' ' ' ' '.
PERFORM frm_get_fieldcat USING 'ARBPL ' '工作中心' 'CRHD' 'ARBPL ' ' '.
PERFORM frm_get_fieldcat USING 'KTEXT ' '工作中心描述' 'CRTX' 'KTEXT ' ' '.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_GET_FIELDCAT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
FORM frm_get_fieldcat USING VALUE(p_field)
VALUE(p_name)
VALUE(p_reftable)
VALUE(p_reffield)
VALUE(p_edit).
gs_fieldcat-just = 'C'.
gs_fieldcat-fieldname = p_field.
gs_fieldcat-coltext = p_name.
gs_fieldcat-seltext = p_name.
gs_fieldcat-ref_table = p_reftable.
gs_fieldcat-ref_field = p_reffield.
gs_fieldcat-edit = p_edit.
APPEND gs_fieldcat TO gt_fieldcat.
CLEAR:gs_fieldcat.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DISPLAY_ALV
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_display_alv .
*报表展示
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid "当前程序
i_callback_pf_status_set = 'SET_PF_STATUS'
i_callback_user_command = 'ALV_USER_COMMAND'
is_layout_lvc = gs_layout "界面格式"
it_fieldcat_lvc = gt_fieldcat "字段属性"
TABLES
t_outtab = gt_out
EXCEPTIONS
program_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.
ENDIF.
ENDFORM.