Batch Input

Batch Input utilizando a técnica de Call Transaction e Pasta.

************************************************************************
***                                                                  ***
***        PROGRAMA: X                                        ***
***        OBJETIVO: Modelo de Interface Inbound - Batch Input       ***
***                  utilizando técnia call transaction e Pasta      ***

REPORT X LINE-SIZE 172 LINE-COUNT 65.

*****************************************
*** Tabelas do Banco de Dados do R/3  ***
*****************************************
* tables:                     "Descrição da Tabela

*********************************
***  Definição de Parâmetros  ***
*********************************
SELECTION-SCREEN BEGIN OF BLOCK param WITH FRAME TITLE text-001.
PARAMETERS: p_modo(1) TYPE c DEFAULT 'N'.
SELECTION-SCREEN: ULINE.
PARAMETERS: p_file TYPE string
            DEFAULT 'c:clientes9.txt' OBLIGATORY.
SELECTION-SCREEN END OF BLOCK param.

*************************************
*** Variáveis Globais do Programa ***
*************************************
DATA: w_contatot  TYPE i,        "Total de Registros do Arquivo
      w_contaval  TYPE i,        "Registros Validos
      w_containv  TYPE i,        "Registros Invalidos
      w_contapro  TYPE i,        "Registros processados em Call Trans
      w_contabdc  TYPE i,        "Registros postos em pasta BDC
      w_numrec    TYPE i,        "Numero do Registro corrente
      w_numerros  TYPE i,        "Contador de Erros de Call Transac.
      w_codmsg(5) TYPE c,        "Codigo de Mensagem na Forma IINNN
      w_valido(1) TYPE c,        "Flag para Validação de Registros
      w_conta     TYPE i,        "Calculos de uso Geral
      w_subrc       LIKE sy-subrc.

*********************************
*** Tabelas Internas Globais  ***
*********************************
*** Tabela cm Definição das Transações Batch Input
DATA: BEGIN OF t_bdc OCCURS 100.
        INCLUDE STRUCTURE bdcdata.
DATA: END OF t_bdc.


*** Tabela para Registro de Mensagens de Erro das Transações
DATA: BEGIN OF t_msg OCCURS 10.
        INCLUDE STRUCTURE bdcmsgcoll.
DATA: END OF t_msg.

************************************************************************
***                          TYPES                                   ***
************************************************************************
TYPES: BEGIN OF ty_entrada,
         kunnr(4)  TYPE c, "Código do cliente
         vkorg(4)  TYPE c, "Organização de vendas
         vtweg(2)  TYPE c, "Canal de distribuição
         spart(2)  TYPE c, "Setor de Atividade
         ktokd(4)  TYPE c, "Grupo de Contas
         name1(18) TYPE c, "Nome do Cliente
         land1(2)  TYPE c, "País
         spras(2)  TYPE c, "Idioma
       END OF ty_entrada.

************************************************************************
***               TABELAS INTERNAS                                   ***
************************************************************************
DATA:
    t_entrada TYPE TABLE OF ty_entrada,

************************************************************************
***                    ESTRUTURAS                                    ***
************************************************************************
    e_entrada TYPE ty_entrada.

*************************************
***  Evento: at selection screen  ***
*************************************
AT SELECTION-SCREEN.

*** Valida modo de call transaction
  IF NOT p_modo CA 'AEN'.
    MESSAGE e000(zcpx001_13) WITH text-m01.
*   &
  ENDIF.

**********************************
*** Evento start-of-selection  ***
**********************************
START-OF-SELECTION.

* Rotina para leitura do arquivo
  PERFORM le_arquivo.

* Rotina para processamento dos dados de Batch Input
  PERFORM processa.

* Rotina para executar Batch Input
  PERFORM executa.

***********************************************************
*** Subrotina genérica para adicionar telas em bdcdata  ***
***********************************************************
FORM bdc_tela USING programa tela.
  CLEAR t_bdc.
  t_bdc-program  = programa.
  t_bdc-dynpro   = tela.
  t_bdc-dynbegin = 'X'.
  APPEND t_bdc.
ENDFORM.                    "BDC_TELA

**********************************************************
*** Subrotina genérica para adicionar telas em bdcdata ***
**********************************************************
FORM bdc_campo USING campo valor.
  CLEAR t_bdc.
  t_bdc-dynpro = space.
  t_bdc-fnam = campo.
  t_bdc-fval = valor.
  APPEND t_bdc.
ENDFORM.                    "BDC_CAMPO

***********************************
***   Processa registro no R/3  ***
***********************************
FORM processa.

  REFRESH t_bdc.



  LOOP AT t_entrada INTO e_entrada.

*** Tela inicial da Transação
    PERFORM bdc_tela  USING 'SAPMF02D' '0107'.
    PERFORM bdc_campo USING 'BDC_OKCODE' '/00'.
    PERFORM bdc_campo USING 'RF02D-KUNNR' e_entrada-kunnr.
    PERFORM bdc_campo USING 'RF02D-VKORG' e_entrada-vkorg.
    PERFORM bdc_campo USING 'RF02D-VTWEG' e_entrada-vtweg.
    PERFORM bdc_campo USING 'RF02D-SPART' e_entrada-spart.
    PERFORM bdc_campo USING 'RF02D-KTOKD' e_entrada-ktokd.

*** Tela de Dados principais
    PERFORM bdc_tela  USING 'SAPMF02D' '110'.
    PERFORM bdc_campo USING 'KNA1-NAME1'   e_entrada-name1.
    PERFORM bdc_campo USING 'KNA1-LAND1'   e_entrada-land1.
    PERFORM bdc_campo USING 'KNA1-SPRAS'   e_entrada-spras.
    PERFORM bdc_campo USING 'BDC_OKCODE' '=UPDA'.

*** Tela de Dados principais
    PERFORM bdc_tela  USING 'SAPMF02D' '315'.
    PERFORM bdc_campo USING 'BDC_OKCODE' '=UPDA'.
    PERFORM bdc_campo USING 'KNVV-KZAZU' 'X'.
    PERFORM bdc_campo USING 'KNVV-VSBED' '01'.
    PERFORM bdc_campo USING 'KNVV-ANTLF' '9'.

*** Tela de Dados principais
    PERFORM bdc_tela  USING 'SAPMF02D' '1350'.
    PERFORM bdc_campo USING 'KNVI-TAXKD(01)' '0'.


  ENDLOOP.

ENDFORM.                    "PROCESSA
*&---------------------------------------------------------------------*
*&      Form  LE_ARQUIVO
*&---------------------------------------------------------------------*
*       Lê arquivo de texto
*----------------------------------------------------------------------*
FORM le_arquivo .

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                      = p_file
     filetype                      = 'ASC'
    TABLES
      data_tab                      = t_entrada.
  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.                    " LE_ARQUIVO
*&---------------------------------------------------------------------*
*&      Form  EXECUTA
*&---------------------------------------------------------------------*
*       Executa Batch Input
*----------------------------------------------------------------------*
FORM executa .
*** Tenta executar com Técnica Call Transaction
  REFRESH t_msg.

  CALL TRANSACTION 'V-03' USING t_bdc MODE p_modo UPDATE 'S'
       MESSAGES INTO t_msg.
  w_subrc = sy-subrc.
  CLEAR w_numerros.
  LOOP AT t_msg.
    IF t_msg-msgtyp NA 'IWS'.
      w_numerros = w_numerros + 1.
    ENDIF.
  ENDLOOP.

*** Se transação terminou com erro, guarda dados em Pasta Batch Input
  IF w_subrc <> 0 OR w_numerros <> 0.

*** Insere transação em Pasta Batch Input
    IF w_contabdc = 0.
      CALL FUNCTION 'BDC_OPEN_GROUP'
        EXPORTING
          client = sy-mandt
          group  = 'CLIENTES_09'
          keep   = 'X'
          user   = sy-uname.
    ENDIF.
    CALL FUNCTION 'BDC_INSERT'
      EXPORTING
        tcode     = 'V-03'
      TABLES
        dynprotab = t_bdc.
    w_contabdc  = w_contabdc + 1.
  ELSE.
    w_contapro = w_contapro + 1.
  ENDIF.

ENDFORM.                    " EXECUTA

Queremos saber sua opinião