/*
@C:\WEB_565\Sql\End_Conv\Oracle\5061_FISSTPO_CONSULTA_FISCAL_PARAM.SQL

Descrição:
Procedure para consulta dos parametros fiscais

*/

CREATE OR REPLACE PROCEDURE CONSULTA_FISCAL_PARAM(
   P_CDEMPRESA     IN EMPRESA.CDEMPRESA%TYPE,
   P_IDENTRSAID    IN INCIDENCIA.IDENTRSAIDIN%TYPE,
   P_CDTIPOOPER    IN INCIDENCIA.CDTIPOOPER%TYPE,
   P_CDINSCESTA    IN INCIDENCIA.CDINSCESTA%TYPE,
   P_CDPRODINI     IN PRODUTO.CDARVPROD%TYPE,
   P_CDPRODFIM     IN PRODUTO.CDARVPROD%TYPE,
   P_INIVALID      IN ALIQUOTA.DTINIVALID%TYPE,
   P_CDIMPOSTO     IN ALIQUOTA.CDIMPOSTO%TYPE,
   P_FILIAL        IN FILIAL.CDFILIAL%TYPE,
   P_LANCTONF      IN NOTAFISCAL.NRLANCTONF%TYPE,
   P_DESTNF	  	    IN VARCHAR2,
   P_CDFILIAL	    IN VARCHAR2,
   P_TCDFILIAL     IN VARCHAR2,
   P_CDFORNECED    IN VARCHAR2,
   P_TCDFORNECED   IN VARCHAR2,
   P_CDCLIENTE     IN VARCHAR2,
   P_TCDCLIENTE    IN VARCHAR2,
   P_FILIORIG      IN VARCHAR2,
   P_VALORFAIXA    IN DOUBLE PRECISION
) IS
	V_INCITEMP    	VARCHAR2(14);
	V_CDGRUPOFISC   VARCHAR2(03);
	V_PAISORIG	    VARCHAR2(04);
	V_ESTAORIG	    VARCHAR2(02);
	V_MUNIORIG      VARCHAR2(04);

	/* PROCEDIMENTO INTERNO DE CONSULTA */
    PROCEDURE CONSULTA_PROD_PARAM(
		P_INCITEMP     IN VARCHAR2,
		P_CDPRODINI    IN VARCHAR2,
		P_CDPRODFIM    IN VARCHAR2,
		P_INIVALID     IN ALIQUOTA.DTINIVALID%TYPE,
		P_CDEMPRESA    IN EMPRESA.CDEMPRESA%TYPE,
		P_CDINSCESTA   IN INCIDENCIA.CDINSCESTA%TYPE,
		P_CDIMPOSTO    IN ALIQUOTA.CDIMPOSTO%TYPE,
		P_IDENTRSAIDOP IN INCIDENCIA.IDENTRSAIDIN%TYPE,
		P_CDTIPOOPER   IN INCIDENCIA.CDTIPOOPER%TYPE,
		P_CDGRUPOFISC  IN GRUPOFISCAL.CDGRUPOFISC%TYPE,
		P_CDCLIENTE    IN VARCHAR2,
		P_CDFORNECED   IN VARCHAR2,
		P_CDFILIEMIT   IN VARCHAR2,
		P_CDFILIRECE   IN VARCHAR2,
		P_IDENDCFOP	   IN VARCHAR2,
		P_IDENDEMIS    IN VARCHAR2,
		P_PAISORIG	   IN VARCHAR2,
		P_ESTAORIG     IN VARCHAR2,
		P_MUNIORIG	   IN VARCHAR2,
      P_VALORFAIXA   IN DOUBLE PRECISION
	) IS
   		V_CDIMPOTEMP VARCHAR2(02);
   		V_CDFISCTEMP VARCHAR2(01);
   		V_SITUTRIBPR VARCHAR2(03);
   		V_PAISDEST	 VARCHAR2(04);
   		V_ESTADEST	 VARCHAR2(02);
   		V_MUNIDEST   VARCHAR2(04);
   		V_PAISCFOP   VARCHAR2(04);
   		V_ESTACFOP   VARCHAR2(02);
   		V_IDINTEREST VARCHAR2(01);
		   V_IDINTERNA	 VARCHAR2(01);
		   V_CDSITTRIBTMP    CODSITUTRIBNFE.CDSITUTRIBNFE%TYPE;
   		V_IDMODBCNFE      CODSITUTRIBNFE.IDMODBCNFE%TYPE;
   		V_IDMOTIDESSTNFE  CODSITUTRIBNFE.IDMOTIDESSTNFE%TYPE;
   		V_IDMODBCSTNFE    CODSITUTRIBNFE.IDMODBCSTNFE%TYPE;
   		V_CDTPCRPICOSTNFE CODSITUTRIBNFE.CDTPCRPICOSTNFE%TYPE;
   		V_CDNATUBCPCSTNFE CODSITUTRIBNFE.CDNATUBCPCSTNFE%TYPE;
   		V_FISOPEITEM   VARCHAR2(11);
   		V_SUFXCFOPPR   VARCHAR2(03);
   		V_PBCSUBTRIB   NUMBER;
   		V_ALIQUTMP     NUMBER;
   		V_PREDBCTMP    NUMBER;
         V_CDREGRATMP   VARCHAR2(10);
         V_CDGRUPOFISCAUX VARCHAR2(03);
         V_IDUTMUNIORIG   VARCHAR2(01);
         V_COUNT          INTEGER;
         V_NRSEQTMPFISC   VARCHAR(20);
    BEGIN

    	IF P_CDCLIENTE IS NOT NULL THEN
    		SELECT MAX(CDPAIS), MAX(SGESTADO), MAX(CDMUNICIPIO)
              INTO V_PAISDEST, V_ESTADEST, V_MUNIDEST
              FROM ENDECLIE
             WHERE (CDCLIENTE    = P_CDCLIENTE)
               AND (IDTPENDECLIE = P_IDENDEMIS);

            IF P_IDENDCFOP = 'I' THEN
               V_PAISCFOP := P_PAISORIG;
               V_ESTACFOP := P_ESTAORIG;
      		ELSE
            	SELECT MAX(CDPAIS), MAX(SGESTADO)
            	  INTO V_PAISCFOP, V_ESTACFOP
            	  FROM ENDECLIE
            	 WHERE (CDCLIENTE    = P_CDCLIENTE)
                  AND (IDTPENDECLIE = P_IDENDCFOP);
      		END IF;

    	ELSE
    		IF P_CDFORNECED IS NOT NULL THEN
    			SELECT MAX(CDPAIS), MAX(SGESTADO), MAX(CDMUNICIPIO)
              INTO V_PAISDEST, V_ESTADEST, V_MUNIDEST
              FROM ENDEFORN
             WHERE (CDFORNECED   = P_CDFORNECED)
               AND (IDTPENDEFORN = P_IDENDEMIS);

	         IF P_IDENDCFOP = 'I' THEN
	            V_PAISCFOP := P_PAISORIG;
	            V_ESTACFOP := P_ESTAORIG;
	      	ELSE
	            SELECT MAX(CDPAIS), MAX(SGESTADO)
	              INTO V_PAISCFOP, V_ESTACFOP
	              FROM ENDEFORN
	             WHERE (CDFORNECED   = P_CDFORNECED)
	               AND (IDTPENDEFORN = P_IDENDCFOP);
	      	END IF;

    		ELSE
    			IF P_CDFILIEMIT IS NOT NULL THEN
					IF P_IDENDEMIS = 'I' THEN
						V_PAISDEST := P_PAISORIG;
						V_ESTADEST := P_ESTAORIG;
						V_MUNIDEST := P_MUNIORIG;
					ELSE
    					SELECT MAX(CDPAIS), MAX(SGESTADO), MAX(CDMUNICIPIO)
              		  INTO V_PAISDEST, V_ESTADEST, V_MUNIDEST
              		  FROM ENDEFILI
             		 WHERE (CDFILIAL     = P_CDFILIEMIT)
               	   AND (IDTPENDEFILI = P_IDENDEMIS);
               END IF;
        			IF P_IDENDCFOP = 'I' THEN
						V_PAISCFOP := P_PAISORIG;
						V_ESTACFOP := P_ESTAORIG;
          		ELSE
	             	SELECT MAX(CDPAIS), MAX(SGESTADO)
	             	  INTO V_PAISCFOP, V_ESTACFOP
	             	  FROM ENDEFILI
	             	 WHERE (CDFILIAL     = P_CDFILIRECE)
	                  AND (IDTPENDEFILI = P_IDENDCFOP);
	       		END IF;
    			ELSE
    				IF P_CDFILIRECE IS NOT NULL THEN
    					IF P_IDENDEMIS = 'I' THEN
    						SELECT MAX(CDPAIS), MAX(SGESTADO), MAX(CDMUNICIPIO)
    						  INTO V_PAISDEST, V_ESTADEST, V_MUNIDEST
    						  FROM INSCRESTAD I, FILIAL F
    						 WHERE F.CDEMPRESA  = I.CDEMPRESA
    						   AND F.CDINSCESTA = I.CDINSCESTA
    						   AND F.CDFILIAL   = P_CDFILIRECE;
    					ELSE
	    					SELECT MAX(CDPAIS), MAX(SGESTADO), MAX(CDMUNICIPIO)
	              		  INTO V_PAISDEST, V_ESTADEST, V_MUNIDEST
	              		  FROM ENDEFILI
	             		 WHERE (CDFILIAL     = P_CDFILIRECE)
	               	   AND (IDTPENDEFILI = P_IDENDEMIS);
                  END IF;

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

    	V_IDINTEREST := 'N';
   		V_IDINTERNA  := 'N';
   		IF ((V_PAISDEST = P_PAISORIG) AND (V_ESTADEST <> P_ESTAORIG)) THEN
      		V_IDINTEREST := 'S';
   		ELSIF (V_PAISDEST <> P_PAISORIG) THEN
      		V_IDINTERNA := 'S';
   		END IF;

      FOR C_PRODUTO IN (
			SELECT P.CDARVPROD, P.CDPRODUTO
			  FROM PRODUTO P, PRODFILI PF
			 WHERE CDARVPROD BETWEEN P_CDPRODINI AND P_CDPRODFIM
            AND IDPRODATIVPR = 'S'
            AND PF.CDFILIAL =  DECODE(P_IDENTRSAIDOP, 'S', P_CDFILIEMIT, P_CDFILIRECE)
            AND P.CDPRODUTO = PF.CDPRODUTO
            AND PF.IDPRODATIV = 'A'
            AND P.CDNVPRODUTO = (SELECT MAX(CDNVPRODUTO)
                                   FROM PRODUTO)
		)
		LOOP

         NOVO_CODIGO('TMPFISCALPARAM', V_NRSEQTMPFISC);
         V_NRSEQTMPFISC := SUBSTR(V_NRSEQTMPFISC,11,10);

			CONSULTA_INCIDENCIA(P_IDENTRSAIDOP, P_CDEMPRESA, C_PRODUTO.CDARVPROD, P_CDINSCESTA, P_CDTIPOOPER, SUBSTR(P_INCITEMP, 1, 4), SUBSTR(P_INCITEMP, 5, 10), P_CDGRUPOFISC, NULL, V_IDINTEREST, 'DF'||P_IDENTRSAIDOP, P_CDFILIRECE, P_CDFORNECED, P_CDCLIENTE, P_INIVALID, P_CDFILIEMIT, P_VALORFAIXA, 'S');

			SELECT MAX(CDIMPOTEMP), MAX(CDFISCTEMP), MAX(CDREGRATMP), MAX(IDUTMUNIORIG)
	 		  INTO V_CDIMPOTEMP, V_CDFISCTEMP, V_CDREGRATMP, V_IDUTMUNIORIG
	 		  FROM INCITEMP IT, IMPOSTO IP
			 WHERE (IT.NRINCITEMP = P_INCITEMP)
	  		   AND (IT.CDIMPOTEMP = P_CDIMPOSTO)
            AND (IP.CDIMPOSTO  = IT.CDIMPOTEMP);

			INSERT INTO
				TMP_FISCAL_PARAM
            (NRSEQTMPFIS,    CDIMPOSTO,   CDEMPRESA,   IDENTRSAIDOP,   CDTIPOOPER,   CDINSCESTA,   CDPRODUTO,           CDGRUPOFISC,   CDFISCAL,     CDCLIENTE,   CDFORNECED,   CDFILIEMIT,   CDFILIRECE,   CDREGRATMP)
			VALUES
				(V_NRSEQTMPFISC, P_CDIMPOSTO, P_CDEMPRESA, P_IDENTRSAIDOP, P_CDTIPOOPER, P_CDINSCESTA, C_PRODUTO.CDPRODUTO, P_CDGRUPOFISC, V_CDFISCTEMP, P_CDCLIENTE, P_CDFORNECED, P_CDFILIEMIT, P_CDFILIRECE, V_CDREGRATMP);

         IF V_CDREGRATMP IS NOT NULL THEN


            SELECT MAX(CDSITUTRIB)
              INTO V_SITUTRIBPR
              FROM TABLE(CONSULTA_REGRA_FISCAL('00', P_CDEMPRESA, P_CDGRUPOFISC, P_IDENTRSAIDOP, P_CDTIPOOPER, P_CDINSCESTA, C_PRODUTO.CDARVPROD, P_INIVALID,
                                                P_PAISORIG, P_ESTAORIG, P_MUNIORIG, V_PAISDEST, V_ESTADEST, V_MUNIDEST, P_VALORFAIXA, 'N'))
             WHERE VRALIQUOTA IS NOT NULL;

            IF V_SITUTRIBPR IS NOT NULL THEN
               UPDATE TMP_FISCAL_PARAM
                  SET CDSITUTRIB   = V_SITUTRIBPR
                WHERE NRSEQTMPFIS = V_NRSEQTMPFISC;
            END IF;

            SELECT MAX(CDSITUTRIB)
              INTO V_CDSITTRIBTMP
              FROM TABLE(CONSULTA_REGRA_FISCAL(P_CDIMPOSTO, P_CDEMPRESA, P_CDGRUPOFISC, P_IDENTRSAIDOP, P_CDTIPOOPER, P_CDINSCESTA, C_PRODUTO.CDARVPROD, P_INIVALID,
                                               P_PAISORIG, P_ESTAORIG, P_MUNIORIG, V_PAISDEST, V_ESTADEST, V_MUNIDEST, P_VALORFAIXA, 'N'))
             WHERE VRALIQUOTA IS NOT NULL;

            IF V_CDSITTRIBTMP IS NOT NULL THEN
               UPDATE TMP_FISCAL_PARAM
                  SET CDSITUTRIBNFE = V_CDSITTRIBTMP
                WHERE NRSEQTMPFIS = V_NRSEQTMPFISC;
            END IF;

            SELECT MAX(CDFISCOPER)
              INTO V_FISOPEITEM
              FROM TABLE(CONSULTA_REGRA_FISCAL(null, P_CDEMPRESA, V_CDGRUPOFISC, P_IDENTRSAIDOP, P_CDTIPOOPER, P_CDINSCESTA, C_PRODUTO.CDARVPROD, P_INIVALID,
                                               P_PAISORIG, P_ESTAORIG, NULL, V_PAISCFOP, V_ESTACFOP, NULL, P_VALORFAIXA, 'S'))
             WHERE CDFISCOPER IS NOT NULL;

            IF V_FISOPEITEM IS NOT NULL THEN
               UPDATE TMP_FISCAL_PARAM
                  SET CDCFOP 	 	 = V_FISOPEITEM
                WHERE NRSEQTMPFIS = V_NRSEQTMPFISC;
            END IF;

            SELECT MAX(VRALIQUOTA), MAX(VRPERREDBC), MAX(VRPBCSUBTRIB)
              INTO V_ALIQUTMP, V_PREDBCTMP, V_PBCSUBTRIB
              FROM TABLE(CONSULTA_REGRA_FISCAL(P_CDIMPOSTO, P_CDEMPRESA, V_CDGRUPOFISC, P_IDENTRSAIDOP, P_CDTIPOOPER, P_CDINSCESTA, C_PRODUTO.CDARVPROD, P_INIVALID,
                                               P_PAISORIG, P_ESTAORIG, P_MUNIORIG, V_PAISDEST, V_ESTADEST, V_MUNIDEST, P_VALORFAIXA, 'N'))
             WHERE VRALIQUOTA IS NOT NULL;


            UPDATE TMP_FISCAL_PARAM
               SET VRALIQUOTA 	= V_ALIQUTMP,
                   VRREDUCAOBC   = V_PREDBCTMP,
                   VRPBCSUBTRIB  = V_PBCSUBTRIB,
                   CDPAISORIG  	= P_PAISORIG,
                   SGESTAORIG    = P_ESTAORIG,
                   CDMUNIORIG    = P_MUNIORIG,
                   CDPAISDEST  	= V_PAISDEST,
                   SGESTADEST    = V_ESTADEST,
                   CDMUNIDEST    = V_MUNIDEST
             WHERE NRSEQTMPFIS = V_NRSEQTMPFISC;

         ELSE

            CONSULTA_CST(P_IDENTRSAIDOP, P_CDEMPRESA, C_PRODUTO.CDARVPROD, P_CDINSCESTA, P_CDTIPOOPER, SUBSTR(P_INCITEMP, 1, 4), SUBSTR(P_INCITEMP, 5, 10), P_CDGRUPOFISC, V_IDINTEREST, V_IDINTERNA, 'DF'||P_IDENTRSAIDOP);

            SELECT MAX(CDSITTRIBTMP)
              INTO V_SITUTRIBPR
              FROM CSTTEMP
             WHERE NRCSTTEMP = P_INCITEMP;

            IF V_SITUTRIBPR IS NOT NULL THEN
               UPDATE TMP_FISCAL_PARAM
                  SET CDSITUTRIB   = V_SITUTRIBPR
                WHERE NRSEQTMPFIS = V_NRSEQTMPFISC;
            END IF;

            CONSULTA_CST_NFE(P_IDENTRSAIDOP, P_CDEMPRESA, C_PRODUTO.CDARVPROD, P_CDINSCESTA, P_CDTIPOOPER, P_CDGRUPOFISC, P_CDIMPOSTO, V_IDINTEREST, V_IDINTERNA, SUBSTR(P_INCITEMP, 1, 4), SUBSTR(P_INCITEMP, 5, 10), '',  V_CDSITTRIBTMP, V_IDMODBCNFE, V_IDMODBCSTNFE, V_CDTPCRPICOSTNFE, V_CDNATUBCPCSTNFE, V_IDMOTIDESSTNFE);

            IF V_CDSITTRIBTMP IS NOT NULL THEN
               UPDATE TMP_FISCAL_PARAM
                  SET CDSITUTRIBNFE = V_CDSITTRIBTMP
                WHERE NRSEQTMPFIS = V_NRSEQTMPFISC;
            END IF;

            CONSULTA_CFOP(P_IDENTRSAIDOP, P_CDEMPRESA, C_PRODUTO.CDARVPROD, P_CDINSCESTA, P_CDTIPOOPER, P_PAISORIG, P_ESTAORIG,
                          V_PAISCFOP, V_ESTACFOP, SUBSTR(P_INCITEMP, 1, 4), SUBSTR(P_INCITEMP, 5, 10), V_CDGRUPOFISC, P_INIVALID);

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

            IF V_FISOPEITEM IS NOT NULL THEN
               UPDATE TMP_FISCAL_PARAM
                  SET CDCFOP 	 	 = V_FISOPEITEM
                WHERE NRSEQTMPFIS = V_NRSEQTMPFISC;
            END IF;

            IF V_IDUTMUNIORIG = 'D' THEN
               CONSULTA_ALIQUOTA(P_CDIMPOSTO, P_IDENTRSAIDOP, P_CDEMPRESA, C_PRODUTO.CDARVPROD, P_CDINSCESTA, P_PAISORIG, P_ESTAORIG, V_PAISDEST, V_ESTADEST, V_MUNIDEST, P_VALORFAIXA, P_INIVALID, SUBSTR(P_INCITEMP, 1, 4), SUBSTR(P_INCITEMP, 5, 10), V_CDGRUPOFISC, P_CDTIPOOPER, 'DF'||P_IDENTRSAIDOP, 'S', P_VALORFAIXA, P_CDFILIRECE, P_CDFORNECED, P_CDCLIENTE);
            ELSE
               CONSULTA_ALIQUOTA(P_CDIMPOSTO, P_IDENTRSAIDOP, P_CDEMPRESA, C_PRODUTO.CDARVPROD, P_CDINSCESTA, P_PAISORIG, P_ESTAORIG, V_PAISDEST, V_ESTADEST, P_MUNIORIG, P_VALORFAIXA, P_INIVALID, SUBSTR(P_INCITEMP, 1, 4), SUBSTR(P_INCITEMP, 5, 10), V_CDGRUPOFISC, P_CDTIPOOPER, 'DF'||P_IDENTRSAIDOP, 'S', P_VALORFAIXA, P_CDFILIRECE, P_CDFORNECED, P_CDCLIENTE);
            END IF;

            SELECT   MAX(VRPREDBCTMP),  MAX(VRALIQUTMP), MAX(VRPBCSTTMP)
              INTO   V_PREDBCTMP,       V_ALIQUTMP, 	 V_PBCSUBTRIB
              FROM ALIQTEMP
             WHERE (NRALIQTEMP = P_INCITEMP);


            UPDATE TMP_FISCAL_PARAM
               SET VRALIQUOTA 	= V_ALIQUTMP,
                   VRREDUCAOBC   = V_PREDBCTMP,
                   VRPBCSUBTRIB  = V_PBCSUBTRIB,
                   CDPAISORIG  	= P_PAISORIG,
                   SGESTAORIG    = P_ESTAORIG,
                   CDMUNIORIG    = P_MUNIORIG,
                   CDPAISDEST  	= V_PAISDEST,
                   SGESTADEST    = V_ESTADEST,
                   CDMUNIDEST    = V_MUNIDEST
             WHERE NRSEQTMPFIS = V_NRSEQTMPFISC;

         END IF;
			DELETE INCITEMP
		    WHERE NRINCITEMP = P_INCITEMP;

		   DELETE CSTTEMP
		    WHERE NRCSTTEMP  = P_INCITEMP;

		   DELETE CFOPTEMP
		    WHERE NRCFOPTEMP = P_INCITEMP;

		   DELETE ALIQTEMP
		    WHERE NRALIQTEMP = P_INCITEMP;

		END LOOP;
    END;
BEGIN
	DELETE FROM TMP_FISCAL_PARAM
	 WHERE CDEMPRESA    = P_CDEMPRESA
	   AND CDINSCESTA   = P_CDINSCESTA
	   AND CDIMPOSTO    = P_CDIMPOSTO
	   AND CDTIPOOPER   = P_CDTIPOOPER
	   AND IDENTRSAIDOP = P_IDENTRSAID;

	V_INCITEMP := P_FILIAL || P_LANCTONF;

    SELECT MAX(CDPAIS), MAX(SGESTADO), MAX(CDMUNICIPIO)
      INTO V_PAISORIG, V_ESTAORIG, V_MUNIORIG
      FROM INSCRESTAD
     WHERE CDEMPRESA  = P_CDEMPRESA
       AND CDINSCESTA = P_CDINSCESTA;

    IF P_DESTNF = 'C' THEN
    	FOR C_DESTINATARIO IN (
    		SELECT DISTINCT CDCLIENTE, NVL(IDENDCFOPCLI,'P') IDENDCFOPCLI, NVL(IDENDEMISCLI,'P') IDENDEMISCLI
              FROM CLIENTE
             WHERE (INSTR(P_CDCLIENTE, '_'||CDCLIENTE||'_') > 0) OR (P_TCDCLIENTE = 'T')
               AND IDCLIEATIV = 'S'
    	)
    	LOOP
         SELECT MAX(G.CDGRUPOFISC)
           INTO V_CDGRUPOFISC
           FROM GRUPOFISCIMPFIL G
          WHERE G.CDFILIAL = P_FILIORIG
            AND NOT G.CDGRUPOFISC IS NULL
             AND G.CDIMPOSTO = P_CDIMPOSTO;

    		SELECT NVL(MAX(CDGRUPOFISC), V_CDGRUPOFISC)
    		  INTO V_CDGRUPOFISC
  			  FROM GRFISIMPIECLIE
 			 WHERE CDIMPOSTO  = P_CDIMPOSTO
   			AND CDEMPRESA  = P_CDEMPRESA
   			AND CDINSCESTA = P_CDINSCESTA
   			AND CDCLIENTE  = C_DESTINATARIO.CDCLIENTE
   			AND CDGRUPOFISC IS NOT NULL;



   		IF V_CDGRUPOFISC IS NULL THEN
    		   CONSULTA_GRUPOFISCAL('C', C_DESTINATARIO.CDCLIENTE, NULL, P_IDENTRSAID, P_CDTIPOOPER, V_CDGRUPOFISC);
       	END IF;

         IF (P_IDENTRSAID = 'S') THEN
       		CONSULTA_PROD_PARAM(V_INCITEMP, P_CDPRODINI, P_CDPRODFIM, P_INIVALID, P_CDEMPRESA, P_CDINSCESTA, P_CDIMPOSTO, P_IDENTRSAID, P_CDTIPOOPER, V_CDGRUPOFISC, C_DESTINATARIO.CDCLIENTE, NULL, P_FILIORIG, NULL, C_DESTINATARIO.IDENDCFOPCLI, C_DESTINATARIO.IDENDEMISCLI, V_PAISORIG, V_ESTAORIG, V_MUNIORIG, P_VALORFAIXA);
         ELSE
            CONSULTA_PROD_PARAM(V_INCITEMP, P_CDPRODINI, P_CDPRODFIM, P_INIVALID, P_CDEMPRESA, P_CDINSCESTA, P_CDIMPOSTO, P_IDENTRSAID, P_CDTIPOOPER, V_CDGRUPOFISC, C_DESTINATARIO.CDCLIENTE, NULL, NULL, P_FILIORIG, C_DESTINATARIO.IDENDCFOPCLI, C_DESTINATARIO.IDENDEMISCLI, V_PAISORIG, V_ESTAORIG, V_MUNIORIG, P_VALORFAIXA);
         END IF;
      END LOOP;
    ELSE
    	IF P_DESTNF = 'F' THEN
			FOR C_DESTINATARIO IN (
    			SELECT DISTINCT CDFORNECED, NVL(IDENDCFOPFOR, 'P') IDENDCFOPFOR, NVL(IDENDEMISFOR, 'P') IDENDEMISFOR
              	  FROM FORNECEDOR
             	 WHERE (INSTR(P_CDFORNECED, '_'||CDFORNECED||'_') > 0) OR (P_TCDFORNECED = 'T')
                  AND IDSITUCADA = 'A'
    		)
    		LOOP
            SELECT MAX(G.CDGRUPOFISC)
              INTO V_CDGRUPOFISC
              FROM GRUPOFISCIMPFIL G
             WHERE G.CDFILIAL = P_FILIORIG
               AND NOT G.CDGRUPOFISC IS NULL
               AND G.CDIMPOSTO = P_CDIMPOSTO;

	    		SELECT NVL(MAX(CDGRUPOFISC), V_CDGRUPOFISC)
	    		  INTO V_CDGRUPOFISC
	  			  FROM GRFISIMPIEFORN
	 			 WHERE CDIMPOSTO  = P_CDIMPOSTO
	   			AND CDEMPRESA  = P_CDEMPRESA
	   			AND CDINSCESTA = P_CDINSCESTA
	   			AND CDFORNECED = C_DESTINATARIO.CDFORNECED
	   			AND CDGRUPOFISC IS NOT NULL;

	   		IF V_CDGRUPOFISC IS NULL THEN
    				CONSULTA_GRUPOFISCAL('F', C_DESTINATARIO.CDFORNECED, NULL, P_IDENTRSAID, P_CDTIPOOPER, V_CDGRUPOFISC);
    			END IF;

            IF P_IDENTRSAID = 'S' THEN
       			CONSULTA_PROD_PARAM(V_INCITEMP,  P_CDPRODINI, P_CDPRODFIM, P_INIVALID, P_CDEMPRESA, P_CDINSCESTA, P_CDIMPOSTO, P_IDENTRSAID, P_CDTIPOOPER, V_CDGRUPOFISC, NULL, C_DESTINATARIO.CDFORNECED, P_FILIORIG, NULL, C_DESTINATARIO.IDENDCFOPFOR, C_DESTINATARIO.IDENDEMISFOR, V_PAISORIG, V_ESTAORIG, V_MUNIORIG, P_VALORFAIXA);
            ELSE
               CONSULTA_PROD_PARAM(V_INCITEMP,  P_CDPRODINI, P_CDPRODFIM, P_INIVALID, P_CDEMPRESA, P_CDINSCESTA, P_CDIMPOSTO, P_IDENTRSAID, P_CDTIPOOPER, V_CDGRUPOFISC, NULL, C_DESTINATARIO.CDFORNECED, NULL, P_FILIORIG, C_DESTINATARIO.IDENDCFOPFOR, C_DESTINATARIO.IDENDEMISFOR, V_PAISORIG, V_ESTAORIG, V_MUNIORIG, P_VALORFAIXA);
            END IF;
    		END LOOP;
    	ELSE
    		IF P_DESTNF = 'L' THEN
    			FOR C_DESTINATARIO IN (
    				SELECT DISTINCT CDFILIAL, NVL(IDENDCFOPFIL,'I') IDENDCFOPFIL, NVL(IDENDEMISFIL,'I') IDENDEMISFIL
              	  FROM FILIAL
             	 WHERE (INSTR(P_CDFILIAL, '_'||CDFILIAL||'_') > 0) OR (P_TCDFILIAL = 'T')
                  AND IDCNTRRESC = 'N'
    			)
    			LOOP
	    			SELECT MAX(CDGRUPOFISC)
		    	     INTO V_CDGRUPOFISC
		  			  FROM GRUPOFISCIMPFIL
		 			 WHERE CDIMPOSTO  = P_CDIMPOSTO
		   	      AND CDFILIAL   = P_FILIORIG
		   		   AND CDGRUPOFISC IS NOT NULL;

	   			IF V_CDGRUPOFISC IS NULL THEN
    					CONSULTA_GRUPOFISCAL('L', C_DESTINATARIO.CDFILIAL, NULL, P_IDENTRSAID, P_CDTIPOOPER, V_CDGRUPOFISC);
    				END IF;
    				IF P_IDENTRSAID = 'E' THEN
    					CONSULTA_PROD_PARAM(V_INCITEMP,  P_CDPRODINI, P_CDPRODFIM, P_INIVALID, P_CDEMPRESA, P_CDINSCESTA, P_CDIMPOSTO, P_IDENTRSAID, P_CDTIPOOPER, V_CDGRUPOFISC, NULL, NULL,  C_DESTINATARIO.CDFILIAL, P_FILIORIG, C_DESTINATARIO.IDENDCFOPFIL, C_DESTINATARIO.IDENDEMISFIL, V_PAISORIG, V_ESTAORIG, V_MUNIORIG, P_VALORFAIXA);
    				ELSE
    					CONSULTA_PROD_PARAM(V_INCITEMP,  P_CDPRODINI, P_CDPRODFIM, P_INIVALID, P_CDEMPRESA, P_CDINSCESTA, P_CDIMPOSTO, P_IDENTRSAID, P_CDTIPOOPER, V_CDGRUPOFISC, NULL, NULL, P_FILIORIG, C_DESTINATARIO.CDFILIAL, C_DESTINATARIO.IDENDCFOPFIL, C_DESTINATARIO.IDENDEMISFIL, V_PAISORIG, V_ESTAORIG, V_MUNIORIG, P_VALORFAIXA);
    				END IF;
    			END LOOP;
    		END IF;
    	END IF;
    END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('CONSULTA_FISCAL_PARAM','5.33.xxx'); END;
/