2018년 12월 6일 목요일

AL11 이용한 첨부파일관리

*&---------------------------------------------------------------------*
* 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 <> 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 MAXseq )
      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_filenamelv_existlv_answerlv_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 ).
        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(zfir9000CHANGING ls_zcmt0120-ernam
                                                  ls_zcmt0120-erdat
                                                  ls_zcmt0120-erzet
                                                  ls_zcmt0120-erdat_lt
                                                  ls_zcmt0120-erzet_lt.

    ADD 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_foldersys_comm.
  MOVE sy-datum+0(4TO 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_foldersys_comm.
  CONCATENATE sy-datum+0(4'/' sy-datum+4(2INTO 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_foldersys_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(1TYPE x.

  DATA i_tab   TYPE STANDARD TABLE OF t_line,
         i_wa(1TYPE x.

  DATA lv_line TYPE sy-tabix.

  DATA lt_files    TYPE filetable,
         ld_rc       TYPE VALUE 1,
         lv_filename TYPE string.

  DATA down_path TYPE string.


  FREE lt_filesi_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.






2018년 11월 2일 금요일

임시전표삭제

  SELECT SINGLE *
    FROM VBKPF
   WHERE VBKPF~BUKRS EQ I_BUKRS
     AND VBKPF~GJAHR EQ I_GJAHR
     AND VBKPF~BELNR EQ I_BELNR.
  IF SY-SUBRC EQ 0.
    CASE VBKPF-AWTYP.
      WHEN 'RMRP'.


      WHEN OTHERS.
*     전표 삭제 시 CO/FM전표 삭제도 동시에 하도록..
        CLEAR LS_ACCDEL.
        LS_ACCDEL-AWTYP      'BKPF'.      "참조절차
        LS_ACCDEL-AWREF      VBKPF-BELNR"전표번호
        LS_ACCDEL-STATUS_OLD '3'.         "이전 대체전표의 상태
        CONCATENATE I_BUKRS I_GJAHR INTO LS_ACCDEL-AWORG.

        CALL FUNCTION 'AC_DOCUMENT_DELETE'
          EXPORTING
            I_ACCDEL          LS_ACCDEL
          EXCEPTIONS
            DELETE_IMPOSSIBLE 1
            OTHERS            2.

        CHECK SY-SUBRC EQ 0.
*       임시전표 삭제 펑션
        CALL FUNCTION 'PRELIMINARY_POSTING_DOC_DELETE'
          EXPORTING
            BELNR              I_BELNR
            BSTAT              'Z'
            BUKRS              I_BUKRS
            GJAHR              I_GJAHR
          EXCEPTIONS
            DOCUMENT_NOT_FOUND 1
            UPDATE_ERROR       2
            OTHERS             3.

        CHECK SY-SUBRC EQ 0.
        CALL FUNCTION 'AC_DOCUMENT_POST'
          EXPORTING
            I_AWTYP LS_ACCDEL-AWTYP
            I_AWREF LS_ACCDEL-AWREF
            I_AWORG LS_ACCDEL-AWORG.

        CHECK SY-SUBRC EQ 0.
        CALL FUNCTION 'FI_CHECK_EXTENDED_WT'
          EXPORTING
            I_BUKRS              I_BUKRS
          EXCEPTIONS
            COMPONENT_NOT_ACTIVE 1
            NOT_FOUND            2
            OTHERS               3.

        CALL FUNCTION 'FI_WT_PRELIMINARY_POST_DOC_DEL' IN UPDATE TASK
          EXPORTING
            I_BELNR I_BELNR
            I_BUKRS I_BUKRS
            I_GJAHR I_GJAHR
          EXCEPTIONS
            OTHERS  1.

        IF SY-SUBRC EQ 0.
'.          COMMIT WORK.
        ELSE.
          ROLLBACK WORK.
        ENDIF.

    ENDCASE.
*
  ELSE.
    ROLLBACK WORK.
  ENDIF.

2018년 10월 30일 화요일

엑셀 업로드

*&---------------------------------------------------------------------*
*& Report ZEV_UPLOAD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zev_upload MESSAGE-ID zfi.


TYPE-POOLS truxs.

SELECTION-SCREEN: BEGIN OF BLOCK a WITH FRAME TITLE TEXT-001.

PARAMETER:  p_table LIKE dd02l-tabname,
            p_file LIKE rlgrap-filename. " DEFAULT 'c:\temp\zcvaa.csv'.

SELECTION-SCREEN: END OF BLOCK a.

DATA: it_tab TYPE REF TO data.
DATA: gt_table TYPE REF TO cl_salv_table.

IF p_table+0(1) NE 'Z'.
  MESSAGE s000 WITH 'CBO테이블만 가능합니다' DISPLAY LIKE 'E'.
  EXIT.
ENDIF.

CREATE DATA it_tab TYPE TABLE OF (p_table).

FIELD-SYMBOLS: <fs_tab> TYPE STANDARD TABLE.

ASSIGN it_tab->* TO <fs_tab>.

DATA: it_type   TYPE truxs_t_text_data.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      field_name    = 'P_FILE'
    IMPORTING
      file_name     = p_file.

START-OF-SELECTION.

  IF p_table+0(1) NE 'Z'.
    MESSAGE e000 WITH 'CBO테이블만 가능합니다'.
    EXIT.
  ENDIF.

* uploading the data in the file into internal table
  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
    EXPORTING
*     I_FIELD_SEPERATOR    =
      i_line_header        = 'X'
      i_tab_raw_data       = it_type
      i_filename           = p_file
    TABLES
      i_tab_converted_data = <fs_tab>
    EXCEPTIONS
      conversion_failed    = 1
      OTHERS               = 2.
  IF sy-subrc NE  0.
    MESSAGE ID sy-msgid
            TYPE sy-msgty
            NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

END-OF-SELECTION.

MODIFY (p_table) FROM TABLE <fs_tab>.
DESCRIBE TABLE <fs_tab> LINES DATA(dcr).

WRITE: dcr, 'lines got updated in table', p_table.

템플릿 - Z*PROGRAM ID* _C01

*&---------------------------------------------------------------------*
*& Include          ZEVR0010C01
*&---------------------------------------------------------------------*
*******************************
* LOCAL CLASSES: DEFINITION
*******************************
* 클래스 정의
CLASS lcl_def_receiver DEFINITION.

  PUBLIC SECTION.

    METHODS: handle_top_of_page
               FOR EVENT top_of_page OF cl_gui_alv_grid
               IMPORTING e_dyndoc_id table_index.

    METHODS : handle_toolbar
                 FOR EVENT toolbar OF cl_gui_alv_grid
                 IMPORTING sender
                           e_object
                           e_interactive.

    METHODS : handle_user_command
                 FOR EVENT user_command OF cl_gui_alv_grid
                 IMPORTING sender e_ucomm.

    METHODS : handle_data_changed
                 FOR EVENT data_changed OF cl_gui_alv_grid
                 IMPORTING sender er_data_changed.

    METHODS : handle_after_user_command
                     FOR EVENT after_user_command OF cl_gui_alv_grid.

    METHODS : handle_data_changed_finished
                 FOR EVENT data_changed_finished OF cl_gui_alv_grid
                 IMPORTING sender e_modified
                           et_good_cells.

    METHODS : handle_hotspot_click
                 FOR EVENT hotspot_click OF cl_gui_alv_grid
                 IMPORTING sender e_row_id
                           e_column_id.

    METHODS : handle_double_click
                 FOR EVENT double_click OF cl_gui_alv_grid
                 IMPORTING sender
                           e_row
                           e_column
                           es_row_no.

    METHODS: handle_on_f4 FOR EVENT onf4 OF cl_gui_alv_grid
       IMPORTING sender
                 e_fieldname
                 e_fieldvalue
                 es_row_no
                 er_event_data
                 et_bad_cells
                 e_display.

ENDCLASS.  " LCL_DEF_RECEIVER
*******************************
* LOCAL CLASSES: IMPLEMENTATION
*******************************
* 클래스 구현
CLASS lcl_def_receiver IMPLEMENTATION.

  METHOD handle_top_of_page.
*    PERFORM top_of_page USING e_dyndoc_id table_index.

  ENDMETHOD.                    "HANDLE_TOP_OF_PAGE

  METHOD handle_toolbar.
    IF sender = go_alv1.
*      PERFORM TOOBAR_PART USING SENDER E_OBJECT E_INTERACTIVE.
    ENDIF.
  ENDMETHOD.                    "handle_toolbar

*----------------------------------------------------------------------*
*       CLASS LCL_EVENT_RECEIVER IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
  METHOD handle_after_user_command.
*    PERFORM AFTER_USER_COMMAND.
  ENDMETHOD.                    "HANDLE_AFTER_USER_COMMAND

  METHOD handle_user_command.
    IF sender = go_alv1.
      PERFORM user_command_part USING e_ucomm.
    ENDIF.
  ENDMETHOD.                    "handle_user_command

  METHOD handle_data_changed.
    IF sender = go_alv1.
      PERFORM data_changed USING er_data_changed.
    ENDIF.
  ENDMETHOD.                    "handle_data_changed

  METHOD handle_data_changed_finished.
    IF sender = go_alv1.
      PERFORM data_changed_finished USING e_modified et_good_cells.
    ENDIF.
  ENDMETHOD.                    "handle_data_changed_finished

  METHOD handle_hotspot_click.
    IF sender = go_alv1.
      PERFORM hotspot_click USING e_row_id e_column_id.
    ENDIF.
  ENDMETHOD.                    "handle_hotspot_click

  METHOD handle_double_click.
    IF sender = go_alv1.
      PERFORM double_click USING e_row e_column es_row_no.
    ENDIF.
  ENDMETHOD.                    "handle_double_click

  METHOD handle_on_f4.
    PERFORM on_f4 USING sender
                        e_fieldname
                        e_fieldvalue
                        es_row_no
                        er_event_data
                        et_bad_cells
                        e_display.
  ENDMETHOD.                    "handle_on_f4

ENDCLASS.  " LCL_DEF_RECEIVER
*&---------------------------------------------------------------------*
*&      FORM  TOOBAR_PART
*&---------------------------------------------------------------------*
*       TOOBAR_PART
*----------------------------------------------------------------------*
FORM toobar_part USING sender
                       p_c_object TYPE REF TO cl_alv_event_toolbar_set
                       p_interactive.

*  PERFORM TOOBAR_PART_100 USING P_C_OBJECT P_INTERACTIVE.

ENDFORM.                    " TOOBAR_PART

*&---------------------------------------------------------------------*
*&      FORM  USER_COMMAND_PART
*&---------------------------------------------------------------------*
*       TEXT
*----------------------------------------------------------------------*
*      -->P_E_UCOMM  TEXT
*----------------------------------------------------------------------*
FORM user_command_part  USING p_ucomm.

  IF p_ucomm EQ 'SUB'.
    PERFORM get_selected_row USING go_alv1.
    PERFORM user_command.
  ENDIF.

ENDFORM.                    " USER_COMMAND_PART

*&---------------------------------------------------------------------*
*&      FORM  DATA_CHANGED
*&---------------------------------------------------------------------*
*       TEXT
*----------------------------------------------------------------------*
*      -->P_ER_DATA_CHANGED  TEXT
*----------------------------------------------------------------------*
FORM data_changed USING p_c_data_changed
                          TYPE REF TO cl_alv_changed_data_protocol.

ENDFORM.                    " DATA_CHANGED

*&---------------------------------------------------------------------*
*&      Form  F_OUT_MESSAGE_PROC
*&---------------------------------------------------------------------*
*       에러처리를 한다.
*----------------------------------------------------------------------*
*      -->P_DATA_CHANGED  text
*      -->P_MODF_FIELD    text
*----------------------------------------------------------------------*
FORM f_out_message_proc
        USING p_data_changed TYPE REF TO cl_alv_changed_data_protocol
              p_modf_field   TYPE lvc_s_modi
              ps_msg TYPE t_msg.


*  CALL METHOD P_DATA_CHANGED->ADD_PROTOCOL_ENTRY

*    EXPORTING

*      I_MSGID     = PS_MSG-MSGID

*      I_MSGTY     = PS_MSG-MSGTY

*      I_MSGNO     = PS_MSG-MSGNO

*      I_MSGV1     = PS_MSG-MSG1

*      I_MSGV2     = PS_MSG-MSG2

*      I_MSGV3     = PS_MSG-MSG3

*      I_MSGV4     = PS_MSG-MSG4

*      I_FIELDNAME = P_MODF_FIELD-FIELDNAME

*      I_ROW_ID    = P_MODF_FIELD-ROW_ID.

ENDFORM.                    " F_OUT_MESSAGE_PROC

*&---------------------------------------------------------------------*

*&      FORM  DATA_CHANGED_FINISHED

*&---------------------------------------------------------------------*

*       TEXT

*----------------------------------------------------------------------*

*      -->P_E_MODIFIED  TEXT

*      -->P_ET_GOOD_CELLS  TEXT

*----------------------------------------------------------------------*

FORM data_changed_finished  USING  p_e_modified

                                   et_good_cells. " TYPE LVC_T_MODI



ENDFORM.                    " DATA_CHANGED_FINISHED

*&---------------------------------------------------------------------*
*&      FORM  HOTSPOT_CLICK
*&---------------------------------------------------------------------*
*       TEXT
*----------------------------------------------------------------------*
*      -->P_E_ROW_ID  TEXT
*      -->P_E_COLUMN_ID  TEXT
*----------------------------------------------------------------------*
FORM hotspot_click  USING   p_row_id
                            p_column_id.

  IF p_column_id EQ 'BELNR'.
    IF p_sel01 EQ 'X'.
      CLEAR gs_card.
      READ TABLE gt_card INTO gs_card INDEX p_row_id.
      CHECK sy-subrc EQ 0.

    ELSEIF p_sel02 EQ 'X'.
      CLEAR gs_dti.
      READ TABLE gt_dti INTO gs_dti INDEX p_row_id.
      CHECK sy-subrc EQ 0.

    ELSEIF p_sel03 EQ 'X'.
      CLEAR gs_etc.
      READ TABLE gt_etc INTO gs_etc INDEX p_row_id.
      CHECK sy-subrc EQ 0.

    ENDIF.
  ENDIF.

ENDFORM.                    " HOTSPOT_CLICK

*&---------------------------------------------------------------------*
*&      FORM  DOUBLE_CLICK
*&---------------------------------------------------------------------*
*       TEXT
*----------------------------------------------------------------------*
*      -->P_E_ROW  TEXT
*      -->P_E_COLUMN  TEXT
*      -->P_ES_ROW_NO  TEXT
*----------------------------------------------------------------------*
FORM double_click  USING  p_row TYPE lvc_s_row
                          p_column
                          p_s_row_no.

  RANGES : lr_cardno FOR ztficd10-zcardno,
           lr_mail   FOR zdti_header-zbyre,
           lr_supno  FOR zdti_header-supno,
           lr_pernr  FOR usr02-bname.

  CHECK p_row-index NE 0.

  IF p_sel01 EQ 'X'.
    CLEAR gs_card.
    READ TABLE gt_card INTO gs_card INDEX p_row-index.
    add_range_eq_space : lr_cardno  gs_card-zcardno.
  ELSEIF p_sel02 EQ 'X'.
    CLEAR gs_dti.
    READ TABLE gt_dti INTO gs_dti INDEX p_row-index.
    add_range_eq_space : lr_mail    gs_dti-email,
                         lr_supno   gs_dti-supno.
  ELSEIF p_sel03 EQ 'X'.
    CLEAR gs_etc.
    READ TABLE gt_etc INTO gs_etc INDEX p_row-index.
    add_range_eq_space : lr_pernr   gs_etc-empno.
  ENDIF.

  SUBMIT zevr0020 USING SELECTION-SCREEN 1000
               WITH p_bukrs  EQ p_bukrs
               WITH s_budat  IN s_budat
               WITH p_sel01  EQ p_sel01
               WITH p_sel02  EQ p_sel02
               WITH p_sel03  EQ p_sel03
               WITH p_tot    EQ p_tot
               WITH p_fi     EQ p_fi
               WITH p_tr     EQ p_tr
               WITH s_cardno IN lr_cardno
               WITH s_mail   IN lr_mail
               WITH s_supno  IN lr_supno
               WITH s_pernr  IN lr_pernr
               AND RETURN.

ENDFORM.                    " DOUBLE_CLICK

*&---------------------------------------------------------------------*
*&      Form  ON_F4
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_SENDER  text
*      -->P_E_FIELDNAME  text
*      -->P_E_FIELDVALUE  text
*      -->P_ES_ROW_NO  text
*      -->P_ER_EVENT_DATA  text
*      -->P_ET_BAD_CELLS  text
*      -->P_E_DISPLAY  text
*----------------------------------------------------------------------*
FORM on_f4  USING    p_c_sender     TYPE REF TO cl_gui_alv_grid
                     p_fieldname    TYPE lvc_fname
                     p_fieldvalue   TYPE lvc_value
                     p_s_row_no     TYPE lvc_s_roid
                     p_c_event_data TYPE REF TO cl_alv_event_data
                     p_t_bad_cells  TYPE lvc_t_modi
                     p_display      TYPE char01.

*  PERFORM ON_F4_100 USING P_C_SENDER
*                          P_FIELDNAME
*                          P_FIELDVALUE
*                          P_S_ROW_NO
*                          P_C_EVENT_DATA
*                          P_T_BAD_CELLS
*                          P_DISPLAY.

ENDFORM.                                                    " ON_F4

*&---------------------------------------------------------------------*

*&      Form  ON_F4_100

*&---------------------------------------------------------------------*

*       text

*----------------------------------------------------------------------*

*      -->P_P_C_SENDER  text

*      -->P_P_FIELDNAME  text

*      -->P_P_FIELDVALUE  text

*      -->P_P_S_ROW_NO  text

*      -->P_P_C_EVENT_DATA  text

*      -->P_P_T_BAD_CELLS  text

*      -->P_P_DISPLAY  text

*----------------------------------------------------------------------*

*FORM ON_F4_100  USING    P_C_SENDER        TYPE REF TO CL_GUI_ALV_GRID

*                         P_FIELDNAME   TYPE LVC_FNAME

*                         P_FIELDVALUE  TYPE LVC_VALUE

*                         P_S_ROW_NO     TYPE LVC_S_ROID

*                         P_C_EVENT_DATA TYPE REF TO CL_ALV_EVENT_DATA

*                         P_T_BAD_CELLS  TYPE LVC_T_MODI

*                         P_DISPLAY     TYPE CHAR01.

*

*  DATA : LT_RETURN   LIKE DDSHRETVAL OCCURS 0 WITH HEADER LINE.

*  DATA : LS_LVC_MODI TYPE LVC_S_MODI.

*

*  FIELD-SYMBOLS : <FS_DATA> TYPE LVC_T_MODI.

*  CLEAR : P_DISPLAY .

*

*  CASE P_FIELDNAME.

*    WHEN 'MEINS'.

*  ENDCASE.

*

*ENDFORM.                                                    " ON_F4_100

*&---------------------------------------------------------------------*
*&      Form  SET_ALV_LIST
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_LCL_GRID  text
*      -->P_0062   text
*      -->P_0063   text
*----------------------------------------------------------------------*
FORM display_alv_list USING pv_alv_grid  TYPE REF TO lcl_def_grid
                            p_tname
                            p_cnt.

* 로컬변수선언
  DATA : lv_table LIKE feld-name.

  FIELD-SYMBOLS : <table>   TYPE STANDARD TABLE,
                  <ft_fcat> TYPE STANDARD TABLE,
                  <ft_sort> TYPE STANDARD TABLE,
                  <ft_func> TYPE STANDARD TABLE,
                  <fs_layo> TYPE any.

  DATA : l_subrc LIKE sy-subrc.
  DATA : l_fieldname LIKE dd03l-fieldname.


  CONCATENATE p_tname '[]' INTO lv_table.
  ASSIGN (lv_table) TO <table>.

*-- TOOLBAR ASSIGN
  CONCATENATE 'GT_UI_FUNCTIONS' p_cnt INTO lv_table.
  ASSIGN (lv_table) TO <ft_func>.

*-- FIELDCAT ASSIGN
  CONCATENATE 'GT_LVC_FCAT' p_cnt '[]' INTO lv_table.
  ASSIGN (lv_table) TO <ft_fcat>.

*-- GT_LVC_SORT ASSIGN
  CONCATENATE 'GT_LVC_SORT' p_cnt '[]' INTO lv_table.
  ASSIGN (lv_table) TO <ft_sort>.

  PERFORM check_sort_parameter USING <ft_fcat>[] <ft_sort>[]
    CHANGING l_fieldname l_subrc.

  IF l_subrc NE 0.
    CONCATENATE l_fieldname ` : `
    `Not Exist SORT Field` INTO l_fieldname.
    MESSAGE s000 DISPLAY LIKE 'E' WITH l_fieldname.
    EXIT.
  ENDIF.

  CONCATENATE 'GS_LVC_LAYO' p_cnt INTO lv_table.
  ASSIGN (lv_table) TO <fs_layo>.

  SORT <table>.

  CHECK NOT pv_alv_grid IS INITIAL.

*  gv_consistency_check = 'X'.

  CALL METHOD pv_alv_grid->set_table_for_first_display
    EXPORTING
      is_variant                    = gs_variant
      i_save                        = 'A'
      i_default                     = 'X'
      is_layout                     = <fs_layo>
      i_consistency_check           = gv_consistency_check
      it_toolbar_excluding          = <ft_func>
    CHANGING
      it_outtab                     = <table>
      it_fieldcatalog               = <ft_fcat>
      it_sort                       = <ft_sort>
    EXCEPTIONS
      invalid_parameter_combination = 1
      program_error                 = 2
      too_many_lines                = 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.
  ENDIF.

ENDFORM.                    " SET_ALV_LIST

*&---------------------------------------------------------------------*
*&      Form  SET_FIELDCATALOG_MERGE_X01
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_C_STRUC  text
*      <--P_GT_LVC_FCAT1  text
*----------------------------------------------------------------------*
FORM set_fcat_ddic_x01
  USING p_strnam LIKE dd02l-tabname
  CHANGING p_t_lvc_fcat TYPE lvc_t_fcat.

  DATA : ls_lvc_fcat TYPE lvc_s_fcat.
  DATA : lt_dd03t TYPE TABLE OF dd03t WITH HEADER LINE.


  CLEAR:   p_t_lvc_fcat, p_t_lvc_fcat[], ls_lvc_fcat.

  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
      i_structure_name       = p_strnam
      i_bypassing_buffer     = 'X'
    CHANGING
      ct_fieldcat            = p_t_lvc_fcat
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.

*-- 사용자 정의 구조 추가
*  PERFORM FILL_FIELD_CATALOGS TABLES P_T_LVC_FCAT USING:
*          'S' 'FIELDNAME'     'STATUS',
*          ' ' 'KEY'           'X',
*          ' ' 'JUST'          'C',
*          ' ' 'OUTPUTLEN'     '3',
*          ' ' 'ICON'          'X',
*          ' ' 'COL_POS'       '1',
*          ' ' 'REF_TABLE'     'ICON',
*          ' ' 'REF_FIELD'     'ID',
*          'E' 'SCRTEXT_L'     'STATUS',
*
*          'S' 'FIELDNAME'     'MESSAGE',
*          ' ' 'JUST'          'L',
*          ' ' 'OUTPUTLEN'     '60',
*          ' ' 'COL_POS'       '90',
*          ' ' 'DATATYPE'      'CHAR',
*          ' ' 'INTLEN'        '255',
*          'E' 'SCRTEXT_L'     'MESSAGE'.

  LOOP AT p_t_lvc_fcat INTO ls_lvc_fcat.
*-- 숫자는 오른쪽 정렬
    IF ls_lvc_fcat-datatype EQ 'DEC' OR ls_lvc_fcat-datatype CP 'INT+'
    OR ls_lvc_fcat-datatype EQ 'QUAN' OR ls_lvc_fcat-datatype EQ 'CURR'.
      ls_lvc_fcat-just = 'R'.
    ENDIF.

**-- 필드별 속성 재정의
*    PERFORM FCAT_SCR_0100_X01 USING P_STRNAM
*                         CHANGING LS_LVC_FCAT.

*- 필드 텍스트 넣기
    IF ls_lvc_fcat-scrtext_l IS NOT INITIAL.
      ls_lvc_fcat-coltext = ls_lvc_fcat-scrtext_l.
    ELSE.
      ls_lvc_fcat-coltext = ls_lvc_fcat-reptext.
    ENDIF.

    ls_lvc_fcat-seltext = ls_lvc_fcat-coltext.

    MODIFY p_t_lvc_fcat FROM ls_lvc_fcat.
  ENDLOOP.

ENDFORM.                    " SET_FIELDCATALOG_MERGE_X01

*&---------------------------------------------------------------------*
*&      Form  FILL_FIELD_CATALOGS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_T_LVC_FCAT  text
*      -->P_GUB         text
*      -->P_FNAME       text
*      -->P_VALUE       text
*----------------------------------------------------------------------*
FORM fill_field_catalogs  TABLES p_t_lvc_fcat TYPE lvc_t_fcat
                           USING p_gub  p_fname  p_value.

  IF p_gub = 'S'.
    CLEAR gs_lvc_fcat.
  ENDIF.

  DATA l_fname(40).
  FIELD-SYMBOLS <fs> TYPE any.

  CONCATENATE 'GS_LVC_FCAT-' p_fname INTO l_fname.
  ASSIGN (l_fname) TO <fs>.
  <fs> = p_value.

  IF p_gub = 'E'.
    APPEND gs_lvc_fcat TO p_t_lvc_fcat.
  ENDIF.

ENDFORM.                    " FILL_FIELD_CATALOGS

*&---------------------------------------------------------------------*
*&      Form  FILL_FCAT_X01
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_&1  text
*      -->P_&2  text
*      -->P_&3  text
*      -->P_P_SFCAT  text
*      -->P_P_TFCAT  text
*      -->P_P_RFCAT  text
*----------------------------------------------------------------------*
FORM fill_fcat  USING p_gub p_fname p_con
      p_sfcat TYPE lvc_s_fcat
      p_tfcat TYPE lvc_t_fcat
      p_ref_fcat TYPE lvc_t_fcat.

  DATA : ls_fcat TYPE lvc_s_fcat.


  CLEAR : ls_fcat.

  IF   p_gub =  'S'. " 시작에서 담고 계속 돌면서..''E'를 만났을 때 APPEND해준다.
    CLEAR p_sfcat.
    READ  TABLE p_ref_fcat INTO ls_fcat
         WITH KEY fieldname  = p_fname.

    MOVE-CORRESPONDING ls_fcat TO p_sfcat.

    p_sfcat-fieldname  = p_fname.
    "gt_fieldcat_h와 gs_fieldcat에서 Name이 다른 Field 정보를 넘겨준다.
    p_sfcat-scrtext_l  = ls_fcat-scrtext_l.
    p_sfcat-scrtext_m  = ls_fcat-scrtext_m.
    p_sfcat-scrtext_s  = ls_fcat-scrtext_s.
    p_sfcat-reptext    = ls_fcat-reptext.
    p_sfcat-colddictxt = ls_fcat-colddictxt.
    p_sfcat-ref_field  = ls_fcat-ref_field.
    p_sfcat-ref_table  = ls_fcat-ref_table.
    p_sfcat-roundfield = ls_fcat-roundfield.
    p_sfcat-decmlfield = ls_fcat-decmlfield.
    p_sfcat-decimals   = ls_fcat-decimals.
    EXIT.
  ENDIF.


  DATA : l_col(40).

  FIELD-SYMBOLS : <fs>.

  CONCATENATE 'GS_LVC_FCAT-' p_fname INTO l_col.
  ASSIGN (l_col) TO <fs>.
  MOVE p_con TO <fs>.

  IF p_gub = 'E'.
    IF p_sfcat-datatype  = 'CURR'.
      IF    p_sfcat-cfieldname IS INITIAL
        AND p_sfcat-currency   IS INITIAL.
        READ TABLE p_ref_fcat INTO ls_fcat
              WITH KEY datatype  = 'CUKY'.
        IF sy-subrc EQ 0.
          p_sfcat-cfieldname = ls_fcat-fieldname.
        ELSE.
          p_sfcat-currency  = 'KRW'.
        ENDIF.
      ENDIF.
    ENDIF.

    IF p_sfcat-datatype = 'QUAN'.
      IF p_sfcat-qfieldname IS INITIAL.
        READ TABLE p_ref_fcat INTO ls_fcat
          WITH KEY datatype = 'UNIT'.
        IF sy-subrc EQ 0.
          p_sfcat-qfieldname = ls_fcat-fieldname.
        ENDIF.
      ENDIF.
    ENDIF.

    gv_alv_colpos = gv_alv_colpos + 1.

    p_sfcat-col_pos = gv_alv_colpos.

    IF p_sfcat-outputlen IS INITIAL.
      p_sfcat-outputlen = ls_fcat-intlen + 2.
    ENDIF.

    " 숫자형은 오른쪽 정렬.
    IF p_sfcat-datatype EQ 'DEC' OR p_sfcat-datatype CP 'INT+'
    OR p_sfcat-datatype EQ 'QUAN' OR p_sfcat-datatype EQ 'CURR'.
      p_sfcat-just = 'R'.
    ENDIF.

    p_sfcat-seltext = p_sfcat-coltext.

    APPEND p_sfcat TO p_tfcat.

    CLEAR : p_sfcat.
  ENDIF.

ENDFORM.                    " FILL_FCAT_X01



*&---------------------------------------------------------------------*

*&      Form  GET_LIKEITAB_FACT

*&---------------------------------------------------------------------*

*       text

*----------------------------------------------------------------------*

*      -->P_P_ITAB_NAME  text

*      -->P_P_STRUCTURE_NAME  text

*      -->P_P_RFCAT  text

*----------------------------------------------------------------------*

FORM get_likeitab_fact  USING p_likeitab_name

                                 p_ddic_fcat TYPE lvc_t_fcat.



  DATA : lt_table LIKE dd02l-tabname OCCURS 0 WITH HEADER LINE.

  DATA : lt_fieldcat   TYPE slis_t_fieldcat_alv.

  DATA : lt_alv_cat   TYPE TABLE OF lvc_s_fcat.

  DATA : l_alv_cat TYPE lvc_s_fcat.



  CLEAR : p_ddic_fcat[], p_ddic_fcat.

  CLEAR : lt_table[], lt_table.

  CLEAR : lt_fieldcat[], lt_fieldcat.

  CLEAR : lt_alv_cat[], lt_alv_cat.



*  "Internal Table을 참조하여 Field Catalog 정보를 구성한다.

  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

    EXPORTING

      i_program_name         = sy-repid

      i_internal_tabname     = p_likeitab_name

      i_inclname             = sy-repid

*     I_STRUCTURE_NAME       = P_STRUCTURE_NAME

      i_bypassing_buffer     = 'X'

*     I_BUFFER_ACTIVE        = 'X'

    CHANGING

      ct_fieldcat            = lt_fieldcat[]

    EXCEPTIONS

      inconsistent_interface = 1

      program_error          = 2.



  CALL FUNCTION 'LVC_TRANSFER_FROM_SLIS'

    EXPORTING

      it_fieldcat_alv = lt_fieldcat

    IMPORTING

      et_fieldcat_lvc = lt_alv_cat

    TABLES

      it_data         = lt_table[].



  CALL FUNCTION 'LVC_FIELDCAT_COMPLETE'

    CHANGING

      ct_fieldcat = lt_alv_cat.



  CLEAR : p_ddic_fcat, p_ddic_fcat[].



  LOOP AT lt_alv_cat INTO l_alv_cat WHERE no_out =  'X'.

    CLEAR : l_alv_cat-no_out.

    MODIFY lt_alv_cat FROM l_alv_cat INDEX sy-tabix.

  ENDLOOP.



  p_ddic_fcat[] = lt_alv_cat[].

  CLEAR : lt_alv_cat, lt_alv_cat[].



ENDFORM.                    " GET_LIKEITAB_FACT

*&---------------------------------------------------------------------*
*&      Form  SET_TOP_OF_PAGE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GO_CONTAINER_TOP1  text
*      -->P_GO_ALV1  text
*      -->P_GO_DD_DOCUMENT1  text
*      -->P_GO_HTML_VIEW1  text
*----------------------------------------------------------------------*
FORM set_top_of_page    USING
        p_top_cont TYPE REF TO cl_gui_container
        p_grid TYPE REF TO lcl_def_grid
        p_doc TYPE REF TO cl_dd_document
        p_html TYPE REF TO cl_gui_html_viewer.


  DATA : dl_length  TYPE i.                           " Length
  DATA : dl_background_id TYPE sdydo_key VALUE 'ALV_BACKGROUND'.
  DATA : l_alv_grid TYPE sdydo_attribute VALUE 'CL_GUI_ALV_GRID'.

  IF p_doc IS INITIAL.
    CREATE OBJECT p_doc
      EXPORTING
        style = l_alv_grid.
  ENDIF.

  CALL METHOD p_doc->initialize_document.
  CALL METHOD p_grid->list_processing_events
    EXPORTING
      i_event_name = 'TOP_OF_PAGE'
      i_dyndoc_id  = p_doc.

*CREATING HTML CONTROL
  IF p_html IS INITIAL.
    CREATE OBJECT p_html
      EXPORTING
        parent = p_top_cont.
  ENDIF.

* REUSE_ALV_GRID_COMMENTARY_SET
  CALL FUNCTION 'REUSE_ALV_GRID_COMMENTARY_SET'
    EXPORTING
      document = p_doc
      bottom   = space
    IMPORTING
      length   = dl_length.

* GET TOP->HTML_TABLE READY
  CALL METHOD p_doc->merge_document.

* Set wallpaper
  CALL METHOD p_doc->set_document_background
    EXPORTING
      picture_id = dl_background_id.

  PERFORM set_top_of_page_text USING p_doc.

* Connect TOP document to HTML-Control
  p_doc->html_control = p_html.

* Display TOP document
  CALL METHOD p_doc->display_document
    EXPORTING
      reuse_control      = 'X'
      parent             = p_top_cont
    EXCEPTIONS
      html_display_error = 1.

ENDFORM.                    " SET_TOP_OF_PAGE

*&---------------------------------------------------------------------*
*&      Form  SET_TOP_OF_PAGE_TEXT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM set_top_of_page_text USING
      p_doc TYPE REF TO cl_dd_document.

  LOOP AT gt_top_of_page_txt.
    CALL METHOD p_doc->add_text
      EXPORTING
        text         = gt_top_of_page_txt-top_text
        sap_fontsize = gt_top_of_page_txt-fontsize
        sap_color    = gt_top_of_page_txt-color.
    CALL METHOD p_doc->new_line.
  ENDLOOP.

ENDFORM.                    " SET_TOP_OF_PAGE_TEXT