Etiquetas

20111220

ABAP: Ejemplo de Batch Input con resumen en ALV

Un ejemplo para hacer copy/paste de un Batch Input con salida en LVC, donde hubo que hacer un split a la tabla ya que la misma función de carga GUI_UPLOAD no estaba separando los valores.

*&---------------------------------------------------------------------*
*& Report 
zhr_sl_batch_it9092
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT zhr_sl_batch_it9092
       NO STANDARD PAGE HEADING LINE-SIZE 255.

**********************************************************************
*DECLARACION DATOS
**********************************************************************
TYPE-POOLS: slis, truxs.
INCLUDE <icon>.

*INCLUDE bdcrecx1.
TABLES t100.
DATA ctumode LIKE ctu_params-dismode VALUE 'N'.
DATA ctu VALUE 'X'.
DATA nodata VALUE '/'.
DATA cupdate LIKE ctu_params-updmode VALUE 'L'.

DATA bdcdata LIKE bdcdata    OCCURS 0 WITH HEADER LINE.
DATA messtab LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE.
*`

TYPES: BEGIN OF st_data,
        pernr(8),       "num personal
        fch_caja(10),   "fecha de inicio, fin, caja
        idconcepto(4),  "tipo venta
        monto(11),      "monto CLP
       END OF st_data.

DATA: BEGIN OF ti_salida OCCURS 0,
        pernr TYPE persno,
        fch_caja TYPE begda,
        idconcepto TYPE zed_sl_tipovta,
        monto TYPE betrg,
        waers TYPE waers,
        icon(4),
        message TYPE string,
      END OF ti_salida.

DATA ti_data TYPE TABLE OF st_data WITH HEADER LINE.
DATA inicio TYPE i.

CONSTANTS c_ok TYPE string VALUE 'OK'.

RANGES r_subty_betrg FOR p9092-subty.
RANGES r_subty_menge FOR p9092-subty.

************************************************************************
* DATOS PARA MANEJO ALV                                                *
************************************************************************
DATA:
      gs_layout TYPE lvc_s_layo,
      gs_fieldcat TYPE lvc_t_fcat,
      g_exit_caused_by_caller,
      gs_exit_caused_by_user TYPE slis_exit_by_user,
      g_repid LIKE sy-repid.
*Variants
DATA: gs_variant LIKE disvariant,
      g_save.
*"Callback
DATA:
    gt_events      TYPE slis_t_event,
    gt_list_top_of_page TYPE slis_t_listheader.


**********************************************************************
*PANTALLA SELECCION
**********************************************************************
PARAMETERS: p_file TYPE rlgrap-filename OBLIGATORY.

SELECTION-SCREEN BEGIN OF LINE.
* text-c01: ¿Archivo tiene linea de cabecera?
SELECTION-SCREEN COMMENT (34) text-c01 FOR FIELD p_header.
PARAMETERS p_header AS CHECKBOX.
SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN BEGIN OF LINE.
* text-c02: Ejecutar modo test
SELECTION-SCREEN COMMENT (34) text-c02 FOR FIELD p_test.
PARAMETERS p_test AS CHECKBOX DEFAULT 'X'.
SELECTION-SCREEN END OF LINE.

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



**********************************************************************
*DATOS PANTALLA
**********************************************************************
INITIALIZATION.


**********************************************************************
*INICIO BI
**********************************************************************
START-OF-SELECTION.

  PERFORM rangos.

  PERFORM upload_data TABLES ti_data
                      USING  p_file.

  LOOP AT ti_data.

    IF p_test = space.
      PERFORM graba_registro USING ti_data.
      PERFORM procesar_salida USING ti_data.

    ELSE.
      PERFORM simula_registro USING ti_data.

    ENDIF.

  ENDLOOP.

  PERFORM inicializa_alv.

**********************************************************************
* END-OF-SELECTION
**********************************************************************
END-OF-SELECTION.

  PERFORM muestra_alv TABLES ti_salida.


*&---------------------------------------------------------------------*
*&      define  LLENA_CEROS
*&---------------------------------------------------------------------*
  DEFINE llena_ceros.

    call function 'CONVERSION_EXIT_ALPHA_INPUT'
      exporting
        input  = &1
      importing
        output = &1.

  END-OF-DEFINITION.

*&---------------------------------------------------------------------*
*&      Form  UPLOAD_DATA
*&---------------------------------------------------------------------*
FORM upload_data  TABLES   p_ti_data STRUCTURE ti_data
                  USING    p_file TYPE rlgrap-filename.

  DATA: ti_str TYPE string OCCURS 0 WITH HEADER LINE,
        file TYPE string.

  MOVE p_file TO file.

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                = file
      has_field_separator     = 'X'
    TABLES
      data_tab                = ti_str
    EXCEPTIONS
      file_open_error         = 1
      file_read_error         = 2
      no_batch                = 3
      gui_refuse_filetransfer = 4
      invalid_type            = 5
      no_authority            = 6
      unknown_error           = 7
      bad_data_format         = 8
      header_not_allowed      = 9
      separator_not_allowed   = 10
      header_too_long         = 11
      unknown_dp_error        = 12
      access_denied           = 13
      dp_out_of_memory        = 14
      disk_full               = 15
      dp_timeout              = 16
      OTHERS                  = 17.
  IF sy-subrc <> 0.
    WRITE / sy-subrc.
    EXIT.
  ENDIF.

  IF p_header IS INITIAL.
    inicio = 1.
  ELSE.
    inicio = 2.
  ENDIF.

  LOOP AT ti_str FROM inicio.
    SPLIT ti_str AT ';' INTO
                            p_ti_data-pernr
                            p_ti_data-fch_caja
                            p_ti_data-idconcepto
                            p_ti_data-monto.

    llena_ceros: p_ti_data-pernr,
                 p_ti_data-idconcepto.


    APPEND p_ti_data.
  ENDLOOP.


ENDFORM.                    " UPLOAD_DATA

*&---------------------------------------------------------------------*
*&      Form  PROCESAR_SALIDA
*&---------------------------------------------------------------------*
FORM procesar_salida  USING    p_ti_data STRUCTURE ti_data.

  CLEAR ti_salida.
  MOVE p_ti_data-pernr TO ti_salida-pernr.
  MOVE p_ti_data-idconcepto TO ti_salida-idconcepto.

  CONCATENATE p_ti_data-fch_caja+6(4) p_ti_data-fch_caja+3(2) p_ti_data-fch_caja+0(2)
    INTO ti_salida-fch_caja.

  REPLACE ALL OCCURRENCES OF '.' IN p_ti_data-monto WITH ''.
  REPLACE ALL OCCURRENCES OF ',' IN p_ti_data-monto WITH '.'.
  DATA monto TYPE wert8.
  MOVE p_ti_data-monto TO monto.
  DIVIDE monto BY 100.
  MOVE monto TO ti_salida-monto.

  ti_salida-waers = 'CLP'.

  READ TABLE messtab WITH KEY msgid = 'PG' msgnr = '102'.
  IF sy-subrc = 0.
    WRITE '@08@' TO ti_salida-icon AS ICON. "OK
  ELSE.
    WRITE '@0A@' TO ti_salida-icon AS ICON. "Error
  ENDIF.

  PERFORM procesa_mensaje CHANGING ti_salida-message.

  APPEND ti_salida.

ENDFORM.                    " PROCESAR_SALIDA

*&---------------------------------------------------------------------*
*&      Form  PROCESA_ERROR
*&---------------------------------------------------------------------*
FORM procesa_mensaje  CHANGING message TYPE string.
  DATA: l_mstring(480).

  LOOP AT messtab.
    SELECT SINGLE * FROM t100 WHERE sprsl = messtab-msgspra
                              AND   arbgb = messtab-msgid
                              AND   msgnr = messtab-msgnr.
    IF sy-subrc = 0.
      l_mstring = t100-text.
      IF l_mstring CS '&1'.
        REPLACE '&1' WITH messtab-msgv1 INTO l_mstring.
        REPLACE '&2' WITH messtab-msgv2 INTO l_mstring.
        REPLACE '&3' WITH messtab-msgv3 INTO l_mstring.
        REPLACE '&4' WITH messtab-msgv4 INTO l_mstring.
      ELSE.
        REPLACE '&' WITH messtab-msgv1 INTO l_mstring.
        REPLACE '&' WITH messtab-msgv2 INTO l_mstring.
        REPLACE '&' WITH messtab-msgv3 INTO l_mstring.
        REPLACE '&' WITH messtab-msgv4 INTO l_mstring.
      ENDIF.
      CONDENSE l_mstring.
      CONCATENATE message l_mstring(250) INTO message SEPARATED BY space.
    ELSE.
      WRITE: / messtab.
    ENDIF.
  ENDLOOP.

ENDFORM.                    " PROCESA_ERROR

*&---------------------------------------------------------------------*
*&      Form  GRABA_REGISTRO
*&---------------------------------------------------------------------*

FORM graba_registro  USING  ti_data STRUCTURE ti_data.

  PERFORM bdc_dynpro      USING 'SAPMP50A' '1000'.
*    PERFORM bdc_field       USING 'BDC_CURSOR'
*                                  'RP50G-PERNR'.
  PERFORM bdc_field       USING 'BDC_OKCODE'
                                '=INS'.
  PERFORM bdc_field       USING 'RP50G-PERNR'
                                ti_data-pernr.
  PERFORM bdc_field       USING 'RP50G-TIMR6'
                                'X'.
  PERFORM bdc_field       USING 'RP50G-CHOIC'
                                '9092'.
  PERFORM bdc_field       USING 'RP50G-SUBTY'
                                ti_data-idconcepto.
  PERFORM bdc_dynpro      USING 'MP909200' '2000'.
*    PERFORM bdc_field       USING 'BDC_CURSOR'
*                                  'P9092-FCH_CAJA'.
  PERFORM bdc_field       USING 'BDC_OKCODE'
                                '=UPD'.
  PERFORM bdc_field       USING 'P9092-BEGDA'
                                ti_data-fch_caja.
  PERFORM bdc_field       USING 'P9092-ENDDA'
                                ti_data-fch_caja.
*    PERFORM bdc_field       USING 'P9092-IDCONCEPTO'
*                                  '0002'.

  IF ti_data-idconcepto IN r_subty_betrg.
    PERFORM bdc_field       USING 'P9092-BETRG'
                                  ti_data-monto.

  ELSEIF ti_data-idconcepto IN r_subty_menge.
    PERFORM bdc_field       USING 'P9092-MENGE'
                                  ti_data-monto.

  ENDIF.
*    PERFORM bdc_field       USING 'P9092-WAERS'
*                                  'CLP'.
  PERFORM bdc_field       USING 'P9092-FCH_CAJA'
                                ti_data-fch_caja.
  PERFORM bdc_transaction USING 'PA30'.

ENDFORM.                    " GRABA_REGISTRO


*&---------------------------------------------------------------------*
*&      Form  inicializa_alv
*&---------------------------------------------------------------------*
FORM inicializa_alv.
  g_repid = sy-repid.
  PERFORM layout_init USING gs_layout.
* TODO: inicializar fieldcat
  PERFORM fieldcat_init.
  PERFORM eventtab_build USING gt_events[].
  gs_variant-report = g_repid.
  g_save           = 'X'.

ENDFORM.                    "inicializa_alv

*&---------------------------------------------------------------------*
*&      Form  muestra_alv
*&---------------------------------------------------------------------*
FORM muestra_alv TABLES gt_outtab.
  DATA e VALUE 'E'.

  CHECK gs_fieldcat[] IS NOT INITIAL.

* TODO: inicializar cabecera
  PERFORM comment_build USING gt_list_top_of_page[].

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
         i_buffer_active    = 'X'
         i_callback_program = g_repid
*         i_callback_pf_status_set = 'ALV_STATUS'  "insertar PF-STATUS propio
*         i_callback_user_command = 'USER_COMMAND' "controlar acciones sobre registros
*        i_structure_name   = ''
         it_fieldcat_lvc        = gs_fieldcat[]
         is_layout_lvc          = gs_layout
         i_default          = 'A'
         i_save             = g_save
         is_variant         = gs_variant
         it_events          = gt_events[]

    IMPORTING
         e_exit_caused_by_caller = g_exit_caused_by_caller
         es_exit_caused_by_user  = gs_exit_caused_by_user
    TABLES
         t_outtab = gt_outtab
    EXCEPTIONS
         program_error = 1
         OTHERS        = 2.

  IF sy-subrc <> 0.
    CASE 'X'.
      WHEN g_exit_caused_by_caller.
        MESSAGE 'g_exit_caused_by_caller' TYPE e.
      WHEN gs_exit_caused_by_user-back.
        MESSAGE 'gs_exit_caused_by_user-back' TYPE e.
      WHEN gs_exit_caused_by_user-exit.
        MESSAGE 'gs_exit_caused_by_user-exit' TYPE e.
      WHEN gs_exit_caused_by_user-cancel.
        MESSAGE 'gs_exit_caused_by_user-cancel' TYPE e.
      WHEN OTHERS.
        MESSAGE 'N/E Error ALV' TYPE e.
    ENDCASE.
  ENDIF.

ENDFORM.                    "muestra_alv

*&---------------------------------------------------------------------*
*&      Form  layout_init
*&---------------------------------------------------------------------*
FORM layout_init USING rs_layout TYPE lvc_s_layo.
  rs_layout-zebra             = 'X'.
  rs_layout-edit = ' '.
  rs_layout-cwidth_opt = 'X'.
ENDFORM.                    "layout_init

*&---------------------------------------------------------------------*
*&      Form  fieldcat_init
*&---------------------------------------------------------------------*
FORM fieldcat_init.

  DATA: ls_fieldcat TYPE lvc_s_fcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'PERNR'.
  ls_fieldcat-ref_table = 'PA9020'.
  ls_fieldcat-key = 'X'.
  APPEND ls_fieldcat TO gs_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'FCH_CAJA'.
  ls_fieldcat-ref_table = 'PA9092'.
  ls_fieldcat-key = 'X'.
  APPEND ls_fieldcat TO gs_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'IDCONCEPTO'.
  ls_fieldcat-ref_table = 'PA9092'.
  ls_fieldcat-key = 'X'.
  APPEND ls_fieldcat TO gs_fieldcat.

  DATA ti_data LIKE ti_salida.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'MONTO'.
  ls_fieldcat-ref_table = 'PA9092'.
  ls_fieldcat-ref_field = 'BETRG'.
  ls_fieldcat-cfieldname = 'WAERS'.
  APPEND ls_fieldcat TO gs_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'WAERS'.
  ls_fieldcat-ref_table = 'PA9092'.
  ls_fieldcat-no_out = 'X'.
  APPEND ls_fieldcat TO gs_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'ICON'.
  APPEND ls_fieldcat TO gs_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'MESSAGE'.
  ls_fieldcat-scrtext_l = text-t10.
  ls_fieldcat-scrtext_m = text-t10.
  ls_fieldcat-scrtext_s = text-t10.
  APPEND ls_fieldcat TO gs_fieldcat.

ENDFORM.                    "fieldcat_init

*&---------------------------------------------------------------------*
*&      Form  comment_build
*&---------------------------------------------------------------------*
FORM comment_build USING lt_top_of_page TYPE slis_t_listheader.
  DATA: ls_line TYPE slis_listheader.
  DATA: texto TYPE string.

* LIST HEADING LINE: TYPE H
  CLEAR ls_line.
  ls_line-typ  = 'H'."Cabecera
* LS_LINE-KEY:  NOT USED FOR THIS TYPE
  ls_line-info = sy-title.
  APPEND ls_line TO lt_top_of_page.

ENDFORM.                    "comment_build

*&---------------------------------------------------------------------*
*&      Form  eventtab_build
*&---------------------------------------------------------------------*
FORM eventtab_build USING rt_events TYPE slis_t_event.
  DATA: g_status_set   TYPE slis_formname VALUE 'PF_STATUS_SET',
        g_user_command TYPE slis_formname VALUE 'USER_COMMAND',
        g_top_of_page  TYPE slis_formname VALUE 'TOP_OF_PAGE',
        g_top_of_list  TYPE slis_formname VALUE 'TOP_OF_LIST',
        g_data_changed TYPE slis_formname VALUE 'DATA_CHANGED',
        g_end_of_list  TYPE slis_formname VALUE 'END_OF_LIST'.

  DATA: ls_event TYPE slis_alv_event.
*
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    EXPORTING
      i_list_type = 0
    IMPORTING
      et_events   = rt_events.

* top of page
  READ TABLE rt_events WITH KEY name = slis_ev_top_of_page
                           INTO ls_event.
  IF sy-subrc = 0.
    MOVE g_top_of_page TO ls_event-form.
    APPEND ls_event TO rt_events.
  ENDIF.

* data changed: procesar cambios en datos especificos de alv modificable
  ls_event-name = slis_ev_data_changed.
  ls_event-form = g_data_changed.
  APPEND ls_event TO rt_events.


ENDFORM.                    "eventtab_build

*&---------------------------------------------------------------------*
*&      Form  top_of_page
*&---------------------------------------------------------------------*
FORM top_of_page.
  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      i_logo             = ''     "<-- inserte logo empresa
      it_list_commentary = gt_list_top_of_page.
ENDFORM.                    "top_of_page
*&---------------------------------------------------------------------*
*&      Form  RANGOS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM rangos .

  REFRESH: r_subty_betrg, r_subty_menge.

  APPEND 'IEQ0001' TO r_subty_betrg.
  APPEND 'IEQ0002' TO r_subty_betrg.
  APPEND 'IEQ0005' TO r_subty_betrg.

  APPEND 'IEQ0003' TO r_subty_menge.
  APPEND 'IEQ0004' TO r_subty_menge.
  APPEND 'IEQ0006' TO r_subty_menge.

ENDFORM.                    " RANGOS

*&---------------------------------------------------------------------*
*&      Form  CHECK_TIPO_SUBTY
*&---------------------------------------------------------------------*
FORM check_tipo_subty  USING    p_subty TYPE subty
                       CHANGING p_betrg TYPE char1
                                p_menge TYPE char1.

  PERFORM rangos.
  CLEAR: p_betrg, p_menge.

  IF p_subty IN r_subty_betrg.
    p_betrg = 'X'.
  ELSE.
    p_menge = 'X'.
  ENDIF.

ENDFORM.                    " CHECK_TIPO_SUBTY



*&---------------------------------------------------------------------*
*&      Form  BDC_DYNPRO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PROGRAM    text
*      -->DYNPRO     text
*----------------------------------------------------------------------*
FORM bdc_dynpro USING program dynpro.
  CLEAR bdcdata.
  bdcdata-program  = program.
  bdcdata-dynpro   = dynpro.
  bdcdata-dynbegin = 'X'.
  APPEND bdcdata.
ENDFORM.                    "BDC_DYNPRO

*&---------------------------------------------------------------------*
*&      Form  BDC_FIELD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->FNAM       text
*      -->FVAL       text
*----------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
  IF fval <> nodata.
    CLEAR bdcdata.
    bdcdata-fnam = fnam.
    bdcdata-fval = fval.
    APPEND bdcdata.
  ENDIF.
ENDFORM.                    "BDC_FIELD

*&---------------------------------------------------------------------*
*&      Form  BDC_TRANSACTION
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->TCODE      text
*----------------------------------------------------------------------*
FORM bdc_transaction USING tcode.
  DATA: l_mstring(480).
  DATA: l_subrc LIKE sy-subrc.
* batch input session

  REFRESH messtab.
  CALL TRANSACTION tcode USING bdcdata
                   MODE   ctumode
                   UPDATE cupdate
                   MESSAGES INTO messtab.
  l_subrc = sy-subrc.

  REFRESH bdcdata.
ENDFORM.                    "BDC_TRANSACTION
*&---------------------------------------------------------------------*
*&      Form  SIMULA_REGISTRO
*&---------------------------------------------------------------------*
FORM simula_registro  USING    ti_data STRUCTURE ti_data.
  INFOTYPES 0000.
  DATA t_bloq TYPE seqg3 OCCURS 0 WITH HEADER LINE.
  CONSTANTS gname TYPE seqg3-gname VALUE 'PREL'.

  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = ti_data-pernr
    IMPORTING
      output = ti_data-pernr.


  CLEAR ti_salida.
  MOVE ti_data-pernr TO ti_salida-pernr.
  MOVE ti_data-idconcepto TO ti_salida-idconcepto.
  CONCATENATE ti_data-fch_caja+6(4) ti_data-fch_caja+3(2) ti_data-fch_caja+0(2)
  INTO ti_salida-fch_caja.

  REPLACE ALL OCCURRENCES OF '.' IN ti_data-monto WITH ''.
  REPLACE ALL OCCURRENCES OF ',' IN ti_data-monto WITH '.'.
  DATA monto TYPE wert8.
  MOVE ti_data-monto TO monto.
  DIVIDE monto BY 100.
  MOVE monto TO ti_salida-monto.

  ti_salida-waers = 'CLP'.


  rp-read-infotype ti_data-pernr 0000 p0000 sy-datum sy-datum.

  READ TABLE p0000 INDEX 1.
  IF sy-subrc = 0.

    CLEAR t_bloq[].

    CALL FUNCTION 'ENQUEUE_READ'
      EXPORTING
        gname                 = gname
        guname                = space
      TABLES
        enq                   = t_bloq[]
      EXCEPTIONS
        communication_failure = 1
        system_failure        = 2
        OTHERS                = 3.

    READ TABLE t_bloq WITH KEY garg+3(8) = ti_salida-pernr.
    IF sy-subrc = 0.
      WRITE '@0A@' TO ti_salida-icon AS ICON. "Error
      ti_salida-message = text-e02.
      APPEND ti_salida.

    ELSE.
      WRITE '@08@' TO ti_salida-icon AS ICON. "OK
      ti_salida-message = text-s20.
      APPEND ti_salida.

    ENDIF.

  ELSE.
    WRITE '@0A@' TO ti_salida-icon AS ICON. "Error
    ti_salida-message = text-e01.
    APPEND ti_salida.

  ENDIF.

ENDFORM.                    " SIMULA_REGISTRO

No hay comentarios:

Publicar un comentario en la entrada