*&---------------------------------------------------------------------*
*& Report ZSD_086
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zsd_086.
TABLES: sscrfields.
TYPES: BEGIN OF ty_doc_data,
zcheck TYPE c,
kschl TYPE a304-kschl,
vkorg TYPE a304-vkorg,
vtweg TYPE a304-vtweg,
matnr TYPE a304-matnr,
kbetr TYPE konp-kbetr,
konwa TYPE konp-konwa,
kpein TYPE konp-kpein,
kmein TYPE konp-kmein,
datab TYPE a304-datab,
datbi TYPE a304-datbi,
icon(40) TYPE c, "状态
memo(200) TYPE c, "消息
memo1(25) TYPE c, "消息
memo2(25) TYPE c, "消息
memo3(25) TYPE c, "消息
memo4(25) TYPE c, "消息
memo5(25) TYPE c, "消息
memo6(25) TYPE c, "消息
memo7(25) TYPE c, "消息
memo8(25) TYPE c, "消息
memo9(25) TYPE c, "消息
memo10(25) TYPE c, "消息
END OF ty_doc_data.
TYPES:BEGIN OF ty_upload,
kschl TYPE a304-kschl,
vkorg TYPE a304-vkorg,
vtweg TYPE a304-vtweg,
matnr TYPE a304-matnr,
kbetr TYPE konp-kbetr,
konwa TYPE konp-konwa,
kpein TYPE konp-kpein,
kmein TYPE konp-kmein,
datab TYPE a304-datab,
datbi TYPE a304-datbi,
END OF ty_upload.
*----------------------------------------------------------------------*
* 定义全局变量
*----------------------------------------------------------------------*
DATA: gw_doc_data TYPE ty_doc_data,
gt_doc_data TYPE TABLE OF ty_doc_data,
gw_upload TYPE ty_upload,
gt_upload TYPE TABLE OF ty_upload,
functxt TYPE smp_dyntxt,
it_raw TYPE truxs_t_text_data,
g_down TYPE rlgrap-filename.
DATA: sep(3) VALUE '',
lv_char TYPE i,
lv_memo TYPE string,
error TYPE REF TO cx_sy_conversion_no_number.
DATA: lv_filename TYPE rlgrap-filename.
*----------------------------------------------------------------------*
* ALV层级关系定义
*----------------------------------------------------------------------*
DATA: it_fieldcat TYPE slis_t_fieldcat_alv,
wa_layout TYPE slis_layout_alv.
DATA: wa_fieldcat TYPE slis_fieldcat_alv.
DATA: gt_layo TYPE lvc_s_layo.
*--------------------------------------------------------------------*
* 常量定义
*--------------------------------------------------------------------*
CONSTANTS: cos_template TYPE w3objid VALUE 'ZSD_086',
cos_fiename TYPE string VALUE '二级代理价导入模板.XLS'.
*----------------------------------------------------------------------*
* data definition
*----------------------------------------------------------------------*
* Batchinputdata of single transaction
DATA: bdcdata LIKE bdcdata OCCURS 0 WITH HEADER LINE.
* messages of call transaction
*----------------------------------------------------------------------*
* fieldcat定义
*----------------------------------------------------------------------*
DEFINE macro_fill_fcat.
CLEAR wa_fieldcat.
&1 = &1 + 1.
wa_fieldcat-col_pos = &1.
wa_fieldcat-fieldname = &2.
wa_fieldcat-seltext_l = &3.
wa_fieldcat-seltext_m = &3.
wa_fieldcat-seltext_s = &3.
wa_fieldcat-hotspot = &4.
wa_fieldcat-no_zero = &5.
wa_fieldcat-key = &6.
wa_fieldcat-checkbox = &7. "可以勾选
wa_fieldcat-ref_fieldname = &8. " 内表中数量参照字段
wa_fieldcat-edit = &9. " 内表中数量参照字段
APPEND wa_fieldcat TO it_fieldcat.
END-OF-DEFINITION.
SELECTION-SCREEN: FUNCTION KEY 1 .
SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE TEXT-001.
PARAMETERS: p_file LIKE rlgrap-filename . "文件名
SELECTION-SCREEN END OF BLOCK blk1.
*&---------------------------------------------------------------------*
*& INITIALIZATION. 初始化
*&---------------------------------------------------------------------*
INITIALIZATION.
PERFORM frm_init_func .
*&---------------------------------------------------------------------*
*& AT SELECTION-SCREEN OUTPUT 输出前
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT.
*&--------------------------------------------------------------------*
*& AT SELECTION-SCREEN 交互
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN.
IF sscrfields-ucomm = 'FC01'.
PERFORM f_download_template USING cos_template cos_fiename.
ENDIF.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
PERFORM prm_get_file. "文件路径选择
*&---------------------------------------------------------------------*
*& START-OF-SELECTION 开始
*&---------------------------------------------------------------------*
START-OF-SELECTION.
PERFORM fill_temp_data. "通过Excel上传一般凭证数据
PERFORM frm_deal_data."处理上传数据
PERFORM frm_layout."alv样式
PERFORM frm_init_fieldcat.
PERFORM frm_alv."alv输出
*&---------------------------------------------------------------------*
*& FORM frm_init_func
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* --> p1 TEXT
* <-- p2 TEXT
*----------------------------------------------------------------------*
FORM frm_init_func .
functxt-icon_id = icon_mapped_relation.
functxt-quickinfo = '请下载模板'.
functxt-icon_text = '下载二级代理价导出模板'.
sscrfields-functxt_01 = functxt.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form prm_get_file
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM prm_get_file .
CALL FUNCTION 'TB_LIMIT_WS_FILENAME_GET'
IMPORTING
filename = p_file
EXCEPTIONS
selection_cancel = 1
selection_error = 2
OTHERS = 3.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form F_DOWNLOAD_TEMPLATE
*&---------------------------------------------------------------------*
* 下载模板
*----------------------------------------------------------------------*
* -->P_COS_TEMPLATE text
* -->P_COS_FIENAME text
*----------------------------------------------------------------------*
FORM f_download_template USING uv_objid TYPE w3objid
uv_file_name TYPE string.
DATA: lw_key TYPE wwwdatatab,
l_filename TYPE string,
l_path TYPE string,
l_fullpath TYPE string,
l_destination TYPE rlgrap-filename.
SELECT SINGLE *
INTO CORRESPONDING FIELDS OF lw_key
FROM wwwdata
WHERE relid EQ 'MI'
AND objid EQ uv_objid.
IF sy-subrc NE 0.
MESSAGE s001(zsd_03) WITH uv_objid.
RETURN.
ENDIF.
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
default_extension = cl_gui_frontend_services=>filetype_excel
default_file_name = uv_file_name
CHANGING
filename = l_filename
path = l_path
fullpath = l_fullpath
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
invalid_default_file_name = 4
OTHERS = 5.
IF sy-subrc <> 0.
ENDIF.
CHECK l_fullpath NE ''.
l_destination = l_fullpath.
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
EXPORTING
key = lw_key
destination = l_destination.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FILL_TEMP_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM fill_temp_data .
lv_filename = p_file.
DATA: lt_i_tab_raw_data TYPE truxs_t_text_data.
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
i_line_header = 'X'
i_tab_raw_data = lt_i_tab_raw_data
i_filename = lv_filename
TABLES
i_tab_converted_data = gt_upload
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.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DEAL_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_deal_data .
DATA: gw_upload TYPE ty_upload.
LOOP AT gt_upload INTO gw_upload.
TRY.
MOVE-CORRESPONDING gw_upload TO gw_doc_data.
" 判断金额是否都大于等于零
IF gw_doc_data-kbetr < 0.
gw_doc_data-memo5 = '金额不正确'.
gw_doc_data-icon = '@0A@'.
ENDIF.
CATCH cx_sy_conversion_no_number INTO error."若捕捉到错误则不是数字类型
lv_memo = error->get_longtext( )."错误信息
gw_doc_data-memo5 = lv_memo.
gw_doc_data-icon = '@0A@'.
ENDTRY.
IF gw_upload-kschl IS INITIAL OR gw_upload-kschl EQ space.
gw_doc_data-memo1 = '条件类型不能为空'.
gw_doc_data-icon = '@0A@'.
ENDIF.
IF gw_upload-vkorg IS INITIAL OR gw_upload-vkorg EQ space.
gw_doc_data-memo2 = '销售组织不能为空'.
gw_doc_data-icon = '@0A@'.
ENDIF.
IF gw_upload-vtweg IS INITIAL OR gw_upload-vtweg EQ space.
gw_doc_data-memo3 = '分销渠道不能为空'.
gw_doc_data-icon = '@0A@'.
ENDIF.
IF gw_upload-matnr IS INITIAL OR gw_upload-matnr EQ space.
gw_doc_data-memo4 = '物料编码不能为空'.
gw_doc_data-icon = '@0A@'.
ENDIF.
IF gw_upload-kbetr IS INITIAL OR gw_upload-kbetr EQ space.
gw_doc_data-memo5 = '金额不能为空'.
gw_doc_data-icon = '@0A@'.
ENDIF.
IF gw_upload-konwa IS INITIAL OR gw_upload-konwa EQ space.
gw_doc_data-memo6 = '货币单位不能为空'.
gw_doc_data-icon = '@0A@'.
ENDIF.
IF gw_upload-kpein IS INITIAL OR gw_upload-kpein EQ space.
gw_doc_data-memo7 = '单位不能为空'.
gw_doc_data-icon = '@0A@'.
ENDIF.
IF gw_upload-kmein IS INITIAL OR gw_upload-kmein EQ space.
gw_doc_data-memo8 = '量纲不能为空'.
gw_doc_data-icon = '@0A@'.
ENDIF.
IF gw_upload-datab IS INITIAL OR gw_upload-datab EQ space.
gw_doc_data-memo9 = '有效从不能为空'.
gw_doc_data-icon = '@0A@'.
ENDIF.
IF gw_upload-datbi IS INITIAL OR gw_upload-datbi EQ space.
gw_doc_data-memo10 = '失效期不能为空'.
gw_doc_data-icon = '@0A@'.
ENDIF.
CONCATENATE gw_doc_data-memo1 gw_doc_data-memo2
gw_doc_data-memo3 gw_doc_data-memo4
gw_doc_data-memo5 gw_doc_data-memo6
gw_doc_data-memo7 gw_doc_data-memo8
gw_doc_data-memo9 gw_doc_data-memo10
INTO gw_doc_data-memo SEPARATED BY sep.
APPEND gw_doc_data TO gt_doc_data.
CLEAR: gw_upload,gw_doc_data.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_LAYOUT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_layout .
CLEAR wa_layout.
wa_layout-box_fieldname = 'ZCHECK'.
wa_layout-zebra = 'X'.
wa_layout-detail_popup = 'X'.
wa_layout-colwidth_optimize = 'X'.
ENDFORM. " FRM_LAYOUT
*&---------------------------------------------------------------------*
*& Form FRM_INIT_FIELDCAT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_init_fieldcat .
DATA: l_colpos TYPE lvc_s_fcat-col_pos VALUE 0.
CHECK it_fieldcat IS INITIAL.
macro_fill_fcat:
l_colpos 'ICON' '错误标识' ' ' ' ' ' ' ' ' ' ' ' ',
l_colpos 'KSCHL' '条件类型' ' ' ' ' ' ' ' ' ' ' ' ',
l_colpos 'VKORG' '销售组织' ' ' ' ' ' ' ' ' ' ' ' ',
l_colpos 'VTWEG' '分销渠道' ' ' ' ' ' ' ' ' ' ' ' ',
l_colpos 'MATNR' '物料编码' ' ' 'X' ' ' ' ' ' ' ' ',
l_colpos 'KBETR' '金额' ' ' ' ' ' ' ' ' ' ' ' ',
l_colpos 'KONWA' '货币单位' ' ' ' ' ' ' ' ' ' ' ' ',
l_colpos 'KPEIN' '单位' ' ' ' ' ' ' ' ' ' ' ' ',
l_colpos 'KMEIN' '量纲' ' ' ' ' ' ' ' ' ' ' ' ',
l_colpos 'DATAB' '有效从' ' ' ' ' ' ' ' ' ' ' ' ',
l_colpos 'DATBI' '失效期' ' ' ' ' ' ' ' ' ' ' ' ',
l_colpos 'MEMO' '错误描述' ' ' ' ' ' ' ' ' ' ' ' '.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_ALV
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_alv .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' "调用函数
EXPORTING
i_callback_program = sy-repid
is_layout = wa_layout
it_fieldcat = it_fieldcat
i_save = 'A'
i_callback_pf_status_set = 'FRM_SET_PF_STATUS'
i_callback_user_command = 'FRM_USER_COMMAND'
TABLES
t_outtab = gt_doc_data
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.
FORM frm_user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
DATA: lr_grid TYPE REF TO cl_gui_alv_grid.
DATA : lt_doc_data TYPE TABLE OF ty_doc_data.
DATA : it_msgtab TYPE STANDARD TABLE OF bdcmsgcoll. "BDC消息内表
DATA : lt_return TYPE TABLE OF bapiret2.
DATA : lv_mode TYPE c .
DATA : lv_error TYPE c.
DATA : lv_sucess TYPE c.
DATA : lv_emsg TYPE string.
DATA : lv_smsg TYPE string.
DATA : lv_kbetr TYPE string.
DATA : lv_kpein TYPE string.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = lr_grid.
CALL METHOD lr_grid->check_changed_data.
rs_selfield-refresh = 'X'. "自动刷新
CASE r_ucomm.
WHEN 'SAVE'.
lt_doc_data = gt_doc_data.
SORT lt_doc_data BY icon.
READ TABLE lt_doc_data INTO DATA(ls_doc_data) WITH KEY icon = '@0A@' BINARY SEARCH.
IF sy-subrc = 0.
MESSAGE '请对应‘错误描述’栏位的提示内容处理异常后再保存!' TYPE 'S' DISPLAY LIKE 'E'.
RETURN.
ENDIF.
LOOP AT gt_doc_data ASSIGNING FIELD-SYMBOL() .
CLEAR lv_mode.
PERFORM bdc_dynpro USING 'SAPMV13A' '0100'.
PERFORM bdc_field USING 'BDC_CURSOR'
'RV13A-KSCHL'.
PERFORM bdc_field USING 'BDC_OKCODE'
'/00'.
PERFORM bdc_field USING 'RV13A-KSCHL'
-kschl.
PERFORM bdc_dynpro USING 'SAPLV14A' '0100'.
PERFORM bdc_field USING 'BDC_CURSOR'
'RV130-SELKZ(02)'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=WEIT'.
PERFORM bdc_field USING 'RV130-SELKZ(01)'
space.
PERFORM bdc_field USING 'RV130-SELKZ(02)'
abap_true.
PERFORM bdc_dynpro USING 'SAPMV13A' '1304'.
PERFORM bdc_field USING 'BDC_CURSOR'
'RV13A-DATAB(01)'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=SICH'.
PERFORM bdc_field USING 'KOMG-VKORG'
-vkorg.
PERFORM bdc_field USING 'KOMG-VTWEG'
-vtweg.
PERFORM bdc_field USING 'KOMG-MATNR(01)'
-matnr.
lv_kbetr = -kbetr .
CONDENSE lv_kbetr NO-GAPS.
* SHIFT lv_kbetr LEFT DELETING LEADING space.
PERFORM bdc_field USING 'KONP-KBETR(01)'
lv_kbetr.
PERFORM bdc_field USING 'KONP-KONWA(01)'
-konwa.
lv_kpein = -kpein.
CONDENSE lv_kpein NO-GAPS.
PERFORM bdc_field USING 'KONP-KPEIN(01)'
lv_kpein.
PERFORM bdc_field USING 'KONP-KMEIN(01)'
-kmein.
PERFORM bdc_field USING 'RV13A-DATAB(01)'
-datab.
PERFORM bdc_field USING 'RV13A-DATBI(01)'
-datbi.
lv_mode = 'N'.
CALL TRANSACTION 'VK11' USING bdcdata
MODE lv_mode
MESSAGES INTO it_msgtab.
CALL FUNCTION 'CONVERT_BDCMSGCOLL_TO_BAPIRET2'
TABLES
imt_bdcmsgcoll = it_msgtab
ext_return = lt_return.
LOOP AT lt_return INTO DATA(lw_bapiret).
IF lw_bapiret-number NE '023' .
lv_error = 'X'.
IF lv_emsg IS INITIAL .
lv_emsg = lw_bapiret-message.
ELSE.
CONCATENATE lv_emsg lw_bapiret-message INTO lv_emsg SEPARATED BY ';'.
ENDIF.
ENDIF.
ENDLOOP.
IF lv_error = abap_true.
-memo = lv_emsg.
-icon = '@0A@'.
CLEAR:lv_error.EXIT .
ELSE.
-icon = '@5B@'.
-memo = '条件记录已保存'.
ENDIF.
CLEAR: lv_emsg ,bdcdata[].
ENDLOOP.
ENDCASE.
ENDFORM. "FM_USER_COMMAND
*&---------------------------------------------------------------------*
*& Form SET_PF_STATUS
*&---------------------------------------------------------------------*
* 设置标准工具栏按钮
*----------------------------------------------------------------------*
* -->RT_EXTAB text
*----------------------------------------------------------------------*
FORM frm_set_pf_status USING rt_extab TYPE slis_t_extab.
SET PF-STATUS 'STAT_ALV'.
ENDFORM. "SE
*----------------------------------------------------------------------*
* Start new screen *
*----------------------------------------------------------------------*
FORM bdc_dynpro USING program dynpro.
CLEAR bdcdata.
bdcdata-program = program.
bdcdata-dynpro = dynpro.
bdcdata-dynbegin = 'X'.
APPEND bdcdata.
ENDFORM.
*----------------------------------------------------------------------*
* Insert field *
*----------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
CLEAR bdcdata.
bdcdata-fnam = fnam.
bdcdata-fval = fval.
APPEND bdcdata.
ENDFORM.