DROP FUNCTION CONSULTA_REGRA_FISCAL
/
DROP TYPE REGRAFISCAL_TABLE
/
DROP TYPE REGRAFISCAL_TYPE
/

CREATE TYPE REGRAFISCAL_TYPE AS
OBJECT (CDREGRAFISC      VARCHAR2(10),
        NMREGRAFISC      VARCHAR2(50),
        NRSEQDADOSFISC   VARCHAR2(10),
        CDCLASFISC       VARCHAR2(10),
        CDPRODUTO        VARCHAR2(10),
        DSMENSREGRAFISC  VARCHAR2(150),
        CDIMPOSTO        VARCHAR2(2),
        CDFISCAL         VARCHAR2(1),
        CDSITUTRIB       VARCHAR2(2),
        VRALIQUOTA       FLOAT(126),
        IDMODBCNFE       VARCHAR2(1),
        IDMODBCSTNFE     VARCHAR2(1),
        IDMOTIDESSTNFE   VARCHAR2(2),
        CDTPCRPICOSTNFE  VARCHAR2(3),
        CDNATUBCPCSTNFE  VARCHAR2(2),
        VRINIFAIXA       FLOAT(126),
        VRFIMFAIXA       FLOAT(126),
        VRPARDEDUZ       FLOAT(126),
        VR1PERREDBC      FLOAT(126),
        VRPERREDBC       FLOAT(126),
        VRPBCSUBTRIB     FLOAT(126),
        IDTRATIMPST      VARCHAR2(1),
        IDPMCBASECA      VARCHAR2(1),
        IDCSTINTEREST    VARCHAR2(1),
        IDCSTINTERNA     VARCHAR2(1),
        CDFISCOPER       VARCHAR2(10),
        IDCFOPINTEREST   VARCHAR2(1),
        IDDIFEALIQ       VARCHAR2(1),
        IDDIFALIQCUSPR   VARCHAR2(1),
        IDTIPODESTFISC   VARCHAR2(1),
        CDDETALHEFISC    VARCHAR2(5),
        IDTIPOREGISTRO   VARCHAR2(4),
        IDMENSAGEM       VARCHAR2(1),
        CDENQLEGALIPI    VARCHAR2(3),
        CDCEST           VARCHAR2(9),
        VRADICALIQFECP   FLOAT(126),
        IDIMPODESONERA   VARCHAR2(1),
        IDCONSDESONVRNF  VARCHAR2(1),
        CDBENEFFISCAL    VARCHAR2(10),
        IDCONSMVAPMC     VARCHAR2(1),
        IDCONSCOMPBCPMC  VARCHAR2(1),
        IDCONSDIFALIQ    VARCHAR2(1),
        CDCOMPST         VARCHAR2(20),
        CDRESTST         VARCHAR2(20),
        CDNATUREC        VARCHAR2(10),
        VRIMPOSTOREP     FLOAT(126),
        IDNUTREDBCFECP   VARCHAR2(1));
/

CREATE TYPE REGRAFISCAL_TABLE AS
TABLE OF REGRAFISCAL_TYPE
/

CREATE OR REPLACE FUNCTION CONSULTA_REGRA_FISCAL
(P_CDIMPOSTO   IN IMPOSTO.CDIMPOSTO%TYPE,
 P_EMPRESA     IN INCIDENCIA.CDEMPRESA%TYPE,
 P_CDGRUPOFISC IN INCIDENCIA.CDGRUPOFISC%TYPE,
 P_ENTRSAIDIN  IN INCIDENCIA.IDENTRSAIDIN%TYPE,
 P_TIPOOPER    IN INCIDENCIA.CDTIPOOPER%TYPE,
 P_INSCESTA    IN INCIDENCIA.CDINSCESTA%TYPE,
 P_CDARVPROD   IN PRODUTO.CDARVPROD%TYPE,
 P_DTENTRSAID  IN NOTAFISCAL.DTENTRSAID%TYPE,
 P_PAISORIG    IN PAIS.CDPAIS%TYPE,
 P_ESTAORIG    IN ESTADO.SGESTADO%TYPE,
 P_MUNIORIG    IN MUNICIPIO.CDMUNICIPIO%TYPE,
 P_PAISDEST    IN PAIS.CDPAIS%TYPE,
 P_ESTADEST    IN ESTADO.SGESTADO%TYPE,
 P_MUNIDEST    IN MUNICIPIO.CDMUNICIPIO%TYPE,
 P_BASECALC    IN ALIQUOTA.VRFIMFAIXA%TYPE,
 P_CFOP        IN INCIDENCIA.IDENTRSAIDIN%TYPE,
 P_IDORIGEMPROD IN VARCHAR2 DEFAULT 'N',
 P_NOTAFISCAL   IN VARCHAR2 DEFAULT NULL,
 P_CDORIGDESTNF IN VARCHAR2 DEFAULT NULL) RETURN REGRAFISCAL_TABLE PIPELINED
IS

V_CDREGRAFISC         REGRAFISCAL.CDREGRAFISC%TYPE;
V_NMREGRAFISC         REGRAFISCAL.NMREGRAFISC%TYPE;
V_DSMENSREGRAFISC     REGRAFISCAL.DSMENSREGRAFISC%TYPE;

V_CDFISCOPER          VARCHAR2(10);
V_IDCFOPINTEREST      VARCHAR2(1);
V_IDDIFEALIQ          VARCHAR2(1);
V_IDDIFALIQCUSPR      VARCHAR2(1);

V_UTFAIXVLR           IMPOSTO.IDUTFAIXVLR%TYPE;
V_UTMUNIORIG          IMPOSTO.IDUTMUNIORIG%TYPE;
V_UTUFORIG            IMPOSTO.IDUTUFORIG%TYPE;
V_UTUFDEST            IMPOSTO.IDUTUFDEST%TYPE;

V_CDFISCAL            VARCHAR2(1);
V_CDSITUTRIB          VARCHAR2(2);
V_CDSITUTRIBST        VARCHAR2(2);
V_VRALIQUOTA          FLOAT(126);
V_IDMODBCNFE          VARCHAR2(1);
V_IDMODBCSTNFE        VARCHAR2(1);
V_IDMOTIDESSTNFE      VARCHAR2(2);
V_CDTPCRPICOSTNFE     VARCHAR2(3);
V_CDNATUBCPCSTNFE     VARCHAR2(2);
V_VRINIFAIXA          FLOAT(126);
V_VRFIMFAIXA          FLOAT(126);
V_VRPARDEDUZ          FLOAT(126);
V_VR1PERREDBC         FLOAT(126);
V_VRPERREDBC          FLOAT(126);
V_VRPBCSUBTRIB        FLOAT(126);
V_IDTRATIMPST         VARCHAR2(1);
V_IDPMCBASECA         VARCHAR2(1);
V_IDCSTINTEREST       VARCHAR2(1);
V_IDCSTINTERNA        VARCHAR2(1);
V_IDTIPODESTFISC      VARCHAR2(1);
V_CDDETALHEFISC       VARCHAR2(5);
V_STATUSESTADUAL      VARCHAR2(1);
V_STATUSINTERNACIONAL VARCHAR2(1);
V_IDTIPOREGISTRO      VARCHAR2(4);
V_NRSEQDADOSFISC      VARCHAR2(10);
V_IDMENSAGEM          VARCHAR2(01);
V_CDCLASFISC          VARCHAR2(10);
V_CDPRODUTO           VARCHAR2(10);
V_VRQTDEMB            FLOAT(126);
V_SGUNIDEMB           VARCHAR2(5);
V_UTILRANGE           VARCHAR2(1);
V_IDORIGEMPROD        VARCHAR2(1);
V_CDIMPOSTONFE        VARCHAR2(2);
V_CDENQLEGALIPI       VARCHAR2(3);
V_CDCEST              VARCHAR2(9);
V_IDUTORIGPROD        VARCHAR2(1);
V_VRADICALIQFECP      FLOAT(126);
V_IDIMPODESONERA      VARCHAR2(1);
V_IDCONSDESONVRNF     VARCHAR2(1);
V_CDBENEFFISCAL       VARCHAR2(10);
V_IDCONSMVAPMC        VARCHAR2(01);
V_IDCONSCOMPBCPMC     VARCHAR2(01);
V_IDCONSDIFALIQ       VARCHAR2(01);
V_CDCOMPST            VARCHAR2(20);
V_CDRESTST            VARCHAR2(20);
V_CDNATUREC           VARCHAR2(10);
V_VRIMPOSTOREP        FLOAT(126);
V_IDUTPARFISCENT      VARCHAR2(01);
V_IDNFIMPORTACAO      VARCHAR2(01);
V_DESTINACAO          VARCHAR2(10);
V_IDCONTRIBICMS       VARCHAR2(01);
V_IDCONSFINAL         VARCHAR2(01);
V_IDDISTRIBUIDOR      VARCHAR2(01);
V_IDCENARIO           VARCHAR2(20);
V_IDNUTREDBCFECP      VARCHAR2(01);
V_CODNATOPER          VARCHAR2(03);

OUT_RECORD REGRAFISCAL_TYPE := REGRAFISCAL_TYPE (NULL, NULL, NULL, NULL,
                                                 NULL, NULL, NULL, NULL,
                                                 NULL, NULL, NULL, NULL,
                                                 NULL, NULL, NULL, NULL,
                                                 NULL, NULL, NULL, NULL,
                                                 NULL, NULL, NULL, NULL,
                                                 NULL, NULL, NULL, NULL,
                                                 NULL, NULL, NULL, NULL,
                                                 NULL, NULL, NULL, NULL,
                                                 NULL, NULL, NULL, NULL,
                                                 NULL, NULL, NULL, NULL,
                                                 NULL, NULL, NULL);
BEGIN
   SELECT NVL(IDUTPARFISCENT, 'S') AS IDUTPARFISCENT, NVL(IDNFIMPORTACAO, 'N') AS IDNFIMPORTACAO
     INTO V_IDUTPARFISCENT, V_IDNFIMPORTACAO
     FROM TIPOOPERAC
    WHERE CDTIPOOPER   = P_TIPOOPER
      AND IDENTRSAIDOP = P_ENTRSAIDIN;

      IF ((V_IDUTPARFISCENT = 'N') AND (V_IDNFIMPORTACAO <> 'S')) THEN
      /*BUSCAR PARAMETRIZACAO DA SYSTAX*/

      IF (P_ENTRSAIDIN = 'S') THEN

         SELECT DECODE (NF.IDORIGDESTNF, 'F', NVL(F.IDCONSFINAL, 'N'), NVL(C.IDCONSFINAL, 'N'))     AS IDCONSFINAL,
                DECODE (NF.IDORIGDESTNF, 'F', NVL(F.IDCONTRIBICMS, 'N'), NVL(C.IDCONTRIBICMS, 'N')) AS IDCONTRIBICMS,
                DECODE (NF.IDORIGDESTNF, 'F', NVL(F.IDDISTRIBUIDOR, 'N'), NVL(C.IDDISTRIBUIDOR, 'N')) AS IDDISTRIBUIDOR

           INTO V_IDCONSFINAL, V_IDCONTRIBICMS, V_IDDISTRIBUIDOR
           FROM NOTAFISCAL NF LEFT JOIN CLIENTE C ON NF.CDCLIENTE = C.CDCLIENTE
                              LEFT JOIN FORNECEDOR F ON NF.CDFORNECED = F.CDFORNECED

          WHERE NF.CDFILIAL   = SUBSTR(P_NOTAFISCAL, 1, 4)
            AND NF.NRLANCTONF = SUBSTR(P_NOTAFISCAL, 5, 10);

         IF(V_IDDISTRIBUIDOR = 'S') THEN 
            V_DESTINACAO := '232';

          ELSIF (V_IDCONTRIBICMS = 'S') THEN
            V_DESTINACAO := '3';

          ELSIF ((V_IDCONSFINAL = 'S') AND (V_IDCONTRIBICMS = 'N')) THEN
            V_DESTINACAO := '6';

          END IF;
          SELECT MAX(S.ID_CENARIO), MAX(P.CDPRODUTO), MAX(CEST), MAX(SUBSTR(CFOP, 2, 3)) AS CFOP
            INTO V_IDCENARIO, V_CDPRODUTO, V_CDCEST, V_CDFISCOPER
            FROM SYS_CENARIO S, SYS_MATERIAL M, PRODUTO P, TIPOITEMPROD T
           WHERE UF_ORIGEM             = P_ESTAORIG
                 AND UF_DESTINO        = P_ESTADEST
                 AND ENT_SAI           = DECODE(P_ENTRSAIDIN, 'S', '3', '1')
                 AND DESTINACAO        = V_DESTINACAO
                 AND S.ID_CENARIO      = M.ID_CENARIO
                 AND M.COD_PROD        = P_CDARVPROD 
                 AND M.COD_PROD        = P.CDARVPROD
                 AND T.CDTPITEMPROD    = P.CDTPITEMPROD 
                 AND T.CDORIGEMSYSTAX  = S.ORIGEM;


      ELSE
          IF(V_IDDISTRIBUIDOR = 'S') THEN
             V_CODNATOPER:= '121';
          ELSE
             V_CODNATOPER:= '120';
          END IF;

          V_DESTINACAO := '232';

          SELECT MAX(S.ID_CENARIO), MAX(P.CDPRODUTO), MAX(CEST), MAX(SUBSTR(CFOP, 2, 3)) AS CFOP
            INTO V_IDCENARIO, V_CDPRODUTO, V_CDCEST, V_CDFISCOPER
            FROM SYS_CENARIO S, SYS_MATERIAL M, PRODUTO P
           WHERE UF_ORIGEM             = P_ESTAORIG
                 AND UF_DESTINO        = P_ESTADEST
                 AND ENT_SAI           = DECODE(P_ENTRSAIDIN, 'S', '3', '1')
                 AND DESTINACAO        = V_DESTINACAO
                 AND S.ID_CENARIO      = M.ID_CENARIO
                 AND S.COD_NATOP       = V_CODNATOPER
                 AND M.COD_PROD        = P_CDARVPROD 
                 AND M.COD_PROD        = P.CDARVPROD;

      END IF;

      IF V_IDCENARIO IS NOT NULL THEN

          IF (P_ESTAORIG IS NULL) OR (P_ESTADEST IS NULL) THEN
             V_IDCSTINTEREST := 'T';
          ELSIF (P_ESTAORIG = P_ESTADEST) THEN
             V_IDCSTINTEREST := 'E';
          ELSE
             V_IDCSTINTEREST := 'I';
          END IF;      

          FOR C_REGRASISTAX IN(SELECT NVL(ALIQUOTA, 0) AS ALIQUOTA, NVL(CDIMPOSTO, 'XX') AS CDIMPOSTO, NVL(CDIMPOSTONFE, 'YY') AS CDIMPOSTONFE,
                                      IDTRATIMPO
                                 FROM (SELECT ALIQUOTA, MIN(CDIMPOSTO) AS CDIMPOSTO,
                                              MAX(I.CDIMPOSTONFE)  AS CDIMPOSTONFE, MAX(IDTRATIMPO) AS IDTRATIMPO
                                         FROM SYS_ICMS, IMPOSTONFE I, IMPOSTO II
                                        WHERE ID_CENARIO = V_IDCENARIO
                                          AND COD_PROD   = P_CDARVPROD
                                          AND II.CDIMPOSTONFE = I.CDIMPOSTONFE
                                          AND I.CDIMPOSTONFE  = '00'
                                          AND (II.CDIMPOSTO    = P_CDIMPOSTO OR P_CDIMPOSTO IS NULL)
                                          AND (VIG_DE <= P_DTENTRSAID)
                                          AND (NVL(VIG_ATE, P_DTENTRSAID) >= P_DTENTRSAID)
                                        GROUP BY ALIQUOTA
                                        UNION
                                       SELECT ALIQUOTA, MIN(CDIMPOSTO) AS CDIMPOSTO,
                                              MAX(I.CDIMPOSTONFE) AS CDIMPOSTONFE, MAX(IDTRATIMPO) AS IDTRATIMPO
                                         FROM SYS_IPI, IMPOSTONFE I, IMPOSTO II
                                        WHERE ID_CENARIO = V_IDCENARIO
                                          AND COD_PROD   = P_CDARVPROD
                                          AND II.CDIMPOSTONFE = I.CDIMPOSTONFE
                                          AND I.CDIMPOSTONFE  = '10'
                                          AND (II.CDIMPOSTO    = P_CDIMPOSTO OR P_CDIMPOSTO IS NULL)
                                          AND (VIG_DE <= P_DTENTRSAID)
                                          AND (NVL(VIG_ATE, P_DTENTRSAID) >= P_DTENTRSAID)
                                        GROUP BY ALIQUOTA
                                        UNION
                                       SELECT ALIQUOTA, MIN(CDIMPOSTO) AS CDIMPOSTO,
                                              MAX(I.CDIMPOSTONFE) AS CDIMPOSTONFE, MAX(IDTRATIMPO) AS IDTRATIMPO
                                         FROM SYS_PIS, IMPOSTONFE I, IMPOSTO II
                                        WHERE ID_CENARIO = V_IDCENARIO
                                          AND COD_PROD   = P_CDARVPROD
                                          AND II.CDIMPOSTONFE = I.CDIMPOSTONFE
                                          AND I.CDIMPOSTONFE  = '50'
                                          AND (II.CDIMPOSTO    = P_CDIMPOSTO OR P_CDIMPOSTO IS NULL)
                                          AND (VIG_DE <= P_DTENTRSAID)
                                          AND (NVL(VIG_ATE, P_DTENTRSAID) >= P_DTENTRSAID)
                                        GROUP BY ALIQUOTA
                                        UNION
                                       SELECT ALIQUOTA, MIN(CDIMPOSTO) AS CDIMPOSTO,
                                              MAX(I.CDIMPOSTONFE) AS CDIMPOSTONFE, MAX(IDTRATIMPO) AS IDTRATIMPO
                                         FROM SYS_COFINS, IMPOSTONFE I, IMPOSTO II
                                        WHERE ID_CENARIO = V_IDCENARIO
                                          AND COD_PROD   = P_CDARVPROD
                                          AND II.CDIMPOSTONFE = I.CDIMPOSTONFE
                                          AND I.CDIMPOSTONFE  = '60'
                                          AND (II.CDIMPOSTO    = P_CDIMPOSTO OR P_CDIMPOSTO IS NULL)
                                          AND (VIG_DE <= P_DTENTRSAID)
                                          AND (NVL(VIG_ATE, P_DTENTRSAID) >= P_DTENTRSAID)
                                        GROUP BY ALIQUOTA
                                        UNION
                                       SELECT MAX(ALIQUOTA_ST) AS ALIQUOTA, MIN(CDIMPOSTO) AS CDIMPOSTO,
                                              MAX(I.CDIMPOSTONFE) AS CDIMPOSTONFE, MAX(IDTRATIMPO) AS IDTRATIMPO
                                         FROM SYS_ICMSST, IMPOSTONFE I, IMPOSTO II
                                        WHERE ID_CENARIO = V_IDCENARIO
                                          AND COD_PROD   = P_CDARVPROD
                                          AND II.CDIMPOSTONFE = I.CDIMPOSTONFE
                                          AND I.CDIMPOSTONFE  = '80'
                                          AND (II.CDIMPOSTO    = P_CDIMPOSTO OR P_CDIMPOSTO IS NULL)
                                          AND (VIG_DE <= P_DTENTRSAID)
                                          AND (NVL(VIG_ATE, P_DTENTRSAID) >= P_DTENTRSAID)
                                        )
                                WHERE CDIMPOSTO IS NOT NULL        
                                ORDER BY CDIMPOSTO
          )
          LOOP
             IF (C_REGRASISTAX.IDTRATIMPO = 'A') THEN
                V_IDTRATIMPST := '0';
             END IF;

             IF (C_REGRASISTAX.IDTRATIMPO = 'D') THEN
                V_IDTRATIMPST := '1';
             END IF;

             IF (C_REGRASISTAX.IDTRATIMPO = 'V') THEN
                V_IDTRATIMPST := '2';
             END IF;

             V_IDDIFEALIQ := 'N';

             IF (C_REGRASISTAX.CDIMPOSTONFE = '00') THEN
                   SELECT CASE WHEN ALIQUOTA > 0 THEN '1' ELSE '3' END AS CDFISCAL, MAX(CST), MAX(ALIQUOTA), MAX('3'),
                          MAX('4'), MAX('9'), MAX(P_RED_BC),   MAX('N'),
                          MAX('T'), MAX('E'), MAX('E'),        MAX(''),
                          MAX(NVL(FCP, 0)), MAX('SYS'), MAX('N'),   MAX('N'),
                          MAX('N'), MAX(''), CASE WHEN NVL(ALIQ_UF_DEST, 0) > 0 THEN 'S' ELSE 'N' END
                     INTO
                          V_CDFISCAL,        V_CDSITUTRIB,     V_VRALIQUOTA,      V_IDMODBCNFE,
                          V_IDMODBCSTNFE,    V_IDMOTIDESSTNFE, V_VRPERREDBC,      V_IDPMCBASECA,
                          V_IDCSTINTERNA,    V_IDTIPODESTFISC, V_IDTIPOREGISTRO,  V_CDCLASFISC,
                          V_VRADICALIQFECP,  V_CDREGRAFISC,    V_IDCONSDIFALIQ,   V_IDIMPODESONERA,
                          V_IDCONSDESONVRNF, V_CDENQLEGALIPI,  V_IDDIFEALIQ 
                     FROM SYS_ICMS, IMPOSTONFE I, IMPOSTO II
                    WHERE ID_CENARIO = V_IDCENARIO
                      AND COD_PROD   = P_CDARVPROD
                      AND II.CDIMPOSTONFE = I.CDIMPOSTONFE
                      AND I.CDIMPOSTONFE  = '00'
                      AND (VIG_DE <= P_DTENTRSAID)
                      AND (NVL(VIG_ATE, P_DTENTRSAID) >= P_DTENTRSAID)
                    GROUP BY CASE WHEN ALIQUOTA > 0 THEN '1' ELSE '3' END,
                             CASE WHEN NVL(ALIQ_UF_DEST, 0) > 0 THEN 'S' ELSE 'N' END;

                    V_CDSITUTRIBST := V_CDSITUTRIB;
             END IF;
             /*IPI*/
             IF (C_REGRASISTAX.CDIMPOSTONFE = '10') THEN
                   SELECT CASE WHEN ALIQUOTA > 0 THEN '1' ELSE '3' END AS CDFISCAL, MAX(CST), MAX(ALIQUOTA), MAX('3'),
                          MAX('4'), MAX('9'), MAX(P_RED_BC),   MAX('N'),
                          MAX('T'), MAX('E'), MAX('E'),        MAX(''),
                          MAX('SYS'), MAX('N'),   MAX('N'),
                          MAX('N'), MAX(CENQ), MAX(0)
                     INTO
                          V_CDFISCAL,        V_CDSITUTRIB,     V_VRALIQUOTA,      V_IDMODBCNFE,
                          V_IDMODBCSTNFE,    V_IDMOTIDESSTNFE, V_VRPERREDBC,      V_IDPMCBASECA,
                          V_IDCSTINTERNA,    V_IDTIPODESTFISC, V_IDTIPOREGISTRO,  V_CDCLASFISC,
                          V_CDREGRAFISC,     V_IDCONSDIFALIQ,  V_IDIMPODESONERA,
                          V_IDCONSDESONVRNF, V_CDENQLEGALIPI,  V_VRADICALIQFECP
                     FROM SYS_IPI, IMPOSTONFE I, IMPOSTO II
                    WHERE ID_CENARIO = V_IDCENARIO
                      AND COD_PROD   = P_CDARVPROD
                      AND II.CDIMPOSTONFE = I.CDIMPOSTONFE
                      AND I.CDIMPOSTONFE  = '10'
                      AND (VIG_DE <= P_DTENTRSAID)
                      AND (NVL(VIG_ATE, P_DTENTRSAID) >= P_DTENTRSAID)
                    GROUP BY CASE WHEN ALIQUOTA > 0 THEN '1' ELSE '3' END;
             END IF;

             /*PIS*/
             IF (C_REGRASISTAX.CDIMPOSTONFE = '50') THEN
                   SELECT CASE WHEN ALIQUOTA > 0 THEN '1' ELSE '3' END AS CDFISCAL, MAX(CST), MAX(ALIQUOTA), MAX('3'),
                          MAX('4'), MAX('9'), MAX(P_RED_BC),   MAX('N'),
                          MAX('T'), MAX('E'), MAX('E'),        MAX(''),
                          MAX('SYS'), MAX('N'),   MAX('N'),
                          MAX('N'), MAX(''), MAX(0)
                     INTO
                          V_CDFISCAL,        V_CDSITUTRIB,     V_VRALIQUOTA,      V_IDMODBCNFE,
                          V_IDMODBCSTNFE,    V_IDMOTIDESSTNFE, V_VRPERREDBC,      V_IDPMCBASECA,
                          V_IDCSTINTERNA,    V_IDTIPODESTFISC, V_IDTIPOREGISTRO,  V_CDCLASFISC,
                          V_CDREGRAFISC,     V_IDCONSDIFALIQ,  V_IDIMPODESONERA,
                          V_IDCONSDESONVRNF, V_CDENQLEGALIPI,  V_VRADICALIQFECP
                     FROM SYS_PIS, IMPOSTONFE I, IMPOSTO II
                    WHERE ID_CENARIO = V_IDCENARIO
                      AND COD_PROD   = P_CDARVPROD
                      AND II.CDIMPOSTONFE = I.CDIMPOSTONFE
                      AND I.CDIMPOSTONFE  = '50'
                      AND (VIG_DE <= P_DTENTRSAID)
                      AND (NVL(VIG_ATE, P_DTENTRSAID) >= P_DTENTRSAID)
                    GROUP BY CASE WHEN ALIQUOTA > 0 THEN '1' ELSE '3' END;
             END IF;

             /*COFINS*/
             IF (C_REGRASISTAX.CDIMPOSTONFE = '60') THEN
                   SELECT CASE WHEN ALIQUOTA > 0 THEN '1' ELSE '3' END AS CDFISCAL, MAX(CST), MAX(ALIQUOTA), MAX('3'),
                          MAX('4'), MAX('9'), MAX(P_RED_BC),   MAX('N'),
                          MAX('T'), MAX('E'), MAX('E'),        MAX(''),
                          MAX('SYS'),      MAX('N'),       MAX('N'),
                          MAX('N'), MAX(''),  MAX(0)
                     INTO
                          V_CDFISCAL,        V_CDSITUTRIB,     V_VRALIQUOTA,      V_IDMODBCNFE,
                          V_IDMODBCSTNFE,    V_IDMOTIDESSTNFE, V_VRPERREDBC,      V_IDPMCBASECA,
                          V_IDCSTINTERNA,    V_IDTIPODESTFISC, V_IDTIPOREGISTRO,  V_CDCLASFISC,
                          V_CDREGRAFISC,     V_IDCONSDIFALIQ,  V_IDIMPODESONERA,
                          V_IDCONSDESONVRNF, V_CDENQLEGALIPI , V_VRADICALIQFECP
                     FROM SYS_COFINS, IMPOSTONFE I, IMPOSTO II
                    WHERE ID_CENARIO = V_IDCENARIO
                      AND COD_PROD   = P_CDARVPROD
                      AND II.CDIMPOSTONFE = I.CDIMPOSTONFE
                      AND I.CDIMPOSTONFE  = '60'
                      AND (VIG_DE <= P_DTENTRSAID)
                      AND (NVL(VIG_ATE, P_DTENTRSAID) >= P_DTENTRSAID)
                    GROUP BY CASE WHEN ALIQUOTA > 0 THEN '1' ELSE '3' END;
             END IF;

             /*ICMS ST*/
             IF (C_REGRASISTAX.CDIMPOSTONFE = '80') THEN
                   SELECT CASE WHEN ALIQUOTA_ST > 0 THEN '1' ELSE '3' END AS CDFISCAL, MAX(ALIQUOTA_ST), MAX('3'),
                          MAX('4'), MAX('9'), MAX('N'),
                          MAX('T'), MAX('E'), MAX('E'),        MAX(''),
                          MAX(NVL(FCP, 0)),   MAX('SYS'), MAX('N'),   MAX('N'),
                          MAX('N'), MAX('')
                     INTO
                          V_CDFISCAL,        V_VRALIQUOTA,     V_IDMODBCNFE,
                          V_IDMODBCSTNFE,    V_IDMOTIDESSTNFE, V_IDPMCBASECA,
                          V_IDCSTINTERNA,    V_IDTIPODESTFISC, V_IDTIPOREGISTRO,  V_CDCLASFISC,
                          V_VRADICALIQFECP,  V_CDREGRAFISC,    V_IDCONSDIFALIQ,   V_IDIMPODESONERA,
                          V_IDCONSDESONVRNF, V_CDENQLEGALIPI
                     FROM SYS_ICMSST, IMPOSTONFE I, IMPOSTO II
                    WHERE ID_CENARIO = V_IDCENARIO
                      AND COD_PROD   = P_CDARVPROD
                      AND II.CDIMPOSTONFE = I.CDIMPOSTONFE
                      AND I.CDIMPOSTONFE  = '80'
                      AND (VIG_DE <= P_DTENTRSAID)
                      AND (NVL(VIG_ATE, P_DTENTRSAID) >= P_DTENTRSAID)
                    GROUP BY CASE WHEN ALIQUOTA_ST > 0 THEN '1' ELSE '3' END;
                    V_CDSITUTRIB := V_CDSITUTRIBST;
             END IF;

             V_CDREGRAFISC := V_CDREGRAFISC||V_IDCENARIO;

             IF V_IDCENARIO IS NOT NULL THEN
                IF V_CDFISCAL        IS NOT NULL OR
                   V_CDSITUTRIB      IS NOT NULL OR
                   V_VRALIQUOTA      IS NOT NULL OR
                   V_IDMODBCNFE      IS NOT NULL OR
                   V_IDMODBCSTNFE    IS NOT NULL OR
                   V_IDMOTIDESSTNFE  IS NOT NULL OR
                   V_CDTPCRPICOSTNFE IS NOT NULL OR
                   V_CDNATUBCPCSTNFE IS NOT NULL OR
                   V_VRINIFAIXA      IS NOT NULL OR
                   V_VRFIMFAIXA      IS NOT NULL OR
                   V_VRPARDEDUZ      IS NOT NULL OR
                   V_VR1PERREDBC     IS NOT NULL OR
                   V_VRPERREDBC      IS NOT NULL OR
                   V_VRPBCSUBTRIB    IS NOT NULL OR
                   V_IDTRATIMPST     IS NOT NULL OR
                   V_IDPMCBASECA     IS NOT NULL OR
                   V_IDCSTINTEREST   IS NOT NULL OR
                   V_IDCSTINTERNA    IS NOT NULL OR
                   V_CDFISCOPER      IS NOT NULL OR
                   V_IDCFOPINTEREST  IS NOT NULL OR
                   V_IDDIFEALIQ      IS NOT NULL OR
                   V_IDDIFALIQCUSPR  IS NOT NULL OR
                   V_IDTIPODESTFISC  IS NOT NULL OR
                   V_CDDETALHEFISC   IS NOT NULL OR
                   V_IDTIPOREGISTRO  IS NOT NULL OR
                   V_NRSEQDADOSFISC  IS NOT NULL OR
                   V_IDMENSAGEM      IS NOT NULL OR
                   V_CDCLASFISC      IS NOT NULL OR
                   V_CDPRODUTO       IS NOT NULL OR
                   V_CDENQLEGALIPI   IS NOT NULL OR
                   V_CDCEST          IS NOT NULL OR
                   V_VRADICALIQFECP  IS NOT NULL OR
                   V_IDIMPODESONERA  IS NOT NULL OR
                   V_IDCONSDESONVRNF IS NOT NULL OR
                   V_CDBENEFFISCAL   IS NOT NULL OR
                   V_IDCONSMVAPMC    IS NOT NULL OR
                   V_IDCONSCOMPBCPMC IS NOT NULL OR
                   V_IDCONSDIFALIQ   IS NOT NULL OR
                   V_CDCOMPST        IS NOT NULL OR
                   V_CDRESTST        IS NOT NULL OR
                   V_CDNATUREC       IS NOT NULL OR
                   V_VRIMPOSTOREP    IS NOT NULL OR
                   V_IDNUTREDBCFECP  IS NOT NULL THEN
                   OUT_RECORD.CDREGRAFISC        := V_CDREGRAFISC;
                   OUT_RECORD.NMREGRAFISC        := V_NMREGRAFISC;
                   OUT_RECORD.DSMENSREGRAFISC    := V_DSMENSREGRAFISC;
                   OUT_RECORD.CDIMPOSTO          := C_REGRASISTAX.CDIMPOSTO;
                   OUT_RECORD.CDFISCAL           := V_CDFISCAL;
                   OUT_RECORD.CDSITUTRIB         := V_CDSITUTRIB;
                   OUT_RECORD.VRALIQUOTA         := V_VRALIQUOTA;
                   OUT_RECORD.IDMODBCNFE         := V_IDMODBCNFE;
                   OUT_RECORD.IDMODBCSTNFE       := V_IDMODBCSTNFE;
                   OUT_RECORD.IDMOTIDESSTNFE     := V_IDMOTIDESSTNFE;
                   OUT_RECORD.CDTPCRPICOSTNFE    := V_CDTPCRPICOSTNFE;
                   OUT_RECORD.CDNATUBCPCSTNFE    := V_CDNATUBCPCSTNFE;
                   OUT_RECORD.VRINIFAIXA         := V_VRINIFAIXA;
                   OUT_RECORD.VRFIMFAIXA         := V_VRFIMFAIXA;
                   OUT_RECORD.VRPARDEDUZ         := V_VRPARDEDUZ;
                   OUT_RECORD.VR1PERREDBC        := V_VR1PERREDBC;
                   OUT_RECORD.VRPERREDBC         := V_VRPERREDBC;
                   OUT_RECORD.VRPBCSUBTRIB       := V_VRPBCSUBTRIB;
                   OUT_RECORD.IDTRATIMPST        := V_IDTRATIMPST;
                   OUT_RECORD.IDPMCBASECA        := V_IDPMCBASECA;
                   OUT_RECORD.IDCSTINTEREST      := V_IDCSTINTEREST;
                   OUT_RECORD.IDCSTINTERNA       := V_IDCSTINTERNA;
                   OUT_RECORD.CDFISCOPER         := V_CDFISCOPER;
                   OUT_RECORD.IDCFOPINTEREST     := V_IDCFOPINTEREST;
                   OUT_RECORD.IDDIFEALIQ         := V_IDDIFEALIQ;
                   OUT_RECORD.IDDIFALIQCUSPR     := V_IDDIFALIQCUSPR;
                   OUT_RECORD.IDTIPODESTFISC     := V_IDTIPODESTFISC;
                   OUT_RECORD.CDDETALHEFISC      := V_CDDETALHEFISC;
                   OUT_RECORD.IDTIPOREGISTRO     := V_IDTIPOREGISTRO;
                   OUT_RECORD.NRSEQDADOSFISC     := V_NRSEQDADOSFISC;
                   OUT_RECORD.IDMENSAGEM         := V_IDMENSAGEM;
                   OUT_RECORD.CDCLASFISC         := V_CDCLASFISC;
                   OUT_RECORD.CDPRODUTO          := V_CDPRODUTO;
                   OUT_RECORD.CDENQLEGALIPI      := V_CDENQLEGALIPI;
                   OUT_RECORD.CDCEST             := V_CDCEST;
                   OUT_RECORD.VRADICALIQFECP     := V_VRADICALIQFECP;
                   OUT_RECORD.IDIMPODESONERA     := V_IDIMPODESONERA;
                   OUT_RECORD.IDCONSDESONVRNF    := V_IDCONSDESONVRNF;
                   OUT_RECORD.CDBENEFFISCAL      := V_CDBENEFFISCAL;
                   OUT_RECORD.IDCONSMVAPMC       := V_IDCONSMVAPMC;
                   OUT_RECORD.IDCONSCOMPBCPMC    := V_IDCONSCOMPBCPMC;
                   OUT_RECORD.IDCONSDIFALIQ      := V_IDCONSDIFALIQ;
                   OUT_RECORD.CDCOMPST           := V_CDCOMPST;
                   OUT_RECORD.CDRESTST           := V_CDRESTST;
                   OUT_RECORD.CDNATUREC          := V_CDNATUREC;
                   OUT_RECORD.VRIMPOSTOREP       := V_VRIMPOSTOREP;
                   OUT_RECORD.IDNUTREDBCFECP     := V_IDNUTREDBCFECP;
                   PIPE ROW (OUT_RECORD);
                END IF;
             END IF;
          END LOOP;
       END IF;
   END IF;

   IF  (V_IDNFIMPORTACAO = 'S') OR (V_IDUTPARFISCENT = 'S') OR ((V_IDUTPARFISCENT = 'N') AND (V_IDCENARIO IS NULL)) THEN /*BUSCAR PARAMETRIZACAO DA CENTRALIZADA*/
      FOR C_REGRAFISCAL IN
         (SELECT V.CDIMPOSTO, MIN(V.NRORDPRIORIDADE||'_'||R.CDREGRAFISC) ORDEM
            FROM REGRAFISCAL R,
             (
              SELECT MAX(TO_CHAR(REGAUX.DTVALREGRAFISC, 'YYYYMMDD')||'_'||REGAUX.CDREGRAFISC) NUMERO, NRORDPRIORIDADE, IMPREGRAFISCAL.CDIMPOSTO
                FROM VINCULOFISREGRA, IMPREGRAFISCAL, REGRAFISCAL REGAUX
               WHERE VINCULOFISREGRA.CDEMPRESA      = P_EMPRESA
                 AND VINCULOFISREGRA.CDREGRAFISC    = REGAUX.CDREGRAFISC
                 AND REGAUX.IDATIVREGRAFISC = 'S'
                 AND ((REGAUX.DTVALREGRAFISC <= TRUNC(P_DTENTRSAID)) OR (REGAUX.DTVALREGRAFISC IS NULL) OR (P_DTENTRSAID IS NULL))
                 AND (VINCULOFISREGRA.IDENTRSAIDOP  = P_ENTRSAIDIN   OR IDENTRSAIDOP IS NULL)
                 AND (VINCULOFISREGRA.CDTIPOOPER    = P_TIPOOPER     OR CDTIPOOPER IS NULL)
                 AND (VINCULOFISREGRA.CDINSCESTA    = P_INSCESTA     OR CDINSCESTA IS NULL)
                 AND (IMPREGRAFISCAL.CDIMPOSTO      = P_CDIMPOSTO    OR P_CDIMPOSTO IS NULL)
                 AND (IMPREGRAFISCAL.CDREGRAFISC = REGAUX.CDREGRAFISC)
                 AND (IMPREGRAFISCAL.IDIMPOBRIGREGRA = 'S')
                 AND ((VINCULOFISREGRA.DTVALVINCFISC >= TRUNC(P_DTENTRSAID)) OR (VINCULOFISREGRA.DTVALVINCFISC IS NULL) OR (P_DTENTRSAID IS NULL))
                 AND ((P_CDGRUPOFISC IS NULL     AND NOT EXISTS (SELECT 1 FROM GRUPOFISCREGRA  WHERE GRUPOFISCREGRA.CDREGRAFISC = REGAUX.CDREGRAFISC)) OR
                      (P_CDGRUPOFISC IS NOT NULL AND     EXISTS (SELECT 1 FROM GRUPOFISCREGRA WHERE GRUPOFISCREGRA.CDREGRAFISC = REGAUX.CDREGRAFISC AND GRUPOFISCREGRA.CDGRUPOFISC = P_CDGRUPOFISC)))
              GROUP BY NRORDPRIORIDADE, IMPREGRAFISCAL.CDIMPOSTO) V
          WHERE R.CDREGRAFISC = SUBSTR(V.NUMERO, (INSTR(V.NUMERO, '_')) + 1, 10)
          GROUP BY V.CDIMPOSTO
          ORDER BY V.CDIMPOSTO
          )
      LOOP
         BEGIN
            SELECT IDUTFAIXVLR, IDUTMUNIORIG, IDUTUFORIG, IDUTUFDEST, CDIMPOSTONFE
              INTO V_UTFAIXVLR, V_UTMUNIORIG, V_UTUFORIG, V_UTUFDEST, V_CDIMPOSTONFE
              FROM IMPOSTO
             WHERE IMPOSTO.CDIMPOSTO = C_REGRAFISCAL.CDIMPOSTO;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_UTFAIXVLR    := 'N';
               V_UTMUNIORIG   := 'N';
               V_UTUFORIG     := 'N';
               V_UTUFDEST     := 'N';
               V_CDIMPOSTONFE := NULL;
            END;
         END;

         V_CDFISCOPER     := NULL;
         V_IDCFOPINTEREST := NULL;
         V_IDDIFEALIQ     := NULL;
         V_IDDIFALIQCUSPR := NULL;
         V_IDTIPOREGISTRO := NULL;
         V_IDMENSAGEM     := NULL;
         V_CDCLASFISC     := NULL;
         V_CDPRODUTO      := NULL;
         V_IDORIGEMPROD   := NULL;

         IF (V_CDIMPOSTONFE = '00') THEN
            BEGIN
               SELECT NVL(IDUTORIGPROD, 'N')
                 INTO V_IDUTORIGPROD
                 FROM PARAMGERAL
                WHERE CDPARGERAL = '0001';
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_IDUTORIGPROD := 'N';
            END;
            IF V_IDUTORIGPROD = 'S' THEN
               V_IDORIGEMPROD := P_IDORIGEMPROD;
            ELSE
               V_IDORIGEMPROD := 'N';
            END IF;
         ELSE
            V_IDORIGEMPROD := 'N';
         END IF;

         V_CDREGRAFISC := SUBSTR(C_REGRAFISCAL.ORDEM, (INSTR(C_REGRAFISCAL.ORDEM, '_')) + 1, 10);
         BEGIN
            IF (P_PAISORIG||P_ESTAORIG) <> (P_PAISDEST||P_ESTADEST) THEN
               SELECT MAX(CDFISCOPER), MAX(IDCFOPINTEREST), MAX(IDDIFEALIQ), MAX(IDDIFALIQCUSPR), TO_CHAR(MAX('PROD')), MAX(CFOPREGRAFISCAL.CDPRODUTO)
                 INTO V_CDFISCOPER, V_IDCFOPINTEREST, V_IDDIFEALIQ, V_IDDIFALIQCUSPR, V_IDTIPOREGISTRO, V_CDPRODUTO
                 FROM CFOPREGRAFISCAL, PRODUTO PRODUTOREGRA
                WHERE CFOPREGRAFISCAL.CDREGRAFISC = V_CDREGRAFISC
                  AND PRODUTOREGRA.CDPRODUTO = CFOPREGRAFISCAL.CDPRODUTO
                  AND ((IDCFOPINTEREST = 'I') OR (IDCFOPINTEREST = 'T'))
                  AND PRODUTOREGRA.CDARVPROD = (SELECT MAX(B.CDARVPROD)
                                                  FROM CFOPREGRAFISCAL A, PRODUTO B
                                                 WHERE A.CDREGRAFISC = V_CDREGRAFISC
                                                   AND A.CDPRODUTO = B.CDPRODUTO
                                                   AND INSTR(P_CDARVPROD, B.CDARVPROD) = 1);
               BEGIN
                  SELECT DECODE(COUNT(MSGCFOPREGFISC.CDREGRAFISC), 0, 'N', 'S')
                    INTO V_IDMENSAGEM
                    FROM MSGCFOPREGFISC
                   WHERE CDREGRAFISC = V_CDREGRAFISC
                     AND IDCFOPINTEREST = V_IDCFOPINTEREST
                     AND CDPRODUTO = V_CDPRODUTO;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN BEGIN
                     V_IDMENSAGEM      := 'N';
                  END;
               END;

               IF V_CDFISCOPER IS NULL THEN
                  SELECT MAX(CDFISCOPER), MAX(IDCFOPINTEREST), MAX(IDDIFEALIQ), MAX(IDDIFALIQCUSPR), TO_CHAR(MAX('NCM')), MAX(CDCLASFISC)
                    INTO V_CDFISCOPER, V_IDCFOPINTEREST, V_IDDIFEALIQ, V_IDDIFALIQCUSPR, V_IDTIPOREGISTRO, V_CDCLASFISC
                    FROM CFOPNCMREGRAFIS
                   WHERE CFOPNCMREGRAFIS.CDREGRAFISC = V_CDREGRAFISC
                     AND ((IDCFOPINTEREST = 'I') OR (IDCFOPINTEREST = 'T'))
                     AND CFOPNCMREGRAFIS.CDCLASFISC = (SELECT MAX(A.CDCLASFISC)
                                                         FROM CFOPNCMREGRAFIS A, PRODUTO B
                                                        WHERE A.CDREGRAFISC = V_CDREGRAFISC
                                                          AND B.CDARVPROD = P_CDARVPROD
                                                          AND INSTR(B.CDCLASFISC, A.CDCLASFISC)=1);

                  BEGIN
                     SELECT DECODE(COUNT(MSGCFOPNCM.CDREGRAFISC), 0, 'N', 'S')
                       INTO V_IDMENSAGEM
                       FROM MSGCFOPNCM
                      WHERE CDREGRAFISC = V_CDREGRAFISC
                        AND CDCLASFISC = V_CDCLASFISC
                        AND IDCFOPINTEREST = V_IDCFOPINTEREST;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN BEGIN
                        V_IDMENSAGEM      := 'N';
                     END;
                  END;

                  IF V_CDFISCOPER IS NULL THEN
                     SELECT MAX(CDFISCOPER), MAX(IDCFOPINTEREST), MAX(IDDIFEALIQ), MAX(IDDIFALIQCUSPR), TO_CHAR(MAX('PAD'))
                       INTO V_CDFISCOPER, V_IDCFOPINTEREST, V_IDDIFEALIQ, V_IDDIFALIQCUSPR, V_IDTIPOREGISTRO
                       FROM CFOPADRAOREGFIS
                      WHERE CFOPADRAOREGFIS.CDREGRAFISC = V_CDREGRAFISC
                        AND ((IDCFOPINTEREST = 'I') OR (IDCFOPINTEREST = 'T'));

                     BEGIN
                        SELECT DECODE(COUNT(MSGCFOPADRAO.CDREGRAFISC), 0, 'N', 'S')
                          INTO V_IDMENSAGEM
                          FROM MSGCFOPADRAO
                         WHERE CDREGRAFISC = V_CDREGRAFISC
                           AND IDCFOPINTEREST = V_IDCFOPINTEREST;
                     EXCEPTION
                        WHEN NO_DATA_FOUND THEN BEGIN
                           V_IDMENSAGEM      := 'N';
                        END;
                     END;
                  END IF;
               END IF;
            ELSE
               SELECT MAX(CDFISCOPER), MAX(IDCFOPINTEREST), MAX(IDDIFEALIQ), MAX(IDDIFALIQCUSPR), TO_CHAR(MAX('PROD')), MAX(CFOPREGRAFISCAL.CDPRODUTO)
                 INTO V_CDFISCOPER, V_IDCFOPINTEREST, V_IDDIFEALIQ, V_IDDIFALIQCUSPR, V_IDTIPOREGISTRO, V_CDPRODUTO
                 FROM CFOPREGRAFISCAL, PRODUTO PRODUTOREGRA
                WHERE CFOPREGRAFISCAL.CDREGRAFISC = V_CDREGRAFISC
                  AND PRODUTOREGRA.CDPRODUTO = CFOPREGRAFISCAL.CDPRODUTO
                  AND ((IDCFOPINTEREST = 'E') OR (IDCFOPINTEREST = 'T'))
                  AND PRODUTOREGRA.CDARVPROD = (SELECT MAX(B.CDARVPROD)
                                                  FROM CFOPREGRAFISCAL A, PRODUTO B
                                                 WHERE A.CDREGRAFISC = V_CDREGRAFISC
                                                   AND A.CDPRODUTO = B.CDPRODUTO
                                                   AND INSTR(P_CDARVPROD, B.CDARVPROD) = 1);
               BEGIN
                  SELECT DECODE(COUNT(MSGCFOPREGFISC.CDREGRAFISC), 0, 'N', 'S')
                    INTO V_IDMENSAGEM
                    FROM MSGCFOPREGFISC
                   WHERE CDREGRAFISC = V_CDREGRAFISC
                     AND IDCFOPINTEREST = V_IDCFOPINTEREST
                     AND CDPRODUTO = V_CDPRODUTO;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN BEGIN
                     V_IDMENSAGEM      := 'N';
                  END;
               END;

               IF V_CDFISCOPER IS NULL THEN
                  SELECT MAX(CDFISCOPER), MAX(IDCFOPINTEREST), MAX(IDDIFEALIQ), MAX(IDDIFALIQCUSPR), TO_CHAR(MAX('NCM')), MAX(CDCLASFISC)
                    INTO V_CDFISCOPER, V_IDCFOPINTEREST, V_IDDIFEALIQ, V_IDDIFALIQCUSPR, V_IDTIPOREGISTRO, V_CDCLASFISC
                    FROM CFOPNCMREGRAFIS
                   WHERE CFOPNCMREGRAFIS.CDREGRAFISC = V_CDREGRAFISC
                     AND ((IDCFOPINTEREST = 'E') OR (IDCFOPINTEREST = 'T'))
                     AND CFOPNCMREGRAFIS.CDCLASFISC = (SELECT MAX(A.CDCLASFISC)
                                                         FROM CFOPNCMREGRAFIS A, PRODUTO B
                                                        WHERE A.CDREGRAFISC = V_CDREGRAFISC
                                                          AND B.CDARVPROD = P_CDARVPROD
                                                          AND INSTR(B.CDCLASFISC, A.CDCLASFISC)=1);

                  BEGIN
                     SELECT DECODE(COUNT(MSGCFOPNCM.CDREGRAFISC), 0, 'N', 'S')
                       INTO V_IDMENSAGEM
                       FROM MSGCFOPNCM
                      WHERE CDREGRAFISC = V_CDREGRAFISC
                        AND CDCLASFISC = V_CDCLASFISC
                        AND IDCFOPINTEREST = V_IDCFOPINTEREST;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN BEGIN
                        V_IDMENSAGEM      := 'N';
                     END;
                  END;

                  IF V_CDFISCOPER IS NULL THEN
                     SELECT MAX(CDFISCOPER), MAX(IDCFOPINTEREST), MAX(IDDIFEALIQ), MAX(IDDIFALIQCUSPR), TO_CHAR(MAX('PAD'))
                       INTO V_CDFISCOPER, V_IDCFOPINTEREST, V_IDDIFEALIQ, V_IDDIFALIQCUSPR, V_IDTIPOREGISTRO
                       FROM CFOPADRAOREGFIS
                      WHERE CFOPADRAOREGFIS.CDREGRAFISC = V_CDREGRAFISC
                        AND ((IDCFOPINTEREST = 'E') OR (IDCFOPINTEREST = 'T'));

                     BEGIN
                        SELECT DECODE(COUNT(MSGCFOPADRAO.CDREGRAFISC), 0, 'N', 'S')
                          INTO V_IDMENSAGEM
                          FROM MSGCFOPADRAO
                         WHERE CDREGRAFISC = V_CDREGRAFISC
                           AND IDCFOPINTEREST = V_IDCFOPINTEREST;
                     EXCEPTION
                        WHEN NO_DATA_FOUND THEN BEGIN
                           V_IDMENSAGEM      := 'N';
                        END;
                     END;
                  END IF;
               END IF;
            END IF;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_CDFISCOPER     := NULL;
               V_IDCFOPINTEREST := NULL;
               V_IDDIFEALIQ     := NULL;
               V_IDDIFALIQCUSPR := NULL;
               V_IDTIPOREGISTRO := NULL;
               V_IDMENSAGEM     := NULL;
               V_CDCLASFISC     := NULL;
               V_CDPRODUTO      := NULL;
            END;
         END;

         V_CDFISCAL        := NULL;
         V_CDSITUTRIB      := NULL;
         V_VRALIQUOTA      := NULL;
         V_IDMODBCNFE      := NULL;
         V_IDMODBCSTNFE    := NULL;
         V_IDMOTIDESSTNFE  := NULL;
         V_CDTPCRPICOSTNFE := NULL;
         V_CDNATUBCPCSTNFE := NULL;
         V_VRINIFAIXA      := NULL;
         V_VRFIMFAIXA      := NULL;
         V_VRPARDEDUZ      := NULL;
         V_VR1PERREDBC     := NULL;
         V_VRPERREDBC      := NULL;
         V_VRPBCSUBTRIB    := NULL;
         V_IDTRATIMPST     := NULL;
         V_IDPMCBASECA     := NULL;
         V_IDCSTINTEREST   := NULL;
         V_IDCSTINTERNA    := NULL;
         V_IDTIPODESTFISC  := NULL;
         V_CDDETALHEFISC   := NULL;
         V_CDENQLEGALIPI   := NULL;
         V_CDCEST          := NULL;
         V_VRADICALIQFECP  := NULL;
         V_IDIMPODESONERA  := NULL;
         V_IDCONSDESONVRNF := NULL;
         V_CDBENEFFISCAL   := NULL;
         V_IDCONSMVAPMC    := NULL;
         V_IDCONSCOMPBCPMC := NULL;
         V_IDCONSDIFALIQ   := NULL;
         V_CDCOMPST        := NULL;
         V_CDRESTST        := NULL;
         V_CDNATUREC       := NULL;
         V_VRIMPOSTOREP    := NULL;
         V_IDNUTREDBCFECP  := NULL;

         IF P_CFOP = 'N' THEN
            IF (P_PAISORIG||P_ESTAORIG) <> (P_PAISDEST||P_ESTADEST) THEN
               V_STATUSESTADUAL := 'I';
            ELSE
               V_STATUSESTADUAL := 'E';
            END IF;
            IF (P_PAISORIG) <> (P_PAISDEST) THEN
               V_STATUSINTERNACIONAL := 'I';
            ELSE
               V_STATUSINTERNACIONAL := 'N';
            END IF;

            BEGIN
               SELECT
                  CDFISCAL,
                  CDSITUTRIB,
                  VRALIQUOTA,
                  IDMODBCNFE,
                  IDMODBCSTNFE,
                  IDMOTIDESSTNFE,
                  CDTPCRPICOSTNFE,
                  CDNATUBCPCSTNFE,
                  VRINIFAIXA,
                  VRFIMFAIXA,
                  VRPARDEDUZ,
                  VR1PERREDBC,
                  VRPERREDBC,
                  VRPBCSUBTRIB,
                  IDTRATIMPST,
                  IDPMCBASECA,
                  IDCSTINTEREST,
                  IDCSTINTERNA,
                  IDTIPODESTFISC,
                  CDDETALHEFISC,
                  TO_CHAR('PROD'),
                  NRSEQDADOSFISC,
                  '',
                  DADOSFISCPROD.CDPRODUTO,
                  CDFISCOPER,
                  IDDIFEALIQ,
                  IDDIFALIQCUSPR,
                  CDENQLEGALIPI,
                  DADOSFISCPROD.CDCEST,
                  DADOSFISCPROD.VRADICALIQFECP,
                  DADOSFISCPROD.IDIMPODESONERA,
                  DADOSFISCPROD.IDCONSDESONVRNF,
                  DADOSFISCPROD.CDBENEFFISCAL,
                  DADOSFISCPROD.IDCONSMVAPMC,
                  DADOSFISCPROD.IDCONSCOMPBCPMC,
                  DADOSFISCPROD.IDCONSDIFALIQ,
                  DADOSFISCPROD.CDCOMPST,
                  DADOSFISCPROD.CDRESTST,
                  DADOSFISCPROD.CDNATUREC,
                  DADOSFISCPROD.VRIMPOSTOREP,
                  IDNUTREDBCFECP
               INTO
                  V_CDFISCAL,
                  V_CDSITUTRIB,
                  V_VRALIQUOTA,
                  V_IDMODBCNFE,
                  V_IDMODBCSTNFE,
                  V_IDMOTIDESSTNFE,
                  V_CDTPCRPICOSTNFE,
                  V_CDNATUBCPCSTNFE,
                  V_VRINIFAIXA,
                  V_VRFIMFAIXA,
                  V_VRPARDEDUZ,
                  V_VR1PERREDBC,
                  V_VRPERREDBC,
                  V_VRPBCSUBTRIB,
                  V_IDTRATIMPST,
                  V_IDPMCBASECA,
                  V_IDCSTINTEREST,
                  V_IDCSTINTERNA,
                  V_IDTIPODESTFISC,
                  V_CDDETALHEFISC,
                  V_IDTIPOREGISTRO,
                  V_NRSEQDADOSFISC,
                  V_CDCLASFISC,
                  V_CDPRODUTO,
                  V_CDFISCOPER,
                  V_IDDIFEALIQ,
                  V_IDDIFALIQCUSPR,
                  V_CDENQLEGALIPI,
                  V_CDCEST,
                  V_VRADICALIQFECP,
                  V_IDIMPODESONERA,
                  V_IDCONSDESONVRNF,
                  V_CDBENEFFISCAL,
                  V_IDCONSMVAPMC,
                  V_IDCONSCOMPBCPMC,
                  V_IDCONSDIFALIQ,
                  V_CDCOMPST,
                  V_CDRESTST,
                  V_CDNATUREC,
                  V_VRIMPOSTOREP,
                  V_IDNUTREDBCFECP
                 FROM DADOSFISCPROD, PRODUTO PRODUTOREGRA
                WHERE DADOSFISCPROD.CDREGRAFISC = V_CDREGRAFISC
                  AND DADOSFISCPROD.CDIMPOSTO = C_REGRAFISCAL.CDIMPOSTO
                  AND PRODUTOREGRA.CDPRODUTO = DADOSFISCPROD.CDPRODUTO
                  AND (IDCSTINTEREST = V_STATUSESTADUAL OR IDCSTINTEREST = 'T')
                  AND (IDCSTINTERNA  = V_STATUSINTERNACIONAL OR IDCSTINTERNA = 'T')
                  AND (((CDPAISORIG = P_PAISORIG) AND (SGESTAORIG = P_ESTAORIG)) OR  V_UTUFORIG = 'N' OR IDTIPOORIGFISC = 'T')
                  AND (((CDPAISDEST = P_PAISDEST) AND (SGESTADEST = P_ESTADEST)) OR  V_UTUFDEST = 'N' OR IDTIPODESTFISC = 'T')
                  AND (((CDMUNIORIG = P_MUNIORIG  AND V_UTMUNIORIG = 'O') OR (IDTIPOORIGFISC = 'T') OR ((CDMUNIORIG IS NULL) AND  (V_UTMUNIORIG = 'D'))) AND
                       ((CDMUNIDEST = P_MUNIDEST  AND V_UTMUNIORIG = 'D') OR (IDTIPODESTFISC = 'T') OR ((CDMUNIDEST IS NULL) AND  (V_UTMUNIORIG = 'O'))) OR
                       (V_UTMUNIORIG = 'N'))
                  AND ((VRINIFAIXA  <= P_BASECALC) AND (VRFIMFAIXA  >= P_BASECALC) OR  V_UTFAIXVLR = 'N')
                  AND (NVL(IDORIGEMPRODIMP, 'N') = NVL(V_IDORIGEMPROD, 'N'))
                  AND PRODUTOREGRA.CDARVPROD = (SELECT MAX(B.CDARVPROD)
                                                  FROM DADOSFISCPROD A, PRODUTO B
                                                 WHERE A.CDREGRAFISC = V_CDREGRAFISC
                                                   AND A.CDPRODUTO = B.CDPRODUTO
                                                   AND A.CDIMPOSTO = C_REGRAFISCAL.CDIMPOSTO
                                                   AND INSTR(P_CDARVPROD, B.CDARVPROD) = 1
                                                   AND (IDCSTINTEREST = V_STATUSESTADUAL OR IDCSTINTEREST = 'T')
                                                   AND (IDCSTINTERNA  = V_STATUSINTERNACIONAL OR IDCSTINTERNA = 'T')
                                                   AND (((CDPAISORIG = P_PAISORIG) AND (SGESTAORIG = P_ESTAORIG)) OR  V_UTUFORIG = 'N' OR IDTIPOORIGFISC = 'T')
                                                   AND (((CDPAISDEST = P_PAISDEST) AND (SGESTADEST = P_ESTADEST)) OR  V_UTUFDEST = 'N' OR IDTIPODESTFISC = 'T')
                                                   AND (((CDMUNIORIG = P_MUNIORIG  AND V_UTMUNIORIG = 'O') OR (IDTIPOORIGFISC = 'T') OR ((CDMUNIORIG IS NULL) AND  (V_UTMUNIORIG = 'D'))) AND
                                                        ((CDMUNIDEST = P_MUNIDEST  AND V_UTMUNIORIG = 'D') OR (IDTIPODESTFISC = 'T') OR ((CDMUNIDEST IS NULL) AND  (V_UTMUNIORIG = 'O'))) OR
                                                        (V_UTMUNIORIG = 'N'))
                                                   AND (NVL(IDORIGEMPRODIMP, 'N') = NVL(V_IDORIGEMPROD, 'N')));
            EXCEPTION
               WHEN TOO_MANY_ROWS THEN BEGIN
                  RAISE_APPLICATION_ERROR(-20000, 'Operacao Bloqueada! Existe cadastro duplicado na regra fiscal ' || V_CDREGRAFISC || ' (imposto ' || C_REGRAFISCAL.CDIMPOSTO || ', produto ' || P_CDARVPROD || ', pagina "Por Produto").', TRUE);
               END;
               WHEN NO_DATA_FOUND THEN BEGIN
                  V_CDFISCAL        := NULL;
                  V_CDSITUTRIB      := NULL;
                  V_VRALIQUOTA      := NULL;
                  V_IDMODBCNFE      := NULL;
                  V_IDMODBCSTNFE    := NULL;
                  V_IDMOTIDESSTNFE  := NULL;
                  V_CDTPCRPICOSTNFE := NULL;
                  V_CDNATUBCPCSTNFE := NULL;
                  V_VRINIFAIXA      := NULL;
                  V_VRFIMFAIXA      := NULL;
                  V_VRPARDEDUZ      := NULL;
                  V_VR1PERREDBC     := NULL;
                  V_VRPERREDBC      := NULL;
                  V_VRPBCSUBTRIB    := NULL;
                  V_IDTRATIMPST     := NULL;
                  V_IDPMCBASECA     := NULL;
                  V_IDCSTINTEREST   := NULL;
                  V_IDCSTINTERNA    := NULL;
                  V_IDTIPODESTFISC  := NULL;
                  V_CDDETALHEFISC   := NULL;
                  V_IDTIPOREGISTRO  := NULL;
                  V_NRSEQDADOSFISC  := NULL;
                  V_CDCLASFISC      := NULL;
                  V_CDPRODUTO       := NULL;
                  V_CDENQLEGALIPI   := NULL;
                  V_CDCEST          := NULL;
                  V_VRADICALIQFECP  := NULL;
                  V_IDIMPODESONERA  := NULL;
                  V_IDCONSDESONVRNF := NULL;
                  V_CDBENEFFISCAL   := NULL;
                  V_IDCONSMVAPMC    := NULL; 
                  V_IDCONSCOMPBCPMC := NULL;
                  V_IDCONSDIFALIQ   := NULL;
                  V_CDCOMPST        := NULL;
                  V_CDRESTST        := NULL;
                  V_CDNATUREC       := NULL;
                  V_VRIMPOSTOREP    := NULL;
                  V_IDNUTREDBCFECP  := NULL;
               END;
            END;
            BEGIN
               SELECT DECODE(COUNT(MSGFISCPROD.CDREGRAFISC), 0, 'N', 'S')
                 INTO V_IDMENSAGEM
                 FROM MSGFISCPROD
                WHERE CDREGRAFISC = V_CDREGRAFISC
                  AND CDIMPOSTO = C_REGRAFISCAL.CDIMPOSTO
                  AND NRSEQDADOSFISC = V_NRSEQDADOSFISC
                  AND CDPRODUTO = V_CDPRODUTO;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN BEGIN
                  V_IDMENSAGEM      := 'N';
               END;
            END;

            IF V_CDFISCAL IS NULL THEN
               SELECT NVL(MAX(VRQTDEMB), NULL) VRQTDEMB,
                      NVL(MAX(SGUNIDEMB), NULL) SGUNIDEMB
                 INTO V_VRQTDEMB, V_SGUNIDEMB
                 FROM PRODUTO
                WHERE CDARVPROD = P_CDARVPROD;

               BEGIN
                  SELECT
                     CDFISCAL,
                     CDSITUTRIB,
                     VRALIQUOTA,
                     IDMODBCNFE,
                     IDMODBCSTNFE,
                     IDMOTIDESSTNFE,
                     CDTPCRPICOSTNFE,
                     CDNATUBCPCSTNFE,
                     VRINIFAIXA,
                     VRFIMFAIXA,
                     VRPARDEDUZ,
                     VR1PERREDBC,
                     VRPERREDBC,
                     VRPBCSUBTRIB,
                     IDTRATIMPST,
                     IDPMCBASECA,
                     IDCSTINTEREST,
                     IDCSTINTERNA,
                     IDTIPODESTFISC,
                     CDDETALHEFISC,
                     TO_CHAR('NCM'),
                     NRSEQDADOSFISC,
                     CDCLASFISC,
                     '',
                     CDFISCOPER,
                     IDDIFEALIQ,
                     IDDIFALIQCUSPR,
                     CDENQLEGALIPI,
                     CDCEST,
                     VRADICALIQFECP,
                     IDIMPODESONERA,
                     IDCONSDESONVRNF,
                     CDBENEFFISCAL,
                     IDCONSMVAPMC,
                     IDCONSCOMPBCPMC,
                     IDCONSDIFALIQ,
                     CDCOMPST,
                     CDRESTST,
                     CDNATUREC,
                     VRIMPOSTOREP,
                     IDNUTREDBCFECP
                  INTO
                     V_CDFISCAL,
                     V_CDSITUTRIB,
                     V_VRALIQUOTA,
                     V_IDMODBCNFE,
                     V_IDMODBCSTNFE,
                     V_IDMOTIDESSTNFE,
                     V_CDTPCRPICOSTNFE,
                     V_CDNATUBCPCSTNFE,
                     V_VRINIFAIXA,
                     V_VRFIMFAIXA,
                     V_VRPARDEDUZ,
                     V_VR1PERREDBC,
                     V_VRPERREDBC,
                     V_VRPBCSUBTRIB,
                     V_IDTRATIMPST,
                     V_IDPMCBASECA,
                     V_IDCSTINTEREST,
                     V_IDCSTINTERNA,
                     V_IDTIPODESTFISC,
                     V_CDDETALHEFISC,
                     V_IDTIPOREGISTRO,
                     V_NRSEQDADOSFISC,
                     V_CDCLASFISC,
                     V_CDPRODUTO,
                     V_CDFISCOPER,
                     V_IDDIFEALIQ,
                     V_IDDIFALIQCUSPR,
                     V_CDENQLEGALIPI,
                     V_CDCEST,
                     V_VRADICALIQFECP,
                     V_IDIMPODESONERA,
                     V_IDCONSDESONVRNF,
                     V_CDBENEFFISCAL,
                     V_IDCONSMVAPMC,
                     V_IDCONSCOMPBCPMC,
                     V_IDCONSDIFALIQ,
                     V_CDCOMPST,
                     V_CDRESTST,
                     V_CDNATUREC,
                     V_VRIMPOSTOREP,
                     V_IDNUTREDBCFECP
                    FROM DADOSFISCNCM
                   WHERE DADOSFISCNCM.CDREGRAFISC = V_CDREGRAFISC
                     AND DADOSFISCNCM.CDIMPOSTO = C_REGRAFISCAL.CDIMPOSTO
                     AND (IDCSTINTEREST = V_STATUSESTADUAL OR IDCSTINTEREST = 'T')
                     AND (IDCSTINTERNA  = V_STATUSINTERNACIONAL OR IDCSTINTERNA = 'T')
                     AND (((CDPAISORIG = P_PAISORIG) AND (SGESTAORIG = P_ESTAORIG)) OR  V_UTUFORIG = 'N' OR IDTIPOORIGFISC = 'T')
                     AND (((CDPAISDEST = P_PAISDEST) AND (SGESTADEST = P_ESTADEST)) OR  V_UTUFDEST = 'N' OR IDTIPODESTFISC = 'T')
                     AND (((CDMUNIORIG = P_MUNIORIG  AND V_UTMUNIORIG = 'O') OR (IDTIPOORIGFISC = 'T') OR ((CDMUNIORIG IS NULL) AND  (V_UTMUNIORIG = 'D'))) AND
                          ((CDMUNIDEST = P_MUNIDEST  AND V_UTMUNIORIG = 'D') OR (IDTIPODESTFISC = 'T') OR ((CDMUNIDEST IS NULL) AND  (V_UTMUNIORIG = 'O'))) OR
                          (V_UTMUNIORIG = 'N'))
                     AND ((VRINIFAIXA  <= P_BASECALC) AND (VRFIMFAIXA  >= P_BASECALC) OR  V_UTFAIXVLR = 'N')
                     AND ((VRRANGINI   <= V_VRQTDEMB) AND (VRRANGFIM   >= V_VRQTDEMB) AND  SGUNIDEMB = V_SGUNIDEMB)
                     AND (NVL(IDORIGEMPRODIMP, 'N') = NVL(V_IDORIGEMPROD, 'N'))
                     AND DADOSFISCNCM.CDCLASFISC = (SELECT MAX(A.CDCLASFISC)
                                                      FROM DADOSFISCNCM A, PRODUTO B
                                                     WHERE A.CDREGRAFISC = V_CDREGRAFISC
                                                       AND B.CDARVPROD = P_CDARVPROD
                                                       AND A.CDIMPOSTO = C_REGRAFISCAL.CDIMPOSTO
                                                       AND INSTR(B.CDCLASFISC, A.CDCLASFISC)=1
                                                       AND (IDCSTINTEREST = V_STATUSESTADUAL OR IDCSTINTEREST = 'T')
                                                       AND (IDCSTINTERNA  = V_STATUSINTERNACIONAL OR IDCSTINTERNA = 'T')
                                                       AND (((CDPAISORIG = P_PAISORIG) AND (SGESTAORIG = P_ESTAORIG)) OR  V_UTUFORIG = 'N' OR IDTIPOORIGFISC = 'T')
                                                       AND (((CDPAISDEST = P_PAISDEST) AND (SGESTADEST = P_ESTADEST)) OR  V_UTUFDEST = 'N' OR IDTIPODESTFISC = 'T')
                                                       AND (((CDMUNIORIG = P_MUNIORIG  AND V_UTMUNIORIG = 'O') OR (IDTIPOORIGFISC = 'T') OR ((CDMUNIORIG IS NULL) AND  (V_UTMUNIORIG = 'D'))) AND
                                                            ((CDMUNIDEST = P_MUNIDEST  AND V_UTMUNIORIG = 'D') OR (IDTIPODESTFISC = 'T') OR ((CDMUNIDEST IS NULL) AND  (V_UTMUNIORIG = 'O'))) OR
                                                            (V_UTMUNIORIG = 'N'))
                                                       AND (NVL(IDORIGEMPRODIMP, 'N') = NVL(V_IDORIGEMPROD, 'N')));

               EXCEPTION
                  WHEN TOO_MANY_ROWS THEN BEGIN
                     RAISE_APPLICATION_ERROR(-20000, 'Operacao Bloqueada! Existe cadastro duplicado na regra fiscal ' || V_CDREGRAFISC || ' (imposto ' || C_REGRAFISCAL.CDIMPOSTO || ', produto ' || P_CDARVPROD || ', pagina "Por NCM").', TRUE);
                  END;
                  WHEN NO_DATA_FOUND THEN BEGIN
                     V_CDFISCAL        := NULL;
                     V_CDSITUTRIB      := NULL;
                     V_VRALIQUOTA      := NULL;
                     V_IDMODBCNFE      := NULL;
                     V_IDMODBCSTNFE    := NULL;
                     V_IDMOTIDESSTNFE  := NULL;
                     V_CDTPCRPICOSTNFE := NULL;
                     V_CDNATUBCPCSTNFE := NULL;
                     V_VRINIFAIXA      := NULL;
                     V_VRFIMFAIXA      := NULL;
                     V_VRPARDEDUZ      := NULL;
                     V_VR1PERREDBC     := NULL;
                     V_VRPERREDBC      := NULL;
                     V_VRPBCSUBTRIB    := NULL;
                     V_IDTRATIMPST     := NULL;
                     V_IDPMCBASECA     := NULL;
                     V_IDCSTINTEREST   := NULL;
                     V_IDCSTINTERNA    := NULL;
                     V_IDTIPODESTFISC  := NULL;
                     V_CDDETALHEFISC   := NULL;
                     V_IDTIPOREGISTRO  := NULL;
                     V_NRSEQDADOSFISC  := NULL;
                     V_CDCLASFISC      := NULL;
                     V_CDPRODUTO       := NULL;
                     V_CDFISCOPER      := NULL;
                     V_IDDIFEALIQ      := NULL;
                     V_IDDIFALIQCUSPR  := NULL;
                     V_CDENQLEGALIPI   := NULL;
                     V_CDCEST          := NULL;
                     V_VRADICALIQFECP  := NULL;
                     V_IDIMPODESONERA  := NULL;
                     V_IDCONSDESONVRNF := NULL;
                     V_CDBENEFFISCAL   := NULL;
                     V_IDCONSMVAPMC    := NULL;
                     V_IDCONSCOMPBCPMC := NULL;
                     V_IDCONSDIFALIQ   := NULL;
                     V_CDCOMPST        := NULL;
                     V_CDRESTST        := NULL;
                     V_CDNATUREC       := NULL;
                     V_VRIMPOSTOREP    := NULL;
                     V_IDNUTREDBCFECP  := NULL;
                  END;
               END;

               BEGIN
                  SELECT DECODE(COUNT(MSGFISCNCM.CDREGRAFISC), 0, 'N', 'S')
                    INTO V_IDMENSAGEM
                    FROM MSGFISCNCM
                   WHERE CDREGRAFISC = V_CDREGRAFISC
                     AND CDIMPOSTO = C_REGRAFISCAL.CDIMPOSTO
                     AND CDCLASFISC = V_CDCLASFISC
                     AND NRSEQDADOSFISC = V_NRSEQDADOSFISC;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN BEGIN
                     V_IDMENSAGEM      := 'N';
                  END;
               END;

               IF V_CDFISCAL IS NULL THEN
                  BEGIN
                     SELECT
                        CDFISCAL,
                        CDSITUTRIB,
                        VRALIQUOTA,
                        IDMODBCNFE,
                        IDMODBCSTNFE,
                        IDMOTIDESSTNFE,
                        CDTPCRPICOSTNFE,
                        CDNATUBCPCSTNFE,
                        VRINIFAIXA,
                        VRFIMFAIXA,
                        VRPARDEDUZ,
                        VR1PERREDBC,
                        VRPERREDBC,
                        VRPBCSUBTRIB,
                        IDTRATIMPST,
                        IDPMCBASECA,
                        IDCSTINTEREST,
                        IDCSTINTERNA,
                        IDTIPODESTFISC,
                        CDDETALHEFISC,
                        TO_CHAR('NCM'),
                        NRSEQDADOSFISC,
                        CDCLASFISC,
                        '',
                        CDFISCOPER,
                        IDDIFEALIQ,
                        IDDIFALIQCUSPR,
                        CDENQLEGALIPI,
                        CDCEST,
                        VRADICALIQFECP,
                        IDIMPODESONERA,
                        IDCONSDESONVRNF,
                        CDBENEFFISCAL,
                        IDCONSMVAPMC,
                        IDCONSCOMPBCPMC,
                        IDCONSDIFALIQ,
                        CDCOMPST,
                        CDRESTST,
                        CDNATUREC,
                        VRIMPOSTOREP,
                        IDNUTREDBCFECP
                     INTO
                        V_CDFISCAL,
                        V_CDSITUTRIB,
                        V_VRALIQUOTA,
                        V_IDMODBCNFE,
                        V_IDMODBCSTNFE,
                        V_IDMOTIDESSTNFE,
                        V_CDTPCRPICOSTNFE,
                        V_CDNATUBCPCSTNFE,
                        V_VRINIFAIXA,
                        V_VRFIMFAIXA,
                        V_VRPARDEDUZ,
                        V_VR1PERREDBC,
                        V_VRPERREDBC,
                        V_VRPBCSUBTRIB,
                        V_IDTRATIMPST,
                        V_IDPMCBASECA,
                        V_IDCSTINTEREST,
                        V_IDCSTINTERNA,
                        V_IDTIPODESTFISC,
                        V_CDDETALHEFISC,
                        V_IDTIPOREGISTRO,
                        V_NRSEQDADOSFISC,
                        V_CDCLASFISC,
                        V_CDPRODUTO,
                        V_CDFISCOPER,
                        V_IDDIFEALIQ,
                        V_IDDIFALIQCUSPR,
                        V_CDENQLEGALIPI,
                        V_CDCEST,
                        V_VRADICALIQFECP,
                        V_IDIMPODESONERA,
                        V_IDCONSDESONVRNF,
                        V_CDBENEFFISCAL,
                        V_IDCONSMVAPMC,
                        V_IDCONSCOMPBCPMC,
                        V_IDCONSDIFALIQ,
                        V_CDCOMPST,
                        V_CDRESTST,
                        V_CDNATUREC,
                        V_VRIMPOSTOREP,
                        V_IDNUTREDBCFECP
                       FROM DADOSFISCNCM
                      WHERE DADOSFISCNCM.CDREGRAFISC = V_CDREGRAFISC
                        AND DADOSFISCNCM.CDIMPOSTO = C_REGRAFISCAL.CDIMPOSTO
                        AND (IDCSTINTEREST = V_STATUSESTADUAL OR IDCSTINTEREST = 'T')
                        AND (IDCSTINTERNA  = V_STATUSINTERNACIONAL OR IDCSTINTERNA = 'T')
                        AND (((CDPAISORIG = P_PAISORIG) AND (SGESTAORIG = P_ESTAORIG)) OR  V_UTUFORIG = 'N' OR IDTIPOORIGFISC = 'T')
                        AND (((CDPAISDEST = P_PAISDEST) AND (SGESTADEST = P_ESTADEST)) OR  V_UTUFDEST = 'N' OR IDTIPODESTFISC = 'T')
                        AND (((CDMUNIORIG = P_MUNIORIG  AND V_UTMUNIORIG = 'O') OR (IDTIPOORIGFISC = 'T') OR ((CDMUNIORIG IS NULL) AND  (V_UTMUNIORIG = 'D'))) AND
                             ((CDMUNIDEST = P_MUNIDEST  AND V_UTMUNIORIG = 'D') OR (IDTIPODESTFISC = 'T') OR ((CDMUNIDEST IS NULL) AND  (V_UTMUNIORIG = 'O'))) OR
                             (V_UTMUNIORIG = 'N'))
                        AND ((VRINIFAIXA  <= P_BASECALC) AND (VRFIMFAIXA  >= P_BASECALC) OR  V_UTFAIXVLR = 'N')
                        AND ((VRRANGINI IS NULL) AND (VRRANGFIM IS NULL))
                        AND (NVL(IDORIGEMPRODIMP, 'N') = NVL(V_IDORIGEMPROD, 'N'))
                        AND DADOSFISCNCM.CDCLASFISC = (SELECT MAX(A.CDCLASFISC)
                                                         FROM DADOSFISCNCM A, PRODUTO B
                                                        WHERE A.CDREGRAFISC = V_CDREGRAFISC
                                                          AND B.CDARVPROD = P_CDARVPROD
                                                          AND A.CDIMPOSTO = C_REGRAFISCAL.CDIMPOSTO
                                                          AND INSTR(B.CDCLASFISC, A.CDCLASFISC)=1
                                                          AND (IDCSTINTEREST = V_STATUSESTADUAL OR IDCSTINTEREST = 'T')
                                                          AND (IDCSTINTERNA  = V_STATUSINTERNACIONAL OR IDCSTINTERNA = 'T')
                                                          AND (((CDPAISORIG = P_PAISORIG) AND (SGESTAORIG = P_ESTAORIG)) OR  V_UTUFORIG = 'N' OR IDTIPOORIGFISC = 'T')
                                                          AND (((CDPAISDEST = P_PAISDEST) AND (SGESTADEST = P_ESTADEST)) OR  V_UTUFDEST = 'N' OR IDTIPODESTFISC = 'T')
                                                          AND (((CDMUNIORIG = P_MUNIORIG  AND V_UTMUNIORIG = 'O') OR (IDTIPOORIGFISC = 'T') OR ((CDMUNIORIG IS NULL) AND  (V_UTMUNIORIG = 'D'))) AND
                                                               ((CDMUNIDEST = P_MUNIDEST  AND V_UTMUNIORIG = 'D') OR (IDTIPODESTFISC = 'T') OR ((CDMUNIDEST IS NULL) AND  (V_UTMUNIORIG = 'O'))) OR
                                                               (V_UTMUNIORIG = 'N'))
                                                          AND (NVL(IDORIGEMPRODIMP, 'N') = NVL(V_IDORIGEMPROD, 'N')));

                  EXCEPTION
                     WHEN TOO_MANY_ROWS THEN BEGIN
                        RAISE_APPLICATION_ERROR(-20000, 'Operacao Bloqueada! Existe cadastro duplicado na regra fiscal ' || V_CDREGRAFISC || ' (imposto ' || C_REGRAFISCAL.CDIMPOSTO || ', produto ' || P_CDARVPROD || ', pagina "Por NCM").', TRUE);
                     END;
                     WHEN NO_DATA_FOUND THEN BEGIN
                        V_CDFISCAL        := NULL;
                        V_CDSITUTRIB      := NULL;
                        V_VRALIQUOTA      := NULL;
                        V_IDMODBCNFE      := NULL;
                        V_IDMODBCSTNFE    := NULL;
                        V_IDMOTIDESSTNFE  := NULL;
                        V_CDTPCRPICOSTNFE := NULL;
                        V_CDNATUBCPCSTNFE := NULL;
                        V_VRINIFAIXA      := NULL;
                        V_VRFIMFAIXA      := NULL;
                        V_VRPARDEDUZ      := NULL;
                        V_VR1PERREDBC     := NULL;
                        V_VRPERREDBC      := NULL;
                        V_VRPBCSUBTRIB    := NULL;
                        V_IDTRATIMPST     := NULL;
                        V_IDPMCBASECA     := NULL;
                        V_IDCSTINTEREST   := NULL;
                        V_IDCSTINTERNA    := NULL;
                        V_IDTIPODESTFISC  := NULL;
                        V_CDDETALHEFISC   := NULL;
                        V_IDTIPOREGISTRO  := NULL;
                        V_NRSEQDADOSFISC  := NULL;
                        V_CDCLASFISC      := NULL;
                        V_CDPRODUTO       := NULL;
                        V_CDFISCOPER      := NULL;
                        V_IDDIFEALIQ      := NULL;
                        V_IDDIFALIQCUSPR  := NULL;
                        V_CDENQLEGALIPI   := NULL;
                        V_CDCEST          := NULL;
                        V_VRADICALIQFECP  := NULL;
                        V_IDIMPODESONERA  := NULL;
                        V_IDCONSDESONVRNF := NULL;
                        V_CDBENEFFISCAL   := NULL;
                        V_IDCONSMVAPMC    := NULL;
                        V_IDCONSCOMPBCPMC := NULL;
                        V_IDCONSDIFALIQ   := NULL;
                        V_CDCOMPST        := NULL;
                        V_CDRESTST        := NULL;
                        V_CDNATUREC       := NULL;
                        V_VRIMPOSTOREP    := NULL;
                        V_IDNUTREDBCFECP  := NULL;
                     END;
                  END;
                  BEGIN
                     SELECT DECODE(COUNT(MSGFISCNCM.CDREGRAFISC), 0, 'N', 'S')
                       INTO V_IDMENSAGEM
                       FROM MSGFISCNCM
                      WHERE CDREGRAFISC = V_CDREGRAFISC
                        AND CDIMPOSTO = C_REGRAFISCAL.CDIMPOSTO
                        AND CDCLASFISC = V_CDCLASFISC
                        AND NRSEQDADOSFISC = V_NRSEQDADOSFISC;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN BEGIN
                        V_IDMENSAGEM      := 'N';
                     END;
                  END;

               END IF;

               IF V_CDFISCAL IS NULL THEN
                  SELECT NVL(MAX(VRQTDEMB), NULL) VRQTDEMB,
                         NVL(MAX(SGUNIDEMB), NULL) SGUNIDEMB
                    INTO V_VRQTDEMB, V_SGUNIDEMB
                    FROM PRODUTO
                   WHERE CDARVPROD = P_CDARVPROD;

                  SELECT NVL(MAX(IDUTRANGEMB), NULL) IDUTRANGEMB
                    INTO V_UTILRANGE
                    FROM IMPOSTO
                   WHERE CDIMPOSTO = C_REGRAFISCAL.CDIMPOSTO;

                  BEGIN
                     SELECT
                        CDFISCAL,
                        CDSITUTRIB,
                        VRALIQUOTA,
                        IDMODBCNFE,
                        IDMODBCSTNFE,
                        IDMOTIDESSTNFE,
                        CDTPCRPICOSTNFE,
                        CDNATUBCPCSTNFE,
                        VRINIFAIXA,
                        VRFIMFAIXA,
                        VRPARDEDUZ,
                        VR1PERREDBC,
                        VRPERREDBC,
                        VRPBCSUBTRIB,
                        IDTRATIMPST,
                        IDPMCBASECA,
                        IDCSTINTEREST,
                        IDCSTINTERNA,
                        IDTIPODESTFISC,
                        CDDETALHEFISC,
                        TO_CHAR('PAD'),
                        NRSEQDADOSFISC,
                        '',
                        '',
                        CDFISCOPER,
                        IDDIFEALIQ,
                        IDDIFALIQCUSPR,
                        CDENQLEGALIPI,
                        CDCEST,
                        VRADICALIQFECP,
                        IDIMPODESONERA,
                        IDCONSDESONVRNF,
                        CDBENEFFISCAL,
                        IDCONSMVAPMC,
                        IDCONSCOMPBCPMC,
                        IDCONSDIFALIQ,
                        CDCOMPST,
                        CDRESTST,
                        CDNATUREC,
                        VRIMPOSTOREP,
                        IDNUTREDBCFECP
                     INTO
                        V_CDFISCAL,
                        V_CDSITUTRIB,
                        V_VRALIQUOTA,
                        V_IDMODBCNFE,
                        V_IDMODBCSTNFE,
                        V_IDMOTIDESSTNFE,
                        V_CDTPCRPICOSTNFE,
                        V_CDNATUBCPCSTNFE,
                        V_VRINIFAIXA,
                        V_VRFIMFAIXA,
                        V_VRPARDEDUZ,
                        V_VR1PERREDBC,
                        V_VRPERREDBC,
                        V_VRPBCSUBTRIB,
                        V_IDTRATIMPST,
                        V_IDPMCBASECA,
                        V_IDCSTINTEREST,
                        V_IDCSTINTERNA,
                        V_IDTIPODESTFISC,
                        V_CDDETALHEFISC,
                        V_IDTIPOREGISTRO,
                        V_NRSEQDADOSFISC,
                        V_CDCLASFISC,
                        V_CDPRODUTO,
                        V_CDFISCOPER,
                        V_IDDIFEALIQ,
                        V_IDDIFALIQCUSPR,
                        V_CDENQLEGALIPI,
                        V_CDCEST,
                        V_VRADICALIQFECP,
                        V_IDIMPODESONERA,
                        V_IDCONSDESONVRNF,
                        V_CDBENEFFISCAL,
                        V_IDCONSMVAPMC,
                        V_IDCONSCOMPBCPMC,
                        V_IDCONSDIFALIQ,
                        V_CDCOMPST,
                        V_CDRESTST,
                        V_CDNATUREC,
                        V_VRIMPOSTOREP,
                        V_IDNUTREDBCFECP
                       FROM DADOSFISCREGRA
                     WHERE DADOSFISCREGRA.CDREGRAFISC = V_CDREGRAFISC
                        AND DADOSFISCREGRA.CDIMPOSTO = C_REGRAFISCAL.CDIMPOSTO
                        AND (IDCSTINTEREST = V_STATUSESTADUAL OR IDCSTINTEREST = 'T')
                        AND (IDCSTINTERNA  = V_STATUSINTERNACIONAL OR IDCSTINTERNA = 'T')
                        AND (((CDPAISORIG = P_PAISORIG) AND (SGESTAORIG = P_ESTAORIG)) OR  V_UTUFORIG = 'N' OR IDTIPOORIGFISC = 'T')
                        AND (((CDPAISDEST = P_PAISDEST)  AND (SGESTADEST = P_ESTADEST)) OR V_UTUFDEST = 'N' OR IDTIPODESTFISC = 'T')
                        AND (((CDMUNIORIG = P_MUNIORIG  AND V_UTMUNIORIG = 'O') OR (IDTIPOORIGFISC = 'T') OR ((CDMUNIORIG IS NULL) AND  (V_UTMUNIORIG = 'D'))) AND
                             ((CDMUNIDEST = P_MUNIDEST AND V_UTMUNIORIG = 'D') OR (IDTIPODESTFISC = 'T') OR ((CDMUNIDEST IS NULL) AND  (V_UTMUNIORIG = 'O'))) OR
                             (V_UTMUNIORIG = 'N'))
                        AND ((VRINIFAIXA  <= P_BASECALC) AND (VRFIMFAIXA  >= P_BASECALC) OR  V_UTFAIXVLR = 'N')
                        AND (((V_VRQTDEMB IS NULL) AND (V_UTILRANGE = 'S')) OR (V_UTILRANGE <> 'S'))
                        AND (NVL(IDORIGEMPRODIMP, 'N') = NVL(V_IDORIGEMPROD, 'N'));
                  EXCEPTION
                     WHEN TOO_MANY_ROWS THEN BEGIN
                        RAISE_APPLICATION_ERROR(-20000, 'Operacao Bloqueada! Existe cadastro duplicado na regra fiscal ' || V_CDREGRAFISC || ' (imposto ' || C_REGRAFISCAL.CDIMPOSTO || ', pagina "Padrao").', TRUE);
                     END;
                     WHEN NO_DATA_FOUND THEN BEGIN
                        V_CDFISCAL        := NULL;
                        V_CDSITUTRIB      := NULL;
                        V_VRALIQUOTA      := NULL;
                        V_IDMODBCNFE      := NULL;
                        V_IDMODBCSTNFE    := NULL;
                        V_IDMOTIDESSTNFE  := NULL;
                        V_CDTPCRPICOSTNFE := NULL;
                        V_CDNATUBCPCSTNFE := NULL;
                        V_VRINIFAIXA      := NULL;
                        V_VRFIMFAIXA      := NULL;
                        V_VRPARDEDUZ      := NULL;
                        V_VR1PERREDBC     := NULL;
                        V_VRPERREDBC      := NULL;
                        V_VRPBCSUBTRIB    := NULL;
                        V_IDTRATIMPST     := NULL;
                        V_IDPMCBASECA     := NULL;
                        V_IDCSTINTEREST   := NULL;
                        V_IDCSTINTERNA    := NULL;
                        V_IDTIPODESTFISC  := NULL;
                        V_CDDETALHEFISC   := NULL;
                        V_IDTIPOREGISTRO  := NULL;
                        V_NRSEQDADOSFISC  := NULL;
                        V_CDCLASFISC      := NULL;
                        V_CDPRODUTO       := NULL;
                        V_CDENQLEGALIPI   := NULL;
                        V_CDCEST          := NULL;
                        V_VRADICALIQFECP  := NULL;
                        V_IDIMPODESONERA  := NULL;
                        V_IDCONSDESONVRNF := NULL;
                        V_CDBENEFFISCAL   := NULL;
                        V_IDCONSMVAPMC    := NULL;
                        V_IDCONSCOMPBCPMC := NULL;
                        V_IDCONSDIFALIQ   := NULL;
                        V_CDCOMPST        := NULL;
                        V_CDRESTST        := NULL;
                        V_CDNATUREC       := NULL;
                        V_VRIMPOSTOREP    := NULL;
                        V_IDNUTREDBCFECP  := NULL;
                     END;
                  END;
                  BEGIN
                     SELECT DECODE(COUNT(MSGFISCREGRA.CDREGRAFISC), 0, 'N', 'S')
                       INTO V_IDMENSAGEM
                       FROM MSGFISCREGRA
                      WHERE CDREGRAFISC = V_CDREGRAFISC
                        AND CDIMPOSTO = C_REGRAFISCAL.CDIMPOSTO
                        AND NRSEQDADOSFISC = V_NRSEQDADOSFISC;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN BEGIN
                        V_IDMENSAGEM      := 'N';
                     END;
                  END;
               END IF;
            END IF;
         END IF;
         BEGIN
            SELECT NMREGRAFISC, DSMENSREGRAFISC
              INTO V_NMREGRAFISC, V_DSMENSREGRAFISC
              FROM REGRAFISCAL
             WHERE CDREGRAFISC = V_CDREGRAFISC;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_NMREGRAFISC     := NULL;
               V_DSMENSREGRAFISC := NULL;
            END;
         END;
         IF V_CDREGRAFISC IS NOT NULL THEN
            IF V_CDFISCAL        IS NOT NULL OR
               V_CDSITUTRIB      IS NOT NULL OR
               V_VRALIQUOTA      IS NOT NULL OR
               V_IDMODBCNFE      IS NOT NULL OR
               V_IDMODBCSTNFE    IS NOT NULL OR
               V_IDMOTIDESSTNFE  IS NOT NULL OR
               V_CDTPCRPICOSTNFE IS NOT NULL OR
               V_CDNATUBCPCSTNFE IS NOT NULL OR
               V_VRINIFAIXA      IS NOT NULL OR
               V_VRFIMFAIXA      IS NOT NULL OR
               V_VRPARDEDUZ      IS NOT NULL OR
               V_VR1PERREDBC     IS NOT NULL OR
               V_VRPERREDBC      IS NOT NULL OR
               V_VRPBCSUBTRIB    IS NOT NULL OR
               V_IDTRATIMPST     IS NOT NULL OR
               V_IDPMCBASECA     IS NOT NULL OR
               V_IDCSTINTEREST   IS NOT NULL OR
               V_IDCSTINTERNA    IS NOT NULL OR
               V_CDFISCOPER      IS NOT NULL OR
               V_IDCFOPINTEREST  IS NOT NULL OR
               V_IDDIFEALIQ      IS NOT NULL OR
               V_IDDIFALIQCUSPR  IS NOT NULL OR
               V_IDTIPODESTFISC  IS NOT NULL OR
               V_CDDETALHEFISC   IS NOT NULL OR
               V_IDTIPOREGISTRO  IS NOT NULL OR
               V_NRSEQDADOSFISC  IS NOT NULL OR
               V_IDMENSAGEM      IS NOT NULL OR
               V_CDCLASFISC      IS NOT NULL OR
               V_CDPRODUTO       IS NOT NULL OR
               V_CDENQLEGALIPI   IS NOT NULL OR
               V_CDCEST          IS NOT NULL OR
               V_VRADICALIQFECP  IS NOT NULL OR
               V_IDIMPODESONERA  IS NOT NULL OR
               V_IDCONSDESONVRNF IS NOT NULL OR
               V_CDBENEFFISCAL   IS NOT NULL OR
               V_IDCONSMVAPMC    IS NOT NULL OR
               V_IDCONSCOMPBCPMC IS NOT NULL OR
               V_IDCONSDIFALIQ   IS NOT NULL OR
               V_CDCOMPST        IS NOT NULL OR
               V_CDRESTST        IS NOT NULL OR
               V_CDNATUREC       IS NOT NULL OR
               V_VRIMPOSTOREP    IS NOT NULL OR
               V_IDNUTREDBCFECP  IS NOT NULL THEN
               OUT_RECORD.CDREGRAFISC        := V_CDREGRAFISC;
               OUT_RECORD.NMREGRAFISC        := V_NMREGRAFISC;
               OUT_RECORD.DSMENSREGRAFISC    := V_DSMENSREGRAFISC;
               OUT_RECORD.CDIMPOSTO          := C_REGRAFISCAL.CDIMPOSTO;
               OUT_RECORD.CDFISCAL           := V_CDFISCAL;
               OUT_RECORD.CDSITUTRIB         := V_CDSITUTRIB;
               OUT_RECORD.VRALIQUOTA         := V_VRALIQUOTA;
               OUT_RECORD.IDMODBCNFE         := V_IDMODBCNFE;
               OUT_RECORD.IDMODBCSTNFE       := V_IDMODBCSTNFE;
               OUT_RECORD.IDMOTIDESSTNFE     := V_IDMOTIDESSTNFE;
               OUT_RECORD.CDTPCRPICOSTNFE    := V_CDTPCRPICOSTNFE;
               OUT_RECORD.CDNATUBCPCSTNFE    := V_CDNATUBCPCSTNFE;
               OUT_RECORD.VRINIFAIXA         := V_VRINIFAIXA;
               OUT_RECORD.VRFIMFAIXA         := V_VRFIMFAIXA;
               OUT_RECORD.VRPARDEDUZ         := V_VRPARDEDUZ;
               OUT_RECORD.VR1PERREDBC        := V_VR1PERREDBC;
               OUT_RECORD.VRPERREDBC         := V_VRPERREDBC;
               OUT_RECORD.VRPBCSUBTRIB       := V_VRPBCSUBTRIB;
               OUT_RECORD.IDTRATIMPST        := V_IDTRATIMPST;
               OUT_RECORD.IDPMCBASECA        := V_IDPMCBASECA;
               OUT_RECORD.IDCSTINTEREST      := V_IDCSTINTEREST;
               OUT_RECORD.IDCSTINTERNA       := V_IDCSTINTERNA;
               OUT_RECORD.CDFISCOPER         := V_CDFISCOPER;
               OUT_RECORD.IDCFOPINTEREST     := V_IDCFOPINTEREST;
               OUT_RECORD.IDDIFEALIQ         := V_IDDIFEALIQ;
               OUT_RECORD.IDDIFALIQCUSPR     := V_IDDIFALIQCUSPR;
               OUT_RECORD.IDTIPODESTFISC     := V_IDTIPODESTFISC;
               OUT_RECORD.CDDETALHEFISC      := V_CDDETALHEFISC;
               OUT_RECORD.IDTIPOREGISTRO     := V_IDTIPOREGISTRO;
               OUT_RECORD.NRSEQDADOSFISC     := V_NRSEQDADOSFISC;
               OUT_RECORD.IDMENSAGEM         := V_IDMENSAGEM;
               OUT_RECORD.CDCLASFISC         := V_CDCLASFISC;
               OUT_RECORD.CDPRODUTO          := V_CDPRODUTO;
               OUT_RECORD.CDENQLEGALIPI      := V_CDENQLEGALIPI;
               OUT_RECORD.CDCEST             := V_CDCEST;
               OUT_RECORD.VRADICALIQFECP     := V_VRADICALIQFECP;
               OUT_RECORD.IDIMPODESONERA     := V_IDIMPODESONERA;
               OUT_RECORD.IDCONSDESONVRNF    := V_IDCONSDESONVRNF;
               OUT_RECORD.CDBENEFFISCAL      := V_CDBENEFFISCAL;
               OUT_RECORD.IDCONSMVAPMC       := V_IDCONSMVAPMC;
               OUT_RECORD.IDCONSCOMPBCPMC    := V_IDCONSCOMPBCPMC;
               OUT_RECORD.IDCONSDIFALIQ      := V_IDCONSDIFALIQ;
               OUT_RECORD.CDCOMPST           := V_CDCOMPST;
               OUT_RECORD.CDRESTST           := V_CDRESTST;
               OUT_RECORD.CDNATUREC          := V_CDNATUREC;
               OUT_RECORD.VRIMPOSTOREP       := V_VRIMPOSTOREP;
               OUT_RECORD.IDNUTREDBCFECP     := V_IDNUTREDBCFECP;
               PIPE ROW (OUT_RECORD);
            END IF;
         END IF;
      END LOOP;
   END IF;
END;
/