You are on page 1of 12

Tabela MIC para o Local de Expedição (ex.

3152)
Z para envio da NF à SEFAZ

Include: LJ_1B_NFEF42

Tabela : ZTSD_NFE_DRP
ENHANCEMENT: ZSDE3031_BRAZIL_MIC_NFE

*----------------------------------------------------------------------*
***INCLUDE  LJ_1B_NFEF42 .
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&       Form  Call_xi
*&---------------------------------------------------------------------*
*         text
*----------------------------------------------------------------------*
*   -->  p1          text
*   <--  p2          text
*----------------------------------------------------------------------*
FORM call_xi USING    p_bukrs TYPE bukrs
                      p_branch TYPE j_1bbranc_
                      p_model  TYPE j_1bmodel       "V1.10
                      p_parallel TYPE flag          "1276438
                      p_resend   TYPE flag          "1933985
             CHANGING p_rfcerror TYPE sy-subrc
                      p_msstat   TYPE flag
                      p_docsta   TYPE j_1bnfedocstatus.   "2807629
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""$"$\SE:(1)  Form
.  CALL_XI, Início                                                                                                    
                                                    A
*$*$-Start: (1)---------------------------------------------------------------------------------$*$*
ENHANCEMENT 1  ZSDE3031_BRAZIL_MIC_NFE.    "active version
*---------------------------------------------------------------------------------------*
*                          MODIFICATION HISTORY                                                            *
*---------------------------------------------------------------------------------------*
*  Author                               :   André Ferian                                                 *
*  Date                                 :   18/01/2012                                                  *
*  Specification Name                 :    CHG72212                                                      *
*  Functional Analyst                 :    Denis Santos                                                *
*  Technical Analyst                 :   André Ferian                                                 *
*  Enhancement point                 :   ZSDE3031_BRAZIL_MIC_NFE                                  *
*  Transport Request                 :   EDXK913669                                                  *
*  Change number                      :   72212                                                         *
*  Description                         :   Does't sent the NF  to GRC  if the NFe is  from MIC   *
*---------------------------------------------------------------------------------------*

  TABLES: ZTSD_NFE_DRP.                                                    "CHG72212
                                                                           "CHG72212
  SELECT SINGLE *                                                          "CHG72212
    FROM ZTSD_NFE_DRP                                                      "CHG72212
    INTO ZTSD_NFE_DRP                                                      "CHG72212
    WHERE BUKRS  EQ p_bukrs                                                "CHG72212
      AND BRANCH EQ p_BRANCH                                               "CHG72212
      AND VSTEL  EQ WK_HEADER-VSTEL.                                       "CHG72212
    IF syst-subrc = 0 and ZTSD_NFE_DRP-ENVIA_XML IS INITIAL.               "CHG72212
      p_rfcerror = sy-subrc.                                               "CHG72212
      EXIT.                                                                "CHG72212
    ENDIF.                                                                 "CHG72212
*BREAK-POINT.
  DATA: BEGIN OF itab_nfe_xml_key OCCURS 0,
        DOCNUM  TYPE J_1BNFE_ACTIVE-DOCNUM,
        SERIE   TYPE J_1BNFE_ACTIVE-SERIE,
        NFNUM9  TYPE J_1BNFE_ACTIVE-NFNUM9,
        MATNR   TYPE MATNR,
        key     TYPE char100,
      END OF   itab_nfe_xml_key.

  DATA: wa_nfe_xml_key LIKE LINE OF itab_nfe_xml_key,
        itab_xml_ext1_tab TYPE STANDARD TABLE OF J1B_NF_XML_EXTENSION1,
        wa_xml_ext1_tab LIKE LINE OF itab_xml_ext1_tab,
        itab_xml_ext2_tab TYPE STANDARD TABLE OF J1B_NF_XML_EXTENSION2,
        wa_xml_ext2_tab LIKE LINE OF itab_xml_ext2_tab,
        lv_item_num TYPE J_1BDOCNUM.

  IMPORT tab = itab_nfe_xml_key
         FROM MEMORY ID 'XML_KEY'.
*BREAK-POINT.
  LOOP AT itab_nfe_xml_key INTO wa_nfe_xml_key.
    wa_xml_ext1_tab-docnum = xmlh-docnum.
    wa_xml_ext2_tab-docnum = xmlh-docnum.
    lv_item_num = sy-tabix.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        INPUT         = lv_item_num
      IMPORTING
        OUTPUT        = lv_item_num
              .
    wa_xml_ext1_tab-itmnum = lv_item_num.
    wa_xml_ext1_tab-field1 = 'REFNFE'.
    APPEND wa_xml_ext1_tab to itab_xml_ext1_tab.
    wa_xml_ext2_tab-itmnum = lv_item_num.
    wa_xml_ext2_tab-param  = 'REFNFE'.
    wa_xml_ext2_tab-row    = '1'.
    wa_xml_ext2_tab-field  = 'REFNFE'.
    wa_xml_ext2_tab-value  = 'REFNFE'.
    wa_xml_ext2_tab-type   = 'CHAR100'.
    wa_xml_ext2_tab-length = '100'.
    APPEND wa_xml_ext2_tab to itab_xml_ext2_tab.
  ENDLOOP.

  IF itab_xml_ext1_tab IS NOT INITIAL.
    xml_ext1_tab[] = itab_xml_ext1_tab[].
    xml_ext2_tab[] = itab_xml_ext2_tab[].
  ENDIF.

ENDENHANCEMENT.
*$*$-End:    (1)---------------------------------------------------------------------------------$*$*

*  error handling with xNFe                           "1254565


  DATA: lt_error type J_1BNFE_INBOUND_TAB,       "1254565
        ls_error type j_1bnfe_inbound,           "1254565
        lv_subrc type sy-subrc.                  "1254565
  DATA: lt_bapiret2 TYPE bapirettab.             "1362971

  DATA: lv_error_status TYPE j_1bnfe_errstatus.           "1933985

*  Define messaging-system destination for RFC
  DATA: lv_rfcdest       TYPE rfcdest.
  DATA: lv_xnfeactive    TYPE j_1bxnfeactive.     "V1.10
  DATA: lv_dummy         TYPE c.                  "1533948

*  Government Version (Schema Package Number)
  DATA  lv_govversion    TYPE j_1bnfegovversion.    "1485135

  DATA lv_bad_request TYPE abap_bool.               "2807629

  CONSTANTS  lc_cte TYPE j_1bmodel VALUE '57'.             "1724476

  CLEAR: p_rfcerror, p_msstat.

  CALL FUNCTION 'J_1B_NFE_CHECK_RFC_DESTINATION'
    EXPORTING
      i_bukrs   = p_bukrs
      i_branch  = p_branch
      i_model   = p_model                           "V1.10
    IMPORTING
      e_rfcdest    = lv_rfcdest
      e_xnfeactive = lv_xnfeactive                  "V1.10
    EXCEPTIONS
      rfc_error = 1
      OTHERS    = 2.
*
  p_rfcerror = sy-subrc.
*  Map data  to outbound  interface only  when RFC  connection is available

  IF sy-subrc IS INITIAL.
*  non SAP  messaging system                              "V1.10
    if lv_xnfeactive is initial.                    "V1.10

      IF  p_model NE lc_cte.                               "1724476

*  NF-e Layout 3.10 changes                                      "1933985


      IF xmlh-version >= gc_nfe_version_3.                "1933985
                                                          "1933985
          PERFORM call_message_system_comm                "1933985
                  USING lv_rfcdest                        "1933985
                        p_resend                          "1933985
                        lv_xnfeactive                     "1933985
                  CHANGING lv_error_status                "1933985
                           p_rfcerror                     "1933985
                           lt_bapiret2                    "1933985
                           lv_bad_request.                "2807629
                                                          "1933985
      ELSE.                                               "1933985
                                                          "1933985
*  Call function  asych. when  RFC connection  available
*  gv_callmode can be switched in debbuging  to call the RFC  synchronous
*  do not reset gv_callmode direct after the RFC  call
      if gv_callmode is initial.
        CALL FUNCTION 'J_1B_NFE_XML_OUT'
          IN BACKGROUND TASK
          DESTINATION lv_rfcdest
          EXPORTING
            xml_in                = xmlh
            xml_item_tab          = xmli_tab
            xml_batch             = xmlb_tab
            xml_ref               = xmlr_tab
            xml_dup               = xmld_tab
            xml_vol               = xmlv_tab
            xml_head_tab          = xmlh_tab          "V1.10
            xml_imp               = xml_import_tab    "V1.10
            xml_ext1              = xml_ext1_tab      "V1.10
            xml_ext2              = xml_ext2_tab      "V1.10
            xml_adi               = xml_adi_tab       "V2.00
            xml_reb               = xml_reb_tab       "V2.00
            xml_cane_dia          = xml_cane_dia_tab  "V2.00
            xml_cane_ded          = xml_cane_ded_tab. "V2.00
*             EXCEPTIONS
*               communication_failure = 1
*               system_failure         = 2.
        IF NOT sy-subrc IS INITIAL.
          p_rfcerror = sy-subrc.
        ENDIF.
      ELSE.
        CALL FUNCTION 'J_1B_NFE_XML_OUT'
          DESTINATION lv_rfcdest
          EXPORTING
            xml_in                = xmlh
            xml_item_tab          = xmli_tab
            xml_batch             = xmlb_tab
            xml_ref               = xmlr_tab
            xml_dup               = xmld_tab
            xml_vol               = xmlv_tab
            xml_head_tab          = xmlh_tab          "V1.10
            xml_imp               = xml_import_tab    "V1.10
            xml_ext1              = xml_ext1_tab      "V1.10
            xml_ext2              = xml_ext2_tab      "V1.10
            xml_adi               = xml_adi_tab       "V2.00
            xml_reb               = xml_reb_tab       "V2.00
            xml_cane_dia          = xml_cane_dia_tab  "V2.00
            xml_cane_ded          = xml_cane_ded_tab  "V2.00
          EXCEPTIONS
            communication_failure = 1
            system_failure        = 2.
        IF NOT sy-subrc IS INITIAL.
          p_rfcerror = sy-subrc.
        ENDIF.
      ENDIF.
                                                          "1933985
      ENDIF.                                              "1933985

*begin of note  1724476
      ELSE.   "p_model  = lc_cte

        IF gv_callmode IS INITIAL.
          TRY.
            CALL FUNCTION 'J_1B_CTE_XML_OUT'
              IN BACKGROUND TASK
              DESTINATION lv_rfcdest
              EXPORTING
                iv_docnum       = xmlh-docnum
                iv_accesskey    = xmlh-id
                iv_docstat      = xmlh-docstat
                iv_scsstat      = xmlh-scsstat
                iv_authcode     = xmlh-authcod  "1756060
                iv_version      = xmlh-version
                iv_logsys       = xmlh-logsys
                it_xml_ext2     = xml_ext2_tab  "1756060
*                IMPORTING                            "1756060
*                   ev_rfcerror     = p_rfcerror     "1756060
*                   et_bapiret2     = lt_bapiret2.   "1756060
                .
            CATCH cx_sy_dyn_call_illegal_func.
          ENDTRY.

        ELSE.
          TRY.
            CALL FUNCTION 'J_1B_CTE_XML_OUT'
              DESTINATION lv_rfcdest
              EXPORTING
                iv_docnum       = xmlh-docnum
                iv_accesskey    = xmlh-id
                iv_docstat      = xmlh-docstat
                iv_scsstat      = xmlh-scsstat
                iv_authcode     = xmlh-authcod  "1756060
                iv_version      = xmlh-version
                iv_logsys       = xmlh-logsys
                it_xml_ext2     = xml_ext2_tab  "1756060
              IMPORTING
                ev_rfcerror     = p_rfcerror
                et_bapiret2     = lt_bapiret2.
            CATCH cx_sy_dyn_call_illegal_func.
          ENDTRY.
        ENDIF.
        IF NOT sy-subrc IS INITIAL.
          p_rfcerror = sy-subrc.
        ENDIF.

        IF NOT lt_bapiret2[] IS INITIAL.
*             CALL  FUNCTION 'J_1B_CTE_BAPIRET2_MAP_TO_LOG'    "1751895
          CALL FUNCTION 'J_1B_NFE_BAPIRET2_MAP_TO_LOG1'   "1751895
            EXPORTING
              iv_docnum         = xmlh-docnum
              it_bapiret2       = lt_bapiret2.
        ENDIF.

      ENDIF.   "p_model = lc_cte
*end  of note 1724476

*  SAP xNFe  messaging system                                   "V1.10


    ELSE.                                               "V1.10
*
*  for xNFe  the header data are always handled in a    "xNFe
*  table                                                       "xNFe
      append xmlh to xmlh_tab.                       "xNFe

      IF xmlh-version >= 2.                                "1485135
        lv_govversion = '006'.                             "1485135
      ENDIF.                                               "1485135
*
*        if  gv_callmode is initial.                                     "1254565
*          call function '/XNFE/NFE_CREATE'                    "V1.10  1254565
*            IN BACKGROUND TASK                                  "V1.10  1254565
*            destination lv_rfcdest                               "V1.10 1254565
*            exporting                                             "V1.10 1254565
*              is_nfe_header             = xmlh                  "V1.10  1254565
*              it_nfe_item_tab           = xmli_tab             "V1.10  1254565
*              it_nfe_med                = xmlb_tab             "V1.10 1254565
*              it_nfe_ref                = xmlr_tab             "V1.10 1254565
*              it_nfe_dup                = xmld_tab             "V1.10 1254565
*              it_nfe_vol                = xmlv_tab             "V1.10 1254565
*              it_nfe_header             = xmlh_tab             "V1.10  1254565
*              it_nfe_imp                = xml_import_tab     "V1.10 1254565
*              it_nfe_ext1               = xml_ext1_tab        "V1.10  1254565
*              it_nfe_ext2               = xml_ext2_tab        "V1.10  1254565
*              iv_syncron                = gv_callmode.    "xNFe       1254565
*            EXCEPTIONS                                            "V1.10  1254565
*              communication_failure = 1                         "V1.10 1254565
*              system_failure         = 2.                        "V1.10 1254565
*          IF NOT sy-subrc IS INITIAL.                          "V1.10 1254565
*             p_rfcerror = sy-subrc.                             "V1.10  1254565
*          ENDIF.                                                   "V1.10  1254565
*        ELSE.                                                              "1254565

*NF-e version 2
      IF xmlh-version < gc_nfe_version_3.                 "1933985

        call function '/XNFE/NFE_CREATE'                "V1.10
         destination lv_rfcdest                         "V1.10
         exporting                                      "V1.10
           is_nfe_header            = xmlh              "V1.10
           it_nfe_item_tab          = xmli_tab          "V1.10
           it_nfe_med               = xmlb_tab          "V1.10
           it_nfe_ref               = xmlr_tab          "V1.10
           it_nfe_dup               = xmld_tab          "V1.10
           it_nfe_vol               = xmlv_tab          "V1.10
           it_nfe_header            = xmlh_tab          "V1.10
           it_nfe_imp               = xml_import_tab    "V1.10
           it_nfe_ext1              = xml_ext1_tab      "V1.10
           it_nfe_ext2              = xml_ext2_tab      "V1.10
           iv_syncron               = gv_callmode    "xNFe
           iv_parallel              = p_parallel         "1276438
           it_nfe_adi               = xml_adi_tab       "V2.00
           it_nfe_reb               = xml_reb_tab       "V2.00
           it_nfe_cane_dia          = xml_cane_dia_tab  "V2.00
           it_nfe_cane_ded          = xml_cane_ded_tab  "V2.00
           it_nfe_obs_cont          = xml_obs_cont_tab       "1869063
           it_nfe_obs_fisco         = xml_obs_fisco_tab      "1869063
           it_nfe_proc_ref          = xml_proc_ref_tab       "1869063
           iv_version               = lv_govversion          "1485135
         IMPORTING                                   "xNFe
           ev_msgtyp                = p_msstat       "xNFe
         tables                                      "1254565
           ET_ERROR_STATUS          = lt_error       "1254565
           et_bapiret2              = lt_bapiret2    "1362971
         EXCEPTIONS                                     "V1.10
           communication_failure = 1                    "V1.10
           system_failure        = 2.                   "V1.10
        IF NOT sy-subrc IS INITIAL.                     "V1.10
          p_rfcerror = sy-subrc.                        "V1.10
        ENDIF.
*        ENDIF.                                                    "V1.10 1254565

*  check xNFe errors                                                 "1254565


*  at the moment only one document is processed  therfore       "1254565
*  return table  can have only one  entry. When the table is    "1254565
*  empty, everything is OK.                                         "1254565
       lv_subrc = sy-subrc.                                 "1254565

       PERFORM transfer_bapiret2_to_error_log               "1362971
               USING lt_bapiret2.                           "1362971

       read table lt_error into ls_error index 1.           "1254565
       if sy-subrc is initial.                              "1254565
         move ls_error-i_msgtyp to p_msstat.                "1254565
       else.                                                "1254565
         move c_a to p_msstat.                              "1254565
       endif.                                               "1254565
       sy-subrc = lv_subrc.                                 "1254565
                                                          "1933985
     ELSE.                                                "1933985
        PERFORM call_message_system_comm                  "1933985
                  USING lv_rfcdest                        "1933985
                        p_resend                          "1933985
                        lv_xnfeactive                     "1933985
                  CHANGING lv_error_status                "1933985
                           p_rfcerror                     "1933985
                           lt_bapiret2                    "1933985
                           lv_bad_request.                "2807629
        lv_subrc = p_rfcerror.                            "1933985
                                                          "1933985
        IF NOT lt_bapiret2[] IS INITIAL.                  "1933985
          CALL FUNCTION 'J_1B_NFE_BAPIRET2_MAP_TO_LOG1'   "1933985
            EXPORTING                                     "1933985
              iv_docnum         = xmlh-docnum             "1933985
              it_bapiret2       = lt_bapiret2.            "1933985
        ENDIF.                                            "1933985
        IF lv_error_status IS NOT INITIAL.                "1933985
           move lv_error_status to p_msstat.              "1933985
        ELSE.                                             "1933985
           move c_a to p_msstat.                          "1933985
           IF lv_bad_request = abap_true.                 "2807629
             p_docsta = 2.                                "2807629
           ENDIF.                                         "2807629
        ENDIF.                                            "1933985
        sy-subrc = lv_subrc.                              "1933985
                                                          "1933985
     ENDIF.                                               "1933985
*
    ENDIF.                                              "V1.10
  ELSE.
    p_rfcerror = sy-subrc.
  ENDIF.
  IF NOT p_rfcerror IS INITIAL.                              "1533948
    if p_rfcerror <> 2.                                      "1785471
      IF p_rfcerror = '9'.
        lv_dummy = abap_true.
      ELSE.
        MESSAGE e066 WITH lv_rfcdest INTO lv_dummy.          "1533948
      ENDIF.
    else.                                                    "1785471
      MESSAGE e554 WITH lv_rfcdest INTO lv_dummy.            "1785471
    endif.                                                   "1785471
  ENDIF.                                                     "1533948

ENDFORM.                    " Call_xi
Setup para Chamada do Serviço de Mensageria

Include: LJ_1B_NFEF71
JOB para Numeração da NF

You might also like