Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

*&---------------------------------------------------------------------*
*& Report  ZNFE_JOB_LOTE_RESEND
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ZNFE_JOB_LOTE_RESEND.

TABLES: /XNFE/BATSTA.

DATA: TI_BATCH   TYPE TABLE OF /XNFE/BATSTA.
DATA: LT_BATSTA        TYPE /XNFE/BATSTA_T.

DATA: LT_ELEMENT   TYPE          WDR_CONTEXT_ELEMENT_SET,
      LT_BATCH     TYPE          /XNFE/BATCH_T,
      LT_NFEHD     TYPE          /XNFE/NFEHD_T,
      LT_NFE       TYPE          /XNFE/NFE_STRING_TAB,
      LT_STATUS    TYPE          /XNFE/BAT_HIST_T.

DATA: LS_DATA      TYPE          /XNFE/WD_BATCH_S,
      LS_NFEHD     TYPE          /XNFE/NFEHD,
      LS_BATCH     TYPE          /XNFE/BATCH_S,
      LS_STATUS    TYPE          /XNFE/BAT_HIST.

DATA: LL_BATCH         LIKE LINE OF  TI_BATCH,
      LL_STATHIS       LIKE LINE OF  LT_STATUS.

DATA: LV_BATCHID   TYPE          /XNFE/BATCHID,
      LV_FUNCMOD   TYPE          /XNFE/FUNCMOD,
      LV_GOVVERS   TYPE          /XNFE/GOVVERS,
      LV_ERRORSTAT TYPE          /XNFE/ERROR_STATUS,
      LV_ERROR     TYPE          STRING,
      LV_TIMESTAMP TYPE          TIMESTAMPL,
      LV_VERSION   TYPE          /XNFE/VERSION,
      LV_TEXT      TYPE          STRING,
      LV_P1        TYPE          SYST-MSGV1,
      LV_P2        TYPE          SYST-MSGV2,
      LV_CABEC     TYPE          /XNFE/CABEC_VERS,
      LV_LOTE      TYPE          /XNFE/BATCHID.

DATA: LT_KEY     TYPE          /XNFE/BAT_HIST_PK_T,
      LS_KEY     LIKE LINE OF  LT_KEY,
      LT_STATHIS TYPE          /XNFE/BAT_HIST_T,
      LS_STATHIS LIKE LINE OF  LT_STATHIS.

DATA: IT_BAT_HIST LIKE LINE OF  LT_STATHIS.

DATA: LV_SENDTMPL_FROM TYPE          TIMESTAMPL,
      LV_SENDTMPL_TO   TYPE          TIMESTAMPL,
      DH_ERROR_REPR    TYPE          TIMESTAMPL.

*---- Error Status
CONSTANTS: BEGIN OF GC_ERROR,                               "#EC NEEDED
             XI         TYPE /XNFE/ERROR_STATUS  VALUE '01', "generell
             XMLVALID   TYPE /XNFE/ERROR_STATUS  VALUE '10', "Validation
             SSERVICE   TYPE /XNFE/ERROR_STATUS  VALUE '20', "Signatur
             SZERTI     TYPE /XNFE/ERROR_STATUS  VALUE '21', "Signatur
             SV_SERV    TYPE /XNFE/ERROR_STATUS  VALUE '22', "Signatur
             SV_XMLV    TYPE /XNFE/ERROR_STATUS  VALUE '23', "Signatur
             SXISYS     TYPE /XNFE/ERROR_STATUS  VALUE '24', "Signatur
             SXIAPP     TYPE /XNFE/ERROR_STATUS  VALUE '25', "Signatur
             SKEYST     TYPE /XNFE/ERROR_STATUS  VALUE '26', "Signatur
             STSRV      TYPE /XNFE/ERROR_STATUS  VALUE '27', "Signatur
             SIGERROR   TYPE /XNFE/ERROR_STATUS  VALUE '28', "Signatur
             BFINISHED  TYPE /XNFE/ERROR_STATUS  VALUE '31', "Batch
             BINPROC    TYPE /XNFE/ERROR_STATUS  VALUE '32', "Batch
             BSEFAZ     TYPE /XNFE/ERROR_STATUS  VALUE '33', "Batch
             BSCAN      TYPE /XNFE/ERROR_STATUS  VALUE '34', "Batch
             BSIGN_S    TYPE /XNFE/ERROR_STATUS  VALUE '35', "Batch
             BXISYS     TYPE /XNFE/ERROR_STATUS  VALUE '36', "Batch
             BXIAPP     TYPE /XNFE/ERROR_STATUS  VALUE '37', "Batch
             BSERV      TYPE /XNFE/ERROR_STATUS  VALUE '38', "Batch
             BSRXISYS   TYPE /XNFE/ERROR_STATUS  VALUE '40', "B Stat Req
             BSRXIAPP   TYPE /XNFE/ERROR_STATUS  VALUE '41', "B Stat Req
             BSRMAXREQ  TYPE /XNFE/ERROR_STATUS  VALUE '42', "B Stat Req
             BSRBNFOUND TYPE /XNFE/ERROR_STATUS  VALUE '43', "B Stat Req
             BSROTHERS  TYPE /XNFE/ERROR_STATUS  VALUE '44', "B Stat Req
             BSRB2B     TYPE /XNFE/ERROR_STATUS  VALUE '45', "B Stat Req
             BREJECT    TYPE /XNFE/ERROR_STATUS  VALUE '46', "rejected Batch
             CXISYS     TYPE /XNFE/ERROR_STATUS  VALUE '50', "Canc/Skip
             CXIAPP     TYPE /XNFE/ERROR_STATUS  VALUE '51', "Canc/Skip
             COTHERS    TYPE /XNFE/ERROR_STATUS  VALUE '52', "Canc/Skip
             SSYSCAN    TYPE /XNFE/ERROR_STATUS  VALUE '53', "Sign/Canc
             SAPPCAN    TYPE /XNFE/ERROR_STATUS  VALUE '54', "Sign/Canc
             SSYSSKIP   TYPE /XNFE/ERROR_STATUS  VALUE '55', "Sign/Skip
             SAPPSKIP   TYPE /XNFE/ERROR_STATUS  VALUE '56', "Sign/Skip
             GAPSTATERR TYPE /XNFE/ERROR_STATUS  VALUE '57', "Gap/status_error
             GAPIDERR   TYPE /XNFE/ERROR_STATUS  VALUE '58', "Gap/id_error
             SSERVICE_S TYPE /XNFE/ERROR_STATUS  VALUE '60', "Sign Scan
             SZERTI_S   TYPE /XNFE/ERROR_STATUS  VALUE '61', "Sign Scan
             SV_SERV_S  TYPE /XNFE/ERROR_STATUS  VALUE '62', "Sign Scan
             SV_XMLV_S  TYPE /XNFE/ERROR_STATUS  VALUE '63', "Sign Scan
             SXISYS_S   TYPE /XNFE/ERROR_STATUS  VALUE '64', "Sign Scan
             SXIAPP_S   TYPE /XNFE/ERROR_STATUS  VALUE '65', "Sign Scan
             SKEYST_S   TYPE /XNFE/ERROR_STATUS  VALUE '66', "Sign Scan
             STSRV_S    TYPE /XNFE/ERROR_STATUS  VALUE '67', "Sign Scan
             SIGERROR_S TYPE /XNFE/ERROR_STATUS  VALUE '68', "Sign Scan
             ERROR_GOV  TYPE /XNFE/ERROR_STATUS  VALUE '70', "Error Goverment
             ERROR_REJ  TYPE /XNFE/ERROR_STATUS  VALUE '71', "Error Rejected
             ERROR_DEN  TYPE /XNFE/ERROR_STATUS  VALUE '72', "Error Denied
             TMP_ERR_SEFAZ TYPE /XNFE/ERROR_STATUS  VALUE '73', "Error Goverment
             ERR_XML_GOV TYPE /XNFE/ERROR_STATUS VALUE '74', "XML-Error Goverment
             UNEXPEC_ERR TYPE /XNFE/ERROR_STATUS VALUE '75', "999 Batch Error Goverment
             SRV_XISYS  TYPE /XNFE/ERROR_STATUS  VALUE '80', "Service Status
             SRV_XIAPP  TYPE /XNFE/ERROR_STATUS  VALUE '81', "Service Status
             SRV_OTHERS TYPE /XNFE/ERROR_STATUS  VALUE '82', "Service Status
             NSC_XISYS  TYPE /XNFE/ERROR_STATUS  VALUE '90', "Nfe Status Check
             NSC_XIAPP  TYPE /XNFE/ERROR_STATUS  VALUE '91', "Nfe Status Check
             B2B_XISYS  TYPE /XNFE/ERROR_STATUS  VALUE '92', "B2B
             B2B_XIAPP  TYPE /XNFE/ERROR_STATUS  VALUE '93', "B2B
           END OF GC_ERROR.

* check if SEFAZ system is online
DATA:
   LS_SRVSTA     TYPE /XNFE/SRVSTA,
   LT_SRVSTA     TYPE /XNFE/SRVSTA_T.

*&---------------------------------------------------------------------*
* PARAMETROS DE SELEÇÃO
*&---------------------------------------------------------------------*
SELECT-OPTIONS: SO_BATID    FOR /XNFE/BATSTA-BATCHID,
                SO_UF       FOR /XNFE/BATSTA-UF_EMIT,
                SO_STA      FOR /XNFE/BATSTA-STATCODE.
PARAMETERS: PA_DTFR     LIKE SY-DATUM,
            PA_HRFR     LIKE SY-UZEIT,
            PA_DTTO     LIKE SY-DATUM,
            PA_HRTO     LIKE SY-UZEIT.

*&---------------------------------------------------------------------*
START-OF-SELECTION.
*&---------------------------------------------------------------------*

*convert timestamp - date from
  CONVERT DATE PA_DTFR TIME PA_HRFR
               INTO TIME STAMP LV_SENDTMPL_FROM TIME ZONE SY-ZONLO.

*convert timestamp - date to
  CONVERT DATE PA_DTTO TIME PA_HRTO
               INTO TIME STAMP LV_SENDTMPL_TO TIME ZONE SY-ZONLO.

  IF LV_SENDTMPL_FROM IS INITIAL AND LV_SENDTMPL_TO IS INITIAL.

    SELECT * FROM /XNFE/BATSTA
             INTO TABLE TI_BATCH
             WHERE BATCHID      IN SO_BATID
               AND UF_EMIT      IN SO_UF
               AND STATCODE     IN SO_STA.
    IF SY-SUBRC <> 0.
      EXIT.
    ENDIF.

  ELSE.

    SELECT * FROM /XNFE/BATSTA
           INTO TABLE TI_BATCH
           WHERE BATCHID      IN SO_BATID
             AND UF_EMIT      IN SO_UF
             AND SENDTMPL BETWEEN LV_SENDTMPL_FROM AND LV_SENDTMPL_TO
             AND STATCODE     IN SO_STA.
    IF SY-SUBRC <> 0.
      EXIT.
    ENDIF.

  ENDIF.

* determine Batchstatus and Errorstatus
  LOOP AT TI_BATCH INTO LL_BATCH.
    LS_KEY-BATCHID = LL_BATCH-BATCHID.
    APPEND LS_KEY TO LT_KEY.
  ENDLOOP.

  CALL FUNCTION '/XNFE/BAT_HIST_READ_AKTUAL'
    EXPORTING
      IT_BATKEY        = LT_KEY
    IMPORTING
      ET_AKT_BATSTATUS = LT_STATHIS
    EXCEPTIONS
      KEY_TABLE_EMPTY  = 0
      NO_STATUS_FOUND  = 0
      OTHERS           = 0.

  LOOP AT TI_BATCH INTO LL_BATCH.

    CLEAR: LS_STATHIS.

*read actual status
    READ TABLE LT_STATHIS INTO LS_STATHIS
                          WITH KEY BATCHID = LL_BATCH-BATCHID.

    IF SY-SUBRC = 0 AND LS_STATHIS-ERROR_STATUS NE SPACE.

      IF LS_STATHIS-ERROR_STATUS = GC_ERROR-XI     OR
         LS_STATHIS-ERROR_STATUS = GC_ERROR-BSEFAZ OR
         LS_STATHIS-ERROR_STATUS = GC_ERROR-BSCAN  OR
         LS_STATHIS-ERROR_STATUS = GC_ERROR-BXISYS OR
         LS_STATHIS-ERROR_STATUS = GC_ERROR-BXIAPP OR
         LS_STATHIS-ERROR_STATUS = GC_ERROR-BSERV  OR    "38
         LS_STATHIS-ERROR_STATUS = GC_ERROR-UNEXPEC_ERR. "999 Batch error

        SELECT * FROM /XNFE/SRVSTA INTO TABLE LT_SRVSTA
         WHERE CUF   = LL_BATCH-CUF
           AND TPAMB = LL_BATCH-TPAMB
           AND GOVSYS = SPACE.

        SORT LT_SRVSTA BY CHECKTMPL DESCENDING.
        READ TABLE LT_SRVSTA INTO LS_SRVSTA INDEX 1.

        CHECK LS_SRVSTA-STATUS = '107'.

* enqueue batch id
        CALL FUNCTION 'ENQUEUE_/XNFE/ENQ_BATSTA'
          EXPORTING
            MODE_/XNFE/BATSTA = 'X'
            MANDT             = SY-MANDT
            BATCHID           = LL_BATCH-BATCHID
            X_BATCHID         = ' '
            _SCOPE            = '2'
            _WAIT             = ' '
            _COLLECT          = ' '
          EXCEPTIONS
            FOREIGN_LOCK      = 1
            SYSTEM_FAILURE    = 2
            OTHERS            = 3.
        IF SY-SUBRC = 1.
          MESSAGE S069(/XNFE/APP) WITH LL_BATCH-BATCHID.
          CONTINUE.
        ENDIF.

        MOVE-CORRESPONDING LL_BATCH TO LS_DATA.

*read all nfe from batch
        CLEAR LT_BATCH.
        CALL FUNCTION '/XNFE/READ_NFE_FROMBATCH'
          EXPORTING
            IS_BATCHDATA  = LS_DATA
            IV_BATCHID    = LL_BATCH-BATCHID
          IMPORTING
            ET_BATCH      = LT_BATCH
          EXCEPTIONS
            EMPTY_BATCHID = 0
            OTHERS        = 0.

* read all NF-e-strings
        CLEAR LT_NFE.
        CALL FUNCTION '/XNFE/GET_NFE_STRING'
          EXPORTING
            IT_BATCH     = LT_BATCH
          IMPORTING
            ET_NFESTRING = LT_NFE
          EXCEPTIONS
            NO_XML       = 1
            OTHERS       = 2.

        CALL FUNCTION '/XNFE/GET_XML_VERSION'
          EXPORTING
            IV_CUF                    = LL_BATCH-CUF
            IV_TPAMB                  = LL_BATCH-TPAMB
            IV_ELEMENT                = 'NFE'
          IMPORTING
            EV_GOVVERS                = LV_GOVVERS
            EV_VERSION                = LV_VERSION
            EV_CABEC                  = LV_CABEC
          EXCEPTIONS
            GOVERNMENT_VERSION_MISSED = 1
            VERSIONS_NOT_MAINTAINED   = 2
            OTHERS                    = 3.

        CALL FUNCTION '/XNFE/005A_BATCH_NFE_OUT'
          EXPORTING
            IV_CUF          = LL_BATCH-CUF
            IV_TPEMIS       = LL_BATCH-TPEMIS
            IV_TPAMB        = LL_BATCH-TPAMB
            IV_VERSAO       = LV_VERSION
            IV_VERSAO_CABEC = LV_CABEC
            IV_VERSAO_DADOS = LV_VERSION
            IV_ID_LOTE      = LL_BATCH-BATCHID
            IT_NFE          = LT_NFE
          IMPORTING
            EV_ERROR        = LV_ERROR.

        IF LV_ERROR IS NOT INITIAL.
          LV_ERRORSTAT = '01'.
        ENDIF.

        CLEAR LT_STATUS.
        GET TIME STAMP FIELD LV_TIMESTAMP.
        LS_STATUS-ERTIME       = LV_TIMESTAMP.
        PERFORM GET_TIME_STAMP IN PROGRAM /XNFE/SAPLAPPL_PROCESSES CHANGING LS_STATUS-ERTIME.
        LS_STATUS-BATCHID      = LL_BATCH-BATCHID.
        LS_STATUS-BATSTAT      = '02'.
        LS_STATUS-ERROR_STATUS = LV_ERRORSTAT.
        LS_STATUS-ERNAME       = SY-UNAME.
        APPEND LS_STATUS TO  LT_STATUS.

*set status
        CALL FUNCTION '/XNFE/BAT_HIST_UPDATE'
          EXPORTING
            IT_BAT_HIST        = LT_STATUS
          EXCEPTIONS
            UPDATE_TABLE_EMPTY = 0
            OTHERS             = 0.

        CALL FUNCTION 'DEQUEUE_/XNFE/ENQ_BATSTA'
          EXPORTING
            MODE_/XNFE/BATSTA = 'X'
            MANDT             = SY-MANDT
            BATCHID           = LL_BATCH-BATCHID
            X_BATCHID         = ' '
            _SCOPE            = '3'
            _SYNCHRON         = ' '
            _COLLECT          = ' '.

        CALL FUNCTION '/XNFE/COMMIT_WORK'.

        WRITE :/001 'Lote Processado: ' ,
                    LL_BATCH-BATCHID.

      ENDIF.

    ENDIF.

  ENDLOOP.

  MESSAGE S007(/XNFE/APP).

  • No labels