DECLARE
   V_PAISORIG     VARCHAR2(04);
   V_ESTAORIG     VARCHAR2(02);
   V_MUNIORIG     VARCHAR2(04);
   V_PAISDEST     VARCHAR2(04);
   V_ESTADEST     VARCHAR2(02);
   V_CDORIGDESTNF VARCHAR2(21);
   V_CFOPTEMP     VARCHAR2(14);
   V_IDENDCFOP    VARCHAR2(01);
   V_IDENDEMIS    VARCHAR2(01);
   V_PAISCFOP     VARCHAR2(04);
   V_ESTACFOP     VARCHAR2(02);
   V_CDFILIFATUR  VARCHAR2(04);
   V_ORIGDESTOP   VARCHAR2(01);
   V_CDGRUPOFISC  VARCHAR2(03);
   V_FISOPEITEM   VARCHAR2(11);
   V_SUFXCFOPPR   VARCHAR2(03);
   V_VRIMPOSCONT  NUMBER;
   V_SEQUIMPOSTNF NUMBER;
BEGIN
   FOR C_NOTAFISCAL IN (SELECT N.CDEMPRESA, N.CDFILIAL, N.NRLANCTONF, N.CDINSCESTA, N.IDORIGDESTNF,
                               N.CDFILIEMIT, N.CDFILIRECE, N.CDFORNECED, N.CDCLIENTE, N.IDENTRSAIDOP,
                               N.CDTIPOOPER, N.IDSISTEMA, N.DTENTRSAID, N.DTEMISSAO, M.NRSEQUMAPAPU
                        FROM NOTAFISCAL N, IMPOSTONF I, MAPAAPURAC M
                        WHERE M.CDEMPRESA = '01'                  AND
                              M.CDIMPOSTO = '00'                  AND
                              M.DTINIPERIODO BETWEEN '01/01/2004' AND '31/01/2004' AND
                              M.NRSEQUMAPAPU = I.NRSEQUMAPAPU     AND
                              N.CDFILIAL    = I.CDFILIAL          AND
                              N.NRLANCTONF  = I.NRLANCTONF      ) LOOP

      V_CFOPTEMP := C_NOTAFISCAL.CDFILIAL||C_NOTAFISCAL.NRLANCTONF;

      IF C_NOTAFISCAL.IDENTRSAIDOP = 'E' THEN
         SELECT MAX(CDPAIS), MAX(SGESTADO)
         INTO V_PAISDEST, V_ESTADEST
         FROM INSCRESTAD
         WHERE (CDEMPRESA  = C_NOTAFISCAL.CDEMPRESA) AND
               (CDINSCESTA = C_NOTAFISCAL.CDINSCESTA);

        SELECT MAX(IDORIGDESTOP)
        INTO V_ORIGDESTOP
        FROM TIPOOPERAC
        WHERE IDENTRSAIDOP = 'E' AND
              CDTIPOOPER   = C_NOTAFISCAL.CDTIPOOPER;

         IF V_ORIGDESTOP = 'L' THEN
            SELECT MAX(CDPAIS), MAX(SGESTADO), MAX(CDMUNICIPIO)
            INTO V_PAISORIG, V_ESTAORIG, V_MUNIORIG
            FROM ENDEFILI
            WHERE (CDFILIAL     = C_NOTAFISCAL.CDFILIEMIT) AND
                  (IDTPENDEFILI = 'P');
            V_CDORIGDESTNF := C_NOTAFISCAL.CDFILIEMIT;
         ELSIF V_ORIGDESTOP = 'C' THEN
            SELECT MAX(CDPAIS), MAX(SGESTADO), MAX(CDMUNICIPIO)
            INTO V_PAISORIG, V_ESTAORIG, V_MUNIORIG
            FROM ENDECLIE
            WHERE (CDCLIENTE    = C_NOTAFISCAL.CDCLIENTE) AND
                  (IDTPENDECLIE = 'P');
            V_CDORIGDESTNF := C_NOTAFISCAL.CDCLIENTE;
         ELSIF V_ORIGDESTOP = 'F' THEN
            SELECT MAX(CDPAIS), MAX(SGESTADO), MAX(CDMUNICIPIO)
            INTO V_PAISORIG, V_ESTAORIG, V_MUNIORIG
            FROM ENDEFORN
            WHERE (CDFORNECED   = C_NOTAFISCAL.CDFORNECED) AND
                  (IDTPENDEFORN = 'P');
            V_CDORIGDESTNF := C_NOTAFISCAL.CDFORNECED;
         END IF;
         
         CONSULTA_GRUPOFISCAL(C_NOTAFISCAL.IDORIGDESTNF, V_CDORIGDESTNF, '', 'E', C_NOTAFISCAL.CDTIPOOPER, V_CDGRUPOFISC);
      ELSE
         SELECT MAX(NVL(IDENDCFOPFIL,'I'))
         INTO V_IDENDCFOP
         FROM FILIAL
         WHERE (CDFILIAL = C_NOTAFISCAL.CDFILIEMIT);

         IF V_IDENDCFOP = 'I' THEN
            SELECT MAX(CDPAIS), MAX(SGESTADO), MAX(CDMUNICIPIO)
            INTO   V_PAISORIG, V_ESTAORIG, V_MUNIORIG
            FROM INSCRESTAD
            WHERE CDEMPRESA  = C_NOTAFISCAL.CDEMPRESA AND
                  CDINSCESTA = C_NOTAFISCAL.CDINSCESTA;
         ELSE
            SELECT MAX(CDPAIS), MAX(SGESTADO), MAX(CDMUNICIPIO)
            INTO   V_PAISORIG, V_ESTAORIG, V_MUNIORIG
            FROM ENDEFILI
            WHERE (CDFILIAL     = C_NOTAFISCAL.CDFILIEMIT) AND
                  (IDTPENDEFILI = V_IDENDCFOP);
         END IF;

         IF C_NOTAFISCAL.IDORIGDESTNF IS NULL THEN
            SELECT MAX(IDORIGDESTOP)
            INTO V_ORIGDESTOP
            FROM TIPOOPERAC
            WHERE IDENTRSAIDOP = 'S' AND
                  CDTIPOOPER   = C_NOTAFISCAL.CDTIPOOPER;
         ELSE
            V_ORIGDESTOP := C_NOTAFISCAL.IDORIGDESTNF;
         END IF;

         IF V_ORIGDESTOP = 'L' THEN
            SELECT MAX(NVL(IDENDCFOPFIL,'I')),
                   MAX(NVL(IDENDEMISFIL,'I'))
            INTO V_IDENDCFOP, V_IDENDEMIS
            FROM FILIAL
            WHERE (CDFILIAL = C_NOTAFISCAL.CDFILIRECE);

            IF V_IDENDCFOP = 'I' THEN
               SELECT MAX(CDPAIS), MAX(SGESTADO)
               INTO V_PAISCFOP, V_ESTACFOP
               FROM INSCRESTAD
               WHERE CDEMPRESA  = C_NOTAFISCAL.CDEMPRESA AND
                     CDINSCESTA = C_NOTAFISCAL.CDINSCESTA;
            ELSE
               SELECT MAX(CDPAIS), MAX(SGESTADO)
               INTO V_PAISCFOP, V_ESTACFOP
               FROM ENDEFILI
               WHERE (CDFILIAL     = C_NOTAFISCAL.CDFILIRECE) AND
                     (IDTPENDEFILI = V_IDENDCFOP);
            END IF;

            IF V_IDENDEMIS = 'I' THEN
               SELECT MAX(CDPAIS), MAX(SGESTADO)
               INTO V_PAISDEST, V_ESTADEST
               FROM INSCRESTAD
               WHERE CDEMPRESA  = C_NOTAFISCAL.CDEMPRESA AND
                     CDINSCESTA = C_NOTAFISCAL.CDINSCESTA;
            ELSE
               SELECT MAX(CDPAIS), MAX(SGESTADO)
               INTO V_PAISDEST, V_ESTADEST
               FROM ENDEFILI
               WHERE CDFILIAL     = C_NOTAFISCAL.CDFILIRECE AND
                     IDTPENDEFILI = V_IDENDEMIS;
            END IF;
            V_CDORIGDESTNF := C_NOTAFISCAL.CDFILIRECE;

         ELSIF V_ORIGDESTOP = 'F' THEN

            SELECT NVL(MAX(IDENDCFOPFOR),'P'), NVL(MAX(IDENDEMISFOR),'P')
            INTO V_IDENDCFOP, V_IDENDEMIS
            FROM FORNECEDOR
            WHERE (CDFORNECED = C_NOTAFISCAL.CDFORNECED);

            SELECT MAX(CDPAIS), MAX(SGESTADO)
            INTO V_PAISCFOP, V_ESTACFOP
            FROM ENDEFORN
            WHERE (CDFORNECED   = C_NOTAFISCAL.CDFORNECED) AND
                  (IDTPENDEFORN = V_IDENDCFOP);

            SELECT MAX(CDPAIS), MAX(SGESTADO)
            INTO V_PAISDEST, V_ESTADEST
            FROM ENDEFORN
            WHERE (CDFORNECED   = C_NOTAFISCAL.CDFORNECED) AND
                  (IDTPENDEFORN = V_IDENDEMIS);

            V_CDORIGDESTNF := C_NOTAFISCAL.CDFORNECED;

         ELSE

            SELECT NVL(MAX(IDENDCFOPCLI),'P'), NVL(MAX(IDENDEMISCLI),'P')
            INTO V_IDENDCFOP, V_IDENDEMIS
            FROM CLIENTE
            WHERE (CDCLIENTE = C_NOTAFISCAL.CDCLIENTE);

            SELECT MAX(CDPAIS), MAX(SGESTADO)
            INTO V_PAISCFOP, V_ESTACFOP
            FROM ENDECLIE
            WHERE (CDCLIENTE    = C_NOTAFISCAL.CDCLIENTE) AND
                  (IDTPENDECLIE = V_IDENDCFOP);

            SELECT MAX(CDPAIS), MAX(SGESTADO)
            INTO V_PAISDEST, V_ESTADEST
            FROM ENDECLIE
            WHERE (CDCLIENTE    = C_NOTAFISCAL.CDCLIENTE) AND
                  (IDTPENDECLIE = V_IDENDEMIS);

            V_CDORIGDESTNF := C_NOTAFISCAL.CDCLIENTE;
         END IF;

         IF (C_NOTAFISCAL.IDSISTEMA IN ('FAI', 'FAC', 'FAS', 'FAH')) THEN
            V_CDFILIFATUR := C_NOTAFISCAL.CDFILIEMIT;
         ELSE
            V_CDFILIFATUR := '';
         END IF;

         IF C_NOTAFISCAL.IDORIGDESTNF = 'U' THEN
            CONSULTA_GRUPOFISCAL('C', V_CDORIGDESTNF, V_CDFILIFATUR, 'S', C_NOTAFISCAL.CDTIPOOPER, V_CDGRUPOFISC);
         ELSE
            CONSULTA_GRUPOFISCAL(C_NOTAFISCAL.IDORIGDESTNF, V_CDORIGDESTNF, V_CDFILIFATUR, 'S', C_NOTAFISCAL.CDTIPOOPER, V_CDGRUPOFISC);
         END IF;
      END IF;

      FOR C_ITEMPRODUTO IN (SELECT VRTOTAITEM, VRDESCITEM, I.CDPRODUTO, NRSEQITPROD, P.CDARVPROD
                            FROM ITEMPRODUTO I, PRODUTO P
                            WHERE CDFILIAL    = C_NOTAFISCAL.CDFILIAL   AND
                                  NRLANCTONF  = C_NOTAFISCAL.NRLANCTONF AND
                                  I.CDPRODUTO = P.CDPRODUTO) LOOP

         IF C_NOTAFISCAL.IDENTRSAIDOP = 'E' THEN
            CONSULTA_CFOP(C_NOTAFISCAL.IDENTRSAIDOP, C_NOTAFISCAL.CDEMPRESA, C_ITEMPRODUTO.CDARVPROD, C_NOTAFISCAL.CDINSCESTA, C_NOTAFISCAL.CDTIPOOPER,
                          V_PAISORIG, V_ESTAORIG, V_PAISDEST, V_ESTADEST, C_NOTAFISCAL.CDFILIAL, C_NOTAFISCAL.NRLANCTONF, V_CDGRUPOFISC, C_NOTAFISCAL.DTENTRSAID);
         ELSE
            CONSULTA_CFOP('S', C_NOTAFISCAL.CDEMPRESA, C_ITEMPRODUTO.CDARVPROD, C_NOTAFISCAL.CDINSCESTA, C_NOTAFISCAL.CDTIPOOPER,
                          V_PAISORIG, V_ESTAORIG, V_PAISCFOP, V_ESTACFOP, C_NOTAFISCAL.CDFILIAL, C_NOTAFISCAL.NRLANCTONF, V_CDGRUPOFISC, C_NOTAFISCAL.DTEMISSAO);

         END IF;

         SELECT MAX(CDFISOPETMP), MAX(CDSUFCFOPTMP)
         INTO V_FISOPEITEM, V_SUFXCFOPPR
         FROM CFOPTEMP
         WHERE NRCFOPTEMP = V_CFOPTEMP;

         UPDATE ITEMPRODUTO
         SET CDFISOPEITEM = V_FISOPEITEM,
             CDSUFXCFOPPR = V_SUFXCFOPPR
         WHERE CDFILIAL    = C_NOTAFISCAL.CDFILIAL   AND
               NRLANCTONF  = C_NOTAFISCAL.NRLANCTONF AND
               NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD;

         UPDATE ITEMPROIMPOS
         SET CDFISCOPERIT = V_FISOPEITEM,
             CDSUFXCFOPIT = V_SUFXCFOPPR
         WHERE CDFILIAL    = C_NOTAFISCAL.CDFILIAL   AND
               NRLANCTONF  = C_NOTAFISCAL.NRLANCTONF AND
               NRSEQITPROD = C_ITEMPRODUTO.NRSEQITPROD;

         DELETE CFOPTEMP
         WHERE NRCFOPTEMP = V_CFOPTEMP;
      END LOOP;

      DELETE IMPOSTONF
      WHERE CDFILIAL    = C_NOTAFISCAL.CDFILIAL   AND
            NRLANCTONF  = C_NOTAFISCAL.NRLANCTONF AND
            CDIMPOSTO   = '00';

      V_SEQUIMPOSTNF := '01';

      FOR C_IMPOSTONF IN (
         SELECT IT.CDIMPOSTO, VRALIQITEM, VRPERREDBCIT, VRPARDEDIT, CDFISCALIT,
                CDSITUTRIBIT, CDFISCOPERIT, CDSUFXCFOPIT, NRITPROIMPOS, SUM(VRCONTITIMP) AS VRCONTIMPOIT,
                SUM(VRBASECALCIT) AS VRBCALCIMPOIT, SUM(VRIMPOSTOIT) AS VRIMPOSIT, I.IDTRATIMPO,
                SUM(VRIMPOSTOIT) AS VRIMPOSCONT
         FROM ITEMPROIMPOS IT, IMPOSTO I
         WHERE (CDFILIAL     = C_NOTAFISCAL.CDFILIAL)    AND
               (NRLANCTONF   = C_NOTAFISCAL.NRLANCTONF)  AND
               (IT.CDIMPOSTO = I.CDIMPOSTO)              AND
               (I.CDIMPOSTO = '00')                      AND
               ((((CDFISCALIT = '1')) AND (IDIMPORECUP = 'S')) OR
                ((IDIMPORECUP <> 'S') AND (VRIMPOSTOIT <> 0)) OR (NRITPROIMPOS = '3'))
         GROUP BY IT.CDIMPOSTO, VRALIQITEM, VRPERREDBCIT, VRPARDEDIT, CDFISCALIT,
                  CDSITUTRIBIT, CDFISCOPERIT, CDSUFXCFOPIT, NRITPROIMPOS, I.IDTRATIMPO
         UNION
         SELECT IT.CDIMPOSTO, 0 AS VRALIQITEM, 0 AS VRPERREDBCIT, 0 AS VRPARDEDIT, CDFISCALIT,
                CDSITUTRIBIT, CDFISCOPERIT, CDSUFXCFOPIT, NRITPROIMPOS, SUM(VRCONTITIMP) AS VRCONTIMPOIT,
                0 AS VRBCALCIMPOIT, 0 AS VRIMPOSIT, I.IDTRATIMPO,
                SUM(VRIMPOSTOIT) AS VRIMPOSCONT
         FROM ITEMPROIMPOS IT, IMPOSTO I
         WHERE (CDFILIAL      = C_NOTAFISCAL.CDFILIAL)    AND
               (NRLANCTONF    = C_NOTAFISCAL.NRLANCTONF)  AND
               (IT.CDIMPOSTO  = I.CDIMPOSTO)              AND
               (I.CDIMPOSTO   = '00')                     AND
               (NRITPROIMPOS   <> '3')                    AND
               ((((CDFISCALIT <> '1')) AND (IDIMPORECUP = 'S')) OR
                ((IDIMPORECUP  <> 'S') AND (VRIMPOSTOIT = 0)))
         GROUP BY IT.CDIMPOSTO, VRALIQITEM, VRPERREDBCIT, VRPARDEDIT, CDFISCALIT,
                  CDSITUTRIBIT, CDFISCOPERIT, CDSUFXCFOPIT, NRITPROIMPOS, I.IDTRATIMPO) LOOP

         V_VRIMPOSCONT := 0;
         IF C_IMPOSTONF.IDTRATIMPO = 'A' THEN
            V_VRIMPOSCONT := C_IMPOSTONF.VRIMPOSCONT;
         ELSIF C_IMPOSTONF.IDTRATIMPO = 'D' THEN
            V_VRIMPOSCONT := - C_IMPOSTONF.VRIMPOSCONT;
         END IF;

         INSERT INTO IMPOSTONF(
            CDFILIAL, NRLANCTONF, CDIMPOSTO, NRIMPOSNF, VRCONTABILNF, VRPERREDBCNF, VRBASECALCNF,
            VRALIQNF, VRPARDEDNF, VRIMPOSTONF, CDFISCALNF, CDSITUTRIBNF, CDFISCOPERNF, CDSUFXCFOPNF,
            IDIMPOSTONF, NRSEQUMAPAPU)
         VALUES(
            C_NOTAFISCAL.CDFILIAL, C_NOTAFISCAL.NRLANCTONF, C_IMPOSTONF.CDIMPOSTO, V_SEQUIMPOSTNF,
            C_IMPOSTONF.VRCONTIMPOIT + V_VRIMPOSCONT, C_IMPOSTONF.VRPERREDBCIT, C_IMPOSTONF.VRBCALCIMPOIT,
            C_IMPOSTONF.VRALIQITEM, C_IMPOSTONF.VRPARDEDIT, C_IMPOSTONF.VRIMPOSIT, C_IMPOSTONF.CDFISCALIT,
            C_IMPOSTONF.CDSITUTRIBIT, C_IMPOSTONF.CDFISCOPERIT, C_IMPOSTONF.CDSUFXCFOPIT, C_IMPOSTONF.NRITPROIMPOS,
            C_NOTAFISCAL.NRSEQUMAPAPU);

         V_SEQUIMPOSTNF := LPAD(RTRIM(TO_CHAR(TO_NUMBER(V_SEQUIMPOSTNF) + 1)),2,0);
      END LOOP;
   END LOOP;
END;
/