VinceCoder
5/8/2019 - 10:09 AM

SALV: EVENTI E ALV DISPLAY

eSEMPIO DI EVENTI E Display

*----------------------------------------------------------------------*
*       CLASS lcl_handle_events DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_handle_events DEFINITION.
  PUBLIC SECTION.
    METHODS:
      on_user_command FOR EVENT added_function OF cl_salv_events
        IMPORTING e_salv_function.
ENDCLASS.                    "lcl_handle_events DEFINITION


CLASS lcl_handle_events IMPLEMENTATION.

  METHOD on_user_command.
    PERFORM user_command USING e_salv_function .

  ENDMETHOD.                    "on_user_command

ENDCLASS.                    "lcl_handle_events IMPLEMENTATION


FORM user_command USING i_function TYPE salv_de_function.
  DATA: lt_sel_rows TYPE salv_t_row, " righe selezionate
        ls_sel_row  TYPE LINE OF salv_t_row,
        ls_output   TYPE ty_order_info,
        lt_data     TYPE ty_t_order_info,
        lv_count    TYPE i.

  CASE i_function.
    WHEN 'SEND'.
      CALL FUNCTION 'AUTHORITY_CHECK'
        EXPORTING
          user                = sy-uname
          object              = 'ZSCADENZE'
        EXCEPTIONS
          user_dont_exist     = 1
          user_is_authorized  = 2
          user_not_authorized = 3
          user_is_locked      = 4
          OTHERS              = 5.

      IF sy-subrc NE 2.
        MESSAGE e398(00) WITH 'Utente non autorizzato a inviare email'.
        EXIT.
      ENDIF.
      lt_sel_rows = obj_alv->get_sel_rows( ).

      IF lt_sel_rows IS INITIAL.
        "Nessuna riga selezionata
        MESSAGE TEXT-e01 TYPE 'W'.
        EXIT.
      ENDIF.

      LOOP AT lt_sel_rows INTO ls_sel_row.
        CLEAR: ls_output.
        READ TABLE gt_output INTO ls_output INDEX ls_sel_row.
        IF sy-subrc EQ 0.
          APPEND ls_output TO  lt_data.
        ENDIF.
      ENDLOOP.

      PERFORM send_email USING lt_data.

    WHEN 'MODIFY'.
      lt_sel_rows = obj_alv->get_sel_rows( ).

      IF lt_sel_rows IS INITIAL.
        MESSAGE TEXT-e01 TYPE 'W'.
        EXIT.
      ENDIF.

      DESCRIBE TABLE lt_sel_rows LINES lv_count.
      IF lv_count > 1.
        MESSAGE TEXT-e03 TYPE 'W'.
        EXIT.
      ELSE.
        READ TABLE lt_sel_rows INTO ls_sel_row INDEX 1.
        READ TABLE gt_output INTO ls_output INDEX  ls_sel_row.
        PERFORM open_me22n USING ls_output-ebeln.
      ENDIF.
  ENDCASE.

ENDFORM.                    "user_command

FORM open_me22n USING pi_ebeln TYPE ebeln.

  SET PARAMETER ID 'BES' FIELD  pi_ebeln.

  CALL TRANSACTION 'ME22N' AND SKIP FIRST SCREEN.

ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  DISPLAY_ALV
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GT_OUTPUT  text
*      <--P_GV_MSG  text
*----------------------------------------------------------------------*
FORM display_alv  CHANGING    p_gt_output TYPE ty_t_order_info
  p_gv_msg TYPE bapi_msg.

  DATA: lt_column_ref   TYPE salv_t_column_ref,
        lr_columns      TYPE REF TO cl_salv_columns_table,
        lr_event        TYPE REF TO lcl_handle_events,
        lr_events       TYPE REF TO cl_salv_events_table,
        lv_string_excel TYPE xstring ,         " mod.ydv200717
        lc_excel_alv    TYPE REF TO zcl_alv,   " mod.ydv200717
        lv_emailaddr    TYPE adr6-smtp_addr,   " mod.ydv200717
*        lt_tvarvc       TYPE TABLE OF tvarvc,  " mod.ydv200717    "CM - 06/10/17 - old
*        ls_tvarvc       TYPE tvarvc ,          " mod.ydv200717    "CM - 06/10/17 - old
        ls_sort         TYPE lvc_s_sort,       " mod.ydv200717
        lt_sort         TYPE TABLE OF lvc_s_sort, " mod.ydv200717
        ls_layout       TYPE lvc_s_layo. "tdv20170720


  DATA ls_output LIKE LINE OF p_gt_output.                  "as170720

  DATA: ls_components TYPE abap_compdescr.
  DATA: lo_strucdescr TYPE REF TO cl_abap_structdescr,
        lt_components TYPE TABLE OF abap_compdescr.

  DATA: lt_fcat TYPE lvc_t_fcat,
        ls_fcat LIKE LINE OF lt_fcat.

  "" Leggo L'istanza del oggetto e se non esiste lo crea
  PERFORM get_instance CHANGING obj_alv
    p_gt_output.

  ""Attribuisco gli eventi
  lr_events = obj_alv->r_table->get_event( ).
  CREATE OBJECT lr_event.
  SET HANDLER lr_event->on_user_command FOR lr_events.

  "mod.ydv200717

  IF sy-batch = 'X'.
    ls_sort-fieldname = 'EINDT_EXP'.
    APPEND ls_sort TO lt_sort.

***     <as170720>

    lo_strucdescr ?= cl_abap_typedescr=>describe_by_data( ls_output ).
    lt_components = lo_strucdescr->components.
    LOOP AT lo_strucdescr->components INTO ls_components.
      ls_fcat-fieldname = ls_components-name.
      ls_fcat-tabname = 'P_GT_OUTPUT'.
      CASE ls_fcat-fieldname.
        WHEN 'LIFNR'.
          ls_fcat-coltext = TEXT-l07.
        WHEN 'NAME1'.
          ls_fcat-coltext = TEXT-l08.
        WHEN 'TELF1'.
          ls_fcat-coltext = TEXT-l09.
        WHEN 'ADRNR'.
          ls_fcat-coltext = TEXT-l10.
          ls_fcat-tech = abap_true.
        WHEN 'STRAS'.
          ls_fcat-coltext = TEXT-l11.
        WHEN 'EBELN'.
          ls_fcat-coltext = TEXT-l12.
        WHEN 'EBELP'.
          ls_fcat-coltext = TEXT-l13.
        WHEN 'AEDAT'.
          ls_fcat-coltext = TEXT-l14.
        WHEN 'EKGRP'.
          ls_fcat-coltext = TEXT-l15.
*viian 08.05.2019 10:29:29 beg
        WHEN 'BSART'.
          ls_fcat-coltext = TEXT-l22.
*viian 08.05.2019 10:29:34 end
        WHEN 'EINDT_REQ'.
          ls_fcat-coltext = TEXT-l01.
        WHEN 'EINDT_CONF'.
          ls_fcat-coltext = TEXT-l02.
        WHEN 'EINDT_EXP'.
          ls_fcat-coltext = TEXT-l03.
        WHEN 'MATNR'.
          ls_fcat-coltext = TEXT-l16.
        WHEN 'MAKTX'.
          ls_fcat-coltext = TEXT-l17.
        WHEN 'MENGE_ORD'.
          ls_fcat-coltext = TEXT-l04.
        WHEN 'MENGE_CONF'.
          ls_fcat-coltext = TEXT-l05.
        WHEN 'WEMNG'.
          ls_fcat-coltext = TEXT-l18.
        WHEN 'MENGE_RES'.
          ls_fcat-coltext = TEXT-l06.
        WHEN 'NETWR'.
          ls_fcat-coltext = TEXT-l19.
          ls_fcat-do_sum = 'X'.
*        ls_fcat-EMPHASIZE = 'C311'.
      ENDCASE.
      APPEND ls_fcat TO lt_fcat.
      CLEAR ls_fcat.
    ENDLOOP.

***     </as170720>

    CREATE OBJECT lc_excel_alv.

    lc_excel_alv->create_xls_from_itab(
      EXPORTING
     it_sort         = lt_sort
*     it_filt         =
*     is_layout       = ls_layout
        i_xlsx          = 'X'
*     i_apserv        =
*     i_path          =
      IMPORTING
        e_xstring_excel = lv_string_excel
      CHANGING
     it_fieldcat    = lt_fcat
        ct_data         = p_gt_output
           ).

**CM - 06/07/17 - begin
*    SELECT *
*    FROM tvarvc
*    INTO TABLE lt_tvarvc
*    WHERE name = 'Z_INVIO_SCADENZE_MAIL_XLS'.
*
*    LOOP AT lt_tvarvc INTO ls_tvarvc.
*      lv_emailaddr = ls_tvarvc-low.
*      PERFORM job_send_mail USING lv_string_excel lv_emailaddr.    "CM - 06/10/17 - old
*    ENDLOOP.

    PERFORM job_send_mail USING lv_string_excel.    "CM - 06/10/17 - new
**CM - 06/07/17 - end

  ELSE.
    "endmod.ydv

    " Mostro la alv

    obj_alv->display(
    EXPORTING
      p_f_cols_sett = 'column_settings'
      p_status = gv_status
    CHANGING
      pt_data = p_gt_output

      ).
  ENDIF.
ENDFORM.


*&---------------------------------------------------------------------*
*&      Form  get_instance
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PR_ALV     text
*----------------------------------------------------------------------*
FORM get_instance CHANGING pr_alv TYPE REF TO zcl_alv
  pt_output.

  IF pr_alv IS INITIAL.
    CREATE OBJECT pr_alv.
    IF pr_alv IS NOT INITIAL.
      pr_alv->init(
      EXPORTING
        p_f_cols_sett = 'column_settings' " Form Per il set del field catalog
        p_status = gv_status
      CHANGING
        ct_data = pt_output
        ).
    ENDIF.
  ENDIF.

ENDFORM.                    "get_instance

FORM column_settings USING  pr_columns TYPE REF TO cl_salv_columns_table.

  DATA: lr_column     TYPE REF TO cl_salv_column_table,
        lt_column_ref TYPE salv_t_column_ref,
        ls_column_ref TYPE salv_s_column_ref,
        lt_colo       TYPE STANDARD TABLE OF lvc_s_colo,
        ls_colo       TYPE lvc_s_colo.

  lt_column_ref = pr_columns->get( ).

  LOOP AT lt_column_ref INTO ls_column_ref.
    TRY.
        lr_column ?= pr_columns->get_column( ls_column_ref-columnname ).
      CATCH cx_salv_not_found.
    ENDTRY.
    DATA: ls_green TYPE lvc_s_colo.
    DATA: ls_green1 TYPE lvc_s_colo.
    ls_green-col = 5.
    ls_green-inv = 1.
    ls_green1 = ls_green.
    ls_green1-int = 1.

    IF lr_column IS NOT INITIAL.

**  Rinomino header colonne
      CASE ls_column_ref-columnname.
        WHEN 'ADRNR'.
          lr_column->set_technical( 'X' ).
        WHEN  'EINDT_REQ'.
          lr_column->set_long_text( TEXT-l01 ) .
          lr_column->set_medium_text( TEXT-m01 ).
          lr_column->set_short_text( TEXT-s01 ).

        WHEN  'EINDT_CONF'.
          lr_column->set_long_text( TEXT-l02 ).
          lr_column->set_medium_text( TEXT-m02 ).
          lr_column->set_short_text( TEXT-s02 ).

        WHEN  'EINDT_EXP'.
          lr_column->set_long_text( TEXT-l03 ).
          lr_column->set_medium_text( TEXT-m03 ).
          lr_column->set_short_text( TEXT-s03 ).
          lr_column->set_color( ls_green ).
        WHEN  'MENGE_ORD'.
          lr_column->set_long_text( TEXT-l04 ).
          lr_column->set_medium_text( TEXT-m04 ).
          lr_column->set_short_text( TEXT-s04 ).
        WHEN  'MENGE_CONF'.
          lr_column->set_long_text( TEXT-l05 ).
          lr_column->set_medium_text( TEXT-m05 ).
          lr_column->set_short_text( TEXT-s05 ).
        WHEN  'MENGE_RES'.
          lr_column->set_long_text( TEXT-l06 ).
          lr_column->set_medium_text( TEXT-m06 ).
          lr_column->set_short_text( TEXT-s06 ).
      ENDCASE.

*  Imposto colore colonna per date
      IF lr_column->get_ddic_datatype( ) EQ 'DATS'.
        CLEAR : ls_colo.
        MOVE 2 TO ls_colo-col.
        MOVE 1 TO ls_colo-int.
        MOVE 1 TO ls_colo-inv.
        lr_column->set_color( ls_colo ).
      ENDIF.

    ENDIF.
  ENDLOOP.
ENDFORM.                    "column_settings