*&---------------------------------------------------------------------*
* COMMON DEFINITION
*&---------------------------------------------------------------------*
DATA : gv_ok_code LIKE sy-ucomm.
DATA : gv_tar_fpath TYPE string.
DATA : gv_src_fpath TYPE string.
DATA : gv_number TYPE string.
DATA : gv_wf_attach_key TYPE ze_wf_attach_key.
DATA : gv_wf_file_path TYPE ze_file_path.
DATA : gv_wf_file_path2 TYPE ze_file_path.
DATA : lv_cfilename TYPE rcgfiletr-ftfront, "Full File Name ( Character )
lv_sfilename TYPE string, "Full File Name ( String )
lv_sfilename_org TYPE string. "File Name ( String )
DATA : lt_data_tab LIKE rcgrepfile OCCURS 10 WITH HEADER LINE.
DATA : lt_file_table TYPE filetable.
DATA : lv_filelength TYPE i.
DATA : ls_rcgfiletr TYPE rcgfiletr.
DATA : lv_url TYPE ze_wf_url.
DATA : l_rc TYPE i.
DATA : l_len TYPE i.
DATA : i_lines TYPE i.
DATA : l_all_lines_len TYPE i.
DATA : l_diff_len TYPE i.
DATA : lg_max_len TYPE i VALUE 2550.
DATA : lv_max TYPE zeseq3.
DATA : lv_filename TYPE string.
DATA : lv_exist.
DATA : lv_answer.
DATA : lv_text TYPE string.
CLEAR : lv_cfilename,
lv_sfilename,
lv_sfilename_org,
ls_rcgfiletr,
l_rc,
lv_max,
ls_zcmt0120,
ls_zcmt0121,
lv_text.
FREE : lt_data_tab,
lt_zcmt0120,
lt_zcmt0121,
lt_file_table.
1. 첨부파일 추가
"1) 사용자가 FILE을 선택한다.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
window_title = ' '
default_extension = ' '
default_filename = ' '
multiselection = 'X'
CHANGING
file_table = lt_file_table[]
rc = l_rc
EXCEPTIONS
file_open_dialog_failed = 1
cntl_error = 2
error_no_gui = 3
not_supported_by_gui = 4
OTHERS = 5.
IF sy-subrc <> 0 OR l_rc = -1.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
RETURN.
ENDIF.
* 증빙번호 생성
IF gv_wf_attach_key EQ space.
PERFORM get_next_number USING gc_01
gc_zdcm03
CHANGING gv_number.
gv_number = sy-datum && gv_number.
* 폴더생성
PERFORM create_folder USING gc_3
CHANGING gv_wf_file_path.
ELSE.
SELECT MAX( seq )
INTO lv_max
FROM zcmt0121
WHERE wf_attach_key EQ gv_wf_attach_key.
ENDIF.
LOOP AT lt_file_table INTO DATA(ls_file_table).
CLEAR : lv_sfilename,
ls_rcgfiletr,
lv_sfilename_org,
lv_filelength,
i_lines,
l_len,
l_all_lines_len,
l_diff_len,
ls_zcmt0120,
ls_zcmt0121.
FREE : lt_data_tab.
lv_sfilename = ls_file_table-filename.
ls_rcgfiletr-ftfront = ls_file_table-filename.
"2) 선택된 파일의 [순수이름]을 구한다.
CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH'
EXPORTING
full_name = lv_sfilename
IMPORTING
stripped_name = lv_sfilename_org
EXCEPTIONS
OTHERS = 1.
"3) 선택된 파일의 존재유무확인
CLEAR : lv_filename, lv_exist, lv_answer, lv_text.
lv_filename = gv_wf_file_path && lv_sfilename_org.
OPEN DATASET lv_filename FOR INPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc EQ 0.
CLOSE DATASET lv_filename.
CLEAR lv_answer.
CONCATENATE lv_filename TEXT-t10 INTO lv_text SEPARATED BY space.
PERFORM popup_to_confirm USING TEXT-t09
lv_text
'1'
' '
CHANGING lv_answer.
IF lv_answer EQ gc_1.
lv_exist = gc_x.
ELSE.
CONTINUE.
ENDIF.
ENDIF.
"4) local을 파일을 업로드한다.
CALL FUNCTION 'C13Z_UPLOAD'
EXPORTING
* CODEPAGE = ' '
filename = ls_rcgfiletr-ftfront
filetype = 'BIN'
IMPORTING
filelength = lv_filelength
TABLES
data_tab = lt_data_tab.
ls_rcgfiletr-ftappl = gv_wf_file_path && lv_sfilename_org.
ls_rcgfiletr-iefow = 'X'.
ls_rcgfiletr-ftftype = 'BIN'.
DESCRIBE TABLE lt_data_tab LINES i_lines.
OPEN DATASET ls_rcgfiletr-ftappl FOR OUTPUT IN BINARY MODE.
l_len = lg_max_len.
IF lv_filelength = 0.
i_lines = 0.
ENDIF.
LOOP AT lt_data_tab.
IF sy-tabix = i_lines.
l_all_lines_len = lg_max_len * ( i_lines - 1 ).
l_diff_len = lv_filelength - l_all_lines_len.
l_len = l_diff_len.
ENDIF.
CATCH SYSTEM-EXCEPTIONS dataset_write_error = 1
OTHERS = 4.
TRANSFER lt_data_tab TO ls_rcgfiletr-ftappl LENGTH l_len.
ENDCATCH.
IF NOT sy-subrc IS INITIAL.
* RAISE WRITE_FAILED.
ENDIF.
ENDLOOP.
CATCH SYSTEM-EXCEPTIONS dataset_cant_close = 1
OTHERS = 4.
CLOSE DATASET ls_rcgfiletr-ftappl.
ENDCATCH.
IF gv_wf_attach_key EQ space.
MOVE gv_number TO ls_zcmt0120-wf_attach_key.
ELSE.
MOVE gv_wf_attach_key TO ls_zcmt0120-wf_attach_key.
ENDIF.
CHECK lv_exist EQ space.
PERFORM set_user_date_time(zfir9000) CHANGING ls_zcmt0120-ernam
ls_zcmt0120-erdat
ls_zcmt0120-erzet
ls_zcmt0120-erdat_lt
ls_zcmt0120-erzet_lt.
ADD 1 TO lv_max.
MOVE-CORRESPONDING ls_zcmt0120 TO ls_zcmt0121.
MOVE lv_max TO ls_zcmt0121-seq.
MOVE gc_3 TO ls_zcmt0121-flag.
MOVE gv_wf_file_path TO ls_zcmt0121-wf_file_path.
MOVE lv_filelength TO ls_zcmt0121-wf_file_size.
MOVE lv_sfilename_org TO ls_zcmt0121-wf_file_name.
APPEND ls_zcmt0120 TO lt_zcmt0120.
APPEND ls_zcmt0121 TO lt_zcmt0121.
ENDLOOP.
MODIFY zcmt0120 FROM TABLE lt_zcmt0120.
MODIFY zcmt0121 FROM TABLE lt_zcmt0121.
IF sy-subrc EQ 0.
COMMIT WORK.
PERFORM get_message USING gc_s TEXT-s01.
MESSAGE s000 WITH TEXT-s01.
ELSE.
ROLLBACK WORK.
PERFORM get_message USING gc_e TEXT-e03.
MESSAGE s000 WITH TEXT-e03.
ENDIF.
IF gv_number NE space.
MOVE gv_number TO gv_wf_attach_key.
ENDIF.
FORM get_next_number USING p_nrrang p_object
CHANGING p_docno.
DATA : lv_fldnam TYPE string,
lv_nrrang LIKE inrdp-nrrangenr,
lv_object LIKE nriv-object.
lv_nrrang = p_nrrang.
lv_object = p_object.
CALL FUNCTION 'NUMBER_GET_NEXT'
EXPORTING
nr_range_nr = lv_nrrang
object = lv_object
IMPORTING
number = p_docno
EXCEPTIONS
interval_not_found = 1
number_range_not_intern = 2
object_not_found = 3
quantity_is_0 = 4
quantity_is_not_1 = 5
interval_overflow = 6
buffer_overflow = 7
OTHERS = 8.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
CLEAR p_docno.
ENDIF.
ENDFORM. " F_GET_NEXT_NUMBER
FORM create_folder USING pv_flag
CHANGING pv_filepath.
DATA : lv_url TYPE zcmt0191-url.
DATA : lv_folder TYPE string.
DATA : sys_comm LIKE rlgrap-filename.
DATA : BEGIN OF tabl OCCURS 0,
line(400),
END OF tabl.
" 폴더생성
CLEAR lv_url.
SELECT SINGLE url
INTO lv_url
FROM zcmt0191
WHERE sysid EQ syst-sysid
AND flag EQ pv_flag.
*1.연도폴더
CLEAR : lv_folder, sys_comm.
MOVE sy-datum+0(4) TO lv_folder.
CONCATENATE 'mkdir ' lv_url INTO sys_comm SEPARATED BY space.
sys_comm = sys_comm && lv_folder.
CALL 'SYSTEM' ID 'COMMAND' FIELD sys_comm
ID 'TAB' FIELD tabl[].
*2.월폴더
CLEAR : lv_folder, sys_comm.
CONCATENATE sy-datum+0(4) '/' sy-datum+4(2) INTO lv_folder.
CONCATENATE 'mkdir ' lv_url INTO sys_comm SEPARATED BY space.
sys_comm = sys_comm && lv_folder.
CALL 'SYSTEM' ID 'COMMAND' FIELD sys_comm
ID 'TAB' FIELD tabl[].
*3.첨부파일 증빙번호 폴더생성
CLEAR : lv_folder, sys_comm.
CONCATENATE sy-datum+0(4) '/' sy-datum+4(2) '/' gv_number INTO lv_folder.
CONCATENATE 'mkdir ' lv_url INTO sys_comm SEPARATED BY space.
sys_comm = sys_comm && lv_folder.
CALL 'SYSTEM' ID 'COMMAND' FIELD sys_comm
ID 'TAB' FIELD tabl[].
CONCATENATE lv_url lv_folder '/' INTO gv_wf_file_path.
ENDFORM.
2. 첨부파일 삭제
DATA : lv_answer.
DATA : lv_delete_file TYPE string.
PERFORM get_selected_row USING go_alv1.
IF gt_rows[] IS NOT INITIAL.
CLEAR lv_answer.
PERFORM popup_to_confirm USING TEXT-t06 첨부파일삭제
TEXT-t07 선택한 첨부파일을 삭제하시겠습니까?
'1'
' '
CHANGING lv_answer.
CHECK lv_answer EQ gc_1.
CLEAR gs_rows.
LOOP AT gt_rows INTO gs_rows.
CLEAR gs_list.
READ TABLE gt_list INTO gs_list INDEX gs_rows-index.
UPDATE zcmt0121 SET wf_file_del_flag = gc_x
aenam = sy-uname
aedat = sy-datum
aezet = sy-uzeit
aedat_lt = sy-datlo
aezet_lt = sy-timlo
WHERE wf_attach_key EQ gs_list-wf_attach_key
AND seq EQ gs_list-seq.
CLEAR gs_rows.
CLEAR lv_delete_file.
lv_delete_file = gs_list-wf_file_path && '/' && gs_list-wf_file_name.
OPEN DATASET lv_delete_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc EQ 0.
CLOSE DATASET lv_delete_file.
DELETE DATASET lv_delete_file.
ENDIF.
ENDLOOP.
IF sy-subrc EQ 0.
COMMIT WORK.
MESSAGE s000 WITH TEXT-s01.
PERFORM get_message USING gc_s TEXT-s01.
ELSE.
ROLLBACK WORK.
MESSAGE s000 WITH TEXT-e03.
PERFORM get_message USING gc_e TEXT-e03.
ENDIF.
ELSE.
MESSAGE s009 DISPLAY LIKE gc_e.
ENDIF.
3. 다운로드
* File Download
TYPES : t_line(1) TYPE x.
DATA : i_tab TYPE STANDARD TABLE OF t_line,
i_wa(1) TYPE x.
DATA : lv_line TYPE sy-tabix.
DATA : lt_files TYPE filetable,
ld_rc TYPE i VALUE 1,
lv_filename TYPE string.
DATA : down_path TYPE string.
FREE : lt_files, i_tab.
CLEAR : gv_src_fpath,
gv_tar_fpath,
lv_line,
i_wa,
ld_rc,
lv_filename.
PERFORM get_selected_row USING go_alv1.
DESCRIBE TABLE gt_rows LINES lv_line.
IF lv_line = 1.
CLEAR gs_rows.
LOOP AT gt_rows INTO gs_rows.
CLEAR gs_list.
READ TABLE gt_list INTO gs_list INDEX gs_rows-index.
CLEAR gs_rows.
ENDLOOP.
ELSEIF lv_line = 0.
MESSAGE s009 DISPLAY LIKE gc_e.
EXIT.
ELSE.
MESSAGE s027 DISPLAY LIKE gc_e.
EXIT.
ENDIF.
CALL METHOD cl_gui_frontend_services=>directory_get_current
CHANGING
current_directory = down_path.
lv_filename = gs_list-wf_file_name.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
window_title = ' '
default_filename = lv_filename
file_filter = ' '
initial_directory = 'C:\'
CHANGING
file_table = lt_files
rc = ld_rc.
READ TABLE lt_files INTO lv_filename INDEX 1.
IF sy-subrc NE 0.
MESSAGE s014 DISPLAY LIKE gc_e.
EXIT.
ENDIF.
gv_src_fpath = gs_list-wf_file_path && gs_list-wf_file_name.
OPEN DATASET gv_src_fpath FOR INPUT IN BINARY MODE.
DO.
CLEAR i_wa.
READ DATASET gv_src_fpath INTO i_wa.
IF sy-subrc <> 0.
EXIT.
ELSE.
APPEND i_wa TO i_tab.
ENDIF.
ENDDO.
CLOSE DATASET gv_src_fpath.
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
filename = lv_filename
append = ' '
filetype = 'BIN'
CHANGING
data_tab = i_tab
EXCEPTIONS
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21
OTHERS = 24.
IF sy-subrc EQ 0.
MESSAGE s000 WITH TEXT-s01.
PERFORM get_message USING gc_s TEXT-s01.
ELSE.
MESSAGE s000 WITH TEXT-e03.
PERFORM get_message USING gc_e TEXT-e03.
ENDIF.