BEGIN;

-- return 'yes' if I am allowed to barge on the extension of another person
CREATE or REPLACE FUNCTION canbarge(integer,varchar) RETURNS varchar AS '
   DECLARE
       allow varchar;
       bargeable varchar;
       comp integer;
       uid ALIAS for $1;
       ext ALIAS for $2;
   BEGIN

       select into allow contact.allow_barge
       from   contact where id = uid;

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

       select into bargeable contact.bargeable
       from   phoneline left join contact on contact.id = phoneline.lookupid
       where  phoneline.extension = ext and phoneline.company = comp and contact.company = comp and phoneline.status <> ''deleted'';

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

       return bargeable;
   END;
' LANGUAGE plpgsql;

COMMIT;
