#ifdef KPI
/*  $Revision$
 *  $Id$
 */

-- Hulpfuncties voor berekenen normscores en bijbehorende kleuren in de grafiek
CREATE OR REPLACE PACKAGE kpi
AS
   -- Gegeven berekende score en normdrempels, bereken normscore
   FUNCTION getNormScore(pScore           IN NUMBER,
                         p000             IN NUMBER,
                         p070             IN NUMBER,
                         p090             IN NUMBER,
                         p100             IN NUMBER)
      RETURN NUMBER;
   -- Gegeven berekende score en normdrempels, bepaal kleur van grafiek (rood, oranje, groen)
   FUNCTION getKleur(pScore               IN NUMBER,
                     p000                 IN NUMBER,
                     p070                 IN NUMBER,
                     p090                 IN NUMBER,
                     p100                 IN NUMBER)
      RETURN VARCHAR2;
   -- Gegeven normscore, bepaal kleur van grafiek (rood, oranje, groen)
   FUNCTION getKleurNorm(pNormScore       IN NUMBER)
      RETURN VARCHAR2;
END kpi;
/

CREATE OR REPLACE PACKAGE BODY kpi
AS

   FUNCTION getNormScore(pScore           IN NUMBER,
                         p000             IN NUMBER,
                         p070             IN NUMBER,
                         p090             IN NUMBER,
                         p100             IN NUMBER)
      RETURN NUMBER
   IS
      v_normScore NUMBER;
   BEGIN
      IF (p100 < p090)
      THEN
         v_normScore := CASE
                           WHEN pScore < p100
                              THEN 100
                           WHEN pScore < p090
                              THEN  90 + (100-90) * (p090 - pScore) / (p090-p100)
                           WHEN pScore < p070
                              THEN  70 + ( 90-70) * (p070 - pScore) / (p070-p090)
                           WHEN pScore < p000
                              THEN       ( 70- 0) * (p000 - pScore) / (p000-p070)
                           ELSE
                                     0
                        END;
      ELSE
         v_normScore := CASE
                           WHEN pScore > p100
                              THEN 100
                           WHEN pScore > p090
                              THEN  90 + (100-90) * (pScore - p090) / (p100-p090)
                           WHEN pScore > p070
                              THEN  70 + ( 90-70) * (pScore - p070) / (p090-p070)
                           WHEN pScore > p000
                              THEN       ( 70- 0) * (pScore - p000) / (p070-p000)
                           ELSE
                                     0
                        END;
      END IF;

      RETURN v_normScore;
   END getNormScore;

   -- Gegeven berekende score en normdrempels, bepaal kleur van grafiek (rood, oranje, groen)
   FUNCTION getKleur(pScore               IN NUMBER,
                     p000                 IN NUMBER,
                     p070                 IN NUMBER,
                     p090                 IN NUMBER,
                     p100                 IN NUMBER)
      RETURN VARCHAR2
   IS
      v_kleur        VARCHAR2(7);
   BEGIN
      v_kleur :=  CASE
                     WHEN pScore < p100
                        THEN '#55EE00'
                     WHEN pScore < p090
                        THEN '#77BB00'
                     WHEN pScore < p070
                        THEN '#EE9900'
                     WHEN pScore < p000
                        THEN '#CC0000'
                     ELSE
                             '#000000'
                  END;
      RETURN v_kleur;
   END getKleur;

   -- Gegeven normscore, bepaal kleur van grafiek (rood, oranje, groen)
   FUNCTION getKleurNorm(pNormScore       IN NUMBER)
      RETURN VARCHAR2
   IS
      v_kleur        VARCHAR2(7);
   BEGIN
      v_kleur :=  CASE
                     WHEN pNormScore > 100
                        THEN '#55EE00'
                     WHEN pNormScore >  90
                        THEN '#77BB00'
                     WHEN pNormScore >  70
                        THEN '#EE9900'
                     WHEN pNormScore >   0
                        THEN '#CC0000'
                     ELSE
                             '#000000'
                  END;
      RETURN v_kleur;
   END getKleurNorm;

END kpi;
/


REGISTERRUN('$Id$')

#endif // KPI
