You are on page 1of 29

************************************************************************

*
BXTI
*
************************************************************************
* Nombre del Programa: REPORT z_ylrv2_alv3.
*
* Reporte
: ejerciio de joins con salida a alv o txt
*
* Consultor
: Yonathan Lora
*
* Fecha Creacin
: 02 de Diciembre *********************************
REPORT Z_YLR_CAM1V2.
INCLUDE:
z_ylr_cam1v2_top,
z_ylr_cam1v2_s01,
z_ylr_cam1v2_f01.
START-OF-SELECTION.
PERFORM f_join1.
END-OF-SELECTION.

*&---------------------------------------------------------------------*
*& Include
Z_YLR_CAM1V2_TOP
*&---------------------------------------------------------------------*
TABLES:
bkpf,
zylr_cam1db.
TYPES:
BEGIN OF tab1,
belnr TYPE bkpf-belnr,
bukrs TYPE bkpf-bukrs,
blart TYPE bkpf-blart,
budat TYPE bkpf-budat,
monat TYPE bkpf-monat,
gjahr TYPE bkpf-gjahr,
END OF tab1,
BEGIN OF tab2,
dmbtr TYPE bseg-dmbtr,
buzei TYPE bseg-buzei,
augdt TYPE bseg-augdt,
bschl TYPE bseg-bschl,
shkzg TYPE bseg-shkzg,
lifnr TYPE bseg-lifnr,
*
campos para join
belnr TYPE bseg-belnr,

bukrs TYPE bseg-bukrs,


gjahr TYPE bseg-gjahr,
END OF tab2,
BEGIN OF tab3,
butxt TYPE t001-butxt,
land1 TYPE t001-land1,
*
ccampors para el join con tab1
bukrs TYPE t001-bukrs,
END OF tab3,
BEGIN OF tab4,
name1
TYPE lfa1-name1,
stras
TYPE lfa1-stras,
regio
TYPE lfa1-regio,
city1
TYPE adrc-city1,
city_code TYPE adrc-city_code,
*
*
campo para join
lifnr
TYPE lfa1-lifnr,
END OF tab4,

*
*

*
*

BEGIN OF tabi,
belnr
TYPE bkpf-belnr,
bukrs
TYPE bkpf-bukrs,
buzei
TYPE bseg-buzei,
blart
TYPE bkpf-blart,
budat
TYPE bkpf-budat,
monat
TYPE bkpf-monat,
gjahr
TYPE bkpf-gjahr,
tab2
dmbtr
TYPE bseg-dmbtr,
buzei
TYPE bseg-buzei,
augdt
TYPE bseg-augdt,
bschl
TYPE bseg-bschl,
shkzg
TYPE bseg-shkzg,
lifnr
TYPE bseg-lifnr,
tab3
butxt
TYPE t001-butxt,
land1
TYPE t001-land1,
tab4
name1
TYPE lfa1-name1,
stras
TYPE lfa1-stras,
regio
TYPE lfa1-regio,
city1
TYPE adrc-city1,
city_code
TYPE adrc-city_code,
numuser(20) TYPE c,
fecha
TYPE d,
END OF tabi,
BEGIN OF tab5,
chk
TYPE c,

tab1
belnr
bukrs
blart
budat
monat
gjahr
*
tab2
dmbtr
buzei
augdt
bschl
shkzg
lifnr
*
tab3
butxt
land1
*
tab4
name1
stras
regio
city1
city_code
numuser(20)
fecha
check
END OF tab5,
*

TYPE
TYPE
TYPE
TYPE
TYPE
TYPE

bkpf-belnr,
bkpf-bukrs,
bkpf-blart,
bkpf-budat,
bkpf-monat,
bkpf-gjahr,

TYPE
TYPE
TYPE
TYPE
TYPE
TYPE

bseg-dmbtr,
bseg-buzei,
bseg-augdt,
bseg-bschl,
bseg-shkzg,
bseg-lifnr,

TYPE t001-butxt,
TYPE t001-land1,
TYPE
TYPE
TYPE
TYPE
TYPE
TYPE
TYPE
TYPE

lfa1-name1,
lfa1-stras,
lfa1-regio,
adrc-city1,
adrc-city_code,
c,
d,
c,

campo de cabecera TXT


BEGIN OF ty_xlhead,
fld TYPE char40,
END OF ty_xlhead.

"For header name

DATA:
it_tab1
TYPE STANDARD TABLE OF tab1,
it_tab2
TYPE STANDARD TABLE OF tab2,
it_tab3
TYPE STANDARD TABLE OF tab3,
it_tab4
TYPE STANDARD TABLE OF tab4,
it_tab5
TYPE STANDARD TABLE OF tab5,
* it_tabi TYPE STANDARD TABLE OF tabi,
it_tabi
TYPE TABLE OF zylr_cam1db,
wa_tab1
TYPE tab1,
wa_tab2
TYPE tab2,
wa_tab3
TYPE tab3,
wa_tab4
TYPE tab4,
wa_tab5
TYPE tab5,
* wa_tabi TYPE tabi,
wa_tabi
TYPE zylr_cam1db,
ij
TYPE i VALUE 0,
aux
TYPE string,

variables para el
g_it_fieldcat TYPE
g_st_fieldcat TYPE
w_i_fieldcat TYPE

ALV
slis_t_fieldcat_alv,
slis_fieldcat_alv,
slis_fieldcat_alv,

variable para imprimir con colores el write


colortxt
TYPE i VALUE 1.

*DATA: g_repid TYPE sy-repid.


*g_repid = sy-repid.

DATA: i_fieldcat
i_layout
v_repid
it_sort
wa_sort

TYPE
TYPE
LIKE
TYPE
TYPE

slis_t_fieldcat_alv,
slis_layout_alv,
sy-repid,
slis_t_sortinfo_alv,
slis_sortinfo_alv,

datos para la cabecera del TXT


it_xlhead
TYPE STANDARD TABLE OF ty_xlhead,
wa_xlhead
TYPE ty_xlhead,

* datos para guardar fichero


filename
TYPE string,
rutafinal
TYPE string,
v_usr_action TYPE i,
v_path
TYPE string, "directorio del archivo
v_fullpath
TYPE string, "ruta del arhivo completa
v_filename
TYPE string, "nombre del archivo
p_file
TYPE file_name,
num_selec_cb TYPE i.

CONSTANTS:
lc_status TYPE slis_formname VALUE 'F_PF_STATUS_SET',
c_ucomm
TYPE slis_formname VALUE 'F_USER_COMMAND',
c_check(1) VALUE 'X'.

FIELD-SYMBOLS: <fs_tabi>
<fs_tab1>
<fs_tab2>
<fs_tab3>
<fs_tab4>
<fs_tab5>
<fs_itab5>

LIKE
LIKE
LIKE
LIKE
LIKE
LIKE
TYPE

LINE OF
LINE OF
LINE OF
LINE OF
LINE OF
LINE OF
tab5.

it_tabi,
it_tab1,
it_tab2,
it_tab3,
it_tab4,
it_tab5,

*&---------------------------------------------------------------------*
*& Include
Z_YLR_CAM1V2_S01
*&---------------------------------------------------------------------*

SELECTION-SCREEN BEGIN OF BLOCK block1 WITH FRAME TITLE text-001.


PARAMETERS:
t_code
TYPE bkpf-tcode OBLIGATORY,
clas_doc TYPE bkpf-blart OBLIGATORY.
SELECT-OPTIONS:
r_fechas FOR bkpf-bldat OBLIGATORY.
*r_fechas FOR sy-datum.
SELECTION-SCREEN BEGIN OF BLOCK block2 WITH FRAME TITLE text-002.
PARAMETERS: write RADIOBUTTON GROUP rad1 DEFAULT 'X',
alv
RADIOBUTTON GROUP rad1.
SELECTION-SCREEN END OF BLOCK block2.
SELECTION-SCREEN END OF BLOCK block1.
FORM mi_user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
CASE r_ucomm.
WHEN '&BT3'. "Se ha hecho click en el botn
*
MESSAGE 'Has pulsado el botn Gueardar' TYPE 'I'.
PERFORM f_gaurdarBD.

WHEN '&BT4'. "Se ha hecho click en el botn


MESSAGE 'Has pulsado el botn Exportar' TYPE 'I'.
PERFORM f_exportarTXT.

WHEN '&BT5'. "Se ha hecho click en el botn


MESSAGE 'Has pulsado el botn Exportar' TYPE 'I'.
PERFORM f_borrardatos.
ENDCASE.
ENDFORM.
"mi_user_command
*

*&---------------------------------------------------------------------*
*&
Form mi_standard
*&---------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
FORM mi_standard USING rt_extab TYPE slis_t_extab.
SET PF-STATUS 'STANDARD'. "Nombre del Status GUI
ENDFORM.

*&---------------------------------------------------------------------*
*& Include
Z_YLR_CAM1V2_F01
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&
Form JOIN1
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM f_join1 .
SELECT

belnr
bukrs
blart
budat
monat
gjahr
INTO CORRESPONDING FIELDS OF TABLE it_tab1
FROM bkpf
WHERE tcode = t_code
AND blart = clas_doc
AND bldat IN r_fechas.

IF sy-subrc EQ 0 AND NOT it_tab1[] IS INITIAL.


PERFORM f_join2.
ELSE.
MESSAGE i208(00) WITH text-003.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*&
Form JOIN2
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM f_join2 .
SELECT dmbtr
buzei
augdt
bschl
shkzg
lifnr
belnr

bukrs
gjahr
FROM bseg INTO TABLE it_tab2
FOR ALL ENTRIES IN it_tab1
WHERE bukrs = it_tab1-bukrs
AND belnr = it_tab1-belnr
AND gjahr = it_tab1-gjahr.
IF sy-subrc EQ 0 AND NOT it_tab2[] IS INITIAL.
PERFORM f_exist_lifnr.
PERFORM join3.
ELSE.
MESSAGE i208(00) WITH text-004.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*&
Form JOIN3
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM join3 .
SELECT butxt
land1
bukrs
FROM t001 INTO TABLE it_tab3
FOR ALL ENTRIES IN it_tab1
WHERE bukrs = it_tab1-bukrs.
IF sy-subrc EQ 0 AND NOT it_tab3[] IS INITIAL.
IF ij > 0.
PERFORM join4.
ELSE.
MESSAGE i208(00) WITH text-007.
ENDIF.
PERFORM const_alv.
ELSE.
MESSAGE i208(00) WITH text-008.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*&
Form JOIN4
*&---------------------------------------------------------------------*

*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM join4 .
IF ij > 0.
SELECT lfa1~name1
lfa1~stras
lfa1~regio
adrc~city1
adrc~city_code
FROM lfa1
INNER JOIN adrc
ON
adrc~name1 =
INTO CORRESPONDING
FOR ALL ENTRIES IN
WHERE lfa1~lifnr =

lfa1~name1
FIELDS OF TABLE it_tab4
it_tab2
it_tab2-lifnr.

ENDIF.
ENDFORM.

*&---------------------------------------------------------------------*
*&
Form EXIST_LIFNR
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM f_exist_lifnr .
LOOP AT it_tab2 INTO wa_tab2.
aux = ''.
aux = wa_tab2-lifnr.
CONDENSE aux .
IF strlen( aux ) NE 0.
ij = ij + 1.
ENDIF.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*&
Form CONST_ALV
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*

* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM const_alv .
*

ASSIGN it_tab5 TO

<fs_tab5>.

LOOP AT it_tab2 ASSIGNING <fs_tab2>.


READ TABLE it_tab1
ASSIGNING <fs_tab1>
WITH KEY belnr = <fs_tab2>-belnr "ojo aqui en e key no se pone
b1-belnr si no no lo encuentra
bukrs = <fs_tab2>-bukrs
gjahr = <fs_tab2>-gjahr.

it_ta

IF sy-subrc EQ 0. "verificacion de los datos leidos con read no sean


en blanco.
IF <fs_tab1> IS
wa_tab5-belnr
wa_tab5-bukrs
wa_tab5-blart
wa_tab5-budat
wa_tab5-monat
wa_tab5-gjahr
wa_tab5-dmbtr
wa_tab5-buzei
wa_tab5-augdt
wa_tab5-bschl
wa_tab5-shkzg
wa_tab5-lifnr
wa_tab5-lifnr

ASSIGNED.
= <fs_tab1>-belnr.
= <fs_tab1>-bukrs.
= <fs_tab1>-blart.
= <fs_tab1>-budat.
= <fs_tab1>-monat.
= <fs_tab1>-gjahr.
= <fs_tab2>-dmbtr.
= <fs_tab2>-buzei.
= <fs_tab2>-augdt.
= <fs_tab2>-bschl.
= <fs_tab2>-shkzg.
= <fs_tab2>-lifnr.
= <fs_tab2>-lifnr.

ENDIF.
READ TABLE it_tab3
ASSIGNING <fs_tab3>
WITH KEY bukrs = <fs_tab1>-bukrs.
IF sy-subrc EQ 0.
* llemanos los datos de la 3ra tab
wa_tab5-butxt = <fs_tab3>-butxt.
wa_tab5-land1 = <fs_tab3>-land1.
IF ij > 0.
READ TABLE it_tab4
ASSIGNING <fs_tab4>
WITH KEY lifnr = <fs_tab2>-lifnr.
IF sy-subrc EQ 0.

*
*
*
*
*
*
*
*

wa_tab5-name1 = <fs_tab4>-name1.
wa_tab5-stras = <fs_tab4>-stras.
wa_tab5-regio = <fs_tab4>-regio.
wa_tab5-city1 = <fs_tab4>-city1.
wa_tab5-city_code = <fs_tab4>-city_code.
ENDIF.
se hizo el 4to join
ELSE.
no se hizo el 4to join
wa_tab5-name1 = <fs_tab4>-name1.
wa_tab5-stras = <fs_tab4>-stras.
wa_tab5-regio = <fs_tab4>-regio.
wa_tab5-city1 = <fs_tab4>-city1.
wa_tab5-city_code = <fs_tab4>-city_code.
ENDIF.

ENDIF.
*

//agregamos la fecha y el nombre del usuario


wa_tab5-numuser = sy-uname.
wa_tab5-fecha = sy-datum.
APPEND wa_tab5 TO it_tab5.
APPEND INITIAL LINE TO it_tab5 ASSIGNING <fs_tab5>.
ENDIF. "verificacion de los datos leidos con read no sean en blanco.
ENDLOOP.
CASE 'X'.
WHEN alv.
PERFORM imprimealv.
WHEN write.
PERFORM imprimewrite.
ENDCASE.
CLEAR wa_tab5.
UNASSIGN <fs_tab1>.
UNASSIGN <fs_tab3>.
UNASSIGN <fs_tab2>.
IF ij > 0.
UNASSIGN <fs_tab4>.
ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*&
Form IMPRIMEWRITE

*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM imprimewrite .
*escribimos la cabezera
FORMAT INVERSE COLOR = 5.
WRITE: /(12) 'belnr',
(6)'bukrs',
(4)'blart',
(12)'budat',
(4)'monat',
(6)'gjahr',
* ' tab2
(17)'dmbtr',
(6)'buzei',
(6)'augdt',
(12)'bschl',
(6)'shkzg',
(6)'lifnr',
*
tab3
(15)'butxt',
(6)'land1',
*
tab4
(6)'name1',
(6)'stras',
(6)'regio',
(6)'city1',
(10)'city_code'.
FORMAT INVERSE OFF.
ULINE AT 0(170).

LOOP AT it_tab5 ASSIGNING <fs_tab5>.


IF colortxt = 5.
colortxt = 1.
ENDIF.
FORMAT INTENSIFIED COLOR = colortxt.
WRITE:/(12) <fs_tab5>-belnr,
(6) <fs_tab5>-bukrs, (4) <fs_tab5>-blart, (12) <fs_tab5>-budat.
colortxt = colortxt + 1.
FORMAT INTENSIFIED COLOR = colortxt.
WRITE:

(4) <fs_tab5>-monat, (6) <fs_tab5>-gjahr, (17)<fs_tab5>-dmbtr, (6) <f


s_tab5>-buzei.
colortxt = colortxt + 1.
FORMAT INTENSIFIED COLOR = colortxt.
WRITE:
(6)<fs_tab5>-augdt, (12)<fs_tab5>-bschl, (6)<fs_tab5>-shkzg,
(6)<fs_tab5>-lifnr.
colortxt = colortxt + 1.
FORMAT INTENSIFIED COLOR = colortxt.
WRITE:
*
tab3
(15) <fs_tab5>-butxt, (6)<fs_tab5>-land1, (6)<fs_tab5>-name1,
(6)<fs_tab5>-stras.
colortxt = colortxt + 1.
FORMAT INTENSIFIED COLOR = colortxt.
WRITE:
(6)<fs_tab5>-regio, (6)<fs_tab5>-city1, (10)<fs_tab5>-city_code.
ENDLOOP.
FORMAT INTENSIFIED OFF.
UNASSIGN <fs_tab5>.
ENDFORM.

*&---------------------------------------------------------------------*
*&
Form IMPRIMEALV
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM imprimealv .
PERFORM crea_cab_alv.
PERFORM crea_body_alv.
* funcin alv
* CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
*
EXPORTING
*
it_fieldcat
= g_it_fieldcat[]
*
TABLES
*
t_outtab
= it_tab5[]
*
EXCEPTIONS
*
program_error = 1

OTHERS

= 2.

*CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'


* EXPORTING
*
i_callback_program
= g_repid
*
i_callback_pf_status_set = 'MI_STANDARD'
*
i_callback_user_command = 'MI_USER_COMMAND'
*
it_fieldcat
= g_it_fieldcat[]
* TABLES
*
t_outtab
= it_tab5[]
* EXCEPTIONS
*
program_error
= 1
*
OTHERS
= 2.
v_repid = sy-repid.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program
= v_repid
is_layout
= i_layout
it_fieldcat
= i_fieldcat[]
it_sort
= it_sort
i_callback_pf_status_set = 'MI_STANDARD'
i_callback_user_command = 'MI_USER_COMMAND'
TABLES
t_outtab
= it_tab5
EXCEPTIONS
program_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.
ENDFORM.
*&---------------------------------------------------------------------*
*&
Form CREA_CAB_ALV
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM crea_cab_alv .
* Datos del fieldcat para
g_st_fieldcat-fieldname
g_st_fieldcat-seltext_m
g_st_fieldcat-seltext_s
APPEND g_st_fieldcat TO

el ALV
= 'belnr'. "Nombre del campo de la tabla
= 'Sociedad'. "Descripcin media
= 'Sociedad'. "Descripcin corta
g_it_fieldcat.

g_st_fieldcat-fieldname
g_st_fieldcat-seltext_m
g_st_fieldcat-seltext_s
APPEND g_st_fieldcat TO

= 'bukrs'.
= 'Nmero de un documento contable'.
= 'Num. Contable'.
g_it_fieldcat.

g_st_fieldcat-fieldname
g_st_fieldcat-seltext_m
g_st_fieldcat-seltext_s
APPEND g_st_fieldcat TO

= 'blart'.
= 'Fecha de documento en documento'.
= 'Fecha Doc.'.
g_it_fieldcat.

g_st_fieldcat-fieldname
g_st_fieldcat-seltext_m
g_st_fieldcat-seltext_s
APPEND g_st_fieldcat TO

= 'budat'.
= 'Fecha de contabilizacin en el documento'.
= 'Fecha cont.'.
g_it_fieldcat.

g_st_fieldcat-fieldname
g_st_fieldcat-seltext_m
g_st_fieldcat-seltext_s
APPEND g_st_fieldcat TO

= 'monat'.
= 'Mes contable'.
= 'Mes cont.'.
g_it_fieldcat.

g_st_fieldcat-fieldname
g_st_fieldcat-seltext_m
g_st_fieldcat-seltext_s
APPEND g_st_fieldcat TO

= 'gjahr'.
= 'Ejercicio'.
= 'Ejercicio'.
g_it_fieldcat.

tab2
g_st_fieldcat-fieldname
g_st_fieldcat-seltext_m
g_st_fieldcat-seltext_s
APPEND g_st_fieldcat TO

= 'dmbtr'.
= 'Importe en moneda local'.
= 'I. Moneda'.
g_it_fieldcat.

g_st_fieldcat-fieldname
g_st_fieldcat-seltext_m
nto contable'.
g_st_fieldcat-seltext_s
APPEND g_st_fieldcat TO

= 'buzei'.
= 'Nmero del apunte contable dentro del docume
= 'Num Apunt.'.
g_it_fieldcat.

g_st_fieldcat-fieldname
g_st_fieldcat-seltext_m
g_st_fieldcat-seltext_s
APPEND g_st_fieldcat TO

= 'augdt'.
= 'Fecha de la compensacin'.
= 'fec. Comp.'.
g_it_fieldcat.

g_st_fieldcat-fieldname
g_st_fieldcat-seltext_m
g_st_fieldcat-seltext_s
APPEND g_st_fieldcat TO

= 'bschl'.
= 'Clave de contabilizacin'.
= 'Clv. Cont.'.
g_it_fieldcat.

g_st_fieldcat-fieldname
g_st_fieldcat-seltext_m
g_st_fieldcat-seltext_s
APPEND g_st_fieldcat TO

= 'shkzg'.
= 'Indicador debe/haber'.
= 'Ind. D/H.'.
g_it_fieldcat.

g_st_fieldcat-fieldname = 'lifnr'.

g_st_fieldcat-seltext_m = 'Nmero de cuenta del proveedor o acreedor'.


g_st_fieldcat-seltext_s = 'Num. C. P o A.'.
APPEND g_st_fieldcat TO g_it_fieldcat.
*

tabla 3
g_st_fieldcat-fieldname
g_st_fieldcat-seltext_m
g_st_fieldcat-seltext_s
APPEND g_st_fieldcat TO

= 'butxt'.
= 'Denominacin de la sociedad o empresa'.
= 'Den. Soc. o Em.'.
g_it_fieldcat.

g_st_fieldcat-fieldname
g_st_fieldcat-seltext_m
g_st_fieldcat-seltext_s
APPEND g_st_fieldcat TO

= 'land1'.
= 'Clave de pas'.
= 'Cve. pas'.
g_it_fieldcat.

tabla 4
g_st_fieldcat-fieldname
g_st_fieldcat-seltext_m
g_st_fieldcat-seltext_s
APPEND g_st_fieldcat TO

= 'name1'.
= 'Nombre 1'.
= 'Nombre 1'.
g_it_fieldcat.

g_st_fieldcat-fieldname
g_st_fieldcat-seltext_m
g_st_fieldcat-seltext_s
APPEND g_st_fieldcat TO

= 'stras'.
= 'Calle y n'.
= 'Calle y n'.
g_it_fieldcat.

g_st_fieldcat-fieldname
g_st_fieldcat-seltext_m
egin)'.
g_st_fieldcat-seltext_s
APPEND g_st_fieldcat TO
*

tabla 4 - 2
g_st_fieldcat-fieldname
g_st_fieldcat-seltext_m
g_st_fieldcat-seltext_s
APPEND g_st_fieldcat TO

g_st_fieldcat-fieldname
g_st_fieldcat-seltext_m
blacin y calle'.
g_st_fieldcat-seltext_s
APPEND g_st_fieldcat TO

= 'regio'.
= 'Regin (Estado federal, "land", provincia, r
= 'Region'.
g_it_fieldcat.
= 'city1'.
= 'Poblacin'.
= 'Poblacin'.
g_it_fieldcat.
= 'city_code'.
= 'Codificacin de poblacin para fichero de po
= 'Cod. Pobl.'.
g_it_fieldcat.

ENDFORM.

*&---------------------------------------------------------------------*
*&
Form GUARDAR_ARCHIVO
*&---------------------------------------------------------------------*
*
text

*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM f_guardar_archivo .
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
*
initial_directory
= 'C:\Users\Yonathan LR'
initial_directory
= 'C:\Users\'
CHANGING
filename
= v_filename
path
= v_path
fullpath
= v_fullpath
user_action
= v_usr_action
EXCEPTIONS
cntl_error
= 1
error_no_gui
= 2
not_supported_by_gui = 3
OTHERS
= 4.
IF sy-subrc IS INITIAL.
IF v_usr_action EQ cl_gui_frontend_services=>action_ok.
para ver si presiono el boton cancelar
MOVE v_fullpath TO p_file.

"validacion

* METODO PARA GUARDAR ARCHIVO>


CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
filename
= v_fullpath "'C:\Users\Yonathan LR\Deskt
op\ekko.txt'
filetype
= 'ASC'
write_field_separator = 'X'
*
dat_mode
= 'X'
fieldnames
= it_xlhead
CHANGING
data_tab
= it_tabi.
* METODO PARA GUARDAR ARCHIVO>
IF sy-subrc EQ 0. "validacion para ver si se guardo bn el archivo
MESSAGE i208(00) WITH text-006.
ENDIF.
ENDIF.
ENDIF.
ENDFORM.

*&---------------------------------------------------------------------*
*&
Form CREA_BODY_ALV
*&---------------------------------------------------------------------*

*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM crea_body_alv .
*

v_repid = sy-repid.

* Load columns details to report


REFRESH i_fieldcat.
CLEAR w_i_fieldcat.
*
**
*
*
*
*
*
*
*
*

w_i_fieldcat-emphasize = 'C300'. "asi es como lo tenia


w_i_fieldcat-Col_pos = 1. "
w_i_fieldcat-fieldname = 'FLAG'.
w_i_fieldcat-tabname = 'it_tab5'.
w_i_fieldcat-seltext_l = text-002.
w_i_fieldcat-seltext_m = text-002.
w_i_fieldcat-seltext_s = text-002.
w_i_fieldcat-checkbox = 'X'.
w_i_fieldcat-edit = 'X'.
APPEND w_i_fieldcat TO i_fieldcat.
w_i_fieldcat-col_pos = 0.
w_i_fieldcat-fieldname = 'chk'.
w_i_fieldcat-seltext_l = ' Opc '.
w_i_fieldcat-seltext_m = ' Opc '.
w_i_fieldcat-seltext_s = ' Opc '.
w_i_fieldcat-input
= 'X'.
w_i_fieldcat-edit
= 'X'.
w_i_fieldcat-checkbox = 'X'.
APPEND w_i_fieldcat TO i_fieldcat.
CLEAR w_i_fieldcat.
w_i_fieldcat-emphasize
w_i_fieldcat-fieldname
w_i_fieldcat-tabname =
w_i_fieldcat-seltext_l
w_i_fieldcat-seltext_m
w_i_fieldcat-seltext_s
APPEND w_i_fieldcat TO

= 'C300'.
= 'belnr'.
'it_tab5'.
= 'belnr'.
= 'belnr'.
= 'belnr'.
i_fieldcat.

CLEAR w_i_fieldcat.
w_i_fieldcat-emphasize
w_i_fieldcat-fieldname
w_i_fieldcat-tabname =
w_i_fieldcat-seltext_l
w_i_fieldcat-seltext_m
w_i_fieldcat-seltext_s
APPEND w_i_fieldcat TO

= 'C300'.
= 'bukrs'.
'it_tab5'.
= 'bukrs'.
= 'bukrs'.
= 'bukrs'.
i_fieldcat.

CLEAR w_i_fieldcat.
w_i_fieldcat-emphasize
w_i_fieldcat-fieldname
w_i_fieldcat-tabname =
w_i_fieldcat-seltext_l
w_i_fieldcat-seltext_m
w_i_fieldcat-seltext_s
APPEND w_i_fieldcat TO

= 'C300'.
= 'blart'.
'it_tab5'.
= 'blart'.
= 'blart'.
= 'blart'.
i_fieldcat.

CLEAR w_i_fieldcat.
w_i_fieldcat-emphasize
w_i_fieldcat-fieldname
w_i_fieldcat-tabname =
w_i_fieldcat-seltext_l
w_i_fieldcat-seltext_m
w_i_fieldcat-seltext_s
APPEND w_i_fieldcat TO

= 'C300'.
= 'budat'.
'it_tab5'.
= 'budat'.
= 'budat'.
= 'budat'.
i_fieldcat.

CLEAR w_i_fieldcat.
w_i_fieldcat-emphasize
w_i_fieldcat-fieldname
w_i_fieldcat-tabname =
w_i_fieldcat-seltext_l
w_i_fieldcat-seltext_m
w_i_fieldcat-seltext_s
APPEND w_i_fieldcat TO

= 'C300'.
= 'monat'.
'it_tab5'.
= 'monat'.
= 'monat'.
= 'monat'.
i_fieldcat.

CLEAR w_i_fieldcat.
w_i_fieldcat-emphasize
w_i_fieldcat-fieldname
w_i_fieldcat-tabname =
w_i_fieldcat-seltext_l
w_i_fieldcat-seltext_m
w_i_fieldcat-seltext_s
APPEND w_i_fieldcat TO

= 'C300'.
= 'gjahr'.
'it_tab5'.
= 'gjahr'.
= 'gjahr'.
= 'gjahr'.
i_fieldcat.

CLEAR w_i_fieldcat.
tab2
w_i_fieldcat-emphasize
w_i_fieldcat-fieldname
w_i_fieldcat-tabname =
w_i_fieldcat-seltext_l
w_i_fieldcat-seltext_m
w_i_fieldcat-seltext_s

= 'C300'.
= 'dmbtr'.
'it_tab5'.
= 'dmbtr'.
= 'dmbtr'.
= 'dmbtr'.

*-***************************************subtotal ****************
w_i_fieldcat-do_sum
= 'X'.

*-***************************************subtotal ****************
APPEND w_i_fieldcat TO i_fieldcat.
CLEAR w_i_fieldcat.
w_i_fieldcat-emphasize
w_i_fieldcat-fieldname
w_i_fieldcat-tabname =
w_i_fieldcat-seltext_l
w_i_fieldcat-seltext_m
w_i_fieldcat-seltext_s
APPEND w_i_fieldcat TO

= 'C300'.
= 'buzei'.
'it_tab5'.
= 'buzei'.
= 'buzei'.
= 'buzei'.
i_fieldcat.

CLEAR w_i_fieldcat.
w_i_fieldcat-emphasize
w_i_fieldcat-fieldname
w_i_fieldcat-tabname =
w_i_fieldcat-seltext_l
w_i_fieldcat-seltext_m
w_i_fieldcat-seltext_s
APPEND w_i_fieldcat TO

= 'C300'.
= 'augdt'.
'it_tab5'.
= 'augdt'.
= 'augdt'.
= 'augdt'.
i_fieldcat.

CLEAR w_i_fieldcat.
w_i_fieldcat-emphasize
w_i_fieldcat-fieldname
w_i_fieldcat-tabname =
w_i_fieldcat-seltext_l
w_i_fieldcat-seltext_m
w_i_fieldcat-seltext_s
APPEND w_i_fieldcat TO

= 'C300'.
= 'bschl'.
'it_tab5'.
= 'bschl'.
= 'bschl'.
= 'bschl'.
i_fieldcat.

CLEAR w_i_fieldcat.
w_i_fieldcat-emphasize
w_i_fieldcat-fieldname
w_i_fieldcat-tabname =
w_i_fieldcat-seltext_l
w_i_fieldcat-seltext_m
w_i_fieldcat-seltext_s
APPEND w_i_fieldcat TO

= 'C611'.
= 'shkzg'.
'it_tab5'.
= ' shkzg '.
= ' shkzg '.
= ' shkzg '.
i_fieldcat.

CLEAR w_i_fieldcat.
w_i_fieldcat-emphasize
w_i_fieldcat-fieldname
w_i_fieldcat-tabname =
w_i_fieldcat-seltext_l
w_i_fieldcat-seltext_m
w_i_fieldcat-seltext_s
APPEND w_i_fieldcat TO

= 'C611'.
= 'lifnr'.
'it_tab5'.
= ' lifnr '.
= ' lifnr '.
= ' lifnr '.
i_fieldcat.

CLEAR w_i_fieldcat.
tabla 3
w_i_fieldcat-emphasize
w_i_fieldcat-emphasize
w_i_fieldcat-fieldname
w_i_fieldcat-tabname =
w_i_fieldcat-seltext_l
w_i_fieldcat-seltext_m
w_i_fieldcat-seltext_s
APPEND w_i_fieldcat TO

= 'C611'.
= 'C611'.
= 'butxt'.
'it_tab5'.
= ' butxt '.
= ' butxt '.
= ' butxt '.
i_fieldcat.

CLEAR w_i_fieldcat.
w_i_fieldcat-emphasize
w_i_fieldcat-emphasize
w_i_fieldcat-fieldname
w_i_fieldcat-tabname =
w_i_fieldcat-seltext_l
w_i_fieldcat-seltext_m
w_i_fieldcat-seltext_s
APPEND w_i_fieldcat TO

= 'C611'.
= 'C611'.
= 'land1'.
'it_tab5'.
= ' land1 '.
= ' land1 '.
= ' land1 '.
i_fieldcat.

CLEAR w_i_fieldcat.
tabla 4
w_i_fieldcat-emphasize
w_i_fieldcat-emphasize
w_i_fieldcat-fieldname
w_i_fieldcat-tabname =
w_i_fieldcat-seltext_l
w_i_fieldcat-seltext_m
w_i_fieldcat-seltext_s
APPEND w_i_fieldcat TO

= 'C611'.
= 'C611'.
= 'name1'.
'it_tab5'.
= ' name1 '.
= ' name1 '.
= ' name1 '.
i_fieldcat.

CLEAR w_i_fieldcat.
w_i_fieldcat-emphasize
w_i_fieldcat-fieldname
w_i_fieldcat-tabname =
w_i_fieldcat-seltext_l
w_i_fieldcat-seltext_m
w_i_fieldcat-seltext_s
APPEND w_i_fieldcat TO

= 'C611'.
= 'stras'.
'it_tab5'.
= ' stras '.
= ' stras '.
= ' stras '.
i_fieldcat.

CLEAR w_i_fieldcat.
w_i_fieldcat-emphasize
w_i_fieldcat-fieldname
w_i_fieldcat-tabname =
w_i_fieldcat-seltext_l
w_i_fieldcat-seltext_m
w_i_fieldcat-seltext_s

= 'C611'.
= 'regio'.
'it_tab5'.
= ' regio '.
= ' regio '.
= ' regio '.

APPEND w_i_fieldcat TO i_fieldcat.


*

CLEAR w_i_fieldcat.
tabla 4 - 2
w_i_fieldcat-emphasize
w_i_fieldcat-fieldname
w_i_fieldcat-tabname =
w_i_fieldcat-seltext_l
w_i_fieldcat-seltext_m
w_i_fieldcat-seltext_s
APPEND w_i_fieldcat TO

= 'C611'.
= 'city1'.
'it_tab5'.
= ' city1 '.
= ' city1 '.
= ' city1 '.
i_fieldcat.

CLEAR w_i_fieldcat.
w_i_fieldcat-emphasize
w_i_fieldcat-fieldname
w_i_fieldcat-tabname =
w_i_fieldcat-seltext_l
w_i_fieldcat-seltext_m
w_i_fieldcat-seltext_s
APPEND w_i_fieldcat TO

= 'C611'.
= 'city_code'.
'it_tab5'.
= 'city_code'.
= 'city_code'.
= 'city_code'.
i_fieldcat.

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

AGRUPAMIENTO ****************

wa_sort-spos
= 1.
wa_sort-fieldname = 'belnr'.
wa_sort-up
= 'X'.
wa_sort-subtot
= 'X'.
APPEND wa_sort TO it_sort.
*-***************************************

AGRUPAMIENTO ****************

ENDFORM.
*&---------------------------------------------------------------------*
*&
Form LEER_SELECCIONADOS
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM f_leer_seleccionados .
DATA: ref_grid TYPE REF TO cl_gui_alv_grid.
IF ref_grid IS INITIAL.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = ref_grid.

ENDIF.
IF NOT ref_grid IS INITIAL.
CALL METHOD ref_grid->check_changed_data.
ENDIF.
ENDFORM.

*&---------------------------------------------------------------------*
*&
Form DISCRIMINA_SELECCIONADOS
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM f_discrimina_seleccionados .
* LOOP AT it_tab5 INTO wa_tab5 WHERE chk = 'X'.
num_selec_cb = 0.
LOOP AT it_tab5 ASSIGNING <fs_tab5>
wa_tabi-belnr
wa_tabi-bukrs
wa_tabi-blart
wa_tabi-budat
wa_tabi-monat
wa_tabi-gjahr

=
=
=
=
=
=

<fs_tab5>-belnr.
<fs_tab5>-bukrs.
<fs_tab5>-blart.
<fs_tab5>-budat.
<fs_tab5>-monat.
<fs_tab5>-gjahr.

wa_tabi-dmbtr
wa_tabi-buzei
wa_tabi-augdt
wa_tabi-bschl
wa_tabi-shkzg
wa_tabi-lifnr

=
=
=
=
=
=

<fs_tab5>-dmbtr.
<fs_tab5>-buzei.
<fs_tab5>-augdt.
<fs_tab5>-bschl.
<fs_tab5>-shkzg.
<fs_tab5>-lifnr.

WHERE chk = 'X'.

wa_tabi-butxt = <fs_tab5>-butxt.
wa_tabi-land1 = <fs_tab5>-land1.
wa_tabi-name1 = <fs_tab5>-name1.
wa_tabi-stras = <fs_tab5>-stras.
wa_tabi-regio = <fs_tab5>-regio.
wa_tabi-city1 = <fs_tab5>-city1.
wa_tabi-city_code = <fs_tab5>-city_code.
wa_tabi-numuser = <fs_tab5>-numuser.
wa_tabi-fecha = <fs_tab5>-fecha.
*
*

tab4
APPEND INITIAL LINE TO t_mara ASSIGNING <lfs_mara>.
num_selec_cb = num_selec_cb + 1.

APPEND wa_tabi TO it_tabi.


ENDLOOP.
UNASSIGN <fs_tab5>.
* PERFORM inserta_cab_txt.
ENDFORM.
*&---------------------------------------------------------------------*
*&
Form INSERTAR_DB
*&---------------------------------------------------------------------*
*
text funcion encargada de insertar en la BD
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM f_insertar_db .
*

INSERT zylr_cam1db FROM TABLE it_tabi ACCEPTING DUPLICATE KEYS.


INSERT zylr_cam1db FROM TABLE it_tabi ACCEPTING DUPLICATE KEYS.
IF sy-subrc EQ 0.
COMMIT WORK.
MESSAGE 'Se han Insertado los valores Crrectamente.' TYPE 'I'.
ELSEIF sy-subrc EQ 4.
MESSAGE 'Los registros Ya Estan en la BD.' TYPE 'I'.
ELSE.
MESSAGE 'A Ocurrido un errror en la Insercion de los Datos.' TYPE 'I'

.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*&
Form BORRARDATOS
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM f_borrardatos .
DELETE FROM zylr_cam1db.
IF sy-subrc EQ 0.
COMMIT WORK.
MESSAGE i208(00) WITH text-009.
ELSEIF sy-subrc EQ 0.
MESSAGE i208(00) WITH text-010.
ENDIF.

ENDFORM.

*&---------------------------------------------------------------------*
*&
Form INSERTA_CAB_TXT
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM inserta_cab_txt .
* CLEAR wa_tabi.
*
* wa_tabi-belnr ='BELNR'.
* INSERT wa_tabi INTO it_tabi INDEX 0.
ENDFORM.
*&---------------------------------------------------------------------*
*&
Form LIMPIA_IT
*&---------------------------------------------------------------------*
*
text funcion para limpiar los datos de txt
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM f_limpia_it .
*

WHILE lines( it_tabi ) > 0.


WRITE ' Es mayor a cero'.
DELETE it_tabi INDEX 1.
ENDWHILE.

ENDFORM.

*&---------------------------------------------------------------------*
*&
Form GENERA_CAB_TX
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM f_genera_cab_tx .
CLEAR wa_xlhead.
wa_xlhead-fld = 'BELNR'.

APPEND wa_xlhead TO it_xlhead.


CLEAR wa_xlhead.
wa_xlhead-fld = 'BUKRS'.
APPEND wa_xlhead TO it_xlhead.
CLEAR wa_xlhead.
wa_xlhead-fld = 'BUZEI'.
APPEND wa_xlhead TO it_xlhead.
CLEAR wa_xlhead.
wa_xlhead-fld = 'BLART'.
APPEND wa_xlhead TO it_xlhead.
CLEAR wa_xlhead.
wa_xlhead-fld = 'BUDAT'.
APPEND wa_xlhead TO it_xlhead.
CLEAR wa_xlhead.
wa_xlhead-fld = 'MONAT'.
APPEND wa_xlhead TO it_xlhead.
CLEAR wa_xlhead.
wa_xlhead-fld = 'GJAHR'.
APPEND wa_xlhead TO it_xlhead.

CLEAR wa_xlhead.
wa_xlhead-fld = 'DMBTR'.
APPEND wa_xlhead TO it_xlhead.
CLEAR wa_xlhead.
wa_xlhead-fld = 'AUGDT'.
APPEND wa_xlhead TO it_xlhead.
CLEAR wa_xlhead.
wa_xlhead-fld = 'BSCHL'.
APPEND wa_xlhead TO it_xlhead.
CLEAR wa_xlhead.
wa_xlhead-fld = 'SHKZG'.
APPEND wa_xlhead TO it_xlhead.
CLEAR wa_xlhead.
wa_xlhead-fld = 'LIFNR'.
APPEND wa_xlhead TO it_xlhead.
CLEAR wa_xlhead.
wa_xlhead-fld = 'BUTXT'.
APPEND wa_xlhead TO it_xlhead.

CLEAR wa_xlhead.
wa_xlhead-fld = 'LAND1'.
APPEND wa_xlhead TO it_xlhead.
*

TAB4
CLEAR wa_xlhead.
wa_xlhead-fld = ' NAME1'.
APPEND wa_xlhead TO it_xlhead.
CLEAR wa_xlhead.
wa_xlhead-fld = ' STRAS'.
APPEND wa_xlhead TO it_xlhead.
CLEAR wa_xlhead.
wa_xlhead-fld = 'REGIO'.
APPEND wa_xlhead TO it_xlhead.
CLEAR wa_xlhead.
wa_xlhead-fld = 'CITY1'.
APPEND wa_xlhead TO it_xlhead.
CLEAR wa_xlhead.
wa_xlhead-fld = 'CITY_CODE'.
APPEND wa_xlhead TO it_xlhead.

ENDFORM.
*&---------------------------------------------------------------------*
*&
Form F_GAURDARBD
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM f_gaurdarbd .
PERFORM f_limpia_it.
PERFORM f_leer_seleccionados.
PERFORM f_discrimina_seleccionados.
IF num_selec_cb > 0.
PERFORM f_insertar_db.
ELSE.
MESSAGE i208(00) WITH text-008.
ENDIF.
ENDFORM.

*&---------------------------------------------------------------------*
*&
Form F_EXPORTARTXT
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM f_exportartxt .
PERFORM f_limpia_it.
PERFORM f_leer_seleccionados.
PERFORM f_discrimina_seleccionados.
IF num_selec_cb > 0.
PERFORM f_genera_cab_tx.
PERFORM f_guardar_archivo.
ELSE.
MESSAGE i208(00) WITH text-008.
ENDIF.
ENDFORM.