***********************************************************************
* Title : ZMMR107 *
* Application : MM *
* Subject : 批量更新po净价值 *
* Requested by : 采购部 *
* Execution : any time *
* Ref no: : B231 *
* Author : rainy *
* Req Date : 20150804 *
***********************************************************************
* 批量更新po净价值 *
***********************************************************************
***********************************************************************
*1.取数:
*2.输出: *
***********************************************************************
* MODIFICATIONS (latest entry at the top) *
* ------------------------------------------------------------------- *
* REL DATE NAME (COMPANY) DESCRIPTION TASK-NO *
* --- ---- ---- --------- ----------- ------- *
* *
***********************************************************************
REPORT zmmr107 MESSAGE-ID zmm_001.
TABLES:ekpo,
sscrfields.
DATA:BEGIN OF it_text,
ebeln LIKE ekpo-ebeln,
ebelp LIKE ekpo-ebelp,
matnr LIKE ekpo-matnr,
werks LIKE ekpo-werks,
lgort LIKE ekpo-lgort,
netpr LIKE ekpo-netpr,
zicon TYPE char4,
ztext TYPE string,
zline TYPE string.
DATA: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.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE t1.
PARAMETERS: so_file(300).
SELECTION-SCREEN END OF BLOCK b1.
SELECTION-SCREEN FUNCTION KEY 1.
*----------------------------------------------------------------------
* INITIALIZATION
*----------------------------------------------------------------------
INITIALIZATION.
t1 = ‘导入数据‘.
sscrfields-functxt_01 = ‘下载导入模板‘.
*----------------------------------------------------------------------
* AT SELECTION-SCREEN.
*----------------------------------------------------------------------
AT SELECTION-SCREEN.
*&下载模板
PERFORM frm_download_template.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR so_file.
PERFORM getdata USING so_file.
*----------------------------------------------------------------------
* START-OF-SELECTION.
*----------------------------------------------------------------------
START-OF-SELECTION.
*&S1.upload data.
PERFORM frm_upload.
* CHECK gt_text[] IS NOT INITIAL.
*&write text
PERFORM frm_exeupdate.
PERFORM frm_alv_output.
* PRINT-CONTROL INDEX-LINE.
*&---------------------------------------------------------------------*
*& Form getdata
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->SO_FILE text
*----------------------------------------------------------------------*
FORM getdata USING so_file.
* 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 so_file.
ENDFORM. "getdata
*&---------------------------------------------------------------------*
*& Form FRM_UPLOAD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_upload .
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 so_file 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
.
* CALL FUNCTION ‘TEXT_CONVERT_XLS_TO_SAP‘
* EXPORTING
** I_FIELD_SEPERATOR =
** I_LINE_HEADER =
* i_tab_raw_data =
* i_filename =
* TABLES
* i_tab_converted_data =
** 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.
*&删除标题行
* DELETE gt_text FROM 1 TO 1.
* i_line_header = ‘X‘
ENDFORM. " FRM_UPLOAD
*&---------------------------------------------------------------------*
*& 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 ‘\ZM107.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.
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 = ‘ZM107‘. "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_EXEUPDATE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_exeupdate .
FIELD-SYMBOLS:<fs_text> LIKE LINE OF gt_text.
DATA: lv_upd(1).
DATA:lv_date LIKE sy-datum,
lv_day(2) TYPE n,
lv_month(2) TYPE n,
lv_year(4) TYPE n,
lv_calc_date TYPE d.
DATA:l_line TYPE string.
SORT gt_text BY ebeln ebelp.
*&S1.处理数据
LOOP AT gt_text ASSIGNING <fs_text>.
CLEAR:lv_day,
lv_month,
lv_year.
<fs_text>-zline = sy-tabix.
*&S1.1PO#加前导0
CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT‘
EXPORTING
input = <fs_text>-ebeln
IMPORTING
output = <fs_text>-ebeln.
*&判断PO 是否存在
SELECT SINGLE *
FROM ekpo
WHERE ebeln EQ <fs_text>-ebeln
AND ebelp EQ <fs_text>-ebelp.
IF sy-subrc EQ 0.
UPDATE ekpo SET netpr = <fs_text>-netpr
WHERE ebeln = <fs_text>-ebeln
AND ebelp = <fs_text>-ebelp.
IF sy-subrc EQ 0.
<fs_text>-zicon = ‘@[email protected]‘.
<fs_text>-ztext = ‘更新成功!‘.
COMMIT WORK.
ELSE.
<fs_text>-zicon = ‘@[email protected]‘.
<fs_text>-ztext = ‘更新失败!‘.
ROLLBACK WORK.
ENDIF.
ELSE.
<fs_text>-ztext = ‘数据不存在!‘.
ENDIF.
ENDLOOP.
DATA:l_lines TYPE string.
l_lines = lines( gt_text[] ).
IF l_lines EQ 0.
MESSAGE s000(oo) WITH ‘没有找到符合条件的数据!‘ DISPLAY LIKE ‘E‘.
LEAVE TO LIST-PROCESSING.
ELSE.
MESSAGE s000(oo) WITH ‘找到数据‘ l_lines ‘条!‘.
ENDIF.
ENDFORM. " FRM_EXEUPDATE
*&---------------------------------------------------------------------*
*& Form FRM_ALV_OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_alv_output .
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 ‘ZLINE‘ ‘行号‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘.
add_fieldcat ‘EBELN‘ ‘采购凭证‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘.
add_fieldcat ‘EBELP‘ ‘项目‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘.
add_fieldcat ‘MATNR‘ ‘物料号‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘.
add_fieldcat ‘WERKS‘ ‘工厂‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘.
add_fieldcat ‘LGORT‘ ‘库位‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘X‘ ‘‘.
add_fieldcat ‘NETPR‘ ‘需要改成的净价‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘.
add_fieldcat ‘ZTEXT‘ ‘消息‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘.
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_OUTPUT