CREATE OR REPLACE FUNCTION USR_GET_COR_CRONOGRAMA
   (P_CDNIVELITPRJ IN CRONOPROJETO.CDNIVELITPRJ%TYPE,
    P_IDTIPOPROJETO IN PROJETO.IDTIPOPROJETO%TYPE,
    P_IDSITATIVTAR IN PLANATIVTARE.IDSITATIVTAR%TYPE,
    P_IDMARCOPROJ IN CRONOPROJETO.IDMARCOPROJ%TYPE,
    P_DTFIPATIVTAR IN PLANATIVTARE.DTFIPATIVTAR%TYPE,
    P_DTFIRATIVTAR IN PLANATIVTARE.DTFIRATIVTAR%TYPE,
    P_DTFINITCRPRJ IN CRONOPROJETO.DTFINITCRPRJ%TYPE,
    P_VRPERCONCPRJ IN CRONOPROJETO.VRPERCONCPRJ%TYPE) RETURN VARCHAR
IS
   V_COLOR VARCHAR2(10);
   
BEGIN
   V_COLOR := 'DEFAULT';

   IF P_CDNIVELITPRJ = '3' THEN
      IF (P_IDSITATIVTAR <> 'P') THEN
         IF (P_IDSITATIVTAR = 'C') OR (P_IDSITATIVTAR = 'X') THEN
            V_COLOR := '#CCCCCC';
         ELSE
            IF (P_IDSITATIVTAR = 'A') THEN
               V_COLOR := 'green';
            ELSE
               IF (P_IDSITATIVTAR = 'I') THEN
                  V_COLOR := 'navy';
               END IF;
            END IF;
         END IF;
      END IF;

      IF (NOT P_DTFIPATIVTAR IS NULL) THEN
         IF ((P_IDSITATIVTAR = 'I') OR (P_IDSITATIVTAR = 'P')) THEN
            IF (P_DTFINITCRPRJ IS NULL) OR (P_DTFINITCRPRJ < TRUNC(SYSDATE)) THEN
               V_COLOR := 'red';
            END IF;
         END IF;
      END IF;

      IF (NOT P_DTFIRATIVTAR IS NULL) AND 
         ((P_IDSITATIVTAR <> 'A') AND (P_IDSITATIVTAR <> 'C')) THEN
         V_COLOR := 'blue';
      END IF;   
   END IF;

   IF P_CDNIVELITPRJ = '1' THEN
      IF (P_VRPERCONCPRJ = 100) OR (P_IDSITATIVTAR = 'X') THEN
         V_COLOR := '#CCCCCC';
      END IF;   
   ELSIF P_CDNIVELITPRJ = '2' THEN 
      IF P_IDMARCOPROJ = 'S' THEN
         V_COLOR := 'blue';
      END IF;
      IF (P_VRPERCONCPRJ = 100) OR (P_IDSITATIVTAR = 'X') THEN
         V_COLOR := '#CCCCCC';
      END IF;   
   ELSIF ((P_IDTIPOPROJETO = '4') AND (P_CDNIVELITPRJ = 'T')) THEN
      IF (P_IDSITATIVTAR = 'X') OR (P_VRPERCONCPRJ = 100) THEN
         V_COLOR := '#CCCCCC';
      END IF;
   END IF;

   RETURN(V_COLOR);
   
END;
/
