create or replace
PROCEDURE EXPORTA_NF_SAIDA_SAP(P_DATAINI   IN DATE,
                               P_DATAFINAL IN DATE,
                               P_CDFILIAL  IN VARCHAR2,
                               P_TCDFILIAL IN CHAR,
                               P_NUMERORE  IN VARCHAR2 DEFAULT NULL,
                               P_IDTELAEXP IN CHAR DEFAULT 'N')
IS
   V_VERFILIAL  INTEGER;
   V_FILIAL     VARCHAR2(4);
   V_NRLANCORIG VARCHAR2(20);
   V_SERNFORIG  VARCHAR2(5);
   V_FILIORIG   VARCHAR2(4);
   V_FILIDEST   VARCHAR2(4);
   V_FILIALTG   VARCHAR2(4);
   V_FECHDIA    VARCHAR2(1);
   V_DSREMOTEADDR VARCHAR2(15);
   V_DSREMOTEHOST VARCHAR2(15);
   V_DSSERVERRDM  VARCHAR2(20);
   V_DSSERVMETHOD VARCHAR2(30);
   V_DSREMOTEOPER VARCHAR2(12);
   V_CDARVPROD    VARCHAR2(20);

   FUNCTION GET_FILIALTG(V_FILIAL VARCHAR2, V_NRLANCTO VARCHAR2) RETURN VARCHAR2
   IS
      V_RETORNO VARCHAR2(6);
   BEGIN
      SELECT MAX(DECODE(N.IDENTRSAIDOP, NULL, NVL(NVL(P.CDEXFILIAL,N.CDFILIAL),'    '),
                        DECODE(P.IDTIPODADOTG, 'L', NVL(SUBSTR(I.CDLOCALESTOQ, -4),'    '),
                                               'A', NVL(SUBSTR(I.CDALMOXARIFE, -4),'    '),
                                               NVL(NVL(P.CDEXFILIAL,N.CDFILIEMIT),'    ')
                               )
                        )
                 ) CDFILIALTG
        INTO V_RETORNO
        FROM NOTAFISCAL N, ITEMPRODUTO I, PARAMFILIAL P
       WHERE (N.CDFILIAL   = V_FILIAL)
         AND (N.NRLANCTONF = V_NRLANCTO)
         AND (N.CDFILIAL   = I.CDFILIAL)
         AND (N.NRLANCTONF = I.NRLANCTONF)
         AND (N.CDFILIEMIT = P.CDFILIAL);
      RETURN V_RETORNO;
   END;

   FUNCTION GET_PRODUTO(V_CDPRODUTO VARCHAR2, V_FUNCAOFILI VARCHAR2) RETURN VARCHAR2
   IS
      V_CDPROD  VARCHAR2(20);
      V_RETORNO VARCHAR2(20);
   BEGIN
      IF (V_FUNCAOFILI = 'O') THEN
         BEGIN
            SELECT MIN(CDPRARVORIG) CDPRARVORIG
              INTO V_CDPROD
              FROM RELACPRODOL
             WHERE CDPRODARVOL = V_CDPRODUTO;
         EXCEPTION
            WHEN OTHERS THEN
               V_CDPROD := V_CDPRODUTO;
         END;
      ELSE
         V_CDPROD := V_CDPRODUTO;
      END IF;

      IF V_CDPROD IS NULL THEN
         V_CDPROD := V_CDPRODUTO;
      END IF;            
      
      SELECT CDARVPROD
        INTO V_RETORNO
        FROM PRODUTO
       WHERE CDPRODUTO = V_CDPROD;

      RETURN V_RETORNO;
   END;
BEGIN
   SELECT LENGTH(REGEXP_REPLACE(P_CDFILIAL, '\d'))
     INTO V_VERFILIAL
     FROM DUAL;

   WEBTUNNING(V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVERRDM, V_DSSERVMETHOD, V_DSREMOTEOPER);

   IF (P_IDTELAEXP = 'N') AND (V_DSREMOTEOPER <> 'VIA BD') THEN
      V_FECHDIA := 'S';
   ELSE
      V_FECHDIA := 'N';
   END IF;

   IF ((V_VERFILIAL > 2) OR (P_TCDFILIAL = 'T')) THEN
      FOR C_NOTAFISCAL IN
          (SELECT N.CDFILIAL, N.NRLANCTONF, N.CDFILIAL || N.NRLANCTONF CDUNICO, N.IDENTRSAIDOP, N.CDEMPRESA,
                  N.CDFILIEMIT, N.CDTIPOOPER,
                  CASE WHEN N.NRINSJURNF IS NULL
                       THEN N.CDCLIENTE
                       ELSE N.NRINSJURNF
                  END AS CDINSCRICAO,
                  N.CDSERIE, N.NRNOTAFISC, N.CDMODDOCFISC, N.DTEMISSAO, N.DTENTRSAID, N.NRACESSONFE, N.VRNOTAFISC,
                  N.NRSEQCAPADOC, N.DTENTRSAID AS DTCONTABILTG, T.IDORIGDESTOP, N.CDFILIRECE,
                  CASE WHEN T.IDORIGDESTOP = 'L'
                       THEN 'CR'
                       ELSE NVL(T.IDORIGDESTOP, 'C')
                  END AS IDTPINSJURNF, NRPROTOCOLONFE,
                  TO_CHAR(DTHRPROTOCOLNFE, 'DD/MM/YYYY') AS DTAUTPROTOCOLO, TO_CHAR(DTHRPROTOCOLNFE, 'HH24:MI:SS') AS HRAUTPROTOCOLO,
                  N.IDPGTOFRETE AS INCOTERM, N.IDSTATUSNFE, NVL(PF.IDCONSREFECH, 'N') IDCONSREFECH, F.IDFUNCAOFILI
             FROM NOTAFISCAL N, PARAMFILIAL PF, FILIAL F, TIPOOPERAC T
            WHERE N.DTENTRSAID BETWEEN P_DATAINI AND (CASE WHEN P_IDTELAEXP = 'S'
                                                          THEN CASE WHEN (PF.DTPROCESSA  <= P_DATAFINAL)
                                                                    THEN PF.DTPROCESSA - 1
                                                                    ELSE TO_DATE(P_DATAFINAL)
                                                               END
                                                          ELSE TO_DATE(P_DATAFINAL)
                                                     END)
              AND ((INSTR(P_CDFILIAL, '_' || N.CDFILIEMIT || '_') > 0) OR (P_TCDFILIAL = 'T'))
              AND ((INSTR(P_NUMERORE, N.NRSEQCAPADOC) > 0) OR (NVL(P_NUMERORE, 'T') = 'T'))
              AND N.CDFILIEMIT = PF.CDFILIAL
              AND N.CDFILIEMIT = F.CDFILIAL
              AND PF.CDFILIAL  = F.CDFILIAL
              AND PF.CDFILIAL = P_CDFILIAL
              AND N.IDENTRSAIDOP = T.IDENTRSAIDOP
              AND N.CDTIPOOPER   = T.CDTIPOOPER
              AND N.CDMODDOCFISC IS NOT NULL
              AND N.NRSEQCAPADOC IS NOT NULL
              AND N.CDTIPOOPER NOT IN ('01','24','63','65','66','12')
              AND T.IDENTRSAIDOP = 'S'
              AND ((N.IDSTATUS IN ('A','E')) OR (N.IDSTATUSNFE IN ('A', 'D')))
              AND N.CDFILIAL||N.NRLANCTONF NOT IN (SELECT CDUNICO FROM USR_SAP_NOTAFISCAL)
              AND EXISTS (SELECT I.NRLANCTONF
                            FROM ITEMPRODUTO I
                           WHERE I.CDFILIAL = N.CDFILIAL
                             AND I.NRLANCTONF = N.NRLANCTONF)
            UNION ALL

            SELECT N.CDFILIAL, N.NRLANCTONF, N.CDFILIAL || N.NRLANCTONF CDUNICO, N.IDENTRSAIDOP, N.CDEMPRESA,
                  N.CDFILIEMIT, N.CDTIPOOPER,
                  CASE WHEN N.NRINSJURNF IS NULL
                       THEN N.CDCLIENTE
                       ELSE N.NRINSJURNF
                  END AS CDINSCRICAO,
                  N.CDSERIE, N.NRNOTAFISC, N.CDMODDOCFISC, N.DTEMISSAO, N.DTENTRSAID, N.NRACESSONFE, N.VRNOTAFISC,
                  N.NRSEQCAPADOC, N.DTENTRSAID AS DTCONTABILTG, T.IDORIGDESTOP, N.CDFILIRECE,
                  CASE WHEN T.IDORIGDESTOP = 'L'
                       THEN 'CR'
                       ELSE NVL(T.IDORIGDESTOP, 'C')
                  END AS IDTPINSJURNF, NRPROTOCOLONFE,
                  TO_CHAR(DTHRPROTOCOLNFE, 'DD/MM/YYYY') AS DTAUTPROTOCOLO, TO_CHAR(DTHRPROTOCOLNFE, 'HH24:MI:SS') AS HRAUTPROTOCOLO,
                  N.IDPGTOFRETE AS INCOTERM, N.IDSTATUSNFE, NVL(PF.IDCONSREFECH, 'N') IDCONSREFECH, F.IDFUNCAOFILI
             FROM NOTAFISCAL N, PARAMFILIAL PF, FILIAL F, TIPOOPERAC T
            WHERE N.DTENTRSAID BETWEEN P_DATAINI AND (CASE WHEN P_IDTELAEXP = 'S'
                                                          THEN CASE WHEN (PF.DTPROCESSA  <= P_DATAFINAL)
                                                                    THEN PF.DTPROCESSA - 1
                                                                    ELSE TO_DATE(P_DATAFINAL)
                                                               END
                                                          ELSE TO_DATE(P_DATAFINAL)
                                                     END)
              AND ((INSTR(P_CDFILIAL, '_' || N.CDFILIEMIT || '_') > 0) OR (P_TCDFILIAL = 'T'))
              AND ((INSTR(P_NUMERORE, N.NRSEQCAPADOC) > 0) OR (NVL(P_NUMERORE, 'T') = 'T'))
              AND N.CDFILIEMIT = PF.CDFILIAL
              AND N.CDFILIEMIT = F.CDFILIAL
              AND PF.CDFILIAL  = F.CDFILIAL
              AND PF.CDFILIAL = P_CDFILIAL
              AND N.IDENTRSAIDOP = T.IDENTRSAIDOP
              AND N.CDTIPOOPER   = T.CDTIPOOPER
              AND N.CDTIPOOPER NOT IN ('01','24','63','65','66','12')
              AND T.IDENTRSAIDOP = 'S'
              AND N.IDSTATUSNFE = 'C'
              AND N.CDFILIAL||N.NRLANCTONF NOT IN (SELECT CDUNICO FROM USR_SAP_NOTAFISCAL))
      LOOP
         IF ((C_NOTAFISCAL.IDCONSREFECH = 'S') AND (C_NOTAFISCAL.NRSEQCAPADOC IS NOT NULL)) OR C_NOTAFISCAL.IDCONSREFECH = 'N' THEN
            V_FILIORIG := NULL;
            V_FILIDEST := NULL;
            BEGIN
               SELECT NF.NRNOTAFISC, NVL(NF.CDSERIEENTR, NF.CDSERIE)
                 INTO V_NRLANCORIG, V_SERNFORIG
                 FROM ITEMNOTAFISC IT, NOTAFISCAL NF
                WHERE IT.CDFILIAL     = C_NOTAFISCAL.CDFILIAL
                  AND IT.NRLANCTONF   = C_NOTAFISCAL.NRLANCTONF
                  AND IT.CDFILIALIT   = NF.CDFILIAL
                  AND IT.NRLANCTONFIT = NF.NRLANCTONF;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_NRLANCORIG := NULL;
                  V_SERNFORIG  := NULL;
            END;

            IF (C_NOTAFISCAL.IDORIGDESTOP = 'L') THEN
               V_FILIORIG := C_NOTAFISCAL.CDFILIEMIT;
               V_FILIDEST := C_NOTAFISCAL.CDFILIRECE;
            END IF;

            V_FILIALTG := GET_FILIALTG(C_NOTAFISCAL.CDFILIAL, C_NOTAFISCAL.NRLANCTONF);

            INSERT INTO USR_SAP_NOTAFISCAL (CDUNICO, IDENTRSAID, CDEMPRESA, CDFILIAL, CDTIPOOPER,
                                            CDINSCRICAO, CDSERIE, NRNUMERO, CDMODELO, DTEMISSAO,
                                            DTENTRADA, NRACESSONFE, VRTOTALNF, IDINTERFACE, TPINTERFACE, DTINTERFACEORI, DTINTERFACEDEST, STATUS, APPORIGEM, APPDESTINO,
                                            DTPROC, DTCONTABILTG, NRNUMRETG,
                                            NRLANCTORIGNF, CDSERIEORIGNF, CDFILIORIG, CDFILIDEST, IDTPINSJURNF, NRPROTOCOLONFE, DTAUTPROTOCOLO, HRAUTPROTOCOLO,
                                            INCOTERM, IDSTATUSNFE, CDFILIALTG, CDOPERADOREXP, IDEXPORTFECHA)
            VALUES (C_NOTAFISCAL.CDUNICO, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.CDFILIEMIT, C_NOTAFISCAL.CDTIPOOPER,
                    C_NOTAFISCAL.CDINSCRICAO, C_NOTAFISCAL.CDSERIE, C_NOTAFISCAL.NRNOTAFISC, C_NOTAFISCAL.CDMODDOCFISC, C_NOTAFISCAL.DTEMISSAO,
                    C_NOTAFISCAL.DTENTRSAID, C_NOTAFISCAL.NRACESSONFE, C_NOTAFISCAL.VRNOTAFISC, USR_SAP_IDINTERFACE.NEXTVAL, 'I', SYSDATE, NULL, 1, 1, 2,
                    C_NOTAFISCAL.DTENTRSAID, C_NOTAFISCAL.DTCONTABILTG, C_NOTAFISCAL.NRSEQCAPADOC,
                    V_NRLANCORIG, V_SERNFORIG, V_FILIORIG, V_FILIDEST, C_NOTAFISCAL.IDTPINSJURNF, C_NOTAFISCAL.NRPROTOCOLONFE, C_NOTAFISCAL.DTAUTPROTOCOLO, C_NOTAFISCAL.HRAUTPROTOCOLO,
                    C_NOTAFISCAL.INCOTERM, C_NOTAFISCAL.IDSTATUSNFE, V_FILIALTG, V_DSREMOTEOPER, V_FECHDIA);

            FOR C_ITEMPROD IN
                (SELECT I.NRSEQITPROD, I.CDPRODUTO, I.QTITEMPROD, I.VRUNITITEM, I.VRDESCITEM, I.VRTOTAITEM,
                        I.CDALMOXARIFE, I.CDLOCALESTOQ, NVL(I.CDPRODNCMTRANS , P.CDCLASFISC) AS CDNCM, NVL(I.DSPRODEMIS, P.NMPRODUTO) AS DSPRODEMIS,
                        I.CDFISOPEITEM, I.CDSITUTRIBPR, I.NRPEDPROD, I.NRSEQPEDPROD
                   FROM ITEMPRODUTO I, PRODUTO P
                  WHERE CDFILIAL    = C_NOTAFISCAL.CDFILIAL
                    AND NRLANCTONF  = C_NOTAFISCAL.NRLANCTONF
                    AND I.CDPRODUTO = P.CDPRODUTO)
            LOOP
               V_CDARVPROD := GET_PRODUTO(C_ITEMPROD.CDPRODUTO, NVL(C_NOTAFISCAL.IDFUNCAOFILI, 'F'));

               INSERT INTO USR_SAP_ITEMPRODUTO(CDUNICO, NRSEQITEM, CDPRODUTO, VRQUANTIDADE,
                                               VRUNITARIO, VRDESCITEM, VRTOTAITEM, CDALMOXARIFADO, CDLOCALIZACAO,
                                               IDINTERFACE, TPINTERFACE, DTINTERFACEORI, DTINTERFACEDEST, STATUS, APPORIGEM, APPDESTINO,
                                               CDNCM, DSPRODEMIS, CDCFOPITPROD, CDCSTITPROD, NRPEDPROD, NRSEQPEDPROD)
               VALUES (C_NOTAFISCAL.CDUNICO, C_ITEMPROD.NRSEQITPROD, V_CDARVPROD, C_ITEMPROD.QTITEMPROD,
                       C_ITEMPROD.VRUNITITEM, C_ITEMPROD.VRDESCITEM, C_ITEMPROD.VRTOTAITEM, C_ITEMPROD.CDALMOXARIFE, C_ITEMPROD.CDLOCALESTOQ,
                       USR_SAP_IDINTERFACE.NEXTVAL, 'I', SYSDATE, NULL, 1, 1, 2,
                       C_ITEMPROD.CDNCM, C_ITEMPROD.DSPRODEMIS, C_ITEMPROD.CDFISOPEITEM, C_ITEMPROD.CDSITUTRIBPR, C_ITEMPROD.NRPEDPROD, C_ITEMPROD.NRSEQPEDPROD);
               FOR C_ITEMPROIMPOS IN
                   (SELECT CDIMPOSTO, VRPERREDBCIT, VRBASECALCIT, VRALIQITEM, VRIMPOSTOIT, CDFISCALIT, CDSITUTRIBIT, CDFISCOPERIT, 
                           NRITPROIMPOS, CDSITUTRIBITNFE, VRADICALIQFECP, VRADICFECP
                      FROM ITEMPROIMPOS
                     WHERE CDFILIAL = C_NOTAFISCAL.CDFILIAL
                       AND NRLANCTONF = C_NOTAFISCAL.NRLANCTONF
                       AND NRSEQITPROD = C_ITEMPROD.NRSEQITPROD)
               LOOP
                  INSERT INTO USR_SAP_ITEMPROIMPOS(CDUNICO, NRSEQITEM, CDIMPOSTO, VRPERCRED, VRBASECALC,
                                                   VRALIQUOTA, VRIMPOSTO, CDFISCALIT, CDSITUTRIB, CDFISCOPERIT,
                                                   IDINTERFACE, TPINTERFACE, DTINTERFACEORI, DTINTERFACEDEST, STATUS, APPORIGEM, APPDESTINO,
                                                   NRITPROIMPOS, CDSITUTRIBITNFE, VRADICALIQFECP, VRADICFECP)
                  VALUES (C_NOTAFISCAL.CDUNICO, C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, C_ITEMPROIMPOS.VRPERREDBCIT, C_ITEMPROIMPOS.VRBASECALCIT,
                          C_ITEMPROIMPOS.VRALIQITEM, C_ITEMPROIMPOS.VRIMPOSTOIT, C_ITEMPROIMPOS.CDFISCALIT, C_ITEMPROIMPOS.CDSITUTRIBIT, C_ITEMPROIMPOS.CDFISCOPERIT,
                          USR_SAP_IDINTERFACE.NEXTVAL, 'I', SYSDATE, NULL, 1, 1, 2,
                          C_ITEMPROIMPOS.NRITPROIMPOS, C_ITEMPROIMPOS.CDSITUTRIBITNFE, C_ITEMPROIMPOS.VRADICALIQFECP, C_ITEMPROIMPOS.VRADICFECP);
               END LOOP;
            END LOOP;
            FOR C_ITEMENCARGO IN
                (SELECT CDENCARGO, VRITEMENCA
                   FROM ITEMENCARGO
                  WHERE CDFILIAL = C_NOTAFISCAL.CDFILIAL
                    AND NRLANCTONF = C_NOTAFISCAL.NRLANCTONF)
            LOOP
               INSERT INTO USR_SAP_ITEMENCARGO(CDUNICO, CDENCARGO, VRENCARGO, IDINTERFACE, TPINTERFACE, DTINTERFACEORI, DTINTERFACEDEST, STATUS, APPORIGEM, APPDESTINO)
               VALUES (C_NOTAFISCAL.CDUNICO, C_ITEMENCARGO.CDENCARGO, C_ITEMENCARGO.VRITEMENCA, USR_SAP_IDINTERFACE.NEXTVAL, 'I', SYSDATE, NULL, 1, 1, 2);
            END LOOP;
            FOR C_VENCIMENTO IN
                (SELECT NRSEQVENREC, NRPARCELAREC, CDTPCTRECEB, QTDIASVENREC, DTORIGVENREC, VRORIGREC
                   FROM CONTARECEBER
                  WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL
                    AND NRLANCTONF = C_NOTAFISCAL.NRLANCTONF)
            LOOP
               INSERT INTO USR_SAP_VENCIMENTOS(CDUNICO, NRSEQVEN, NRPARCELA, CDTIPOCONTA, QTDIASVEN,
                                               DTORIGVEN, VRORIGVEN, IDINTERFACE, TPINTERFACE, DTINTERFACEORI, DTINTERFACEDEST, STATUS, APPORIGEM, APPDESTINO)
               VALUES (C_NOTAFISCAL.CDUNICO, C_VENCIMENTO.NRSEQVENREC, C_VENCIMENTO.NRPARCELAREC, C_VENCIMENTO.CDTPCTRECEB, C_VENCIMENTO.QTDIASVENREC,
                       C_VENCIMENTO.DTORIGVENREC, C_VENCIMENTO.VRORIGREC, USR_SAP_IDINTERFACE.NEXTVAL, 'I', SYSDATE, NULL, 1, 1, 2);
            END LOOP;

            FOR C_DADOSADIC IN
               (SELECT NRDADOADIC, DSDADOADIC, CDMENSAGEM
                  FROM DADOSADIC
                 WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL
                   AND NRLANCTONF = C_NOTAFISCAL.NRLANCTONF)
            LOOP
               INSERT INTO USR_SAP_DADOSADIC(CDUNICO, NRDADOADIC, DSDADOSADIC, CDMENSAGEM,
                                             IDINTERFACE, TPINTERFACE, DTINTERFACEORI, DTINTERFACEDEST, STATUS, APPORIGEM, APPDESTINO)
               VALUES (C_NOTAFISCAL.CDUNICO, C_DADOSADIC.NRDADOADIC, C_DADOSADIC.DSDADOADIC, C_DADOSADIC.CDMENSAGEM,
                       USR_SAP_IDINTERFACE.NEXTVAL, 'I', SYSDATE, NULL, 1, 1, 2);
            END LOOP;
            COMMIT;
         END IF;
      END LOOP;
   ELSE
      SELECT REGEXP_REPLACE(P_CDFILIAL, '\D')
        INTO V_FILIAL
        FROM DUAL;

      FOR C_NOTAFISCAL IN
          (SELECT N.CDFILIAL, N.NRLANCTONF, N.CDFILIAL || N.NRLANCTONF CDUNICO, N.IDENTRSAIDOP, N.CDEMPRESA,
                  N.CDFILIEMIT, N.CDTIPOOPER,
                  CASE WHEN N.NRINSJURNF IS NULL
                       THEN N.CDCLIENTE
                       ELSE N.NRINSJURNF
                  END AS CDINSCRICAO,
                  N.CDSERIE, N.NRNOTAFISC, N.CDMODDOCFISC, N.DTEMISSAO, N.DTENTRSAID, N.NRACESSONFE, N.VRNOTAFISC,
                  N.NRSEQCAPADOC, N.DTENTRSAID AS DTCONTABILTG, T.IDORIGDESTOP, N.CDFILIRECE,
                    CASE WHEN T.IDORIGDESTOP = 'L'
                         THEN 'CR'
                         ELSE NVL(T.IDORIGDESTOP, 'C')
                    END AS IDTPINSJURNF, NRPROTOCOLONFE,
                    TO_CHAR(DTHRPROTOCOLNFE, 'DD/MM/YYYY') AS DTAUTPROTOCOLO, TO_CHAR(DTHRPROTOCOLNFE, 'HH24:MI:SS') AS HRAUTPROTOCOLO,
                    N.IDPGTOFRETE AS INCOTERM, N.IDSTATUSNFE, NVL(PF.IDCONSREFECH, 'N') IDCONSREFECH, F.IDFUNCAOFILI
             FROM NOTAFISCAL N, PARAMFILIAL PF, FILIAL F, TIPOOPERAC T
            WHERE N.DTENTRSAID BETWEEN P_DATAINI AND (CASE WHEN P_IDTELAEXP = 'S'
                                                          THEN CASE WHEN (PF.DTPROCESSA  <= P_DATAFINAL)
                                                                    THEN PF.DTPROCESSA - 1
                                                                    ELSE TO_DATE(P_DATAFINAL)
                                                               END
                                                          ELSE TO_DATE(P_DATAFINAL)
                                                     END)
              AND N.CDFILIEMIT = V_FILIAL
              AND ((INSTR(P_NUMERORE, N.NRSEQCAPADOC) > 0) OR (NVL(P_NUMERORE, 'T') = 'T'))
              AND N.CDFILIEMIT = PF.CDFILIAL
              AND N.CDFILIEMIT = F.CDFILIAL
              AND PF.CDFILIAL  = F.CDFILIAL
              AND N.IDENTRSAIDOP = T.IDENTRSAIDOP
              AND N.CDTIPOOPER   = T.CDTIPOOPER
              AND N.CDMODDOCFISC IS NOT NULL
              AND N.NRSEQCAPADOC IS NOT NULL
              AND N.CDTIPOOPER NOT IN ('01','24','63','65','66','12')
              AND T.IDENTRSAIDOP = 'S'
              AND ((N.IDSTATUS IN ('A','E')) OR (N.IDSTATUSNFE IN ('A', 'D', 'C')))
              AND N.CDFILIAL||N.NRLANCTONF NOT IN (SELECT CDUNICO FROM USR_SAP_NOTAFISCAL)
              AND EXISTS (SELECT I.NRLANCTONF
                            FROM ITEMPRODUTO I
                           WHERE I.CDFILIAL = N.CDFILIAL
                             AND I.NRLANCTONF = N.NRLANCTONF))
      LOOP
         IF ((C_NOTAFISCAL.IDCONSREFECH = 'S') AND (C_NOTAFISCAL.NRSEQCAPADOC IS NOT NULL)) OR C_NOTAFISCAL.IDCONSREFECH = 'N' THEN
            V_FILIORIG := NULL;
            V_FILIDEST := NULL;
            BEGIN
               SELECT NF.NRNOTAFISC, NVL(NF.CDSERIEENTR, NF.CDSERIE)
                 INTO V_NRLANCORIG, V_SERNFORIG
                 FROM ITEMNOTAFISC IT, NOTAFISCAL NF
                WHERE IT.CDFILIAL     = C_NOTAFISCAL.CDFILIAL
                  AND IT.NRLANCTONF   = C_NOTAFISCAL.NRLANCTONF
                  AND IT.CDFILIALIT   = NF.CDFILIAL
                  AND IT.NRLANCTONFIT = NF.NRLANCTONF;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_NRLANCORIG := NULL;
                  V_SERNFORIG  := NULL;
            END;
               
            IF (C_NOTAFISCAL.IDORIGDESTOP = 'L') THEN
               V_FILIORIG := C_NOTAFISCAL.CDFILIEMIT;
               V_FILIDEST := C_NOTAFISCAL.CDFILIRECE;
            END IF;
            
            V_FILIALTG := GET_FILIALTG(C_NOTAFISCAL.CDFILIAL, C_NOTAFISCAL.NRLANCTONF);
            
            INSERT INTO USR_SAP_NOTAFISCAL (CDUNICO, IDENTRSAID, CDEMPRESA, CDFILIAL, CDTIPOOPER,
                                            CDINSCRICAO, CDSERIE, NRNUMERO, CDMODELO, DTEMISSAO, 
                                            DTENTRADA, NRACESSONFE, VRTOTALNF, IDINTERFACE, TPINTERFACE, DTINTERFACEORI, DTINTERFACEDEST, STATUS, APPORIGEM, APPDESTINO, 
                                            DTPROC, DTCONTABILTG, NRNUMRETG,
                                            NRLANCTORIGNF, CDSERIEORIGNF, CDFILIORIG, CDFILIDEST, IDTPINSJURNF, NRPROTOCOLONFE, DTAUTPROTOCOLO, HRAUTPROTOCOLO,
                                            INCOTERM, IDSTATUSNFE, CDFILIALTG, CDOPERADOREXP, IDEXPORTFECHA)
            VALUES (C_NOTAFISCAL.CDUNICO, C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.CDEMPRESA, C_NOTAFISCAL.CDFILIEMIT, C_NOTAFISCAL.CDTIPOOPER,
                    C_NOTAFISCAL.CDINSCRICAO, C_NOTAFISCAL.CDSERIE, C_NOTAFISCAL.NRNOTAFISC, C_NOTAFISCAL.CDMODDOCFISC, C_NOTAFISCAL.DTEMISSAO,
                    C_NOTAFISCAL.DTENTRSAID, C_NOTAFISCAL.NRACESSONFE, C_NOTAFISCAL.VRNOTAFISC, USR_SAP_IDINTERFACE.NEXTVAL, 'I', SYSDATE, NULL, 1, 1, 2,
                    C_NOTAFISCAL.DTENTRSAID, C_NOTAFISCAL.DTCONTABILTG, C_NOTAFISCAL.NRSEQCAPADOC,
                    V_NRLANCORIG, V_SERNFORIG, V_FILIORIG, V_FILIDEST, C_NOTAFISCAL.IDTPINSJURNF, C_NOTAFISCAL.NRPROTOCOLONFE, C_NOTAFISCAL.DTAUTPROTOCOLO, C_NOTAFISCAL.HRAUTPROTOCOLO,
                    C_NOTAFISCAL.INCOTERM, C_NOTAFISCAL.IDSTATUSNFE, V_FILIALTG, V_DSREMOTEOPER, V_FECHDIA);
                    
            FOR C_ITEMPROD IN
                (SELECT I.NRSEQITPROD, P.CDPRODUTO, I.QTITEMPROD, I.VRUNITITEM, I.VRDESCITEM, I.VRTOTAITEM,
                        I.CDALMOXARIFE, I.CDLOCALESTOQ, NVL(I.CDPRODNCMTRANS , P.CDCLASFISC) AS CDNCM, NVL(I.DSPRODEMIS, P.NMPRODUTO) AS DSPRODEMIS,
                        I.CDFISOPEITEM, I.CDSITUTRIBPR, I.NRPEDPROD, I.NRSEQPEDPROD
                   FROM ITEMPRODUTO I, PRODUTO P
                  WHERE CDFILIAL    = C_NOTAFISCAL.CDFILIAL
                    AND NRLANCTONF  = C_NOTAFISCAL.NRLANCTONF
                    AND I.CDPRODUTO = P.CDPRODUTO)
            LOOP
               V_CDARVPROD := GET_PRODUTO(C_ITEMPROD.CDPRODUTO, NVL(C_NOTAFISCAL.IDFUNCAOFILI, 'F'));
               
               INSERT INTO USR_SAP_ITEMPRODUTO(CDUNICO, NRSEQITEM, CDPRODUTO, VRQUANTIDADE, 
                                               VRUNITARIO, VRDESCITEM, VRTOTAITEM, CDALMOXARIFADO, CDLOCALIZACAO, 
                                               IDINTERFACE, TPINTERFACE, DTINTERFACEORI, DTINTERFACEDEST, STATUS, APPORIGEM, APPDESTINO,
                                               CDNCM, DSPRODEMIS, CDCFOPITPROD, CDCSTITPROD, NRPEDPROD, NRSEQPEDPROD)
               VALUES (C_NOTAFISCAL.CDUNICO, C_ITEMPROD.NRSEQITPROD, V_CDARVPROD, C_ITEMPROD.QTITEMPROD, 
                       C_ITEMPROD.VRUNITITEM, C_ITEMPROD.VRDESCITEM, C_ITEMPROD.VRTOTAITEM, C_ITEMPROD.CDALMOXARIFE, C_ITEMPROD.CDLOCALESTOQ, 
                       USR_SAP_IDINTERFACE.NEXTVAL, 'I', SYSDATE, NULL, 1, 1, 2,
                       C_ITEMPROD.CDNCM, C_ITEMPROD.DSPRODEMIS, C_ITEMPROD.CDFISOPEITEM, C_ITEMPROD.CDSITUTRIBPR, C_ITEMPROD.NRPEDPROD, C_ITEMPROD.NRSEQPEDPROD);
               FOR C_ITEMPROIMPOS IN
                   (SELECT CDIMPOSTO, VRPERREDBCIT, VRBASECALCIT, VRALIQITEM, VRIMPOSTOIT, CDFISCALIT, CDSITUTRIBIT, CDFISCOPERIT, 
                           NRITPROIMPOS, CDSITUTRIBITNFE, VRADICALIQFECP, VRADICFECP
                      FROM ITEMPROIMPOS
                     WHERE CDFILIAL = C_NOTAFISCAL.CDFILIAL
                       AND NRLANCTONF = C_NOTAFISCAL.NRLANCTONF
                       AND NRSEQITPROD = C_ITEMPROD.NRSEQITPROD)
               LOOP
                  INSERT INTO USR_SAP_ITEMPROIMPOS(CDUNICO, NRSEQITEM, CDIMPOSTO, VRPERCRED, VRBASECALC,
                                                   VRALIQUOTA, VRIMPOSTO, CDFISCALIT, CDSITUTRIB, CDFISCOPERIT, 
                                                   IDINTERFACE, TPINTERFACE, DTINTERFACEORI, DTINTERFACEDEST, STATUS, APPORIGEM, APPDESTINO,
                                                   NRITPROIMPOS, CDSITUTRIBITNFE, VRADICALIQFECP, VRADICFECP)
                  VALUES (C_NOTAFISCAL.CDUNICO, C_ITEMPROD.NRSEQITPROD, C_ITEMPROIMPOS.CDIMPOSTO, C_ITEMPROIMPOS.VRPERREDBCIT, C_ITEMPROIMPOS.VRBASECALCIT,
                          C_ITEMPROIMPOS.VRALIQITEM, C_ITEMPROIMPOS.VRIMPOSTOIT, C_ITEMPROIMPOS.CDFISCALIT, C_ITEMPROIMPOS.CDSITUTRIBIT, C_ITEMPROIMPOS.CDFISCOPERIT,
                          USR_SAP_IDINTERFACE.NEXTVAL, 'I', SYSDATE, NULL, 1, 1, 2, 
                          C_ITEMPROIMPOS.NRITPROIMPOS, C_ITEMPROIMPOS.CDSITUTRIBITNFE, C_ITEMPROIMPOS.VRADICALIQFECP, C_ITEMPROIMPOS.VRADICFECP);
               END LOOP;
            END LOOP;
            FOR C_ITEMENCARGO IN
                (SELECT CDENCARGO, VRITEMENCA
                   FROM ITEMENCARGO
                  WHERE CDFILIAL = C_NOTAFISCAL.CDFILIAL
                    AND NRLANCTONF = C_NOTAFISCAL.NRLANCTONF)
            LOOP
               INSERT INTO USR_SAP_ITEMENCARGO(CDUNICO, CDENCARGO, VRENCARGO, IDINTERFACE, TPINTERFACE, DTINTERFACEORI, DTINTERFACEDEST, STATUS, APPORIGEM, APPDESTINO)
               VALUES (C_NOTAFISCAL.CDUNICO, C_ITEMENCARGO.CDENCARGO, C_ITEMENCARGO.VRITEMENCA, USR_SAP_IDINTERFACE.NEXTVAL, 'I', SYSDATE, NULL, 1, 1, 2);
            END LOOP;
            FOR C_VENCIMENTO IN
                (SELECT NRSEQVENREC, NRPARCELAREC, CDTPCTRECEB, QTDIASVENREC, DTORIGVENREC, VRORIGREC
                   FROM CONTARECEBER
                  WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL
                    AND NRLANCTONF = C_NOTAFISCAL.NRLANCTONF)
            LOOP
               INSERT INTO USR_SAP_VENCIMENTOS(CDUNICO, NRSEQVEN, NRPARCELA, CDTIPOCONTA, QTDIASVEN,
                                               DTORIGVEN, VRORIGVEN, IDINTERFACE, TPINTERFACE, DTINTERFACEORI, DTINTERFACEDEST, STATUS, APPORIGEM, APPDESTINO)
               VALUES (C_NOTAFISCAL.CDUNICO, C_VENCIMENTO.NRSEQVENREC, C_VENCIMENTO.NRPARCELAREC, C_VENCIMENTO.CDTPCTRECEB, C_VENCIMENTO.QTDIASVENREC,
                       C_VENCIMENTO.DTORIGVENREC, C_VENCIMENTO.VRORIGREC, USR_SAP_IDINTERFACE.NEXTVAL, 'I', SYSDATE, NULL, 1, 1, 2);
            END LOOP;

            FOR C_DADOSADIC IN
               (SELECT NRDADOADIC, DSDADOADIC, CDMENSAGEM
                  FROM DADOSADIC
                 WHERE CDFILIAL   = C_NOTAFISCAL.CDFILIAL
                   AND NRLANCTONF = C_NOTAFISCAL.NRLANCTONF)
            LOOP
               INSERT INTO USR_SAP_DADOSADIC(CDUNICO, NRDADOADIC, DSDADOSADIC, CDMENSAGEM,
                                             IDINTERFACE, TPINTERFACE, DTINTERFACEORI, DTINTERFACEDEST, STATUS, APPORIGEM, APPDESTINO)
               VALUES (C_NOTAFISCAL.CDUNICO, C_DADOSADIC.NRDADOADIC, C_DADOSADIC.DSDADOADIC, C_DADOSADIC.CDMENSAGEM,
                       USR_SAP_IDINTERFACE.NEXTVAL, 'I', SYSDATE, NULL, 1, 1, 2);
            END LOOP;
            COMMIT;
         END IF;
      END LOOP;
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('EXPORTA_NF_SAIDA_SAP','6.28.XXX'); END;
/

