CREATE OR REPLACE FUNCTION RETORNA_INDICE_ACUMULADO (
   P_CDINDICE       IN MOVINDICE.CDINDICE%TYPE,
   P_DTREFIND       IN MOVINDICE.DTREFIND%TYPE,
   P_IDVALOR        IN VARCHAR2) RETURN NUMBER
AS
   V_CALCULO     NUMBER;  
BEGIN   
   IF P_IDVALOR = 'A' THEN
      FOR C_INDICE IN (SELECT ROUND(((VRINDICE / 100) + 1),8) AS VRCALCULO
                         FROM MOVINDICE
                        WHERE CDINDICE = P_CDINDICE
                          AND DTREFIND BETWEEN TO_DATE('01/01/'||TO_CHAR(TO_DATE(P_DTREFIND),'YYYY')) AND TO_DATE(P_DTREFIND)) LOOP
         V_CALCULO := ROUND(NVL(V_CALCULO,1) * C_INDICE.VRCALCULO,8);         
      END LOOP;
      V_CALCULO := ROUND((NVL(V_CALCULO,1) - 1)*100,4);
   ELSE 
      FOR C_INDICE IN (SELECT ROUND(((VRINDICE / 100) + 1),8) AS VRCALCULO
                         FROM MOVINDICE
                        WHERE CDINDICE = P_CDINDICE
                          AND DTREFIND BETWEEN ADD_MONTHS(TO_DATE(P_DTREFIND),-11) AND TO_DATE(P_DTREFIND)) LOOP
         V_CALCULO := ROUND(NVL(V_CALCULO,1) * C_INDICE.VRCALCULO,8);         
      END LOOP;
      V_CALCULO := ROUND((NVL(V_CALCULO,1) - 1)*100,4);
   END IF;
   RETURN V_CALCULO;
END;
/