-- return 'yes' if I am allowed to page another person
CREATE or REPLACE FUNCTION canpage(integer,varchar) RETURNS varchar AS '
   DECLARE
       roleid integer;
       allow varchar;
       pageable varchar;
       comp integer;
       uid ALIAS for $1;
       ext ALIAS for $2;
   BEGIN

       select into roleid, comp contact.role, contact.company
       from   contact where contact.id = uid;

       select into allow role.allow_page
       from role where role.id = roleid;

       IF  allow = ''no'' THEN
       --  RAISE NOTICE ''not allowed to page other people'';
           return ''no'';
       END IF;

       select into roleid contact.role
       from   phoneline left join contact on contact.id = phoneline.lookupid
       where  phoneline.extension = ext and contact.company = comp and phoneline.company = comp and phoneline.status <> ''deleted'';
       select into pageable role.pageable from role where role.id = roleid;
       return pageable;
   END;
' LANGUAGE plpgsql;

