Al programa le informamos de la factura y nos devuelve su flujo de documentos
*&---------------------------------------------------------------------*
*& Report ZFLUJO_DOC
*&
*&---------------------------------------------------------------------*
*&
*&
*&
*&---------------------------------------------------------------------*
REPORT ZFLUJO_DOC.
************************************************************************
*INCLUDE
************************************************************************
INCLUDE zalv.
INCLUDE zsdin01.
TABLES: *vbrk.
************************************************************************
*DATA
************************************************************************
DATA: BEGIN OF gt_salida_alv OCCURS 0,
vbeln LIKE vbrk-vbeln,
sfakn LIKE vbrk-sfakn,
fkdat LIKE vbrk-fkdat,
fkart LIKE vbrk-fkart,
vkorg LIKE vbrk-vkorg,
vtweg LIKE vbrk-vtweg,
kunrg LIKE vbrk-kunrg,
kunrg_txt LIKE kna1-name1,
posnr LIKE vbrp-posnr,
matnr LIKE vbrp-matnr,
matnr_txt LIKE makt-maktx,
entrega LIKE likp-vbeln,
posicion_entrega LIKE lips-posnr,
kunwe LIKE likp-kunnr,
kunwe_txt LIKE kna1-name1,
wadat_ist LIKE likp-wadat_ist,
ntgew LIKE lips-ntgew,
brgew LIKE lips-brgew,
gewei LIKE lips-gewei,
pedido LIKE vbak-vbeln,
posicion_pedido LIKE vbap-posnr,
bstnk LIKE vbak-bstnk,
bstdk LIKE vbak-bstdk,
ntgew_pedido LIKE vbap-ntgew,
brgew_pedido LIKE vbap-brgew,
gewei_pedido LIKE vbap-gewei,
werks LIKE vbap-werks,
werks_txt LIKE t001w-name1,
pedido_compras LIKE ekko-ebeln,
pos_pedido_compras LIKE ekpo-ebelp,
reswk LIKE ekko-reswk,
reswk_txt LIKE t001w-name1,
augbl LIKE bseg-augbl, " ODJ 16.11.2010
dmbtr LIKE bseg-dmbtr, " ODJ 16.11.2010
pswsl LIKE bseg-pswsl, " ODJ 16.11.2010
END OF gt_salida_alv.
************************************************************************
*SELECT-OPTIONS
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
SELECT-OPTIONS: s_vbeln FOR gt_salida_alv-vbeln,
s_fkart FOR gt_salida_alv-fkart,
s_fkdat FOR gt_salida_alv-fkdat,
s_vkorg FOR gt_salida_alv-vkorg,
s_vtweg FOR gt_salida_alv-vtweg,
s_kunrg FOR gt_salida_alv-kunrg,
s_matnr FOR gt_salida_alv-matnr.
SELECTION-SCREEN SKIP 1.
PARAMETERS: p_gewei LIKE mara-gewei DEFAULT 'KG' OBLIGATORY.
SELECTION-SCREEN SKIP 1.
PARAMETERS: p_agru RADIOBUTTON GROUP r1 DEFAULT 'X',
p_det RADIOBUTTON GROUP r1.
SELECTION-SCREEN END OF BLOCK b1.
SELECTION-SCREEN SKIP 2.
PARAMETERS: p_vari LIKE variant-variant.
************************************************************************
*AT SELECTION-SCREEN ON VALUE-REQUEST
************************************************************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_vari.
DATA: spec_layout TYPE disvariant,
g_exit TYPE c.
CLEAR variant.
variant-report = sy-repid.
CALL FUNCTION 'LVC_VARIANT_F4'
EXPORTING
is_variant = variant
i_save = 'A'
IMPORTING
e_exit = g_exit
es_variant = spec_layout
EXCEPTIONS
not_found = 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.
ELSE.
IF g_exit NE 'X'.
p_vari = spec_layout-variant.
variant = spec_layout.
ENDIF.
ENDIF.
************************************************************************
*START-OF-SELECTION
************************************************************************
START-OF-SELECTION.
PERFORM tomar_datos.
************************************************************************
*END-OF-SELECTION
************************************************************************
END-OF-SELECTION.
PERFORM salida_alv.
************************************************************************
*FORMS
************************************************************************
*&---------------------------------------------------------------------*
*& Form tomar_datos
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM tomar_datos .
DATA: lt_salida_alv LIKE gt_salida_alv OCCURS 0
WITH HEADER LINE.
DATA: lt_likp LIKE likp OCCURS 0 WITH HEADER LINE.
DATA: lt_lips LIKE lips OCCURS 0 WITH HEADER LINE.
DATA: lt_vbak LIKE vbak OCCURS 0 WITH HEADER LINE.
DATA: lt_vbap LIKE vbap OCCURS 0 WITH HEADER LINE.
DATA: lt_ekko LIKE ekko OCCURS 0 WITH HEADER LINE.
DATA: lt_kna1 LIKE kna1 OCCURS 0 WITH HEADER LINE.
DATA: lt_t001w LIKE t001w OCCURS 0 WITH HEADER LINE.
DATA: lt_makt LIKE makt OCCURS 0 WITH HEADER LINE.
DATA: lf_subrc LIKE sy-subrc.
DATA: lf_vkorg_error LIKE zyte81-x_vkorg.
PERFORM validar_organizacion_ventas
TABLES s_vkorg
USING ''
CHANGING lf_subrc
lf_vkorg_error.
IF lf_subrc <> 0.
* Sin autorización para organización de ventas ....
MESSAGE i151(jgbp) WITH lf_vkorg_error.
EXIT.
ENDIF.
SELECT vbeln fkart fkdat vkorg vtweg kunrg sfakn
INTO CORRESPONDING FIELDS OF TABLE lt_salida_alv
FROM vbrk
WHERE vbeln IN s_vbeln
AND fkart IN s_fkart
AND fkdat IN s_fkdat
AND vkorg IN s_vkorg
AND vtweg IN s_vtweg
AND kunrg IN s_kunrg.
LOOP AT lt_salida_alv.
MOVE-CORRESPONDING lt_salida_alv TO gt_salida_alv.
SELECT posnr matnr
INTO (gt_salida_alv-posnr, gt_salida_alv-matnr)
FROM vbrp
WHERE vbeln = lt_salida_alv-vbeln
AND matnr IN s_matnr.
CLEAR: gt_salida_alv-entrega,
gt_salida_alv-posicion_entrega,
gt_salida_alv-pedido,
gt_salida_alv-posicion_pedido.
SELECT SINGLE vbelv posnv
INTO (gt_salida_alv-entrega, gt_salida_alv-posicion_entrega)
FROM vbfa
WHERE vbeln = gt_salida_alv-vbeln
AND posnn = gt_salida_alv-posnr
AND vbtyp_v = 'J'.
IF sy-subrc <> 0.
SELECT SINGLE vbelv posnv
INTO (gt_salida_alv-pedido, gt_salida_alv-posicion_pedido)
FROM vbfa
WHERE vbeln = gt_salida_alv-vbeln
AND posnn = gt_salida_alv-posnr
AND stufe = '0'.
IF sy-subrc <> 0.
SELECT SINGLE vgbel vgpos aubel aupos
INTO (gt_salida_alv-entrega, gt_salida_alv-posicion_entrega,
gt_salida_alv-pedido, gt_salida_alv-posicion_pedido)
FROM vbrp
WHERE vbeln = lt_salida_alv-vbeln
AND posnr = gt_salida_alv-posnr
AND vgtyp = 'J'.
ENDIF.
ELSE.
SELECT SINGLE vbelv posnv
INTO (gt_salida_alv-pedido, gt_salida_alv-posicion_pedido)
FROM vbfa
WHERE vbeln = gt_salida_alv-entrega
AND posnn = gt_salida_alv-posicion_entrega
AND stufe = '0'.
IF sy-subrc <> 0.
SELECT SINGLE vgbel vgpos
INTO (gt_salida_alv-pedido_compras,
gt_salida_alv-pos_pedido_compras)
FROM lips
WHERE vbeln = gt_salida_alv-entrega
AND posnr = gt_salida_alv-posicion_entrega.
ENDIF.
ENDIF.
APPEND gt_salida_alv.
ENDSELECT.
ENDLOOP.
SORT gt_salida_alv BY vbeln posnr
entrega posicion_entrega
pedido posicion_pedido
pedido_compras pos_pedido_compras.
IF NOT gt_salida_alv[] IS INITIAL.
* Datos entrega, cabecera
SELECT vbeln kunnr wadat_ist
INTO CORRESPONDING FIELDS OF TABLE lt_likp
FROM likp
FOR ALL ENTRIES IN gt_salida_alv
WHERE vbeln = gt_salida_alv-entrega.
LOOP AT lt_likp.
gt_salida_alv-kunwe = lt_likp-kunnr.
gt_salida_alv-wadat_ist = lt_likp-wadat_ist.
MODIFY gt_salida_alv TRANSPORTING kunwe wadat_ist
WHERE entrega = lt_likp-vbeln.
ENDLOOP.
* Datos entrega, posición
SELECT vbeln posnr matnr ntgew brgew gewei
INTO CORRESPONDING FIELDS OF TABLE lt_lips
FROM lips
FOR ALL ENTRIES IN gt_salida_alv
WHERE vbeln = gt_salida_alv-entrega
AND posnr = gt_salida_alv-posicion_entrega.
LOOP AT lt_lips.
gt_salida_alv-gewei = p_gewei.
IF p_gewei <> lt_lips-gewei.
CALL FUNCTION 'Z_FT_CONVERTIR_UNIDADES'
EXPORTING
matnr = lt_lips-matnr
meinh = lt_lips-gewei
meins = p_gewei
cantidad = lt_lips-brgew
IMPORTING
salida = gt_salida_alv-brgew.
CALL FUNCTION 'Z_FT_CONVERTIR_UNIDADES'
EXPORTING
matnr = lt_lips-matnr
meinh = lt_lips-gewei
meins = p_gewei
cantidad = lt_lips-ntgew
IMPORTING
salida = gt_salida_alv-ntgew.
ELSE.
gt_salida_alv-ntgew = lt_lips-ntgew.
gt_salida_alv-brgew = lt_lips-brgew.
ENDIF.
MODIFY gt_salida_alv TRANSPORTING ntgew brgew gewei
WHERE entrega = lt_lips-vbeln
AND posicion_entrega = lt_lips-posnr.
ENDLOOP.
* Datos pedido ventas, cabecera
SELECT vbeln bstnk bstdk
INTO CORRESPONDING FIELDS OF TABLE lt_vbak
FROM vbak
FOR ALL ENTRIES IN gt_salida_alv
WHERE vbeln = gt_salida_alv-pedido.
LOOP AT lt_vbak.
gt_salida_alv-bstnk = lt_vbak-bstnk.
gt_salida_alv-bstdk = lt_vbak-bstdk.
MODIFY gt_salida_alv TRANSPORTING bstnk bstdk
WHERE pedido = lt_vbak-vbeln.
ENDLOOP.
* Datos pedido ventas, posición
SELECT vbeln posnr ntgew brgew gewei werks
INTO CORRESPONDING FIELDS OF TABLE lt_vbap
FROM vbap
FOR ALL ENTRIES IN gt_salida_alv
WHERE vbeln = gt_salida_alv-pedido
AND posnr = gt_salida_alv-posicion_pedido.
LOOP AT lt_vbap.
gt_salida_alv-werks = lt_vbap-werks.
gt_salida_alv-gewei_pedido = p_gewei.
IF p_gewei <> lt_vbap-gewei.
CALL FUNCTION 'Z_FT_CONVERTIR_UNIDADES'
EXPORTING
matnr = lt_lips-matnr
meinh = lt_lips-gewei
meins = p_gewei
cantidad = lt_vbap-brgew
IMPORTING
salida = gt_salida_alv-brgew_pedido.
CALL FUNCTION 'Z_FT_CONVERTIR_UNIDADES'
EXPORTING
matnr = lt_lips-matnr
meinh = lt_lips-gewei
meins = p_gewei
cantidad = lt_vbap-ntgew
IMPORTING
salida = gt_salida_alv-ntgew_pedido.
ELSE.
gt_salida_alv-ntgew_pedido = lt_vbap-ntgew.
gt_salida_alv-brgew_pedido = lt_vbap-brgew.
ENDIF.
MODIFY gt_salida_alv TRANSPORTING ntgew_pedido
brgew_pedido
gewei_pedido
werks
WHERE pedido = lt_vbap-vbeln
AND posicion_pedido = lt_vbap-posnr.
ENDLOOP.
SELECT ebeln reswk
INTO CORRESPONDING FIELDS OF TABLE lt_ekko
FROM ekko
FOR ALL ENTRIES IN gt_salida_alv
WHERE ebeln = gt_salida_alv-pedido_compras.
LOOP AT lt_ekko.
gt_salida_alv-reswk = lt_ekko-reswk.
MODIFY gt_salida_alv TRANSPORTING reswk
WHERE pedido_compras = lt_ekko-ebeln.
ENDLOOP.
* Descripción de Resp. Pago, Dest. Mercancía
SELECT kunnr name1
INTO CORRESPONDING FIELDS OF TABLE lt_kna1
FROM kna1
FOR ALL ENTRIES IN gt_salida_alv
WHERE kunnr = gt_salida_alv-kunrg
OR kunnr = gt_salida_alv-kunwe.
LOOP AT lt_kna1.
gt_salida_alv-kunrg_txt = lt_kna1-name1.
MODIFY gt_salida_alv TRANSPORTING kunrg_txt
WHERE kunrg = lt_kna1-kunnr.
gt_salida_alv-kunwe_txt = lt_kna1-name1.
MODIFY gt_salida_alv TRANSPORTING kunwe_txt
WHERE kunwe = lt_kna1-kunnr.
ENDLOOP.
* Datos asociados a los centros
SELECT werks name1
INTO CORRESPONDING FIELDS OF TABLE lt_t001w
FROM t001w
FOR ALL ENTRIES IN gt_salida_alv
WHERE werks = gt_salida_alv-werks
OR werks = gt_salida_alv-reswk.
LOOP AT lt_t001w.
gt_salida_alv-werks_txt = lt_t001w-name1.
MODIFY gt_salida_alv TRANSPORTING werks_txt
WHERE werks = lt_t001w-werks.
gt_salida_alv-reswk_txt = lt_t001w-name1.
MODIFY gt_salida_alv TRANSPORTING reswk_txt
WHERE reswk = lt_t001w-werks.
ENDLOOP.
* Textos de material
SELECT matnr maktx
INTO CORRESPONDING FIELDS OF TABLE lt_makt
FROM makt
FOR ALL ENTRIES IN gt_salida_alv
WHERE matnr = gt_salida_alv-matnr
AND spras = sy-langu.
LOOP AT lt_makt.
gt_salida_alv-matnr_txt = lt_makt-maktx.
MODIFY gt_salida_alv TRANSPORTING matnr_txt
WHERE matnr = lt_makt-matnr.
ENDLOOP.
* Añadimos el doc. compensado y el importe
DATA: lf_vbeln LIKE gt_salida_alv-vbeln.
LOOP AT gt_salida_alv.
SELECT SINGLE bukrs INTO *vbrk-bukrs FROM *vbrk
WHERE vbeln = gt_salida_alv-vbeln.
SELECT SINGLE augbl dmbtr pswsl
INTO (gt_salida_alv-augbl, gt_salida_alv-dmbtr,
gt_salida_alv-pswsl)
FROM bseg
WHERE bukrs = *vbrk-bukrs
AND belnr = gt_salida_alv-vbeln
AND augbl <> space
AND bschl = '01'. " Facturas
IF gt_salida_alv-vbeln <> lf_vbeln.
lf_vbeln = gt_salida_alv-vbeln. " Solo una vez por factura
ELSE.
CLEAR gt_salida_alv-dmbtr.
ENDIF.
MODIFY gt_salida_alv.
ENDLOOP.
ENDIF.
ENDFORM. " tomar_datos
*&---------------------------------------------------------------------*
*& Form SALIDA_ALV
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM salida_alv .
DATA: lf_alv_repid LIKE sy-repid.
DATA: ls_alv_doc_layout TYPE slis_layout_alv,
lt_alv_doc_fieldcat TYPE slis_t_fieldcat_alv,
ls_alv_doc_fieldcat TYPE slis_fieldcat_alv.
DATA: ls_alv_doc_variant LIKE disvariant.
DATA: lt_salida_alv LIKE gt_salida_alv OCCURS 0
WITH HEADER LINE.
lf_alv_repid = 'ZFLUJO_DOC'.
* Tomamos el catálogo de campos.
PERFORM obt_descrip_campos_tabla USING lf_alv_repid
'GT_SALIDA_ALV'.
PERFORM cambio_descripcion USING:
'POSNR' text-001 text-001 text-001 text-001,
'POSICION_ENTREGA' text-002 text-002 text-002 text-002,
'POSICION_PEDIDO' text-003 text-003 text-003 text-003,
'POS_PEDIDO_COMPRAS' text-004 text-004 text-004 text-004.
PERFORM copia_descripcion USING: 'KUNRG_TXT' 'KUNRG',
'KUNWE_TXT' 'KUNWE',
'WERKS_TXT' 'WERKS',
'RESWK_TXT' 'RESWK'.
PERFORM carac_gen_listado.
PERFORM asign_o_quitar_campo_clave USING: 'VBELN' ' ',
'POSNR' ' ',
'ENTREGA' ' ',
'POSICION_ENTREGA' ' ',
'PEDIDO' ' ',
'POSICION_PEDIDO' ' ',
'PEDIDO_COMPRAS' ' ',
'POS_PEDIDO_COMPRAS' ' '.
PERFORM activar_hotspot USING: 'VBELN' 'X',
'SFAKN' 'X',
'ENTREGA' 'X',
'PEDIDO' 'X',
'PEDIDO_COMPRAS' 'X'.
ls_alv_doc_variant-report = lf_alv_repid.
ls_alv_doc_variant-variant = p_vari.
IF p_det IS INITIAL.
LOOP AT gt_salida_alv.
lt_salida_alv = gt_salida_alv.
CLEAR: lt_salida_alv-posnr,
lt_salida_alv-matnr,
lt_salida_alv-matnr_txt,
lt_salida_alv-posicion_entrega,
lt_salida_alv-posicion_pedido,
lt_salida_alv-pos_pedido_compras.
COLLECT lt_salida_alv.
ENDLOOP.
gt_salida_alv[] = lt_salida_alv[].
PERFORM eliminar_campos USING: 'POSNR',
'MATNR',
'MATNR_TXT',
'POSICION_ENTREGA',
'POSICION_PEDIDO',
'POS_PEDIDO_COMPRAS'.
ENDIF.
PERFORM eliminar_campos USING: 'NTGEW',
'BRGEW',
'GEWEI',
'NTGEW_PEDIDO',
'BRGEW_PEDIDO',
'GEWEI_PEDIDO',
'WERKS',
'WERKS_TXT',
'PEDIDO_COMPRAS',
'POS_PEDIDO_COMPRAS',
'ERSWK',
'RESWK_TXT'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = lf_alv_repid
is_layout = layout
it_fieldcat = fieldtab
i_callback_user_command = 'USER_COMMAND'
i_default = 'X'
i_save = 'A'
is_variant = ls_alv_doc_variant
TABLES
t_outtab = gt_salida_alv
EXCEPTIONS
program_error = 1
OTHERS = 99.
ENDFORM. " SALIDA_ALV
*&---------------------------------------------------------------------*
*& Form USER_COMMAND
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM user_command USING pf_ucomm TYPE sy-ucomm
ps_selfield TYPE slis_selfield.
DATA: lf_ebeln LIKE ekko-ebeln.
CASE pf_ucomm.
WHEN '&IC1'.
CASE ps_selfield-fieldname.
WHEN 'VBELN' OR 'SFAKN'.
CHECK NOT ps_selfield-value IS INITIAL.
SET PARAMETER ID 'VF' FIELD ps_selfield-value.
CALL TRANSACTION 'VF03' AND SKIP FIRST SCREEN.
WHEN 'ENTREGA'.
CHECK NOT ps_selfield-value IS INITIAL.
SET PARAMETER ID 'VL' FIELD ps_selfield-value.
CALL TRANSACTION 'VL03N' AND SKIP FIRST SCREEN.
WHEN 'PEDIDO'.
CHECK NOT ps_selfield-value IS INITIAL.
SET PARAMETER ID 'AUN' FIELD ps_selfield-value.
CALL TRANSACTION 'VA03' AND SKIP FIRST SCREEN.
WHEN 'PEDIDO_COMPRAS'.
CHECK NOT ps_selfield-value IS INITIAL.
CLEAR gt_salida_alv.
READ TABLE gt_salida_alv INDEX ps_selfield-tabindex.
CHECK sy-subrc = 0.
CALL FUNCTION 'ME_DISPLAY_PURCHASE_DOCUMENT'
EXPORTING
i_ebeln = gt_salida_alv-pedido_compras
i_ebelp = gt_salida_alv-pos_pedido_compras
i_enjoy = 'X'
i_display_only = 'X'
EXCEPTIONS
not_found = 1
no_authority = 2
invalid_call = 3
preview_not_possible = 4
OTHERS = 5.
ENDCASE.
ENDCASE.
ENDFORM. "USER_COMMAND
*&---------------------------------------------------------------------*
*& Form cambio_descripcion
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM cambio_descripcion USING pf_fieldname
pf_scrtext_l
pf_scrtext_m
pf_scrtext_s
pf_coltext.
READ TABLE fieldtab WITH KEY fieldname = pf_fieldname ASSIGNING <fs>.
IF sy-subrc EQ 0.
<fs>-seltext_l = pf_scrtext_l.
<fs>-seltext_m = pf_scrtext_m.
<fs>-seltext_s = pf_scrtext_s.
<fs>-reptext_ddic = pf_coltext.
ENDIF.
ENDFORM. " cambio_descripcion
*&---------------------------------------------------------------------*
*& Form copia_descripcion
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM copia_descripcion USING pf_fieldname_destino
pf_fieldname_origen.
FIELD-SYMBOLS: <lfs_fieldtab> TYPE slis_fieldcat_alv.
READ TABLE fieldtab WITH KEY fieldname = pf_fieldname_origen
ASSIGNING <lfs_fieldtab>.
CHECK sy-subrc = 0.
READ TABLE fieldtab WITH KEY fieldname = pf_fieldname_destino
ASSIGNING <fs>.
IF sy-subrc EQ 0.
<fs>-seltext_l = <lfs_fieldtab>-seltext_l.
<fs>-seltext_m = <lfs_fieldtab>-seltext_m.
<fs>-seltext_s = <lfs_fieldtab>-seltext_s.
<fs>-reptext_ddic = <lfs_fieldtab>-reptext_ddic.
ENDIF.
ENDFORM. " cambio_descripcion
*&---------------------------------------------------------------------*
*& Form activar_hotspot
*&---------------------------------------------------------------------*
FORM eliminar_campos USING p1 TYPE slis_fieldname.
READ TABLE fieldtab WITH KEY fieldname = p1 ASSIGNING <fs>.
IF sy-subrc = 0.
<fs>-no_out = 'X'.
<fs>-tech = 'X'.
ENDIF.
ENDFORM. " activar_hotspot