create or replace
PROCEDURE CONSULTA_CST_NFE( 
   P_ENTSAICST     IN CODSITUTRIB.IDENTSAICST%TYPE, 
   P_EMPRESA       IN CODSITUTRIB.CDEMPRESA%TYPE, 
   P_CDARVPROD     IN PRODUTO.CDARVPROD%TYPE, 
   P_INSCESTA      IN CODSITUTRIB.CDINSCESTA%TYPE, 
   P_TIPOOPER      IN CODSITUTRIB.CDTIPOOPER%TYPE, 
   P_CDGRUPOFISC   IN CODSITUTRIB.CDGRUPOFISC%TYPE, 
   P_CDIMPOSTO     IN IMPOSTO.CDIMPOSTO%TYPE,
   P_IDCSTINTEREST IN CODSITUTRIB.IDCSTINTEREST%TYPE, 
   P_IDCSTINTERNA  IN CODSITUTRIB.IDCSTINTERNA%TYPE, 
   P_FILIAL        IN FILIAL.CDFILIAL%TYPE, 
   P_LANCTONF      IN NOTAFISCAL.NRLANCTONF%TYPE,
   P_CDCLIENTE     IN NOTAFISCAL.CDCLIENTE%TYPE,
   P_CDSITTRIBTMP  OUT NoCopy VARCHAR2, 
   P_IDMODBCNFE    OUT NoCopy VARCHAR2, 
   P_IDMODBCSTNFE  OUT NoCopy VARCHAR2, 
   P_CDTPCRPICOSTNFE OUT NoCopy VARCHAR2, 
   P_CDNATUBCPCSTNFE OUT NoCopy VARCHAR2, 
   P_IDMOTIDESSTNFE  OUT NoCopy VARCHAR2,
   P_IDORIGEMPROD  IN VARCHAR2 DEFAULT 'N' 
) 
IS 
   V_SEQUENCIA     VARCHAR2(02); 
   V_CST1DIGT      VARCHAR2(1); 
   V_CDARVPROD     VARCHAR2(20); 
   V_ORIGDESTOP    VARCHAR2(1); 
   V_EMPRESA       VARCHAR2(02); 
   V_INSCESTA      VARCHAR2(16); 
   V_CONTADOR      NUMBER; 
   V_CONTINTEREST  NUMBER; 
   V_CONTINTERNA   NUMBER; 
   w_E             CHAR(1) := 'E'; 
   V_CDSITUTRIBNFE CODSITUTRIBNFE.CDSITUTRIBNFE%TYPE; 
   V_IDMODBCNFE    CODSITUTRIBNFE.IDMODBCNFE%TYPE; 
   V_IDMODBCSTNFE  CODSITUTRIBNFE.IDMODBCSTNFE%TYPE; 
   V_CDTPCRPICOSTNFE CODSITUTRIBNFE.CDTPCRPICOSTNFE%TYPE; 
   V_CDNATUBCPCSTNFE CODSITUTRIBNFE.CDNATUBCPCSTNFE%TYPE; 
   V_IDMOTIDESSTNFE  CODSITUTRIBNFE.IDMOTIDESSTNFE%TYPE; 
 
   V_INIVALID       DATE; 
   V_PAISORIG       VARCHAR2(04);  
   V_ESTAORIG       VARCHAR2(02);  
   V_MUNIORIG       VARCHAR2(04);  
   V_PAISDEST       VARCHAR2(04); 
   V_ESTADEST       VARCHAR2(02); 
   V_MUNIDEST       VARCHAR2(04); 
   V_IDENDCFOP      VARCHAR2(01); 
   V_IDORIGDESTNF   VARCHAR2(01); 
   V_CDCLIENTE      CLIENTE.CDCLIENTE%TYPE; 
   V_CDORIGDESTNF VARCHAR2(21); 
   V_IDENDEMIS      VARCHAR2(01); 
   V_CDEMPRESA      VARCHAR2(02); 
   V_CDINSCESTA     VARCHAR2(16); 
   V_CDFILIRECE     VARCHAR2(04); 
   V_CDFORNECED     FORNECEDOR.CDFORNECED%TYPE; 
   V_CDFILIEMIT     VARCHAR2(04); 
   V_VRITEM         FLOAT; 
   V_CDREGRA        VARCHAR2(10); 
   V_CDGRUPOFISC    VARCHAR2(03); 
   V_IDNFPRESTSERV  VARCHAR2(01);
   V_IDUTLOCPRESTSERV VARCHAR2(01);
   V_ESTAPRESTSERV    VARCHAR2(02);
   V_MUNIPRESTSERV    VARCHAR2(04);
   V_CDPAISFCT      VARCHAR2(04);
   V_SGESTADOFCT    VARCHAR2(02);
   V_CDMUNICIPIOFCT VARCHAR2(04);
   V_QTDREGFCT      FLOAT;
   V_IDPRESENCIAL   VARCHAR2(01);
   V_IDOPPRESENDDIF VARCHAR2(01);
   V_IDENDEMISTPOPFIL VARCHAR2(01);
BEGIN
   V_CDSITUTRIBNFE := NULL;
   V_IDMODBCNFE    := NULL;
   V_IDMODBCSTNFE  := NULL;
   V_IDMOTIDESSTNFE := NULL;
   V_SEQUENCIA     := '01';
   V_CONTINTEREST  := 0;
   V_CONTINTERNA   := 0;
   V_CONTADOR      := 0;

   Begin
      SELECT N.DTENTRSAID,   N.CDFILIEMIT,   N.IDORIGDESTNF,   N.CDCLIENTE,
             N.CDFILIRECE,   N.CDFORNECED, (IT.VRTOTAITEM - IT.VRDESCITEM), NVL(N.IDPRESENCIAL, '9'),
             N.SGESTADO,     N.CDMUNICIPIO, NVL(R.IDOPPRESENDDIF, 'N')
        INTO V_INIVALID,   V_CDFILIEMIT, V_IDORIGDESTNF, V_CDCLIENTE,
             V_CDFILIRECE, V_CDFORNECED, V_VRITEM, V_IDPRESENCIAL,
             V_ESTAPRESTSERV, V_MUNIPRESTSERV, V_IDOPPRESENDDIF
      FROM NOTAFISCAL N, ITEMPRODUTO IT, PRODUTO P, PARAMGERAL R
      WHERE N.CDFILIAL = P_FILIAL
        AND N.NRLANCTONF = P_LANCTONF
        AND N.CDFILIAL = IT.CDFILIAL
        AND N.NRLANCTONF = IT.NRLANCTONF
        AND IT.CDPRODUTO = P.CDPRODUTO
        AND P.CDARVPROD  = P_CDARVPROD
        AND ROWNUM = '1'
        AND R.CDPARGERAL = '0001';
   Exception
      When No_Data_Found Then
         V_INIVALID  := Null; V_CDFILIEMIT  := Null; V_IDORIGDESTNF:= Null; V_CDCLIENTE:= Null;
         V_CDFILIRECE := Null; V_CDFORNECED:= Null; V_VRITEM := 0; V_IDPRESENCIAL := '9';
         V_ESTAPRESTSERV := NULL; V_MUNIPRESTSERV := NULL; V_IDOPPRESENDDIF := 'N';
   End;
   IF P_CDCLIENTE IS NOT NULL THEN
      V_CDCLIENTE := P_CDCLIENTE;
   END IF;
   IF (P_ENTSAICST = 'S') THEN 
      Begin 
         SELECT NVL(MAX(IDENDCFOPFIL),'I'), MAX(CDEMPRESA),   MAX(CDINSCESTA) 
         INTO   V_IDENDCFOP,           V_CDEMPRESA, V_CDINSCESTA 
         FROM FILIAL 
         WHERE (CDFILIAL = V_CDFILIEMIT); 
      Exception 
         When No_Data_Found Then 
            V_IDENDCFOP:= 'I'; V_CDEMPRESA:= Null; V_CDINSCESTA:= Null; 
      End; 

      BEGIN
         SELECT NVL(IDENDEMISTPOPFIL, 'P')
           INTO V_IDENDEMISTPOPFIL
           FROM TIPOOPERACFIL
          WHERE IDENTRSAIDOP = 'S'
            AND CDTIPOOPER = P_TIPOOPER
            AND CDFILIAL = V_CDFILIEMIT;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_IDENDEMISTPOPFIL := 'P';
      END;

      IF V_IDENDEMISTPOPFIL <> 'P' THEN
         IF V_IDENDEMISTPOPFIL = 'I' THEN 
            BEGIN 
               SELECT MAX(CDPAIS),
                      MAX(SGESTADO),
                      MAX(CDMUNICIPIO) 
                 INTO V_PAISORIG,
                      V_ESTAORIG,
                      V_MUNIORIG 
                 FROM INSCRESTAD 
                WHERE CDEMPRESA  = P_EMPRESA 
                  AND CDINSCESTA = P_INSCESTA; 
            EXCEPTION 
               WHEN NO_DATA_FOUND THEN
                  V_PAISORIG := NULL;
                  V_ESTAORIG := NULL; 
                  V_MUNIORIG := NULL; 
            END; 
         ELSE 
            BEGIN 
               SELECT MAX(CDPAIS),
                      MAX(SGESTADO),
                      MAX(CDMUNICIPIO) 
                 INTO V_PAISORIG,
                      V_ESTAORIG,
                      V_MUNIORIG 
                 FROM ENDEFILI 
                WHERE CDFILIAL     = V_CDFILIEMIT
                  AND IDTPENDEFILI = V_IDENDEMISTPOPFIL; 
            EXCEPTION 
               WHEN NO_DATA_FOUND THEN
                  V_PAISORIG := NULL;
                  V_ESTAORIG := NULL; 
                  V_MUNIORIG := NULL; 
            END; 
         END IF;
      ELSE
         IF V_IDENDCFOP = 'I' THEN 
            Begin 
               SELECT MAX(CDPAIS)    , MAX(SGESTADO)  , MAX(CDMUNICIPIO) 
               INTO   V_PAISORIG, V_ESTAORIG, V_MUNIORIG 
               FROM INSCRESTAD 
               WHERE CDEMPRESA  = P_EMPRESA AND 
                     CDINSCESTA = P_INSCESTA; 
            Exception 
               When No_Data_Found Then 
                  V_PAISORIG := Null; V_ESTAORIG := Null; V_MUNIORIG := Null; 
            End; 
         ELSE 
            Begin 
               SELECT MAX(CDPAIS)    , MAX(SGESTADO)  , MAX(CDMUNICIPIO) 
               INTO   V_PAISORIG, V_ESTAORIG, V_MUNIORIG 
               FROM ENDEFILI 
               WHERE (CDFILIAL     = V_CDFILIEMIT) AND 
                     (IDTPENDEFILI = V_IDENDCFOP); 
            Exception 
               When No_Data_Found Then 
                  V_PAISORIG := Null; V_ESTAORIG := Null; V_MUNIORIG := Null; 
            End; 
         END IF;
      END IF;

      IF V_IDORIGDESTNF IS NULL THEN
         Begin
            SELECT MAX(IDORIGDESTOP)
            INTO   V_ORIGDESTOP
            FROM TIPOOPERAC
            WHERE IDENTRSAIDOP = 'S' AND 
                  CDTIPOOPER   = P_TIPOOPER; 
         Exception 
            When No_Data_Found Then 
               V_ORIGDESTOP := Null; 
         End; 
      ELSE 
         V_ORIGDESTOP := V_IDORIGDESTNF; 
      END IF; 
 
      IF V_ORIGDESTOP = 'L' THEN  
         V_CDORIGDESTNF := V_CDFILIRECE; 
 
         Begin 
            SELECT NVL(MAX(IDENDCFOPFIL),'I'), NVL(MAX(IDENDEMISFIL),'I'), MAX(CDEMPRESA)  , MAX(CDINSCESTA) 
            INTO   V_IDENDCFOP          , V_IDENDEMIS          , V_CDEMPRESA, V_CDINSCESTA 
            FROM FILIAL 
            WHERE (CDFILIAL = V_CDFILIRECE); 
         Exception 
            When No_Data_Found Then 
               V_IDENDCFOP := 'I'; V_IDENDEMIS := 'I'; V_CDEMPRESA := Null; V_CDINSCESTA := Null; 
         End; 
 
         IF V_IDENDEMIS = 'I' THEN 
            Begin 
               SELECT MAX(CDPAIS)    , MAX(SGESTADO)  , MAX(CDMUNICIPIO) 
               INTO   V_PAISDEST,    V_ESTADEST, V_MUNIDEST 
               FROM INSCRESTAD 
               WHERE CDEMPRESA  = V_CDEMPRESA AND 
                     CDINSCESTA = V_CDINSCESTA; 
            Exception 
               When No_Data_Found Then 
                  V_PAISDEST := Null; V_ESTADEST := Null; V_MUNIDEST := Null; 
            End; 
         ELSE 
            Begin 
               SELECT MAX(CDPAIS)    , MAX(SGESTADO)  , MAX(CDMUNICIPIO) 
               INTO   V_PAISDEST, V_ESTADEST, V_MUNIDEST 
               FROM ENDEFILI 
               WHERE CDFILIAL     = V_CDFILIRECE AND 
                     IDTPENDEFILI = V_IDENDEMIS; 
            Exception 
               When No_Data_Found Then 
                  V_PAISDEST := Null; V_ESTADEST := Null; V_MUNIDEST := Null; 
            End; 
         END IF; 
      ELSIF V_ORIGDESTOP = 'F' THEN  
         V_CDORIGDESTNF := V_CDFORNECED; 
         Begin 
            SELECT NVL(MAX(IDENDCFOPFOR),'P'), NVL(MAX(IDENDEMISFOR),'P') 
            INTO   V_IDENDCFOP          , V_IDENDEMIS 
            FROM FORNECEDOR 
            WHERE (CDFORNECED = V_CDORIGDESTNF); 
         Exception 
            When No_Data_Found Then 
               V_IDENDCFOP := 'P'; V_IDENDEMIS := 'P'; 
         End; 
 
         Begin 
            SELECT MAX(CDPAIS),     MAX(SGESTADO),   MAX(CDMUNICIPIO) 
            INTO   V_PAISDEST, V_ESTADEST, V_MUNIDEST 
            FROM ENDEFORN 
            WHERE (CDFORNECED   = V_CDORIGDESTNF) AND 
                  (IDTPENDEFORN = V_IDENDEMIS); 
         Exception 
            When No_Data_Found Then 
               V_PAISDEST := Null; V_ESTADEST := Null; V_MUNIDEST := Null; 
         End; 
      ELSE  
         V_CDORIGDESTNF := V_CDCLIENTE; 
         Begin 
            SELECT NVL(MAX(IDENDCFOPCLI),'P'), NVL(MAX(IDENDEMISCLI),'P') 
            INTO   V_IDENDCFOP          , V_IDENDEMIS 
            FROM CLIENTE 
            WHERE (CDCLIENTE = V_CDORIGDESTNF); 
         Exception 
            When No_Data_Found Then 
               V_IDENDCFOP := 'P'; V_IDENDEMIS := 'P'; 
         End; 
 
         IF V_IDENDCFOP = 'I' THEN 
            Begin 
               SELECT MAX(CDPAIS)    , MAX(SGESTADO), MAX(CDMUNICIPIO) 
               INTO   V_PAISDEST, V_ESTADEST, V_MUNIDEST 
               FROM INSCRESTAD 
               WHERE CDEMPRESA  = P_EMPRESA AND 
                     CDINSCESTA = P_INSCESTA; 
            Exception 
               When No_Data_Found Then 
                  V_PAISDEST := Null; V_ESTADEST := Null; V_MUNIDEST := Null; 
            End; 
         ELSE 
 
            Begin 
               SELECT MAX(CDPAIS)    , MAX(SGESTADO)  , MAX(CDMUNICIPIO) 
               INTO   V_PAISDEST, V_ESTADEST, V_MUNIDEST 
               FROM ENDECLIE 
               WHERE (CDCLIENTE    = V_CDORIGDESTNF) AND 
                     (IDTPENDECLIE = V_IDENDEMIS); 
            Exception 
               When No_Data_Found Then 
                  V_PAISDEST := Null; V_ESTADEST := Null; V_MUNIDEST := Null; 
            End; 
         END IF; 
      END IF; 
      /* 
      ROTINA PARA VERIFICAR AS NOTAS DO SISTEMA FACILITIES. NESTAS NOTAS O SISTEMA 
      NAO VERIFICARA O PAIS, O ESTADO E NEM O MUNICIPIO DO DESTINATARIO DA NOTA FISCAL 
      E SIM DO LOCAL DE ATENDIMENTO DAQUELA PRESTACAO DE SERVICO
      */
      V_QTDREGFCT := 0;
      BEGIN
         SELECT CDPAIS, 
                SGESTADO, 
                CDMUNICIPIO,
                COUNT(*)
           INTO V_CDPAISFCT,
                V_SGESTADOFCT,
                V_CDMUNICIPIOFCT,
                V_QTDREGFCT
           FROM FCT_DADOSNF
          WHERE CDFILIAL = P_FILIAL
            AND NRLANCTONF = P_LANCTONF
          GROUP BY CDPAIS, 
                SGESTADO, 
                CDMUNICIPIO;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CDPAISFCT := NULL;
            V_SGESTADOFCT := NULL;
            V_CDMUNICIPIOFCT := NULL;
            V_QTDREGFCT := 0;
      END;
      IF (V_QTDREGFCT > 0) THEN
         V_PAISDEST := V_CDPAISFCT;
         V_ESTADEST := V_SGESTADOFCT;
         V_MUNIDEST := V_CDMUNICIPIOFCT;
      END IF;
       /*
      QUANDO A OPERACAO DA NOTA FISCAL FOR PRESENCIAL O SISTEMA USARA COMO ENDERECO
      DE DESTINO O MESMO ENDERECO DA ORIGEM. ESTA CONDICAO FOI NECESSARIA PARA ATENDER 
      A NOTA TECNICA 2015/003 e Se considera endereco de destino igual ao endereco de origem
      */
      IF (V_IDPRESENCIAL = '1') AND (V_IDOPPRESENDDIF = 'N') THEN
         V_PAISDEST := V_PAISORIG;
         V_ESTADEST := V_ESTAORIG;
         V_MUNIDEST := V_MUNIORIG;
      END IF;
   END IF; 
 
   IF (P_ENTSAICST = 'E') THEN 
      Begin 
         SELECT NVL(MAX(IDENDCFOPFIL),'I'), MAX(CDEMPRESA),   MAX(CDINSCESTA) 
         INTO   V_IDENDCFOP,           V_CDEMPRESA, V_CDINSCESTA 
         FROM FILIAL 
         WHERE (CDFILIAL = V_CDFILIRECE); 
      Exception 
         When No_Data_Found Then 
            V_IDENDCFOP:= 'I'; V_CDEMPRESA:= Null; V_CDINSCESTA:= Null; 
      End; 
 
      Begin 
         SELECT MAX(CDPAIS)    , MAX(SGESTADO)  , MAX(CDMUNICIPIO) 
         INTO   V_PAISDEST, V_ESTADEST, V_MUNIDEST 
         FROM INSCRESTAD 
         WHERE CDEMPRESA  = P_EMPRESA AND 
               CDINSCESTA = P_INSCESTA; 
      Exception 
         When No_Data_Found Then 
            V_PAISDEST := Null; V_ESTADEST := Null; V_MUNIDEST := Null; 
      End; 
 
      BEGIN
         SELECT NVL(MAX(IDNFPRESTSERV), 'N'), NVL(MAX(IDUTLOCPRESTSERV), 'N')
           INTO V_IDNFPRESTSERV, V_IDUTLOCPRESTSERV
           FROM TIPOOPERAC
          WHERE IDENTRSAIDOP = 'E'
            AND CDTIPOOPER   = P_TIPOOPER;
          EXCEPTION
             WHEN NO_DATA_FOUND Then
                V_IDNFPRESTSERV := 'N'; V_IDUTLOCPRESTSERV := 'N';
      END; 
 
      IF (V_IDNFPRESTSERV = 'S') THEN 
         BEGIN 
            SELECT MAX(CDPAIS), MAX(SGESTADO), MAX(CDMUNICIPIO) 
              INTO V_PAISDEST, V_ESTADEST, V_MUNIDEST 
              FROM ENDEFILI 
             WHERE (CDFILIAL     = V_CDFILIRECE) AND 
                   (IDTPENDEFILI = 'P'); 
         EXCEPTION 
            WHEN NO_DATA_FOUND THEN 
               V_PAISDEST := NULL; V_ESTADEST := NULL; V_MUNIDEST := NULL; 
         END; 
      END IF; 
 
      IF (V_IDNFPRESTSERV = 'S') AND (V_IDUTLOCPRESTSERV = 'S') THEN
         V_ESTADEST := NVL(V_ESTAPRESTSERV, V_ESTADEST);
         V_MUNIDEST := NVL(V_MUNIPRESTSERV, V_MUNIDEST);
      END IF;

      IF V_IDORIGDESTNF IS NULL THEN
         Begin
            SELECT NVL(MAX(IDORIGDESTOP), 'F')
            INTO   V_ORIGDESTOP
            FROM TIPOOPERAC
            WHERE IDENTRSAIDOP = 'E' AND
                 CDTIPOOPER   = P_TIPOOPER;
         Exception
            When No_Data_Found Then
               V_ORIGDESTOP := Null;
         End;
      ELSE
        V_ORIGDESTOP := V_IDORIGDESTNF;
      END IF;
 
      IF V_ORIGDESTOP = 'L' THEN  
         V_CDORIGDESTNF := V_CDFILIEMIT; 
 
         Begin 
            SELECT NVL(MAX(IDENDCFOPFIL),'I'), NVL(MAX(IDENDEMISFIL),'I'), MAX(CDEMPRESA)  , MAX(CDINSCESTA) 
            INTO   V_IDENDCFOP          , V_IDENDEMIS          , V_CDEMPRESA, V_CDINSCESTA 
            FROM FILIAL 
            WHERE (CDFILIAL = V_CDFILIEMIT); 
         Exception 
            When No_Data_Found Then 
               V_IDENDCFOP := 'I'; V_IDENDEMIS := 'I'; V_CDEMPRESA := Null; V_CDINSCESTA := Null; 
         End; 
 
         IF V_IDENDEMIS = 'I' THEN 
            Begin 
               SELECT MAX(CDPAIS)    , MAX(SGESTADO)  , MAX(CDMUNICIPIO) 
               INTO   V_PAISORIG, V_ESTAORIG, V_MUNIORIG 
               FROM INSCRESTAD 
               WHERE CDEMPRESA  = V_CDEMPRESA AND 
                     CDINSCESTA = V_CDINSCESTA; 
            Exception 
               When No_Data_Found Then 
                  V_PAISORIG := Null; V_ESTAORIG := Null; V_MUNIORIG := Null; 
            End; 
         ELSE 
            Begin 
               SELECT MAX(CDPAIS)    , MAX(SGESTADO)  , MAX(CDMUNICIPIO) 
               INTO   V_PAISORIG, V_ESTAORIG, V_MUNIORIG 
               FROM ENDEFILI 
               WHERE CDFILIAL     = V_CDFILIEMIT AND 
                     IDTPENDEFILI = V_IDENDEMIS; 
            Exception 
               When No_Data_Found Then 
                  V_PAISORIG := Null; V_ESTAORIG := Null; V_MUNIORIG := Null; 
            End; 
         END IF; 
      ELSIF V_ORIGDESTOP = 'F' THEN  
         V_CDORIGDESTNF := V_CDFORNECED; 
         Begin 
            SELECT NVL(MAX(IDENDCFOPFOR),'P'), NVL(MAX(IDENDEMISFOR),'P') 
            INTO   V_IDENDCFOP          , V_IDENDEMIS 
            FROM FORNECEDOR 
            WHERE (CDFORNECED = V_CDORIGDESTNF); 
         Exception 
            When No_Data_Found Then 
               V_IDENDCFOP := 'P'; V_IDENDEMIS := 'P'; 
         End; 
 
         Begin 
            SELECT MAX(CDPAIS)    , MAX(SGESTADO)  , MAX(CDMUNICIPIO) 
            INTO   V_PAISORIG, V_ESTAORIG, V_MUNIORIG 
            FROM ENDEFORN 
            WHERE (CDFORNECED   = V_CDORIGDESTNF) AND 
                  (IDTPENDEFORN = V_IDENDCFOP); 
         Exception 
            When No_Data_Found Then 
               V_PAISORIG := Null; V_ESTAORIG := Null; V_MUNIORIG := Null; 
         End; 
      ELSE  
         V_CDORIGDESTNF := V_CDCLIENTE; 
         Begin 
            SELECT NVL(MAX(IDENDCFOPCLI),'P'), NVL(MAX(IDENDEMISCLI),'P') 
            INTO   V_IDENDCFOP          , V_IDENDEMIS 
            FROM CLIENTE 
            WHERE (CDCLIENTE = V_CDORIGDESTNF); 
         Exception 
            When No_Data_Found Then 
               V_IDENDCFOP := 'P'; V_IDENDEMIS := 'P'; 
         End; 
 
         IF V_IDENDCFOP = 'I' THEN 
            Begin 
               SELECT MAX(CDPAIS)    , MAX(SGESTADO), MAX(CDMUNICIPIO) 
               INTO   V_PAISORIG, V_ESTAORIG, V_MUNIDEST 
               FROM INSCRESTAD 
               WHERE CDEMPRESA  = P_EMPRESA AND 
                     CDINSCESTA = P_INSCESTA; 
            Exception 
               When No_Data_Found Then 
                  V_PAISORIG := Null; V_ESTAORIG := Null; V_MUNIDEST := Null; 
            End; 
         ELSE 
 
            Begin 
               SELECT MAX(CDPAIS)    , MAX(SGESTADO)  , MAX(CDMUNICIPIO) 
               INTO   V_PAISORIG, V_ESTAORIG, V_MUNIORIG 
               FROM ENDECLIE 
               WHERE (CDCLIENTE    = V_CDORIGDESTNF) AND 
                     (IDTPENDECLIE = V_IDENDEMIS); 
            Exception 
               When No_Data_Found Then 
                  V_PAISORIG := Null; V_ESTAORIG := Null; V_MUNIORIG := Null; 
            End; 
         END IF; 
      END IF; 
   END IF; 
 
   V_CDREGRA     := NULL; 
 
   FOR C_REGRA IN (SELECT CDREGRAFISC, CDSITUTRIB, IDMODBCNFE, IDMODBCSTNFE, 
                          CDTPCRPICOSTNFE, CDNATUBCPCSTNFE, IDMOTIDESSTNFE 
                     FROM TABLE(CONSULTA_REGRA_FISCAL(P_CDIMPOSTO, P_EMPRESA, P_CDGRUPOFISC, P_ENTSAICST, P_TIPOOPER, P_INSCESTA, P_CDARVPROD, V_INIVALID, 
                                                      V_PAISORIG, V_ESTAORIG, V_MUNIORIG, V_PAISDEST, V_ESTADEST, V_MUNIDEST, V_VRITEM, 'N', P_IDORIGEMPROD))
                    WHERE VRALIQUOTA IS NOT NULL) LOOP

      V_CDSITUTRIBNFE   := C_REGRA.CDSITUTRIB;
      V_IDMODBCNFE      := C_REGRA.IDMODBCNFE;
      V_IDMODBCSTNFE    := C_REGRA.IDMODBCSTNFE;
      V_CDTPCRPICOSTNFE := C_REGRA.CDTPCRPICOSTNFE;
      V_CDNATUBCPCSTNFE := C_REGRA.CDNATUBCPCSTNFE;
      V_IDMOTIDESSTNFE  := C_REGRA.IDMOTIDESSTNFE;
      V_CDREGRA         := C_REGRA.CDREGRAFISC;
   END LOOP;
 
   IF (V_CDREGRA IS NULL) THEN 
      IF P_IDCSTINTERNA = 'S' THEN 
         IF P_CDARVPROD IS NULL THEN 
            SELECT COUNT(*) 
              INTO V_CONTINTERNA 
              FROM CODSITUTRIB C 
             WHERE (IDENTSAICST     = P_ENTSAICST) 
               AND (CDEMPRESA       = P_EMPRESA) 
               AND (CDTIPOOPER      = P_TIPOOPER) 
               AND (CDINSCESTA      = RTRIM(P_INSCESTA)) 
               AND (C.IDCSTINTEREST = 'N') 
               AND (C.IDCSTINTERNA  = 'S') 
               AND (C.CDPRODUTO IS NULL); 
         ELSE 
            SELECT COUNT(*) 
              INTO V_CONTINTERNA 
              FROM CODSITUTRIB C, PRODUTO P 
             WHERE (IDENTSAICST     = P_ENTSAICST) 
               AND (CDEMPRESA       = P_EMPRESA) 
               AND (CDTIPOOPER      = P_TIPOOPER) 
               AND (CDINSCESTA      = RTRIM(P_INSCESTA)) 
               AND (C.CDPRODUTO     = P.CDPRODUTO(+)) 
               AND (C.IDCSTINTEREST = 'N') 
               AND (C.IDCSTINTERNA  = 'S') 
               AND (INSTR(P_CDARVPROD,NVL(RTRIM(P.CDARVPROD),P_CDARVPROD)) = 1); 
         END IF; 
         IF V_CONTINTERNA > 0 THEN 
            IF (P_CDGRUPOFISC IS NOT NULL) THEN 
               SELECT MAX(P.CDARVPROD), COUNT(*) 
               INTO   V_CDARVPROD, V_CONTADOR 
               FROM   CODSITUTRIB C, PRODUTO P 
               WHERE  (IDENTSAICST     = P_ENTSAICST)       AND 
                      (CDEMPRESA       = P_EMPRESA)         AND 
                      (CDTIPOOPER      = P_TIPOOPER)        AND 
                      (CDINSCESTA      = RTRIM(P_INSCESTA)) AND 
                      (CDGRUPOFISC     = P_CDGRUPOFISC)     AND 
                      (C.CDPRODUTO     = P.CDPRODUTO(+))    AND 
                      (C.IDCSTINTEREST = 'N')               AND 
                      (C.IDCSTINTERNA  = 'S')               AND 
                      (INSTR(P_CDARVPROD,NVL(RTRIM(P.CDARVPROD),P_CDARVPROD)) = 1); 
 
               IF V_CONTADOR > 0 THEN 
                  IF NOT(V_CDARVPROD IS NULL) THEN 
                     FOR C_CST IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                   FROM   CODSITUTRIB C, MENSCST M, PRODUTO P 
                                   WHERE  (C.IDENTSAICST   = P_ENTSAICST)       AND 
                                          (C.CDEMPRESA     = P_EMPRESA)         AND 
                                          (C.CDTIPOOPER    = P_TIPOOPER)        AND 
                                          (C.CDINSCESTA    = RTRIM(P_INSCESTA)) AND 
                                          (C.NRINCIDCST    = M.NRINCIDCST(+))   AND 
                                          (C.CDGRUPOFISC   = P_CDGRUPOFISC)     AND 
                                          (C.CDPRODUTO     = P.CDPRODUTO(+))    AND 
                                          (C.IDCSTINTEREST = 'N')               AND 
                                          (C.IDCSTINTERNA  = 'S')               AND 
                                          (P.CDARVPROD     = V_CDARVPROD)) LOOP 
                         BEGIN 
                           SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                                  DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                                  DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                           INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                                  V_IDMODBCNFE, V_IDMODBCSTNFE 
                           FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                           WHERE  IM.CDIMPOSTO  = CO.CDIMPOSTO     AND 
                                  CO.NRINCIDCST = C_CST.NRINCIDCST AND 
                                  CO.CDIMPOSTO  = P_CDIMPOSTO; 
                         EXCEPTION 
                           WHEN No_Data_Found THEN 
                              V_CDSITUTRIBNFE   := NULL; 
                              V_IDMODBCNFE      := NULL; 
                              V_IDMODBCSTNFE    := NULL; 
                              V_CDTPCRPICOSTNFE := NULL; 
                              V_CDNATUBCPCSTNFE := NULL; 
                              V_IDMOTIDESSTNFE  := NULL; 
                         END; 
                     END LOOP; 
                  ELSIF (V_CDARVPROD IS NULL) THEN 
                     FOR C_CSTNULL IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                       FROM   CODSITUTRIB C, MENSCST M 
                                       WHERE  (C.IDENTSAICST   = P_ENTSAICST)       AND 
                                              (C.CDEMPRESA     = P_EMPRESA)         AND 
                                              (C.CDTIPOOPER    = P_TIPOOPER)        AND 
                                              (C.CDINSCESTA    = RTRIM(P_INSCESTA)) AND 
                                              (C.NRINCIDCST    = M.NRINCIDCST(+))   AND 
                                              (C.CDGRUPOFISC   = P_CDGRUPOFISC)     AND 
                                              (C.IDCSTINTEREST = 'N')               AND 
                                              (C.IDCSTINTERNA  = 'S')               AND 
                                              (C.CDPRODUTO IS NULL)) LOOP 
                        BEGIN 
                           SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                                  DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                                  DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                           INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                                  V_IDMODBCNFE, V_IDMODBCSTNFE 
                           FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                           WHERE  IM.CDIMPOSTO = CO.CDIMPOSTO          AND 
                                  CO.NRINCIDCST = C_CSTNULL.NRINCIDCST AND 
                                  CO.CDIMPOSTO  = P_CDIMPOSTO; 
                        EXCEPTION 
                           WHEN No_Data_Found THEN 
                              V_CDSITUTRIBNFE   := NULL; 
                              V_IDMODBCNFE      := NULL; 
                              V_IDMODBCSTNFE    := NULL; 
                              V_CDTPCRPICOSTNFE := NULL; 
                              V_CDNATUBCPCSTNFE := NULL; 
                              V_IDMOTIDESSTNFE  := NULL; 
                        END; 
                     END LOOP; 
                  END IF; 
               END IF; 
            ELSE 
               SELECT MAX(P.CDARVPROD), COUNT(*) 
               INTO   V_CDARVPROD, V_CONTADOR 
               FROM   CODSITUTRIB C, PRODUTO P 
               WHERE  (IDENTSAICST     = P_ENTSAICST)       AND 
                      (CDEMPRESA       = P_EMPRESA)         AND 
                      (CDTIPOOPER      = P_TIPOOPER)        AND 
                      (CDINSCESTA      = RTRIM(P_INSCESTA)) AND 
                      (C.CDPRODUTO     = P.CDPRODUTO)       AND 
                      (C.IDCSTINTEREST = 'N')               AND 
                      (C.IDCSTINTERNA  = 'S')               AND 
                      (CDGRUPOFISC IS NULL)                 AND 
                      (INSTR(P_CDARVPROD,NVL(RTRIM(P.CDARVPROD),P_CDARVPROD)) = 1); 
               IF NOT(V_CDARVPROD IS NULL) THEN 
                  FOR C_CST IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                FROM   CODSITUTRIB C, MENSCST M, PRODUTO P 
                                WHERE  (C.IDENTSAICST   = P_ENTSAICST)       AND 
                                       (C.CDEMPRESA     = P_EMPRESA)         AND 
                                       (C.CDTIPOOPER    = P_TIPOOPER)        AND 
                                       (C.CDINSCESTA    = RTRIM(P_INSCESTA)) AND 
                                       (C.NRINCIDCST    = M.NRINCIDCST(+))   AND 
                                       (C.CDPRODUTO     = P.CDPRODUTO(+))    AND 
                                       (C.IDCSTINTEREST = 'N')               AND 
                                       (C.IDCSTINTERNA  = 'S')               AND 
                                       (P.CDARVPROD     = V_CDARVPROD)       AND 
                                       (C.CDGRUPOFISC IS NULL)) LOOP 
                     BEGIN 
                        SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                               DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                               DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                        INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                               V_IDMODBCNFE, V_IDMODBCSTNFE 
                        FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                        WHERE  IM.CDIMPOSTO = CO.CDIMPOSTO      AND 
                               CO.NRINCIDCST = C_CST.NRINCIDCST AND 
                               CO.CDIMPOSTO  = P_CDIMPOSTO; 
                     EXCEPTION 
                        WHEN No_Data_Found THEN 
                           V_CDSITUTRIBNFE   := NULL; 
                           V_IDMODBCNFE      := NULL; 
                           V_IDMODBCSTNFE    := NULL; 
                           V_CDTPCRPICOSTNFE := NULL; 
                           V_CDNATUBCPCSTNFE := NULL; 
                           V_IDMOTIDESSTNFE  := NULL; 
                     END; 
                  END LOOP; 
               ELSIF (V_CDARVPROD IS NULL) THEN 
                  FOR C_CSTNULL IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                    FROM   CODSITUTRIB C, MENSCST M 
                                    WHERE  (C.IDENTSAICST   = P_ENTSAICST)       AND 
                                           (C.CDEMPRESA     = P_EMPRESA)         AND 
                                           (C.CDTIPOOPER    = P_TIPOOPER)        AND 
                                           (C.CDINSCESTA    = RTRIM(P_INSCESTA)) AND 
                                           (C.NRINCIDCST    = M.NRINCIDCST(+))   AND 
                                           (C.IDCSTINTEREST = 'N')               AND 
                                           (C.IDCSTINTERNA  = 'S')               AND 
                                           (C.CDGRUPOFISC IS NULL)               AND 
                                           (C.CDPRODUTO IS NULL)) LOOP 
                     BEGIN 
                        SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                               DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                               DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                        INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                               V_IDMODBCNFE, V_IDMODBCSTNFE 
                        FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                        WHERE  IM.CDIMPOSTO  = CO.CDIMPOSTO         AND 
                               CO.NRINCIDCST = C_CSTNULL.NRINCIDCST AND 
                               CO.CDIMPOSTO  = P_CDIMPOSTO; 
                     EXCEPTION 
                        WHEN No_Data_Found THEN 
                           V_CDSITUTRIBNFE   := NULL; 
                           V_IDMODBCNFE      := NULL; 
                           V_IDMODBCSTNFE    := NULL; 
                           V_CDTPCRPICOSTNFE := NULL; 
                           V_CDNATUBCPCSTNFE := NULL; 
                           V_IDMOTIDESSTNFE  := NULL; 
                     END; 
                  END LOOP; 
               END IF; 
            END IF; 
         ELSE 
            IF P_CDARVPROD IS NULL THEN 
               SELECT COUNT(*) 
                 INTO V_CONTINTEREST 
                 FROM CODSITUTRIB C 
                WHERE (IDENTSAICST     = P_ENTSAICST) 
                  AND (CDEMPRESA       = P_EMPRESA) 
                  AND (CDTIPOOPER      = P_TIPOOPER) 
                  AND (CDINSCESTA      = RTRIM(P_INSCESTA)) 
                  AND (C.IDCSTINTEREST = 'S') 
                  AND (C.IDCSTINTERNA  = 'N') 
                  AND (C.CDPRODUTO IS NULL); 
            ELSE 
               SELECT COUNT(*) 
                 INTO V_CONTINTEREST 
                 FROM CODSITUTRIB C, PRODUTO P 
                WHERE (IDENTSAICST     = P_ENTSAICST) 
                  AND (CDEMPRESA       = P_EMPRESA) 
                  AND (CDTIPOOPER      = P_TIPOOPER) 
                  AND (CDINSCESTA      = RTRIM(P_INSCESTA)) 
                  AND (C.CDPRODUTO     = P.CDPRODUTO(+)) 
                  AND (C.IDCSTINTEREST = 'S') 
                  AND (C.IDCSTINTERNA  = 'N') 
                  AND (INSTR(P_CDARVPROD,NVL(RTRIM(P.CDARVPROD),P_CDARVPROD)) = 1); 
            END IF; 
            IF V_CONTINTEREST > 0 THEN 
               IF (P_CDGRUPOFISC IS NOT NULL) THEN 
                  SELECT MAX(P.CDARVPROD), COUNT(*) 
                  INTO   V_CDARVPROD, V_CONTADOR 
                  FROM   CODSITUTRIB C, PRODUTO P 
                  WHERE  (IDENTSAICST     = P_ENTSAICST)       AND 
                         (CDEMPRESA       = P_EMPRESA)         AND 
                         (CDTIPOOPER      = P_TIPOOPER)        AND 
                         (CDINSCESTA      = RTRIM(P_INSCESTA)) AND 
                         (CDGRUPOFISC     = P_CDGRUPOFISC)     AND 
                         (C.CDPRODUTO     = P.CDPRODUTO(+))    AND 
                         (C.IDCSTINTEREST = 'S')               AND 
                         (C.IDCSTINTERNA  = 'N')               AND 
                         (INSTR(P_CDARVPROD,NVL(RTRIM(P.CDARVPROD),P_CDARVPROD)) = 1); 
 
                  IF V_CONTADOR > 0 THEN 
                     IF NOT(V_CDARVPROD IS NULL) THEN 
                        FOR C_CST IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                      FROM   CODSITUTRIB C, MENSCST M, PRODUTO P 
                                      WHERE  (C.IDENTSAICST    = P_ENTSAICST)       AND 
                                             (C.CDEMPRESA      = P_EMPRESA)         AND 
                                             (C.CDTIPOOPER     = P_TIPOOPER)        AND 
                                             (C.CDINSCESTA     = RTRIM(P_INSCESTA)) AND 
                                             (C.NRINCIDCST     = M.NRINCIDCST(+))   AND 
                                             (C.CDGRUPOFISC    = P_CDGRUPOFISC)     AND 
                                             (C.CDPRODUTO      = P.CDPRODUTO(+))    AND 
                                             (C.IDCSTINTEREST  = 'S')               AND 
                                             (C.IDCSTINTERNA   = 'N')               AND 
                                             (P.CDARVPROD      = V_CDARVPROD)) LOOP 
                           BEGIN 
                              SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                                     DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                                     DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                              INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                                     V_IDMODBCNFE, V_IDMODBCSTNFE 
                              FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                              WHERE  IM.CDIMPOSTO  = CO.CDIMPOSTO     AND 
                                     CO.NRINCIDCST = C_CST.NRINCIDCST AND 
                                     CO.CDIMPOSTO  = P_CDIMPOSTO; 
                           EXCEPTION 
                              WHEN No_Data_Found THEN 
                                 V_CDSITUTRIBNFE   := NULL; 
                                 V_IDMODBCNFE      := NULL; 
                                 V_IDMODBCSTNFE    := NULL; 
                                 V_CDTPCRPICOSTNFE := NULL; 
                                 V_CDNATUBCPCSTNFE := NULL; 
                                 V_IDMOTIDESSTNFE  := NULL; 
                           END; 
                        END LOOP; 
                     ELSIF (V_CDARVPROD IS NULL) THEN 
                        FOR C_CSTNULL IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                          FROM   CODSITUTRIB C, MENSCST M 
                                          WHERE  (C.IDENTSAICST    = P_ENTSAICST)       AND 
                                                 (C.CDEMPRESA      = P_EMPRESA)         AND 
                                                 (C.CDTIPOOPER     = P_TIPOOPER)        AND 
                                                 (C.CDINSCESTA     = RTRIM(P_INSCESTA)) AND 
                                                 (C.NRINCIDCST     = M.NRINCIDCST(+))   AND 
                                                 (C.CDGRUPOFISC    = P_CDGRUPOFISC)     AND 
                                                 (C.IDCSTINTEREST  = 'S')               AND 
                                                 (C.IDCSTINTERNA   = 'N')               AND 
                                                 (C.CDPRODUTO IS NULL)) LOOP 
                           BEGIN 
                              SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                                     DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                                     DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                              INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                                     V_IDMODBCNFE, V_IDMODBCSTNFE 
                              FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                              WHERE  IM.CDIMPOSTO  = CO.CDIMPOSTO         AND 
                                     CO.NRINCIDCST = C_CSTNULL.NRINCIDCST AND 
                                     CO.CDIMPOSTO  = P_CDIMPOSTO; 
                           EXCEPTION 
                              WHEN No_Data_Found THEN 
                                 V_CDSITUTRIBNFE   := NULL; 
                                 V_IDMODBCNFE      := NULL; 
                                 V_IDMODBCSTNFE    := NULL; 
                                 V_CDTPCRPICOSTNFE := NULL; 
                                 V_CDNATUBCPCSTNFE := NULL; 
                                 V_IDMOTIDESSTNFE  := NULL; 
                           END; 
                        END LOOP; 
                     END IF; 
                  END IF; 
               ELSE 
                  SELECT MAX(P.CDARVPROD), COUNT(*) 
                  INTO   V_CDARVPROD, V_CONTADOR 
                  FROM   CODSITUTRIB C, PRODUTO P 
                  WHERE  (IDENTSAICST     = P_ENTSAICST)       AND 
                         (CDEMPRESA       = P_EMPRESA)         AND 
                         (CDTIPOOPER      = P_TIPOOPER)        AND 
                         (CDINSCESTA      = RTRIM(P_INSCESTA)) AND 
                         (C.CDPRODUTO     = P.CDPRODUTO)       AND 
                         (C.IDCSTINTEREST = 'S')               AND 
                         (C.IDCSTINTERNA  = 'N')               AND 
                         (CDGRUPOFISC IS NULL)                 AND 
                         (INSTR(P_CDARVPROD,NVL(RTRIM(P.CDARVPROD),P_CDARVPROD)) = 1); 
                  IF NOT(V_CDARVPROD IS NULL) THEN 
                     FOR C_CST IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                   FROM   CODSITUTRIB C, MENSCST M, PRODUTO P 
                                   WHERE  (C.IDENTSAICST   = P_ENTSAICST)       AND 
                                          (C.CDEMPRESA     = P_EMPRESA)         AND 
                                          (C.CDTIPOOPER    = P_TIPOOPER)        AND 
                                          (C.CDINSCESTA    = RTRIM(P_INSCESTA)) AND 
                                          (C.NRINCIDCST    = M.NRINCIDCST(+))   AND 
                                          (C.CDPRODUTO     = P.CDPRODUTO(+))    AND 
                                          (C.IDCSTINTEREST = 'S')               AND 
                                          (C.IDCSTINTERNA  = 'N')               AND 
                                          (P.CDARVPROD     = V_CDARVPROD)       AND 
                                          (C.CDGRUPOFISC IS NULL)) LOOP 
                        BEGIN 
                           SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                                  DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                                  DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                           INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                                  V_IDMODBCNFE, V_IDMODBCSTNFE 
                           FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                           WHERE  IM.CDIMPOSTO  = CO.CDIMPOSTO     AND 
                                  CO.NRINCIDCST = C_CST.NRINCIDCST AND 
                                  CO.CDIMPOSTO  = P_CDIMPOSTO; 
                        EXCEPTION 
                           WHEN No_Data_Found THEN 
                              V_CDSITUTRIBNFE   := NULL; 
                              V_IDMODBCNFE      := NULL; 
                              V_IDMODBCSTNFE    := NULL; 
                              V_CDTPCRPICOSTNFE := NULL; 
                              V_CDNATUBCPCSTNFE := NULL; 
                              V_IDMOTIDESSTNFE  := NULL; 
                        END; 
                     END LOOP; 
                  ELSIF (V_CDARVPROD IS NULL) THEN 
                     FOR C_CSTNULL IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                       FROM   CODSITUTRIB C, MENSCST M 
                                       WHERE  (C.IDENTSAICST = P_ENTSAICST)         AND 
                                              (C.CDEMPRESA     = P_EMPRESA)         AND 
                                              (C.CDTIPOOPER    = P_TIPOOPER)        AND 
                                              (C.CDINSCESTA    = RTRIM(P_INSCESTA)) AND 
                                              (C.NRINCIDCST    = M.NRINCIDCST(+))   AND 
                                              (C.IDCSTINTEREST = 'S')               AND 
                                              (C.IDCSTINTERNA  = 'N')               AND 
                                              (C.CDGRUPOFISC IS NULL)               AND 
                                              (C.CDPRODUTO IS NULL)) LOOP 
                        BEGIN 
                           SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                                  DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                                  DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                           INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                                  V_IDMODBCNFE, V_IDMODBCSTNFE 
                           FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                           WHERE  IM.CDIMPOSTO  = CO.CDIMPOSTO         AND 
                                  CO.NRINCIDCST = C_CSTNULL.NRINCIDCST AND 
                                  CO.CDIMPOSTO  = P_CDIMPOSTO; 
                        EXCEPTION 
                           WHEN No_Data_Found THEN 
                              V_CDSITUTRIBNFE   := NULL; 
                              V_IDMODBCNFE      := NULL; 
                              V_IDMODBCSTNFE    := NULL; 
                              V_CDTPCRPICOSTNFE := NULL; 
                              V_CDNATUBCPCSTNFE := NULL; 
                              V_IDMOTIDESSTNFE  := NULL; 
                        END; 
                     END LOOP; 
                  END IF; 
               END IF; 
            ELSE 
               IF (P_CDGRUPOFISC IS NOT NULL) THEN 
                  SELECT MAX(P.CDARVPROD), COUNT(*) 
                  INTO   V_CDARVPROD, V_CONTADOR 
                  FROM   CODSITUTRIB C, PRODUTO P 
                  WHERE  (IDENTSAICST     = P_ENTSAICST)       AND 
                         (CDEMPRESA       = P_EMPRESA)         AND 
                         (CDTIPOOPER      = P_TIPOOPER)        AND 
                         (CDINSCESTA      = RTRIM(P_INSCESTA)) AND 
                         (CDGRUPOFISC     = P_CDGRUPOFISC)     AND 
                         (C.CDPRODUTO     = P.CDPRODUTO(+))    AND 
                         (C.IDCSTINTEREST = 'N')               AND 
                         (C.IDCSTINTERNA  = 'N')               AND 
                         (INSTR(P_CDARVPROD,NVL(RTRIM(P.CDARVPROD),P_CDARVPROD)) = 1); 
 
                  IF V_CONTADOR > 0 THEN 
                     IF NOT(V_CDARVPROD IS NULL) THEN 
                        FOR C_CST IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                      FROM   CODSITUTRIB C, MENSCST M, PRODUTO P 
                                      WHERE  (C.IDENTSAICST   = P_ENTSAICST)       AND 
                                             (C.CDEMPRESA     = P_EMPRESA)         AND 
                                             (C.CDTIPOOPER    = P_TIPOOPER)        AND 
                                             (C.CDINSCESTA    = RTRIM(P_INSCESTA)) AND 
                                             (C.NRINCIDCST    = M.NRINCIDCST(+))   AND 
                                             (C.CDGRUPOFISC   = P_CDGRUPOFISC)     AND 
                                             (C.CDPRODUTO     = P.CDPRODUTO(+))    AND 
                                             (C.IDCSTINTEREST = 'N')               AND 
                                             (C.IDCSTINTERNA  = 'N')               AND 
                                             (P.CDARVPROD     = V_CDARVPROD)) LOOP 
                           BEGIN 
                              SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                                     DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                                     DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                              INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                                     V_IDMODBCNFE, V_IDMODBCSTNFE 
                              FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                              WHERE  IM.CDIMPOSTO  = CO.CDIMPOSTO     AND 
                                     CO.NRINCIDCST = C_CST.NRINCIDCST AND 
                                     CO.CDIMPOSTO  = P_CDIMPOSTO; 
                           EXCEPTION 
                              WHEN No_Data_Found THEN 
                                 V_CDSITUTRIBNFE   := NULL; 
                                 V_IDMODBCNFE      := NULL; 
                                 V_IDMODBCSTNFE    := NULL; 
                                 V_CDTPCRPICOSTNFE := NULL; 
                                 V_CDNATUBCPCSTNFE := NULL; 
                                 V_IDMOTIDESSTNFE  := NULL; 
                           END; 
                        END LOOP; 
                     ELSIF (V_CDARVPROD IS NULL) THEN 
                        FOR C_CSTNULL IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                          FROM   CODSITUTRIB C, MENSCST M 
                                          WHERE  (C.IDENTSAICST   = P_ENTSAICST)       AND 
                                                 (C.CDEMPRESA     = P_EMPRESA)         AND 
                                                 (C.CDTIPOOPER    = P_TIPOOPER)        AND 
                                                 (C.CDINSCESTA    = RTRIM(P_INSCESTA)) AND 
                                                 (C.NRINCIDCST    = M.NRINCIDCST(+))   AND 
                                                 (C.CDGRUPOFISC   = P_CDGRUPOFISC)     AND 
                                                 (C.IDCSTINTEREST = 'N')               AND 
                                                 (C.IDCSTINTERNA  = 'N')               AND 
                                                 (C.CDPRODUTO IS NULL)) LOOP 
                           BEGIN 
                              SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                                     DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                                     DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                              INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                                     V_IDMODBCNFE, V_IDMODBCSTNFE 
                              FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                              WHERE  IM.CDIMPOSTO  = CO.CDIMPOSTO           AND 
                                     CO.NRINCIDCST = C_CSTNULL.NRINCIDCST AND 
                                     CO.CDIMPOSTO  = P_CDIMPOSTO; 
                           EXCEPTION 
                              WHEN No_Data_Found THEN 
                                 V_CDSITUTRIBNFE   := NULL; 
                                 V_IDMODBCNFE      := NULL; 
                                 V_IDMODBCSTNFE    := NULL; 
                                 V_CDTPCRPICOSTNFE := NULL; 
                                 V_CDNATUBCPCSTNFE := NULL; 
                                 V_IDMOTIDESSTNFE  := NULL; 
                           END; 
                        END LOOP; 
                     END IF; 
                  END IF; 
               ELSE 
                  SELECT MAX(P.CDARVPROD), COUNT(*) 
                  INTO   V_CDARVPROD, V_CONTADOR 
                  FROM   CODSITUTRIB C, PRODUTO P 
                  WHERE  (IDENTSAICST     = P_ENTSAICST)       AND 
                         (CDEMPRESA       = P_EMPRESA)         AND 
                         (CDTIPOOPER      = P_TIPOOPER)        AND 
                         (CDINSCESTA      = RTRIM(P_INSCESTA)) AND 
                         (C.CDPRODUTO     = P.CDPRODUTO)       AND 
                         (C.IDCSTINTEREST = 'N')               AND 
                         (C.IDCSTINTERNA  = 'N')               AND 
                         (CDGRUPOFISC IS NULL)                 AND 
                         (INSTR(P_CDARVPROD,NVL(RTRIM(P.CDARVPROD),P_CDARVPROD)) = 1); 
                  IF NOT(V_CDARVPROD IS NULL) THEN 
                     FOR C_CST IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                   FROM   CODSITUTRIB C, MENSCST M, PRODUTO P 
                                   WHERE  (C.IDENTSAICST   = P_ENTSAICST)       AND 
                                          (C.CDEMPRESA     = P_EMPRESA)         AND 
                                          (C.CDTIPOOPER    = P_TIPOOPER)        AND 
                                          (C.CDINSCESTA    = RTRIM(P_INSCESTA)) AND 
                                          (C.NRINCIDCST    = M.NRINCIDCST(+))   AND 
                                          (C.CDPRODUTO     = P.CDPRODUTO(+))    AND 
                                          (C.IDCSTINTEREST = 'N')               AND 
                                          (C.IDCSTINTERNA  = 'N')               AND 
                                          (P.CDARVPROD     = V_CDARVPROD)       AND 
                                          (C.CDGRUPOFISC IS NULL)) LOOP 
                        BEGIN 
                           SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                                  DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                                  DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                           INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                                  V_IDMODBCNFE, V_IDMODBCSTNFE 
                           FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                           WHERE  IM.CDIMPOSTO  = CO.CDIMPOSTO     AND 
                                  CO.NRINCIDCST = C_CST.NRINCIDCST AND 
                                  CO.CDIMPOSTO  = P_CDIMPOSTO; 
                        EXCEPTION 
                           WHEN No_Data_Found THEN 
                              V_CDSITUTRIBNFE   := NULL; 
                              V_IDMODBCNFE      := NULL; 
                              V_IDMODBCSTNFE    := NULL; 
                              V_CDTPCRPICOSTNFE := NULL; 
                              V_CDNATUBCPCSTNFE := NULL; 
                              V_IDMOTIDESSTNFE  := NULL; 
                        END; 
                     END LOOP; 
                  ELSIF (V_CDARVPROD IS NULL) THEN 
                     FOR C_CSTNULL IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                       FROM   CODSITUTRIB C, MENSCST M 
                                       WHERE  (C.IDENTSAICST   = P_ENTSAICST)       AND 
                                              (C.CDEMPRESA     = P_EMPRESA)         AND 
                                              (C.CDTIPOOPER    = P_TIPOOPER)        AND 
                                              (C.CDINSCESTA    = RTRIM(P_INSCESTA)) AND 
                                              (C.NRINCIDCST    = M.NRINCIDCST(+))   AND 
                                              (C.IDCSTINTEREST = 'N')               AND 
                                              (C.IDCSTINTERNA  = 'N')               AND 
                                              (C.CDGRUPOFISC IS NULL)               AND 
                                              (C.CDPRODUTO IS NULL)) LOOP 
                        BEGIN 
                           SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                                  DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                                  DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                           INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                                  V_IDMODBCNFE, V_IDMODBCSTNFE 
                           FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                           WHERE  IM.CDIMPOSTO  = CO.CDIMPOSTO         AND 
                                  CO.NRINCIDCST = C_CSTNULL.NRINCIDCST AND 
                                  CO.CDIMPOSTO  = P_CDIMPOSTO; 
                        EXCEPTION 
                           WHEN No_Data_Found THEN 
                              V_CDSITUTRIBNFE   := NULL; 
                              V_IDMODBCNFE      := NULL; 
                              V_IDMODBCSTNFE    := NULL; 
                              V_CDTPCRPICOSTNFE := NULL; 
                              V_CDNATUBCPCSTNFE := NULL; 
                              V_IDMOTIDESSTNFE  := NULL; 
                        END; 
                     END LOOP; 
                  END IF; 
               END IF; 
            END IF; 
         END IF; 
      ELSE 
         IF P_IDCSTINTEREST = 'S' THEN 
            IF P_CDARVPROD IS NULL THEN 
               SELECT COUNT(*) 
                 INTO V_CONTINTEREST 
                 FROM CODSITUTRIB C 
                WHERE (IDENTSAICST     = P_ENTSAICST) 
                  AND (CDEMPRESA       = P_EMPRESA) 
                  AND (CDTIPOOPER      = P_TIPOOPER) 
                  AND (CDINSCESTA      = RTRIM(P_INSCESTA)) 
                  AND (C.IDCSTINTEREST = 'S') 
                  AND (C.IDCSTINTERNA  = 'N') 
                  AND (C.CDPRODUTO IS NULL); 
            ELSE 
               SELECT COUNT(*) 
                 INTO V_CONTINTEREST 
                 FROM CODSITUTRIB C, PRODUTO P 
                WHERE (IDENTSAICST     = P_ENTSAICST) 
                  AND (CDEMPRESA       = P_EMPRESA) 
                  AND (CDTIPOOPER      = P_TIPOOPER) 
                  AND (CDINSCESTA      = RTRIM(P_INSCESTA)) 
                  AND (C.CDPRODUTO     = P.CDPRODUTO(+)) 
                  AND (C.IDCSTINTEREST = 'S') 
                  AND (C.IDCSTINTERNA  = 'N') 
                  AND (INSTR(P_CDARVPROD,NVL(RTRIM(P.CDARVPROD),P_CDARVPROD)) = 1); 
            END IF; 
            IF V_CONTINTEREST > 0 THEN 
               IF (P_CDGRUPOFISC IS NOT NULL) THEN 
                  SELECT MAX(P.CDARVPROD), COUNT(*) 
                  INTO   V_CDARVPROD, V_CONTADOR 
                  FROM   CODSITUTRIB C, PRODUTO P 
                  WHERE  (IDENTSAICST     = P_ENTSAICST)       AND 
                         (CDEMPRESA       = P_EMPRESA)         AND 
                         (CDTIPOOPER      = P_TIPOOPER)        AND 
                         (CDINSCESTA      = RTRIM(P_INSCESTA)) AND 
                         (CDGRUPOFISC     = P_CDGRUPOFISC)     AND 
                         (C.CDPRODUTO     = P.CDPRODUTO(+))    AND 
                         (C.IDCSTINTEREST = 'S')               AND 
                         (C.IDCSTINTERNA  = 'N')               AND 
                         (INSTR(P_CDARVPROD,NVL(RTRIM(P.CDARVPROD),P_CDARVPROD)) = 1); 
               END IF; 
               IF V_CONTADOR > 0 THEN 
                  IF NOT(V_CDARVPROD IS NULL) THEN 
                     FOR C_CST IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                   FROM   CODSITUTRIB C, MENSCST M, PRODUTO P 
                                   WHERE  (C.IDENTSAICST    = P_ENTSAICST)       AND 
                                          (C.CDEMPRESA      = P_EMPRESA)         AND 
                                          (C.CDTIPOOPER     = P_TIPOOPER)        AND 
                                          (C.CDINSCESTA     = RTRIM(P_INSCESTA)) AND 
                                          (C.NRINCIDCST     = M.NRINCIDCST(+))   AND 
                                          (C.CDGRUPOFISC    = P_CDGRUPOFISC)     AND 
                                          (C.CDPRODUTO      = P.CDPRODUTO(+))    AND 
                                          (C.IDCSTINTEREST  = 'S')               AND 
                                          (C.IDCSTINTERNA   = 'N')               AND 
                                          (P.CDARVPROD      = V_CDARVPROD)) LOOP 
                        BEGIN 
                           SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                                  DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                                  DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                           INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                                  V_IDMODBCNFE, V_IDMODBCSTNFE 
                           FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                           WHERE  IM.CDIMPOSTO  = CO.CDIMPOSTO     AND 
                                  CO.NRINCIDCST = C_CST.NRINCIDCST AND 
                                  CO.CDIMPOSTO  = P_CDIMPOSTO; 
                        EXCEPTION 
                           WHEN No_Data_Found THEN 
                              V_CDSITUTRIBNFE   := NULL; 
                              V_IDMODBCNFE      := NULL; 
                              V_IDMODBCSTNFE    := NULL; 
                              V_CDTPCRPICOSTNFE := NULL; 
                              V_CDNATUBCPCSTNFE := NULL; 
                              V_IDMOTIDESSTNFE  := NULL; 
                        END; 
                     END LOOP; 
                  ELSIF (V_CDARVPROD IS NULL) THEN 
                     FOR C_CSTNULL IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                       FROM   CODSITUTRIB C, MENSCST M 
                                       WHERE  (C.IDENTSAICST    = P_ENTSAICST)       AND 
                                              (C.CDEMPRESA      = P_EMPRESA)         AND 
                                              (C.CDTIPOOPER     = P_TIPOOPER)        AND 
                                              (C.CDINSCESTA     = RTRIM(P_INSCESTA)) AND 
                                              (C.NRINCIDCST     = M.NRINCIDCST(+))   AND 
                                              (C.CDGRUPOFISC    = P_CDGRUPOFISC)     AND 
                                              (C.IDCSTINTEREST  = 'S')               AND 
                                              (C.IDCSTINTERNA   = 'N')               AND 
                                              (C.CDPRODUTO IS NULL)) LOOP 
                        BEGIN 
                           SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                                  DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                                  DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                           INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                                  V_IDMODBCNFE, V_IDMODBCSTNFE 
                           FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                           WHERE  IM.CDIMPOSTO  = CO.CDIMPOSTO         AND 
                                  CO.NRINCIDCST = C_CSTNULL.NRINCIDCST AND 
                                  CO.CDIMPOSTO  = P_CDIMPOSTO; 
                        EXCEPTION 
                           WHEN No_Data_Found THEN 
                              V_CDSITUTRIBNFE   := NULL; 
                              V_IDMODBCNFE      := NULL; 
                              V_IDMODBCSTNFE    := NULL; 
                              V_CDTPCRPICOSTNFE := NULL; 
                              V_CDNATUBCPCSTNFE := NULL; 
                              V_IDMOTIDESSTNFE  := NULL; 
                        END; 
                     END LOOP; 
                  END IF; 
               ELSE 
                  SELECT MAX(P.CDARVPROD), COUNT(*) 
                  INTO   V_CDARVPROD, V_CONTADOR 
                  FROM   CODSITUTRIB C, PRODUTO P 
                  WHERE  (IDENTSAICST     = P_ENTSAICST)       AND 
                         (CDEMPRESA       = P_EMPRESA)         AND 
                         (CDTIPOOPER      = P_TIPOOPER)        AND 
                         (CDINSCESTA      = RTRIM(P_INSCESTA)) AND 
                         (C.CDPRODUTO     = P.CDPRODUTO)       AND 
                         (C.IDCSTINTEREST = 'S')               AND 
                         (C.IDCSTINTERNA  = 'N')               AND 
                         (CDGRUPOFISC IS NULL)                 AND 
                         (INSTR(P_CDARVPROD,NVL(RTRIM(P.CDARVPROD),P_CDARVPROD)) = 1); 
                  IF NOT(V_CDARVPROD IS NULL) THEN 
                     FOR C_CST IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                   FROM   CODSITUTRIB C, MENSCST M, PRODUTO P 
                                   WHERE  (C.IDENTSAICST   = P_ENTSAICST)       AND 
                                          (C.CDEMPRESA     = P_EMPRESA)         AND 
                                          (C.CDTIPOOPER    = P_TIPOOPER)        AND 
                                          (C.CDINSCESTA    = RTRIM(P_INSCESTA)) AND 
                                          (C.NRINCIDCST    = M.NRINCIDCST(+))   AND 
                                          (C.CDPRODUTO     = P.CDPRODUTO(+))    AND 
                                          (C.IDCSTINTEREST = 'S')               AND 
                                          (C.IDCSTINTERNA  = 'N')               AND 
                                          (P.CDARVPROD     = V_CDARVPROD)       AND 
                                          (C.CDGRUPOFISC IS NULL)) LOOP 
                        BEGIN 
                           SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                                  DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                                  DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                           INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                                  V_IDMODBCNFE, V_IDMODBCSTNFE 
                           FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                           WHERE  IM.CDIMPOSTO  = CO.CDIMPOSTO     AND 
                                  CO.NRINCIDCST = C_CST.NRINCIDCST AND 
                                  CO.CDIMPOSTO  = P_CDIMPOSTO; 
                        EXCEPTION 
                           WHEN No_Data_Found THEN 
                              V_CDSITUTRIBNFE   := NULL; 
                              V_IDMODBCNFE      := NULL; 
                              V_IDMODBCSTNFE    := NULL; 
                              V_CDTPCRPICOSTNFE := NULL; 
                              V_CDNATUBCPCSTNFE := NULL; 
                              V_IDMOTIDESSTNFE  := NULL; 
                        END; 
                     END LOOP; 
                  ELSIF (V_CDARVPROD IS NULL) THEN 
                     FOR C_CSTNULL IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                       FROM   CODSITUTRIB C, MENSCST M 
                                       WHERE  (C.IDENTSAICST = P_ENTSAICST)         AND 
                                              (C.CDEMPRESA     = P_EMPRESA)         AND 
                                              (C.CDTIPOOPER    = P_TIPOOPER)        AND 
                                              (C.CDINSCESTA    = RTRIM(P_INSCESTA)) AND 
                                              (C.NRINCIDCST    = M.NRINCIDCST(+))   AND 
                                              (C.IDCSTINTEREST = 'S')               AND 
                                              (C.IDCSTINTERNA  = 'N')               AND 
                                              (C.CDGRUPOFISC IS NULL)               AND 
                                              (C.CDPRODUTO IS NULL)) LOOP 
                        BEGIN 
                           SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                                  DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                                  DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                           INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                                  V_IDMODBCNFE, V_IDMODBCSTNFE 
                           FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                           WHERE  IM.CDIMPOSTO  = CO.CDIMPOSTO         AND 
                                  CO.NRINCIDCST = C_CSTNULL.NRINCIDCST AND 
                                  CO.CDIMPOSTO  = P_CDIMPOSTO; 
                        EXCEPTION 
                           WHEN No_Data_Found THEN 
                              V_CDSITUTRIBNFE   := NULL; 
                              V_IDMODBCNFE      := NULL; 
                              V_IDMODBCSTNFE    := NULL; 
                              V_CDTPCRPICOSTNFE := NULL; 
                              V_CDNATUBCPCSTNFE := NULL; 
                              V_IDMOTIDESSTNFE  := NULL; 
                        END; 
                     END LOOP; 
                  END IF; 
               END IF; 
            ELSE 
               IF (P_CDGRUPOFISC IS NOT NULL) THEN 
                  SELECT MAX(P.CDARVPROD), COUNT(*) 
                  INTO   V_CDARVPROD, V_CONTADOR 
                  FROM   CODSITUTRIB C, PRODUTO P 
                  WHERE  (IDENTSAICST     = P_ENTSAICST)       AND 
                         (CDEMPRESA       = P_EMPRESA)         AND 
                         (CDTIPOOPER      = P_TIPOOPER)        AND 
                         (CDINSCESTA      = RTRIM(P_INSCESTA)) AND 
                         (CDGRUPOFISC     = P_CDGRUPOFISC)     AND 
                         (C.CDPRODUTO     = P.CDPRODUTO(+))    AND 
                         (C.IDCSTINTEREST = 'N')               AND 
                         (C.IDCSTINTERNA  = 'N')               AND 
                         (INSTR(P_CDARVPROD,NVL(RTRIM(P.CDARVPROD),P_CDARVPROD)) = 1); 
 
                  IF V_CONTADOR > 0 THEN 
                     IF NOT(V_CDARVPROD IS NULL) THEN 
                        FOR C_CST IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                      FROM   CODSITUTRIB C, MENSCST M, PRODUTO P 
                                      WHERE  (C.IDENTSAICST   = P_ENTSAICST)       AND 
                                             (C.CDEMPRESA     = P_EMPRESA)         AND 
                                             (C.CDTIPOOPER    = P_TIPOOPER)        AND 
                                             (C.CDINSCESTA    = RTRIM(P_INSCESTA)) AND 
                                             (C.NRINCIDCST    = M.NRINCIDCST(+))   AND 
                                             (C.CDGRUPOFISC   = P_CDGRUPOFISC)     AND 
                                             (C.CDPRODUTO     = P.CDPRODUTO(+))    AND 
                                             (C.IDCSTINTEREST = 'N')               AND 
                                             (C.IDCSTINTERNA  = 'N')               AND 
                                             (P.CDARVPROD     = V_CDARVPROD)) LOOP 
                           BEGIN 
                              SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                                     DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                                     DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                              INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                                     V_IDMODBCNFE, V_IDMODBCSTNFE 
                              FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                              WHERE  IM.CDIMPOSTO  = CO.CDIMPOSTO     AND 
                                     CO.NRINCIDCST = C_CST.NRINCIDCST AND 
                                     CO.CDIMPOSTO  = P_CDIMPOSTO; 
                           EXCEPTION 
                              WHEN No_Data_Found THEN 
                                 V_CDSITUTRIBNFE   := NULL; 
                                 V_IDMODBCNFE      := NULL; 
                                 V_IDMODBCSTNFE    := NULL; 
                                 V_CDTPCRPICOSTNFE := NULL; 
                                 V_CDNATUBCPCSTNFE := NULL; 
                                 V_IDMOTIDESSTNFE  := NULL; 
                           END; 
                        END LOOP; 
                     ELSIF (V_CDARVPROD IS NULL) THEN 
                        FOR C_CSTNULL IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                          FROM   CODSITUTRIB C, MENSCST M 
                                          WHERE  (C.IDENTSAICST   = P_ENTSAICST)       AND 
                                                 (C.CDEMPRESA     = P_EMPRESA)         AND 
                                                 (C.CDTIPOOPER    = P_TIPOOPER)        AND 
                                                 (C.CDINSCESTA    = RTRIM(P_INSCESTA)) AND 
                                                 (C.NRINCIDCST    = M.NRINCIDCST(+))   AND 
                                                 (C.CDGRUPOFISC   = P_CDGRUPOFISC)     AND 
                                                 (C.IDCSTINTEREST = 'N')               AND 
                                                 (C.IDCSTINTERNA  = 'N')               AND 
                                                 (C.CDPRODUTO IS NULL)) LOOP 
                           BEGIN 
                              SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                                     DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                                     DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                              INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                                     V_IDMODBCNFE, V_IDMODBCSTNFE 
                              FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                              WHERE  IM.CDIMPOSTO = CO.CDIMPOSTO          AND 
                                     CO.NRINCIDCST = C_CSTNULL.NRINCIDCST AND 
                                     CO.CDIMPOSTO  = P_CDIMPOSTO; 
                           EXCEPTION 
                              WHEN No_Data_Found THEN 
                                 V_CDSITUTRIBNFE   := NULL; 
                                 V_IDMODBCNFE      := NULL; 
                                 V_IDMODBCSTNFE    := NULL; 
                                 V_CDTPCRPICOSTNFE := NULL; 
                                 V_CDNATUBCPCSTNFE := NULL; 
                                 V_IDMOTIDESSTNFE  := NULL; 
                           END; 
                        END LOOP; 
                     END IF; 
                  END IF; 
               ELSE 
                  SELECT MAX(P.CDARVPROD), COUNT(*) 
                  INTO   V_CDARVPROD, V_CONTADOR 
                  FROM   CODSITUTRIB C, PRODUTO P 
                  WHERE  (IDENTSAICST     = P_ENTSAICST)       AND 
                         (CDEMPRESA       = P_EMPRESA)         AND 
                         (CDTIPOOPER      = P_TIPOOPER)        AND 
                         (CDINSCESTA      = RTRIM(P_INSCESTA)) AND 
                         (C.CDPRODUTO     = P.CDPRODUTO)       AND 
                         (C.IDCSTINTEREST = 'N')               AND 
                         (C.IDCSTINTERNA  = 'N')               AND 
                         (CDGRUPOFISC IS NULL)                 AND 
                         (INSTR(P_CDARVPROD,NVL(RTRIM(P.CDARVPROD),P_CDARVPROD)) = 1); 
                  IF NOT(V_CDARVPROD IS NULL) THEN 
                     FOR C_CST IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                   FROM   CODSITUTRIB C, MENSCST M, PRODUTO P 
                                   WHERE  (C.IDENTSAICST   = P_ENTSAICST)       AND 
                                          (C.CDEMPRESA     = P_EMPRESA)         AND 
                                          (C.CDTIPOOPER    = P_TIPOOPER)        AND 
                                          (C.CDINSCESTA    = RTRIM(P_INSCESTA)) AND 
                                          (C.NRINCIDCST    = M.NRINCIDCST(+))   AND 
                                          (C.CDPRODUTO     = P.CDPRODUTO(+))    AND 
                                          (C.IDCSTINTEREST = 'N')               AND 
                                          (C.IDCSTINTERNA  = 'N')               AND 
                                          (P.CDARVPROD     = V_CDARVPROD)       AND 
                                          (C.CDGRUPOFISC IS NULL)) LOOP 
                        BEGIN 
                           SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                                  DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                                  DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                           INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                                  V_IDMODBCNFE, V_IDMODBCSTNFE 
                           FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                           WHERE  IM.CDIMPOSTO  = CO.CDIMPOSTO     AND 
                                  CO.NRINCIDCST = C_CST.NRINCIDCST AND 
                                  CO.CDIMPOSTO  = P_CDIMPOSTO; 
                        EXCEPTION 
                           WHEN No_Data_Found THEN 
                              V_CDSITUTRIBNFE   := NULL; 
                              V_IDMODBCNFE      := NULL; 
                              V_IDMODBCSTNFE    := NULL; 
                              V_CDTPCRPICOSTNFE := NULL; 
                              V_CDNATUBCPCSTNFE := NULL; 
                              V_IDMOTIDESSTNFE  := NULL; 
                        END; 
                     END LOOP; 
                  ELSIF (V_CDARVPROD IS NULL) THEN 
                     FOR C_CSTNULL IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                       FROM   CODSITUTRIB C, MENSCST M 
                                       WHERE  (C.IDENTSAICST   = P_ENTSAICST)       AND 
                                              (C.CDEMPRESA     = P_EMPRESA)         AND 
                                              (C.CDTIPOOPER    = P_TIPOOPER)        AND 
                                              (C.CDINSCESTA    = RTRIM(P_INSCESTA)) AND 
                                              (C.NRINCIDCST    = M.NRINCIDCST(+))   AND 
                                              (C.IDCSTINTEREST = 'N')               AND 
                                              (C.IDCSTINTERNA  = 'N')               AND 
                                              (C.CDGRUPOFISC IS NULL)               AND 
                                              (C.CDPRODUTO IS NULL)) LOOP 
                        BEGIN 
                           SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                                  DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                                  DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                           INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                                  V_IDMODBCNFE, V_IDMODBCSTNFE 
                           FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                           WHERE  IM.CDIMPOSTO  = CO.CDIMPOSTO         AND 
                                  CO.NRINCIDCST = C_CSTNULL.NRINCIDCST AND 
                                  CO.CDIMPOSTO  = P_CDIMPOSTO; 
                        EXCEPTION 
                           WHEN No_Data_Found THEN 
                              V_CDSITUTRIBNFE   := NULL; 
                              V_IDMODBCNFE      := NULL; 
                              V_IDMODBCSTNFE    := NULL; 
                              V_CDTPCRPICOSTNFE := NULL; 
                              V_CDNATUBCPCSTNFE := NULL; 
                              V_IDMOTIDESSTNFE  := NULL; 
                        END; 
                     END LOOP; 
                  END IF; 
               END IF; 
            END IF; 
         ELSE 
            IF (P_CDGRUPOFISC IS NOT NULL) THEN 
               SELECT MAX(P.CDARVPROD), COUNT(*) 
               INTO   V_CDARVPROD, V_CONTADOR 
               FROM   CODSITUTRIB C, PRODUTO P 
               WHERE  (IDENTSAICST     = P_ENTSAICST)       AND 
                      (CDEMPRESA       = P_EMPRESA)         AND 
                      (CDTIPOOPER      = P_TIPOOPER)        AND 
                      (CDINSCESTA      = RTRIM(P_INSCESTA)) AND 
                      (CDGRUPOFISC     = P_CDGRUPOFISC)     AND 
                      (C.CDPRODUTO     = P.CDPRODUTO(+))    AND 
                      (C.IDCSTINTEREST = 'N')               AND 
                      (C.IDCSTINTERNA  = 'N')               AND 
                      (INSTR(P_CDARVPROD,NVL(RTRIM(P.CDARVPROD),P_CDARVPROD)) = 1); 
 
               IF V_CONTADOR > 0 THEN 
                  IF NOT(V_CDARVPROD IS NULL) THEN 
                     FOR C_CST IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                   FROM   CODSITUTRIB C, MENSCST M, PRODUTO P 
                                   WHERE  (C.IDENTSAICST   = P_ENTSAICST)       AND 
                                          (C.CDEMPRESA     = P_EMPRESA)         AND 
                                          (C.CDTIPOOPER    = P_TIPOOPER)        AND 
                                          (C.CDINSCESTA    = RTRIM(P_INSCESTA)) AND 
                                          (C.NRINCIDCST    = M.NRINCIDCST(+))   AND 
                                          (C.CDGRUPOFISC   = P_CDGRUPOFISC)     AND 
                                          (C.CDPRODUTO     = P.CDPRODUTO(+))    AND 
                                          (C.IDCSTINTEREST = 'N')               AND 
                                          (C.IDCSTINTERNA  = 'N')               AND 
                                          (P.CDARVPROD     = V_CDARVPROD)) LOOP 
                        BEGIN 
                           SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                                  DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                                  DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                           INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                                  V_IDMODBCNFE, V_IDMODBCSTNFE 
                           FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                           WHERE  IM.CDIMPOSTO  = CO.CDIMPOSTO     AND 
                                  CO.NRINCIDCST = C_CST.NRINCIDCST AND 
                                  CO.CDIMPOSTO  = P_CDIMPOSTO; 
                        EXCEPTION 
                           WHEN No_Data_Found THEN 
                              V_CDSITUTRIBNFE   := NULL; 
                              V_IDMODBCNFE      := NULL; 
                              V_IDMODBCSTNFE    := NULL; 
                              V_CDTPCRPICOSTNFE := NULL; 
                              V_CDNATUBCPCSTNFE := NULL; 
                              V_IDMOTIDESSTNFE  := NULL; 
                        END; 
                     END LOOP; 
                  ELSIF (V_CDARVPROD IS NULL) THEN 
                     FOR C_CSTNULL IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                       FROM   CODSITUTRIB C, MENSCST M 
                                       WHERE  (C.IDENTSAICST   = P_ENTSAICST)       AND 
                                              (C.CDEMPRESA     = P_EMPRESA)         AND 
                                              (C.CDTIPOOPER    = P_TIPOOPER)        AND 
                                              (C.CDINSCESTA    = RTRIM(P_INSCESTA)) AND 
                                              (C.NRINCIDCST    = M.NRINCIDCST(+))   AND 
                                              (C.CDGRUPOFISC   = P_CDGRUPOFISC)     AND 
                                              (C.IDCSTINTEREST = 'N')               AND 
                                              (C.IDCSTINTERNA  = 'N')               AND 
                                              (C.CDPRODUTO IS NULL)) LOOP 
                        BEGIN 
                           SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                                  DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                                  DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                           INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                                  V_IDMODBCNFE, V_IDMODBCSTNFE 
                           FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                           WHERE  IM.CDIMPOSTO  = CO.CDIMPOSTO         AND 
                                  CO.NRINCIDCST = C_CSTNULL.NRINCIDCST AND 
                                  CO.CDIMPOSTO  = P_CDIMPOSTO; 
                        EXCEPTION 
                           WHEN No_Data_Found THEN 
                              V_CDSITUTRIBNFE   := NULL; 
                              V_IDMODBCNFE      := NULL; 
                              V_IDMODBCSTNFE    := NULL; 
                              V_CDTPCRPICOSTNFE := NULL; 
                              V_CDNATUBCPCSTNFE := NULL; 
                              V_IDMOTIDESSTNFE  := NULL; 
                        END; 
                     END LOOP; 
                  END IF; 
               END IF; 
            ELSE 
               SELECT MAX(P.CDARVPROD), COUNT(*) 
               INTO   V_CDARVPROD, V_CONTADOR 
               FROM   CODSITUTRIB C, PRODUTO P 
               WHERE  (IDENTSAICST     = P_ENTSAICST)       AND 
                      (CDEMPRESA       = P_EMPRESA)         AND 
                      (CDTIPOOPER      = P_TIPOOPER)        AND 
                      (CDINSCESTA      = RTRIM(P_INSCESTA)) AND 
                      (C.CDPRODUTO     = P.CDPRODUTO)       AND 
                      (C.IDCSTINTEREST = 'N')               AND 
                      (C.IDCSTINTERNA  = 'N')               AND 
                      (CDGRUPOFISC IS NULL)                 AND 
                      (INSTR(P_CDARVPROD,NVL(RTRIM(P.CDARVPROD),P_CDARVPROD)) = 1); 
 
               IF NOT(V_CDARVPROD IS NULL) THEN 
                  FOR C_CST IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                FROM   CODSITUTRIB C, MENSCST M, PRODUTO P 
                                WHERE  (C.IDENTSAICST   = P_ENTSAICST)       AND 
                                       (C.CDEMPRESA     = P_EMPRESA)         AND 
                                       (C.CDTIPOOPER    = P_TIPOOPER)        AND 
                                       (C.CDINSCESTA    = RTRIM(P_INSCESTA)) AND 
                                       (C.NRINCIDCST    = M.NRINCIDCST(+))   AND 
                                       (C.CDPRODUTO     = P.CDPRODUTO(+))    AND 
                                       (C.IDCSTINTEREST = 'N')               AND 
                                       (C.IDCSTINTERNA  = 'N')               AND 
                                       (P.CDARVPROD     = V_CDARVPROD)       AND 
                                       (C.CDGRUPOFISC IS NULL)) LOOP 
                     BEGIN 
                        SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                               DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                               DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                        INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                               V_IDMODBCNFE, V_IDMODBCSTNFE 
                        FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                        WHERE  IM.CDIMPOSTO  = CO.CDIMPOSTO         AND 
                               CO.NRINCIDCST = C_CST.NRINCIDCST AND 
                               CO.CDIMPOSTO  = P_CDIMPOSTO; 
                     EXCEPTION 
                        WHEN No_Data_Found THEN 
                           V_CDSITUTRIBNFE   := NULL; 
                           V_IDMODBCNFE      := NULL; 
                           V_IDMODBCSTNFE    := NULL; 
                           V_CDTPCRPICOSTNFE := NULL; 
                           V_CDNATUBCPCSTNFE := NULL; 
                           V_IDMOTIDESSTNFE  := NULL; 
                     END; 
                  END LOOP; 
               ELSIF (V_CDARVPROD IS NULL) THEN 
                  FOR C_CSTNULL IN (SELECT CDSITUTRIB, DSMENSCST, C.NRINCIDCST 
                                    FROM   CODSITUTRIB C, MENSCST M 
                                    WHERE  (C.IDENTSAICST   = P_ENTSAICST)       AND 
                                           (C.CDEMPRESA     = P_EMPRESA)         AND 
                                           (C.CDTIPOOPER    = P_TIPOOPER)        AND 
                                           (C.CDINSCESTA    = RTRIM(P_INSCESTA)) AND 
                                           (C.NRINCIDCST    = M.NRINCIDCST(+))   AND 
                                           (C.IDCSTINTEREST = 'N')               AND 
                                           (C.IDCSTINTERNA  = 'N')               AND 
                                           (C.CDGRUPOFISC IS NULL)               AND 
                                           (C.CDPRODUTO IS NULL)) LOOP 
 
                     BEGIN 
                        SELECT CO.CDSITUTRIBNFE, CO.CDTPCRPICOSTNFE, CO.CDNATUBCPCSTNFE, CO.IDMOTIDESSTNFE, 
                               DECODE(IM.IDMODBCNFE, '9', CO.IDMODBCNFE, IM.IDMODBCNFE) AS IDMODBCNFE, 
                               DECODE(IM.IDMODBCSTNFE, '9', CO.IDMODBCSTNFE, IM.IDMODBCSTNFE) AS IDMODBCSTNFE 
                        INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                               V_IDMODBCNFE, V_IDMODBCSTNFE 
                        FROM   CODSITUTRIBNFE CO, IMPOSTO IM 
                        WHERE  IM.CDIMPOSTO  = CO.CDIMPOSTO         AND 
                               CO.NRINCIDCST = C_CSTNULL.NRINCIDCST AND 
                               CO.CDIMPOSTO  = P_CDIMPOSTO; 
                     EXCEPTION 
                        WHEN No_Data_Found THEN 
                           V_CDSITUTRIBNFE   := NULL; 
                           V_IDMODBCNFE      := NULL; 
                           V_IDMODBCSTNFE    := NULL; 
                           V_CDTPCRPICOSTNFE := NULL; 
                           V_CDNATUBCPCSTNFE := NULL; 
                           V_IDMOTIDESSTNFE  := NULL; 
                     END; 
                  END LOOP; 
               END IF; 
            END IF; 
         END IF; 
      END IF; 
       
      IF V_CDSITUTRIBNFE IS NULL THEN 
         V_CONTADOR := 0; 
         IF (P_CDGRUPOFISC IS NOT NULL) THEN 
            SELECT MAX(P.CDARVPROD), COUNT(*) 
            INTO   V_CDARVPROD, V_CONTADOR 
            FROM   CODSITUTRIBNFE C, PRODUTO P 
            WHERE  (IDENTSAICSTNFE  = P_ENTSAICST)    AND 
                   (CDEMPRESA       = P_EMPRESA)      AND 
                   (CDTIPOOPER      = P_TIPOOPER)     AND 
                   (CDGRUPOFISC     = P_CDGRUPOFISC)  AND 
                   (CDIMPOSTO       = P_CDIMPOSTO)    AND 
                   (C.CDPRODUTO     = P.CDPRODUTO(+)) AND 
                   (INSTR(P_CDARVPROD,NVL(RTRIM(P.CDARVPROD),P_CDARVPROD)) = 1); 
            IF V_CONTADOR > 0 THEN 
               IF NOT(V_CDARVPROD IS NULL) THEN 
                  BEGIN 
                     SELECT C.CDSITUTRIBNFE, C.CDTPCRPICOSTNFE, C.CDNATUBCPCSTNFE, C.IDMOTIDESSTNFE, 
                            DECODE(I.IDMODBCNFE, '9', C.IDMODBCNFE, I.IDMODBCNFE) AS IDMODBCNFE, 
                            DECODE(I.IDMODBCSTNFE, '9', C.IDMODBCSTNFE, I.IDMODBCSTNFE) AS IDMODBCSTNFE 
                     INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                            V_IDMODBCNFE, V_IDMODBCSTNFE 
                     FROM   CODSITUTRIBNFE C, PRODUTO P, IMPOSTO I 
                     WHERE  (C.IDENTSAICSTNFE = P_ENTSAICST)    AND 
                            (C.CDEMPRESA      = P_EMPRESA)      AND 
                            (C.CDTIPOOPER     = P_TIPOOPER)     AND 
                            (C.CDGRUPOFISC    = P_CDGRUPOFISC)  AND 
                            (C.CDIMPOSTO        = P_CDIMPOSTO)    AND 
                            (C.CDPRODUTO      = P.CDPRODUTO(+)) AND 
                            (C.CDIMPOSTO      = I.CDIMPOSTO)    AND 
                            (P.CDARVPROD      = V_CDARVPROD); 
                  EXCEPTION 
                     WHEN No_Data_Found THEN 
                        V_CDSITUTRIBNFE   := NULL; 
                        V_IDMODBCNFE      := NULL; 
                        V_IDMODBCSTNFE    := NULL; 
                        V_CDTPCRPICOSTNFE := NULL; 
                        V_CDNATUBCPCSTNFE := NULL; 
                        V_IDMOTIDESSTNFE  := NULL; 
                  END; 
               ELSIF (V_CDARVPROD IS NULL) THEN 
                  BEGIN 
                     SELECT C.CDSITUTRIBNFE, C.CDTPCRPICOSTNFE, C.CDNATUBCPCSTNFE, C.IDMOTIDESSTNFE, 
                            DECODE(I.IDMODBCNFE, '9', C.IDMODBCNFE, I.IDMODBCNFE) AS IDMODBCNFE, 
                            DECODE(I.IDMODBCSTNFE, '9', C.IDMODBCSTNFE, I.IDMODBCSTNFE) AS IDMODBCSTNFE 
                     INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                            V_IDMODBCNFE, V_IDMODBCSTNFE 
                     FROM   CODSITUTRIBNFE C, IMPOSTO I 
                     WHERE  (I.CDIMPOSTO      = C.CDIMPOSTO)   AND 
                            (C.IDENTSAICSTNFE = P_ENTSAICST)   AND 
                            (C.CDEMPRESA      = P_EMPRESA)     AND 
                            (C.CDTIPOOPER     = P_TIPOOPER)    AND 
                            (C.CDGRUPOFISC    = P_CDGRUPOFISC) AND 
                            (C.CDIMPOSTO      = P_CDIMPOSTO)   AND 
                            (C.CDPRODUTO IS NULL); 
                  EXCEPTION 
                     WHEN No_Data_Found THEN 
                        V_CDSITUTRIBNFE   := NULL; 
                        V_IDMODBCNFE      := NULL; 
                        V_IDMODBCSTNFE    := NULL; 
                        V_CDTPCRPICOSTNFE := NULL; 
                        V_CDNATUBCPCSTNFE := NULL; 
                        V_IDMOTIDESSTNFE  := NULL; 
                  END; 
               END IF; 
            END IF; 
         ELSE 
            SELECT MAX(P.CDARVPROD), COUNT(*) 
            INTO   V_CDARVPROD, V_CONTADOR 
            FROM   CODSITUTRIBNFE C, PRODUTO P 
            WHERE  (IDENTSAICSTNFE = P_ENTSAICST) AND 
                   (CDEMPRESA      = P_EMPRESA)   AND 
                   (CDTIPOOPER     = P_TIPOOPER)  AND 
                   (CDIMPOSTO      = P_CDIMPOSTO) AND 
                   (C.CDPRODUTO    = P.CDPRODUTO) AND 
                   (CDGRUPOFISC IS NULL)          AND 
                   (INSTR(P_CDARVPROD,NVL(RTRIM(P.CDARVPROD),P_CDARVPROD)) = 1); 
            IF NOT(V_CDARVPROD IS NULL) THEN 
               BEGIN 
                  SELECT C.CDSITUTRIBNFE, C.CDTPCRPICOSTNFE, C.CDNATUBCPCSTNFE, C.IDMOTIDESSTNFE, 
                         DECODE(I.IDMODBCNFE, '9', C.IDMODBCNFE, I.IDMODBCNFE) AS IDMODBCNFE, 
                         DECODE(I.IDMODBCSTNFE, '9', C.IDMODBCSTNFE, I.IDMODBCSTNFE) AS IDMODBCSTNFE 
                  INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                         V_IDMODBCNFE, V_IDMODBCSTNFE 
                  FROM   CODSITUTRIBNFE C, PRODUTO P, IMPOSTO I 
                  WHERE  (C.IDENTSAICSTNFE = P_ENTSAICST)    AND 
                         (C.CDEMPRESA      = P_EMPRESA)      AND 
                         (C.CDTIPOOPER     = P_TIPOOPER)     AND 
                         (C.CDPRODUTO      = P.CDPRODUTO(+)) AND 
                         (I.CDIMPOSTO      = C.CDIMPOSTO)    AND 
                         (P.CDARVPROD      = V_CDARVPROD)    AND 
                         (C.CDIMPOSTO      = P_CDIMPOSTO)    AND 
                         (C.CDGRUPOFISC IS NULL); 
               EXCEPTION 
                  WHEN No_Data_Found THEN 
                     V_CDSITUTRIBNFE   := NULL; 
                     V_IDMODBCNFE      := NULL; 
                     V_IDMODBCSTNFE    := NULL; 
                     V_CDTPCRPICOSTNFE := NULL; 
                     V_CDNATUBCPCSTNFE := NULL; 
                     V_IDMOTIDESSTNFE  := NULL; 
               END; 
            ELSIF (V_CDARVPROD IS NULL) THEN 
               BEGIN 
                  SELECT C.CDSITUTRIBNFE, C.CDTPCRPICOSTNFE, C.CDNATUBCPCSTNFE, C.IDMOTIDESSTNFE, 
                         DECODE(I.IDMODBCNFE, '9', C.IDMODBCNFE, I.IDMODBCNFE) AS IDMODBCNFE, 
                         DECODE(I.IDMODBCSTNFE, '9', C.IDMODBCSTNFE, I.IDMODBCSTNFE) AS IDMODBCSTNFE 
                  INTO   V_CDSITUTRIBNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE, 
                         V_IDMODBCNFE, V_IDMODBCSTNFE 
                  FROM   CODSITUTRIBNFE C, IMPOSTO I 
                  WHERE  (I.CDIMPOSTO      = C.CDIMPOSTO) AND 
                         (C.IDENTSAICSTNFE = P_ENTSAICST) AND 
                         (C.CDEMPRESA      = P_EMPRESA)   AND 
                         (C.CDTIPOOPER     = P_TIPOOPER)  AND 
                         (C.CDIMPOSTO      = P_CDIMPOSTO) AND 
                         (C.CDGRUPOFISC IS NULL)          AND 
                         (C.CDPRODUTO IS NULL); 
               EXCEPTION 
                  WHEN No_Data_Found THEN 
                     V_CDSITUTRIBNFE   := NULL; 
                     V_IDMODBCNFE      := NULL; 
                     V_IDMODBCSTNFE    := NULL; 
                     V_CDTPCRPICOSTNFE := NULL; 
                     V_CDNATUBCPCSTNFE := NULL; 
                     V_IDMOTIDESSTNFE  := NULL; 
               END; 
            END IF; 
         END IF; 
      END IF; 
   END IF; 
    
   IF V_IDMOTIDESSTNFE IS NULL THEN 
      V_IDMOTIDESSTNFE := '9'; 
   END IF; 
   
   P_CDSITTRIBTMP    := V_CDSITUTRIBNFE; 
   P_IDMODBCNFE      := V_IDMODBCNFE; 
   P_IDMODBCSTNFE    := V_IDMODBCSTNFE; 
   P_CDTPCRPICOSTNFE := V_CDTPCRPICOSTNFE; 
   P_CDNATUBCPCSTNFE := V_CDNATUBCPCSTNFE; 
   P_IDMOTIDESSTNFE  := V_IDMOTIDESSTNFE; 
END; 
/