CREATE or REPLACE FUNCTION canpage(integer,varchar,integer) RETURNS varchar AS '
   DECLARE
       roleid integer;
       allow varchar;
       pageable varchar;
       isreceptionist varchar;
       uid ALIAS for $1;
       ext ALIAS for $2;
       comp ALIAS for $3;
   BEGIN

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

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

       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, isreceptionist role.pageable, role.isreceptionist from role where role.id = roleid;

       IF  isreceptionist = ''yes'' THEN
           RAISE NOTICE ''receptionists can always be paged because of the switchbaord'';
           return ''yes'';
       END IF;

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

       RAISE NOTICE ''pageable = %'', pageable;
       return pageable;
   END;
' LANGUAGE plpgsql;

