You are on page 1of 13

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

*& Report ZMSG_POP


*&---------------------------------------------------------------------*
*& Detalle.......: El programa permite seleccionar los usuarios a los
*&
que se les desea enviar un mensaje, asi como tambin
*&
visualizar los distintos modos activos que tiene en
*&
el momento.
*&
El ejecutar el reporte se repite el mensaje
*&
ingresado tantas veces como la cantidad requerida
*&
por el usuario.
*&---------------------------------------------------------------------*
REPORT zmsg_pop.
TYPE-POOLS:
icon,
rsfs,
rsds,
slis.
TABLES:
sscrfields,
t000,
usr01.

"Campos en las imgenes de seleccin


"Mandantes
"Maestro de usuarios

*----------------------------------------------------------------------*
* PARAMETROS
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE tit1.
SELECTION-SCREEN: BEGIN OF LINE.
* Boton de seleccin de usuario en la DYNPRO.
SELECTION-SCREEN: PUSHBUTTON 1(40) btn USER-COMMAND sel
VISIBLE LENGTH 15.
* Boton de borrar texto en la DYNPRO.
SELECTION-SCREEN: PUSHBUTTON 20(60) btn1 USER-COMMAND clr
VISIBLE LENGTH 1.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN: SKIP.
SELECTION-SCREEN: BEGIN OF LINE,
COMMENT 1(15) text1,
POSITION 20.
PARAMETERS: p_popmsg LIKE sm04dic-popupmsg "Texto ventana dilogo
LOWER CASE.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN: BEGIN OF LINE,
COMMENT 1(15) text2,
POSITION 20.
PARAMETERS: p_cant(1) TYPE n.
SELECTION-SCREEN: END OF LINE.

"Cantidad de mensajes

SELECTION-SCREEN END OF BLOCK blk1.


*----------------------------------------------------------------------* DEFINICION DE VARIABLES, ESTRUCTURAS Y TABLAS INTERNAS.
*-----------------------------------------------------------------------

DATA: BEGIN OF ti_out OCCURS 0,


sel
LIKE sy-input,
tid
LIKE uinfo-tid,
zeit
LIKE uinfo-zeit,
mandt LIKE sy-prefx,
"uinfo-mandt,
bname LIKE uinfo-bname,
name_first LIKE adrp-name_first,
name_last LIKE adrp-name_last,
term
LIKE uinfo-term,
tcode LIKE uinfo-tcode,
extmodi LIKE uinfo-extmodi,
END OF ti_out.
* Copia de la tabla de usuarios.
DATA: BEGIN OF ti_out1 OCCURS 0,
sel
LIKE sy-input,
tid
LIKE uinfo-tid,
zeit
LIKE uinfo-zeit,
mandt LIKE sy-prefx,
"uinfo-mandt,
bname LIKE uinfo-bname,
name_first LIKE adrp-name_first,
name_last LIKE adrp-name_last,
term
LIKE uinfo-term,
tcode LIKE uinfo-tcode,
extmodi LIKE uinfo-extmodi,
END OF ti_out1.
DATA:
ti_uinfo LIKE uinfo OCCURS 0 WITH HEADER LINE,
ti_modos LIKE uinfo2 OCCURS 0 WITH HEADER LINE.
*********************************************************************
* DEFINICION DE Estructuras y tablas para ALV (FM).
*********************************************************************
*====================================================================
DATA:
wa_cat
TYPE slis_fieldcat_alv,
" WA catlogo
ti_cat
TYPE slis_t_fieldcat_alv,
" TI catlogo
ti_cat1
TYPE slis_t_fieldcat_alv,
" TI catlogo
wa_sort
TYPE slis_sortinfo_alv,
" WA sort
ti_sort
TYPE slis_t_sortinfo_alv,
" TI sort
wa_layout TYPE slis_layout_alv,
" WA opciones lista
wa_events TYPE slis_alv_event,
" WA eventos lista
ti_events TYPE slis_t_event,
" TI eventos lista
wa_header TYPE slis_listheader,
" WA cabecera lista
ti_header TYPE slis_t_listheader.
" TI cabecera lista
*----------------------------------------------------------------------* INICIALIZACIN DE VARIABLES
*----------------------------------------------------------------------INITIALIZATION.
* Creo la visualizacin del botn de seleccin de usuario en la
* DYNPRO.
CALL FUNCTION 'ICON_CREATE'
EXPORTING
name = icon_select_block
text = 'Sel.User'
info = 'Sel.User'
IMPORTING

RESULT = btn
EXCEPTIONS
OTHERS = 0.
* Creo la visualizacin del botn de borrar texto en la
* DYNPRO.
CALL FUNCTION 'ICON_CREATE'
EXPORTING
name = icon_delete
text = 'Borrar texto'
info = 'Borrar texto'
IMPORTING
RESULT = btn1
EXCEPTIONS
OTHERS = 0.
tit1 = 'Envo de mensajes:'.
text1 = 'Texto'.
text2 = 'Cant.mensajes'.
p_cant = 1.
*----------------------------------------------------------------------* AT SELECTION-SCREEN
*----------------------------------------------------------------------AT SELECTION-SCREEN.
CASE sscrfields-ucomm.
WHEN 'SEL'.
PERFORM value_req.
WHEN 'CLR'.
* Borro el texto cargado.
CLEAR p_popmsg.
WHEN OTHERS.
* Do Nothing !!.
ENDCASE.
*----------------------------------------------------------------------* TRATAMIENTO DE DATOS DEL PROGRAMA PRINCIPAL
*----------------------------------------------------------------------START-OF-SELECTION.
PERFORM validar_datos.
PERFORM enviar_msg.
*----------------------------------------------------------------------*
* DECLARACIN DE SUBRUTINAS
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&
Form validar_datos
*&---------------------------------------------------------------------*
*
Valido los datos.
*----------------------------------------------------------------------*
FORM validar_datos .
* Verifico si ingresaron algn texto.
IF p_popmsg IS INITIAL.
MESSAGE s893(pz) WITH 'Ingrese texto.'.

STOP.
ENDIF.
* Verifico si ingresaron cantidad.
IF p_cant IS INITIAL.
MESSAGE s893(pz) WITH 'Ingrese cantidad.'.
STOP.
ENDIF.
IMPORT ti_out FROM MEMORY ID sy-repid.
READ TABLE ti_out WITH KEY sel = 'X'.
IF ti_out[] IS INITIAL OR sy-subrc NE 0.
MESSAGE s893(pz) WITH 'Seleccione usuarios.'.
STOP.
ENDIF.
ENDFORM.

" validar_datos

*&---------------------------------------------------------------------*
*&
Form enviar_msg
*&---------------------------------------------------------------------*
*
Armo el POP-UP para los usuarios seleccionados.
*----------------------------------------------------------------------*
FORM enviar_msg .
DATA:
l_cant TYPE i,
l_user TYPE string,
l_text TYPE string.
CLEAR l_cant.
* Elimino los usuarios repetidos.
SORT ti_out BY bname sel.
DELETE ADJACENT DUPLICATES FROM ti_out COMPARING bname sel.
DO p_cant TIMES.
ADD 1 TO l_cant.
LOOP AT ti_out WHERE sel EQ 'X'.
SELECT SINGLE *
FROM t000
WHERE mandt EQ ti_out-mandt.
* Verifico si el mandante es correcto.
CHECK sy-subrc EQ 0.
SELECT SINGLE *
FROM usr01
CLIENT SPECIFIED
WHERE mandt EQ ti_out-mandt
AND bname EQ ti_out-bname.
* Verifico si el usuario es correcto.
CHECK sy-subrc EQ 0.
CALL FUNCTION 'TH_POPUP'
EXPORTING

client
user
MESSAGE
EXCEPTIONS
user_not_found
OTHERS

= ti_out-mandt
= ti_out-bname
= p_popmsg
= 1
= 2.

CHECK sy-subrc IS INITIAL.


CHECK l_cant EQ 1.
CONCATENATE ti_out-bname l_user INTO l_user SEPARATED BY space.
CONCATENATE 'Enviado a:' l_user INTO l_text SEPARATED BY space.
PERFORM progress USING l_text.
ENDLOOP.
ENDDO.
MESSAGE s208(00) WITH l_text.
ENDFORM.

" enviar_msg

*&---------------------------------------------------------------------*
*&
Form value_req
*&---------------------------------------------------------------------*
*
Visualizo la lista de usuario.
*----------------------------------------------------------------------*
FORM value_req .
DATA:
l_repid LIKE sy-repid.
l_repid = sy-repid.
PERFORM obtener_usuarios.
PERFORM construir_catalogo.
PERFORM definir_layout USING
'TI_OUT'
CHANGING wa_layout.
PERFORM construir_eventos.
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
i_callback_program = l_repid
is_layout
= wa_layout
it_fieldcat
= ti_cat
i_default
= 'X'
i_save
= 'A'
it_events
= ti_events
TABLES
t_outtab
= ti_out
EXCEPTIONS
program_error
= 1
OTHERS
= 2.
EXPORT ti_out TO MEMORY ID sy-repid.
ENDFORM.

" value_req

*&---------------------------------------------------------------------*
*&
Form construir_catalogo
*&---------------------------------------------------------------------*
*
Construir catalogo de campos para ALV
*----------------------------------------------------------------------*
FORM construir_catalogo .
DATA:
l_repid LIKE sy-repid.
REFRESH: ti_cat.
l_repid = sy-repid.
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name
= l_repid
i_internal_tabname
= 'TI_OUT'
i_inclname
= l_repid
CHANGING
ct_fieldcat
= ti_cat
EXCEPTIONS
inconsistent_interface = 1
program_error
= 2
OTHERS
= 3.
* Asignar atributos de campo en el catalogo de reporte ALV
PERFORM asignar_campo_a_catalogo TABLES ti_cat
USING:
'SEL'
'MANDT'
'BNAME'
'NAME_FIRST'
'NAME_LAST'
'TERM'
'TID'
'TCODE'
'EXTMODI'
'ZEIT'
ENDFORM.

'X'
'X'
'X'
'X'
'X'
' '
' '
' '
' '
' '

'X'
' '
' '
' '
' '
' '
' '
' '
' '
' '

'X'
' '
' '
' '
' '
' '
' '
' '
' '
' '

'X'
' '
' '
' '
' '
' '
' '
' '
' '
' '

' '
' '
'X'
'X'
'X'
' '
' '
'X'
' '
' '

'Sel',
'Mandante',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' '.

" construir_catalogo

*&---------------------------------------------------------------------*
*&
Form asignar_campo_a_catalogo
*&---------------------------------------------------------------------*
*
Asignar campo al catalogo ALV
*----------------------------------------------------------------------*
*
-->P_FIELDNAME
Nombre de campo en el reporte
*
-->P_KEY
Indicador 'campo clave'
*
-->P_COL_POS
N columna
*----------------------------------------------------------------------*
FORM asignar_campo_a_catalogo TABLES pt_cat STRUCTURE wa_cat
USING p_fieldname
LIKE wa_cat-fieldname
p_key
LIKE wa_cat-key
p_col_pos
TYPE c
p_input
LIKE wa_cat-input
p_checkbox
LIKE wa_cat-checkbox
p_hotspot
LIKE wa_cat-hotspot
p_reptext_ddic LIKE wa_cat-reptext_ddic.

STATICS l_col_pos LIKE sy-cucol.


* Asignar atributos de campo en el catalogo de cabecera ALV
CLEAR wa_cat.
READ TABLE pt_cat INTO wa_cat
WITH KEY fieldname = p_fieldname.
CHECK sy-subrc EQ 0.
CLEAR wa_cat-tech.
IF NOT p_reptext_ddic IS INITIAL.
wa_cat-seltext_l = wa_cat-seltext_m =
wa_cat-seltext_s = wa_cat-reptext_ddic = p_reptext_ddic.
ENDIF.
IF p_col_pos EQ 'X'.
* Inicializo el conteo para ordenar las columnas.
l_col_pos = 1.
ELSE.
ADD 1 TO l_col_pos.
ENDIF.
wa_cat-fieldname
= p_fieldname.
wa_cat-key
= p_key.
wa_cat-col_pos
= l_col_pos.
wa_cat-edit = wa_cat-input = p_input.
wa_cat-checkbox
= p_checkbox.
wa_cat-hotspot
= p_hotspot.
MODIFY pt_cat FROM wa_cat INDEX sy-tabix.
ENDFORM.

" asignar_campo_a_catalogo

*&---------------------------------------------------------------------*
*&
Form definir_layout
*&---------------------------------------------------------------------*
*
Definir atributos del layout de reporte
*----------------------------------------------------------------------*
FORM definir_layout USING
p_tabla
CHANGING p_layout TYPE slis_layout_alv .
CLEAR p_layout.
p_layout-zebra
=
* Marco el campo para la selecin.
p_layout-box_fieldname
=
p_layout-box_tabname
=
p_layout-colwidth_optimize
=
p_layout-no_vline
=
p_layout-no_colhead
=
p_layout-lights_condense
=
p_layout-detail_popup
=
p_layout-detail_initial_lines =
p_layout-flexible_key
=
p_layout-key_hotspot
=
ENDFORM.

'X'.
'SEL'.
p_tabla.
'X'.
' '.
' '.
'X'.
'X'.
'X'.
' '.
' '.

" definir_layout

*&---------------------------------------------------------------------*
*&
Form construir_eventos
*&---------------------------------------------------------------------*
*
Asignar eventos soportados
*----------------------------------------------------------------------*
FORM construir_eventos.
DATA:
l_type(1) TYPE n.
REFRESH: ti_events.
CLEAR: wa_events.
* ALV Simple.
l_type = 0.
* Buscar secciones para eventos
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
EXPORTING
i_list_type = l_type
IMPORTING
et_events = ti_events.
* Leer evento 'TOP_OF_PAGE'
READ TABLE ti_events WITH KEY name = slis_ev_top_of_page
INTO wa_events.
* Asignar Rutina FORM 'TOP_OF_PAGE' a evento
IF sy-subrc = 0.
MOVE slis_ev_top_of_page TO wa_events-form.
MODIFY ti_events FROM wa_events INDEX sy-tabix.
ENDIF.
* Leer evento 'USER_COMMAND'
READ TABLE ti_events WITH KEY name = slis_ev_user_command
INTO wa_events.
* Asignar Rutina FORM 'USER_COMMAND' a evento
IF sy-subrc = 0.
MOVE slis_ev_user_command TO wa_events-form.
MODIFY ti_events FROM wa_events INDEX sy-tabix.
ENDIF.
ENDFORM.

" construir_eventos

*&---------------------------------------------------------------------*
*&
Form top_of_page
*&---------------------------------------------------------------------*
*
Mostrar cabecera de pgina
*---------------------------------------------------------------------FORM top_of_page.
* Construir encabezado del reporte
PERFORM llenar_header.
* Mostrar cabecera
PERFORM show_header.
ENDFORM.
"top_of_page
*&---------------------------------------------------------------------*
*&
Form llenar_header
*&---------------------------------------------------------------------*
*
Construir cabecera del reporte
*----------------------------------------------------------------------*
*
Valores para TYP ( H = Header, S = Selection, A = Action )

*
Cuando TYP = 'H' --> Indicar texto en INFO
*
Cuando TYP = 'S' --> Indicar texto con Parmetro en KEY
*
Indicar texto con valor en INFO
*----------------------------------------------------------------------*
FORM llenar_header.
REFRESH: ti_header.
CLEAR: wa_header.
wa_header-typ = 'H'.
wa_header-info = 'Seleccione el/los usuario/s:'.
APPEND wa_header TO ti_header.
ENDFORM.

" llenar_header

*&---------------------------------------------------------------------*
*&
Form show_header
*&---------------------------------------------------------------------*
*
Mostrar cabecera
*----------------------------------------------------------------------*
FORM show_header.
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
it_list_commentary = ti_header.
ENDFORM.

" show_header

*&---------------------------------------------------------------------*
*&
Form user_command
*&---------------------------------------------------------------------*
*
Evaluar acciones del usuario
*---------------------------------------------------------------------FORM user_command USING ucomm LIKE sy-ucomm
sfields TYPE slis_selfield.
DATA: l_objectname TYPE tstc-tcode.
* Rescatar el registro seleccionado.
IF sfields-tabname EQ 'TI_OUT'.
READ TABLE ti_out INDEX sfields-tabindex.
ELSEIF sfields-tabname EQ 'TI_MODOS'.
READ TABLE ti_modos INDEX sfields-tabindex.
ENDIF.
* Seleccin de opcin.
CASE ucomm.
* DOUBLE CLICK.
WHEN '&IC1' OR '&ICM'.
*
Evaluar valor de campo seleccionado.
CHECK NOT sfields-value IS INITIAL.
CASE sfields-fieldname.
* ########################################################
* #####
Usuario.
#####
* ########################################################
WHEN 'BNAME'.
PERFORM obtener_modos.
PERFORM construir_catalogo1.

PERFORM mostrar_listado.
* ########################################################
* #####
Transaccin.
#####
* ########################################################
WHEN 'TCODE'.
MOVE sfields-value TO l_objectname.
* Visualizo la transaccin.
CALL FUNCTION 'RS_TRANSACTION_SHOW'
EXPORTING
objectname
= l_objectname
EXCEPTIONS
object_not_found = 1
OTHERS
= 2.
*

WHEN OTHERS.
DO NOTHING !!!
ENDCASE.
WHEN OTHERS.
DO NOTHING !!!
ENDCASE.

IF sfields-tabname EQ 'TI_OUT'.
* Hago el refresh de los usuarios en el ALV.
PERFORM obtener_usuarios.
sfields-refresh = 'X'.
ENDIF.
ENDFORM.

"user_command

*&---------------------------------------------------------------------*
*&
Form obtener_modos
*&---------------------------------------------------------------------*
*
Obtengo los modos correspondientes al usuario y mandante.
*----------------------------------------------------------------------*
FORM obtener_modos .
REFRESH ti_modos.
CALL FUNCTION 'TH_LONG_USR_INFO'
EXPORTING
user
= ti_out-bname
TABLES
user_info = ti_modos.
* Elimino los mandantes que no corresponden.
DELETE ti_modos WHERE client NE ti_out-mandt
OR terminal NE ti_out-term.
ENDFORM.

" obtener_modos

*&---------------------------------------------------------------------*
*&
Form construir_catalogo1
*&---------------------------------------------------------------------*
*
Construir catalogo de campos para ALV
*----------------------------------------------------------------------*
FORM construir_catalogo1 .

DATA:
l_repid LIKE sy-repid.
REFRESH: ti_cat1.
l_repid = sy-repid.
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name
= l_repid
i_internal_tabname
= 'TI_MODOS'
i_inclname
= l_repid
CHANGING
ct_fieldcat
= ti_cat1
EXCEPTIONS
inconsistent_interface = 1
program_error
= 2
OTHERS
= 3.
* Asignar atributos de campo en el catalogo de reporte ALV
PERFORM asignar_campo_a_catalogo TABLES ti_cat1
USING:
'SESSION'
'CLIENT'
'USER'
'TERMINAL'
'MODE'
'TCODE'
'TIME'
'LANG'

'X'
'X'
'X'
' '
' '
' '
' '
' '

'X'
' '
' '
' '
' '
' '
' '
' '

'
'
'
'
'
'
'
'

ENDFORM.

'
'
'
'
'
'
'
'

'
'
'
'
'
'
'
'

'
'
'
'
'
'
'
'

' '
' '
' '
' '
' '
'X'
' '
' '

' ',
' ',
' ',
' ',
' ',
' ',
'Hora',
' '.

" construir_catalogo1

*&---------------------------------------------------------------------*
*&
F rm mostrar_listado
*&---------------------------------------------------------------------*
*
Visualizo los modos del usuario.
*----------------------------------------------------------------------*
FORM mostrar_listado .
DATA:
l_repid
LIKE sy-repid,
l_selfield TYPE slis_selfield,
l_exit.
l_repid = sy-repid.
CALL FUNCTION 'REUSE_ALV_POPUP_TO_SELECT'
EXPORTING
i_title
= 'Modos:'
i_selection
= ' '
i_zebra
= 'X'
i_tabname
= 'TI_MODOS'
it_fieldcat
= ti_cat1
i_callback_program
= l_repid
i_callback_user_command = 'USER_COMMAND'
IMPORTING
es_selfield
= l_selfield
e_exit
= l_exit

TABLES
t_outtab
EXCEPTIONS
program_error
OTHERS

= ti_modos

ENDFORM.

= 1
= 2.
" mostrar_listado

*&---------------------------------------------------------------------*
*&
Form progress
*&---------------------------------------------------------------------*
*
Visualizo el mensaje de texto.
*----------------------------------------------------------------------*
*
-->P_TEXT Texto
*----------------------------------------------------------------------*
FORM progress USING p_text TYPE string.
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
text = p_text.
ENDFORM.

" progress

*&---------------------------------------------------------------------*
*&
Form obtener_usuarios
*&---------------------------------------------------------------------*
*
Obtengo los usuarios logueados en el sistema.
*----------------------------------------------------------------------*
FORM obtener_usuarios .
DATA: BEGIN OF lt_user OCCURS 0,
mandt
TYPE usr21-mandt,
bname
TYPE usr21-bname,
persnumber TYPE usr21-persnumber,
name_first TYPE adrp-name_first,
name_last TYPE adrp-name_last,
END OF lt_user.
IMPORT ti_out FROM MEMORY ID sy-repid.
ti_out1[] = ti_out[].
CLEAR ti_out.
REFRESH: ti_out, ti_uinfo.
* Obtengo la lista de usuarios conectados por mandante.
CALL FUNCTION 'TH_USER_LIST'
TABLES
list = ti_uinfo.
IF ti_uinfo[] IS NOT INITIAL.
* Obtengo los nombres de los usuarios.
SELECT a~mandt a~bname a~persnumber b~name_first b~name_last
INTO CORRESPONDING FIELDS OF TABLE lt_user
FROM usr21 AS a INNER JOIN adrp AS b
ON a~persnumber = b~persnumber
AND a~mandt
= b~client
CLIENT SPECIFIED
FOR ALL ENTRIES IN ti_uinfo

WHERE a~mandt EQ ti_uinfo-mandt


AND a~bname EQ ti_uinfo-bname.
ENDIF.
LOOP AT ti_uinfo.
CLEAR ti_out.
MOVE-CORRESPONDING ti_uinfo TO ti_out.
READ TABLE ti_out1 WITH KEY tid
mandt
bname
term
sel

=
=
=
=
=

ti_out-tid
ti_out-mandt
ti_out-bname
ti_out-term
'X'.

* Marco los usuarios marcados con anterioridad.


IF sy-subrc IS INITIAL.
MOVE 'X' TO ti_out-sel.
ELSE.
CLEAR ti_out-sel.
ENDIF.
* Cargo el nombre del usuario.
READ TABLE lt_user WITH KEY mandt = ti_out-mandt
bname = ti_out-bname.
IF sy-subrc IS INITIAL.
MOVE:
lt_user-name_first TO ti_out-name_first,
lt_user-name_last TO ti_out-name_last.
ENDIF.
APPEND ti_out.
ENDLOOP.
SORT ti_out BY mandt bname.
ENDFORM.

" obtener_usuarios