*&---------------------------------------------------------------------*
*& Report ZSDR011
*&
*&---------------------------------------------------------------------*
*&程序名称:装运成本报表
*&事物代码:ZSD031
*&导入模板:无
*&作者:董冬
*&时间:2016/3/5
*&更新时间:2016/3/7
*&-------
REPORT zsdr011.
TABLES:vfkp.
INCLUDE ole2incl.
DATA: gs_fcat TYPE lvc_s_fcat,
gt_fcat TYPE lvc_t_fcat,
gs_layo TYPE lvc_s_layo,
gs_grid TYPE lvc_s_glay.
DATA: gt_event TYPE slis_t_event WITH HEADER LINE,
gt_event_exit TYPE slis_t_event_exit WITH HEADER LINE.
DATA: ref_grid TYPE REF TO cl_gui_alv_grid .
DATA: excel TYPE ole2_object,
workbook TYPE ole2_object,
sheet TYPE ole2_object,
cell TYPE ole2_object,
row TYPE ole2_object.
TYPES: BEGIN OF ty_all,
fknum LIKE vfkp-fknum, "运输成本编号
fkpos LIKE vfkp-fkpos, "项目
vsart LIKE vfkp-vsart, "装运类型
tdlnr LIKE vfkp-tdlnr, "服务代理
ernam LIKE vfkp-ernam, "创建者
erdat LIKE vfkp-erdat, "创建日期
fkart LIKE vfkk-fkart, "成本类别
fkpty LIKE vfkp-fkpty, "项目类别
netwr LIKE vfkp-netwr, "净价值
waers LIKE vfkp-waers, "货币
tplst LIKE vfkp-tplst, "运输计划点
kalsm LIKE vfkp-kalsm, "定价过程
bukrs LIKE vfkp-bukrs, "公司代码
werks LIKE vfkp-werks, "工厂
ekorg LIKE vfkp-ekorg, "采购组织
ekgrp LIKE vfkp-ekgrp, "采购组
ebeln LIKE vfkp-ebeln, "采购凭证
lblni LIKE vfkp-lblni, "条目表
rechs LIKE vfkp-rechs, "服务代理(出票方)
rebel LIKE vfkp-rebel, "参考凭证号
knttp LIKE vfkp-knttp, "科目分配类别
knttp_txt LIKE t163i-knttx, "科目分配类别描述
stabr LIKE vfkp-stabr, "传递状态
stabr_txt(20) TYPE c,
kstau LIKE vfkp-kstau, "成本分配状态
kstau_txt(20) TYPE c,
fksto LIKE vfkp-fksto, "已取消状态
fksto_txt(2) TYPE c,
beizhu(30) TYPE c,
END OF ty_all.
*TYPES:BEGIN OF ty_header,
*
* END OF ty_header.
**单据抬头结束
*
**单据明细开始
*TYPES:BEGIN OF ty_items,
*
* END OF ty_items.
**单据明细结束
DATA:wa_all TYPE ty_all,
gt_all TYPE ty_all OCCURS 0.
*DATA:wa_header TYPE ty_header,
* gt_header TYPE ty_header OCCURS 0.
*
*DATA:wa_items TYPE ty_items,
* gt_items TYPE ty_items OCCURS 0.
*
*DATA: gt_header_show TYPE ty_header OCCURS 0.
*DATA: gt_items_show TYPE ty_items OCCURS 0.
*定义传入到smartforms的变量开始
DATA:fm_name TYPE rs38l_fnam.
DATA:itemsname(32) TYPE c.
DATA:headername(32) TYPE c.
*定义传入到smartforms的变量结束
*定义存储ALV向smartforms传入数据的内表开始
*DATA:gt_header_temp TYPE ty_header OCCURS 0.
*DATA:gt_items_temp TYPE ty_items OCCURS 0.
DATA:gt_all_temp TYPE ty_all OCCURS 0.
*定义存储ALV向smartforms传入数据的内表结束
"控制smartforms参数声明开始
DATA :control_parameters TYPE ssfctrlop.
"控制smartforms参数声明结束
FIELD-SYMBOLS: <f_fs1> , <f_fs2>.
START-OF-SELECTION.
SELECT-OPTIONS:
p_fknum FOR vfkp-fknum ," no-EXTENSION NO INTERVALS,
p_vsart FOR vfkp-vsart,
p_tdlnr FOR vfkp-tdlnr ,
p_knttp FOR vfkp-knttp,
p_ernam FOR vfkp-ernam,
p_erdat FOR vfkp-erdat.
START-OF-SELECTION.
PERFORM getdata.
PERFORM outdata.
FORM getdata.
SELECT *
FROM vfkp AS v
INNER JOIN vfkk AS vk
ON ( v~fknum = vk~fknum )
INTO CORRESPONDING FIELDS OF TABLE gt_all
WHERE (
v~fknum IN p_fknum AND
v~vsart IN p_vsart AND
v~tdlnr IN p_tdlnr AND
v~knttp IN p_knttp AND
v~ernam IN p_ernam AND
v~erdat IN p_erdat ).
SORT gt_all ASCENDING BY fknum.
LOOP AT gt_all INTO wa_all.
SELECT SINGLE knttx FROM t163i INTO wa_all-knttp_txt
WHERE knttp EQ wa_all-knttp.
IF wa_all-stabr EQ ‘A‘.
wa_all-stabr_txt = ‘未转移‘.
ELSEIF wa_all-stabr EQ ‘C‘.
wa_all-stabr_txt = ‘已传送‘.
ELSEIF wa_all-stabr EQ ‘‘.
wa_all-stabr_txt = ‘和传输不相关‘.
ENDIF.
IF wa_all-kstau EQ ‘A‘.
wa_all-kstau_txt = ‘装运成本条目‘.
ELSEIF wa_all-kstau EQ ‘B‘.
wa_all-kstau_txt = ‘交货‘.
ELSEIF wa_all-kstau EQ ‘C‘.
wa_all-kstau_txt = ‘交货项目‘.
ENDIF.
IF wa_all-fksto EQ ‘X‘.
wa_all-fksto_txt = ‘是‘.
ELSE.
wa_all-fksto_txt = ‘否‘.
ENDIF.
MODIFY gt_all FROM wa_all.
ENDLOOP.
ENDFORM. " GETDATA
*&---------------------------------------------------------------------*
*& Form FIXDATA
*&---------------------------------------------------------------------*
FORM frm_catlg_set USING p_field p_text p_key p_edit p_no_out
p_f4availabl p_ref_table p_ref_field .
gs_fcat-fieldname = p_field.
gs_fcat-reptext = p_text.
gs_fcat-key = p_key.
gs_fcat-edit = p_edit.
gs_fcat-no_out = p_no_out.
gs_fcat-ref_field = p_ref_field.
gs_fcat-ref_table = p_ref_table.
gs_fcat-f4availabl = p_f4availabl.
APPEND gs_fcat TO gt_fcat .
CLEAR gs_fcat .
ENDFORM. "frm_catlg_set
FORM fieldcat_init .
DATA: c(2) TYPE n,txt(20) TYPE c .
PERFORM frm_catlg_set USING:
‘FKNUM‘ ‘运输成本编号‘ ‘X‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘,
‘FKART‘ ‘成本类别‘ ‘‘ ‘‘ ‘‘ ‘X‘ ‘VFKK‘ ‘FKART‘,
‘FKPTY‘ ‘项目类别‘ ‘‘ ‘‘ ‘‘ ‘X‘ ‘VFKP‘ ‘FKPTY‘,
‘NETWR‘ ‘净价值‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘,
‘WAERS‘ ‘货币‘ ‘‘ ‘‘ ‘‘ ‘X‘ ‘VFKP‘ ‘WAERS‘,
‘TPLST‘ ‘运输计划点‘ ‘‘ ‘‘ ‘‘ ‘X‘ ‘VFKP‘ ‘TPLST‘,
‘KALSM‘ ‘定价过程‘ ‘‘ ‘‘ ‘‘ ‘X‘ ‘VFKP‘ ‘KALSM‘,
‘BUKRS‘ ‘公司代码‘ ‘‘ ‘‘ ‘‘ ‘X‘ ‘VFKP‘ ‘BUKRS‘,
‘WERKS‘ ‘工厂代码‘ ‘‘ ‘‘ ‘‘ ‘X‘ ‘VFKP‘ ‘WERKS‘,
‘EKORG‘ ‘采购组织代码‘ ‘‘ ‘‘ ‘‘ ‘X‘ ‘VFKP‘ ‘EKORG‘,
‘EKGRP‘ ‘采购组代码‘ ‘‘ ‘‘ ‘‘ ‘X‘ ‘VFKP‘ ‘EKGRP‘,
‘EBELN‘ ‘采购凭证‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘,
‘LBLNI‘ ‘条目表‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘,
‘RECHS‘ ‘服务代理‘ ‘‘ ‘‘ ‘‘ ‘X‘ ‘VFKP‘ ‘RECHS‘,
‘REBEL‘ ‘参考凭证号‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘ ‘‘,
‘KNTTP_TXT‘ ‘科目分配类别‘ ‘‘ ‘‘ ‘‘ ‘X‘ ‘VFKP‘ ‘KNTTP‘,
‘STABR_TXT‘ ‘传送状态‘ ‘‘ ‘‘ ‘‘ ‘X‘ ‘VFKP‘ ‘STABR‘,
‘KSTAU_TXT‘ ‘成本分配状态‘ ‘‘ ‘‘ ‘‘ ‘X‘ ‘VFKP‘ ‘KSTAU‘,
‘FKSTO_TXT‘ ‘已取消状态‘ ‘‘ ‘‘ ‘‘ ‘X‘ ‘VFKP‘ ‘FKSTO‘,
‘BEIZHU‘ ‘备注‘ ‘‘ ‘X‘ ‘X‘ ‘‘ ‘‘ ‘‘.
gs_layo
-zebra = ‘X‘.
gs_layo
-cwidth_opt = ‘X‘.
* gs_layo-grid_title = ‘dongdong‘.
* gs_layo-smalltitle = ‘X‘.
ENDFORM. "fieldcat_init
FORM outdata .
PERFORM fieldcat_init .
PERFORM show_alv TABLES gt_fcat gt_all[]
USING gs_layo.
ENDFORM. " outdata
FORM show_alv TABLES pt_fcat pt_tab USING ps_layout.
gs_grid
-edt_cll_cb = ‘X‘."当屏幕失去焦点时,自动刷新
gt_event
-name = ‘CALLER_EXIT‘. "slis_ev_caller_exit_at_start事件
gt_event
-form = ‘FM_BUTTON‘.
APPEND gt_event .
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY_LVC‘
EXPORTING
i_callback_program
= sy-repid
i_callback_user_command
= ‘USER_COMMAND‘
i_grid_settings
= gs_grid
i_callback_pf_status_set
= ‘SET_PF_STATUS‘
is_layout_lvc
= ps_layout
it_fieldcat_lvc
= pt_fcat[]
it_events
= gt_event[]
i_save
= ‘X‘
* it_event_exit = gt_event_exit[]
TABLES
t_outtab
= pt_tab
EXCEPTIONS
program_error
= 1
OTHERS = 2.
ENDFORM. "csalv_set_fcat
FORM user_command USING r_ucomm LIKE sy-ucomm rs_selfield TYPE slis_selfield.
CASE r_ucomm.
WHEN ‘&DATA_SAVE‘.
WHEN ‘&XSL‘ OR ‘&EXPORT‘ .
"PERFORM PRINT.
DATA: path LIKE rlgrap-filename..
DATA :file_path LIKE rlgrap-filename.
"设置文件存放路径调用函数开始
PERFORM set_file_path CHANGING path.
"设置文件存放路径调用函数结束
DATA:ls_row TYPE lvc_s_row,
lt_rows
TYPE lvc_t_row.
"获取ALV选中的行开始
CALL METHOD ref_grid->get_selected_rows
IMPORTING
et_index_rows
= lt_rows.
"获取ALV选中的行结束
LOOP AT lt_rows INTO ls_row.
READ TABLE gt_all INTO wa_all INDEX ls_row-index.
APPEND wa_all TO gt_all_temp.
ENDLOOP.
* LOOP AT gt_all_temp INTO wa_all.
file_path
= path && ‘\运输成本‘ && ‘‘ && ‘.xls‘.
PERFORM frm_download_template USING file_path."‘C:\Users\DONG\Desktop\发货通知单.XLS‘.
PERFORM data_to_excel USING file_path ." ‘C:\Users\DONG\Desktop\发货通知单.XLS‘.
* ENDLOOP.
"LOOP AT itab_out .
IF sy-subrc EQ 0 .
MESSAGE s004(zmess) WITH ‘导出成功!‘.
ENDIF.
CLEAR path.
CLEAR file_path.
WHEN ‘&BACK‘ OR ‘&UP‘ OR ‘&EXIT‘.
LEAVE PROGRAM.
ENDCASE.
ENDFORM. "user_command
*&---------------------------------------------------------------------*
*& Form SET_PF_STATUS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->RT_EXTAB text
*----------------------------------------------------------------------*
FORM set_pf_status USING rt_extab TYPE slis_t_extab..
SET PF-STATUS ‘STANDARD‘.
ENDFORM. "SET_PF_STATUS
*&---------------------------------------------------------------------*
*& Form fm_button
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->E_GRID text
*----------------------------------------------------------------------*
FORM fm_button USING e_grid TYPE slis_data_caller_exit.
CALL FUNCTION ‘GET_GLOBALS_FROM_SLVC_FULLSCR‘
* EXPORTING
* IR_SALV_FULLSCREEN_ADAPTER =
IMPORTING
* ET_EXCLUDING =
* E_REPID =
* E_CALLBACK_PROGRAM =
* E_CALLBACK_ROUTINE =
e_grid
= ref_grid
* ET_FIELDCAT_LVC =
* ER_TRACE =
* E_FLG_NO_HTML =
* ES_LAYOUT_KKBLO =
* ES_SEL_HIDE =
* ET_EVENT_EXIT =
* ER_FORM_TOL =
* ER_FORM_EOL =
.
* CALL METHOD ref_grid->check_changed_data.
* 设置enter事件
CALL METHOD ref_grid->register_edit_event
EXPORTING
i_event_id
= cl_gui_alv_grid=>mc_evt_enter
EXCEPTIONS
error
= 1
OTHERS = 2.
"CREATE OBJECT gt_event_receiver.
"SET HANDLER gt_event_receiver->handle_modify FOR ref_grid.
ENDFORM. "FM_BUTTON
FORM fill_cell USING i_row i_col p_value.
CALL METHOD OF excel ‘CELLS‘ = cell
EXPORTING #1 = i_row #2 = i_col.
SET PROPERTY OF cell ‘VALUE‘ = p_value.
ENDFORM. "fill_cell
FORM data_to_excel USING c_path .
DATA:lt_tmp_gt TYPE ty_all.
* Create an Excel object and start Excel.
CREATE OBJECT excel ‘EXCEL.APPLICATION‘.
IF sy-subrc <> 0.
MESSAGE ‘The excel object can‘‘t be created‘ TYPE ‘I‘.
ENDIF.
* Create an Excel workbook Object.
CALL METHOD OF excel ‘WORKBOOKS‘ = workbook .
* Transfer the header line to Excel.
CALL METHOD OF workbook ‘OPEN‘ EXPORTING #1 = c_path.
"READ TABLE gt_header_show INTO wa_header INDEX 1.
DATA:l_col TYPE sy-index.
FIELD-SYMBOLS: <f>.
DATA : cust_num TYPE i.
DATA :rows TYPE i VALUE 1."行数据从第十一行开始
LOOP AT gt_all_temp INTO wa_all.
rows = rows + 1.
PERFORM fill_cell USING rows 1 wa_all-fknum.
PERFORM fill_cell USING rows 2 wa_all-fkart.
PERFORM fill_cell USING rows 3 wa_all-fkpty.
PERFORM fill_cell USING rows 4 wa_all-netwr.
PERFORM fill_cell USING rows 5 wa_all-waers.
PERFORM fill_cell USING rows 6 wa_all-tplst.
PERFORM fill_cell USING rows 7 wa_all-kalsm.
PERFORM fill_cell USING rows 8 wa_all-bukrs.
PERFORM fill_cell USING rows 9 wa_all-werks.
PERFORM fill_cell USING rows 10 wa_all-ekorg.
PERFORM fill_cell USING rows 11 wa_all-ekgrp.
PERFORM fill_cell USING rows 12 wa_all-ebeln.
PERFORM fill_cell USING rows 13 wa_all-lblni.
PERFORM fill_cell USING rows 14 wa_all-rechs.
PERFORM fill_cell USING rows 15 wa_all-rebel.
PERFORM fill_cell USING rows 16 wa_all-knttp_txt.
PERFORM fill_cell USING rows 17 wa_all-stabr_txt.
PERFORM fill_cell USING rows 18 wa_all-kstau_txt.
PERFORM fill_cell USING rows 19 wa_all-fksto_txt.
ENDLOOP.
GET PROPERTY OF excel ‘ACTIVEWORKBOOK‘ = workbook.
* release and exit Excel.
CALL METHOD OF workbook ‘SAVE‘.
CALL METHOD OF excel ‘QUIT‘.
* Free all objects
FREE OBJECT cell.
FREE OBJECT workbook.
FREE OBJECT excel.
excel
-handle = -1.
FREE OBJECT row.
ENDFORM.
FORM frm_download_template USING c_path .
DATA: lv_objdata LIKE wwwdatatab,
lv_obj_name
LIKE wwwdatatab-objid,
lv_destination
LIKE rlgrap-filename,
lv_objid
LIKE sy-repid,
lv_subrc
LIKE sy-subrc.
DATA:l_ret TYPE abap_bool, lv_answer
.
DATA:lv_file TYPE string.
MOVE c_path TO lv_file.
CALL METHOD cl_gui_frontend_services=>file_exist
EXPORTING
file
= lv_file
RECEIVING
result
= l_ret
EXCEPTIONS
cntl_error
= 1
error_no_gui
= 2
wrong_parameter
= 3
not_supported_by_gui
= 4
OTHERS = 5.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
EXIT.
ENDIF.
IF l_ret EQ ‘X‘.
* 模版已存在,是否覆盖
CALL FUNCTION ‘POPUP_TO_CONFIRM‘
EXPORTING
text_question
= ‘模版已存在,是否覆盖?‘
text_button_1
= ‘是‘(001)
text_button_2
= ‘否‘(002)
IMPORTING
answer
= lv_answer
EXCEPTIONS
text_not_found
= 1
OTHERS = 2.
IF sy-subrc <> 0.
ENDIF.
IF lv_answer EQ ‘A‘. "取消
EXIT.
ELSEIF lv_answer NE ‘1‘. "否
"p_filepath = c_path.
ENDIF.
ELSE.
lv_answer
= ‘1‘.
ENDIF.
CREATE OBJECT excel ‘EXCEL.APPLICATION‘.
GET PROPERTY OF excel ‘Workbooks‘ = workbook .
CALL METHOD OF workbook
‘Close‘.
IF lv_answer EQ ‘1‘.
MOVE ‘ZSD_XLS_001‘ TO lv_obj_name."引用传入到服务器中xls模板
SELECT relid objid
FROM wwwdata
INTO CORRESPONDING FIELDS OF lv_objdata
UP TO 1 ROWS
WHERE srtf2 = 0 AND relid = ‘MI‘
AND objid = lv_obj_name.
ENDSELECT.
CALL FUNCTION ‘DOWNLOAD_WEB_OBJECT‘
EXPORTING
key = lv_objdata
destination
= c_path
IMPORTING
rc
= lv_subrc.
* IF lv_subrc = 0.
* p_filepath = c_path.
* ENDIF.
ENDIF.
* CALL METHOD OF
* g_workbook
* ‘open‘
*
* EXPORTING
* #1 = c_path.
*
* CALL METHOD OF
* g_excel
* ‘worksheets‘ = g_sheet
* EXPORTING
* #1 = 1.
* CALL METHOD OF
* g_sheet
* ‘activate‘.
"SET PROPERTY OF g_excel ‘visible‘ = 0.
FREE OBJECT sheet.
"FREE OBJECT g_applica.
FREE OBJECT workbook.
FREE OBJECT excel.
ENDFORM. "frm_download_template
FORM set_file_path CHANGING filepath LIKE rlgrap-filename.
DATA: gd_path TYPE string.
DATA: it_tab TYPE filetable,
gd_subrc
TYPE i.
DATA: ld_filename TYPE string,
ld_path
TYPE string,
ld_fullpath
TYPE string,
ld_result
TYPE i,
gd_file
TYPE c.
DATA:"filepath LIKE rlgrap-filename,
openfile
LIKE rlgrap-filename.
CALL METHOD cl_gui_frontend_services=>directory_browse
EXPORTING
window_title
= ‘文件路径选择‘
initial_folder
= ‘C:‘
CHANGING
selected_folder
= gd_path.
CALL METHOD cl_gui_cfw=>flush.
CONCATENATE gd_path ‘‘ INTO filepath.
* CALL METHOD cl_gui_frontend_services=>file_open_dialog
* EXPORTING
* window_title = ‘Select File‘
* default_filename = ‘*.txt‘
* multiselection = ‘X‘
* CHANGING
* file_table = it_tab
* rc = gd_subrc.
** LOOP AT it_tab INTO openfile-low.
** openfile-sign = ‘I‘.
** openfile-option = ‘EQ‘.
** APPEND openfile.
** ENDLOOP.
* CALL METHOD cl_gui_cfw=>flush.
*
*CALL METHOD cl_gui_frontend_services=>file_save_dialog
* EXPORTING
* default_extension = ‘XLS‘
* default_file_name = ‘产品出库单‘
* initial_directory = ‘c:/temp/‘
* CHANGING
* filename = ld_filename
* path = filepath
* fullpath = ld_fullpath
* user_action = ld_result.
ENDFORM.