antelio
8/12/2019 - 8:03 PM

Atualização de Usuários com dados de Funcionários

*&---------------------------------------------------------------------*
*& Report  ZRBC003
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zrbc003.

TABLES: usr01.
TYPE-POOLS: truxs.
SELECTION-SCREEN BEGIN OF BLOCK usr WITH FRAME .
PARAMETERS:
 p_bname LIKE usr01-bname DEFAULT '*'.
SELECTION-SCREEN END OF BLOCK usr.

SELECTION-SCREEN BEGIN OF BLOCK par WITH FRAME .
PARAMETERS: p_file TYPE  rlgrap-filename OBLIGATORY.
PARAMETERS: p_head TYPE char01 DEFAULT 'X'.
SELECTION-SCREEN END OF BLOCK par.

SELECTION-SCREEN BEGIN OF BLOCK var.
PARAMETERS: p_vari LIKE disvariant-variant.
SELECTION-SCREEN END OF BLOCK var.


TYPES: BEGIN OF ty_excel,
  col1(50) TYPE c, "Razão Social
  col2(50) TYPE c, "Nome Funcionário
  col3(14) TYPE c, "Data Admição
  col4(50) TYPE c, "Cargo
  col5(7) TYPE c,  "Centro de Custo
  col6(50) TYPE c, "Descrição Centro de Custo
  col7(60) TYPE c, "E-Mail
  col8(50) TYPE c, "Gestor Imediato
  col9(15) TYPE c, "CPF
END OF ty_excel.
DATA: t_excel TYPE STANDARD TABLE OF ty_excel,
      w_excel LIKE LINE OF t_excel.

DATA: it_raw TYPE truxs_t_text_data.


TYPE-POOLS: slis, sydes.

DATA: "Dynpro Control
      lvc_t_fcat TYPE lvc_t_fcat,
      lvc_t_sort TYPE lvc_t_sort,
      lvc_w_layout TYPE lvc_s_layo,
      lvc_t_filter TYPE lvc_t_filt.

DATA: BEGIN OF t_output OCCURS 0,
  bname LIKE usr01,
  END OF t_output.

DATA: g_save,
      g_exit,
      g_variant TYPE disvariant,
      gx_variant TYPE disvariant.

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

INITIALIZATION.
  g_save = 'A'.
  CLEAR g_variant.
  g_variant-report = sy-cprog.
* Get default variant
  gx_variant = g_variant.
  CALL FUNCTION 'REUSE_ALV_VARIANT_DEFAULT_GET'
    EXPORTING
      i_save     = g_save
    CHANGING
      cs_variant = gx_variant
    EXCEPTIONS
      not_found  = 2.
  IF sy-subrc = 0.
    p_vari = gx_variant-variant.
  ENDIF.
* Process on value request
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_vari.
  CALL FUNCTION 'REUSE_ALV_VARIANT_F4'
  EXPORTING
    is_variant          = g_variant
    i_save              = g_save
*           it_default_fieldcat =
  IMPORTING
    e_exit              = g_exit
    es_variant          = gx_variant
  EXCEPTIONS
    not_found = 2.
  IF sy-subrc = 2.
    MESSAGE ID sy-msgid TYPE 'S'      NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ELSE.
    IF g_exit = space.
      p_vari = gx_variant-variant.
    ENDIF.
  ENDIF.

START-OF-SELECTION.
  PERFORM load_excel.
  PERFORM load_users.


***********************************************************************
* END-OF-SELECTION.
END-OF-SELECTION.
  " For sample, Excel Data transfered to internal table is displayed with write
*  LOOP AT it_datatab INTO wa_datatab.
*    WRITE:/
*      wa_datatab-col1,
*      wa_datatab-col2,
*      wa_datatab-col3,
*      wa_datatab-col4,
*      wa_datatab-col5,
*      wa_datatab-col6,
*      wa_datatab-col7,
*      wa_datatab-col8,
*      wa_datatab-col9.
*  ENDLOOP.

*  CALL FUNCTION 'HR_IT_SHOW_ANY_TABLE_ON_ALV'
*    TABLES
*      table    = t_excel
*    EXCEPTIONS
*      fb_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.
*&---------------------------------------------------------------------*
*&      Form  LOAD_EXCEL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM load_excel .
  " Convert Excel Data to SAP internal Table Data
  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
  EXPORTING
*     I_FIELD_SEPERATOR        =
    i_line_header            =  p_head
    i_tab_raw_data           =  it_raw       " WORK TABLE
    i_filename               =  p_file
  TABLES
    i_tab_converted_data     = t_excel[]  "ACTUAL DATA
  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.                    " LOAD_EXCEL
*&---------------------------------------------------------------------*
*&      Form  LOAD_USERS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM load_users .
  DATA: t_users TYPE TABLE OF  bapiusname,
        t_return TYPE TABLE OF bapiret2.
  CALL FUNCTION 'BAPI_USER_GETLIST'
   EXPORTING
*     MAX_ROWS              = 0
     with_username         = p_bname
*   IMPORTING
*     ROWS                  =
   TABLES
*     SELECTION_RANGE       =
*     SELECTION_EXP         =
     userlist              = t_users
     return                = t_return.

  DATA: w_user LIKE LINE OF t_users,
        w_output LIKE LINE OF t_output.
  CLEAR: t_output[].
  LOOP AT t_users INTO w_user.
    CLEAR w_output.
    w_output-bname = w_user-username.

    APPEND w_output TO t_output.
  ENDLOOP.

ENDFORM.                    " LOAD_USERS