CREATE OR REPLACE FUNCTION obterQuantEmprestimosDentroDoPrazo(p_personid integer)
RETURNS bigint AS
/*************************************************************************************
  NAME: obterQuantEmprestimos
  DESCRIPTION: Obtém a quantidade de empréstimos que a pessoa possui no gnuteca.
**************************************************************************************/
$BODY$ 
BEGIN
    RETURN ( SELECT count(*)
               FROM gtcloan
              WHERE personid = p_personid
                AND returnforecastdate::date >= now()::date
                AND returndate is null);
END; 
$BODY$ 
language plpgsql;
