CREATE OR REPLACE FUNCTION obterQuantPenalidades(p_personid integer)
RETURNS bigint AS
/*************************************************************************************
  NAME: obterQuantPenalidades
  DESCRIPTION: Obtém a quantidade de penalidades que a pessoa possui no gnuteca.
**************************************************************************************/
$BODY$ 
BEGIN
    RETURN ( SELECT COUNT(*)
               FROM gtcpenalty
              WHERE coalesce( penaltyEndDate > now(), penaltyEndDate IS NULL )
                AND personid = p_personid );
END; 
$BODY$ 
language plpgsql;
