CREATE OR REPLACE FUNCTION obterQuantMultas(p_personid integer)
RETURNS bigint AS
/*************************************************************************************
  NAME: obterQuantMultas
  DESCRIPTION: Obtém a quantidade de multas que a pessoa possui no gnuteca.
**************************************************************************************/
$BODY$ 
BEGIN
    RETURN ( SELECT COUNT(*)
               FROM gtcfine f
          LEFT JOIN gtcloan l
                 ON f.loanid = l.loanid
              WHERE finestatusid = ( SELECT value 
                                       FROM basconfig 
                                      WHERE parameter = 'ID_FINESTATUS_OPEN' )::int
                AND personid = p_personid );
END; 
$BODY$ 
language plpgsql;
