alter table contact add column bargeable character varying(3);
update contact set bargeable = 'yes' where bargeable IS NULL;
alter table contact alter column bargeable set default 'no';
alter table contact alter column bargeable set not NULL;
alter table contact add constraint contact_bargeable CHECK (bargeable IN ('yes', 'no'));

alter table contact add column allow_barge character varying(3);
update contact set allow_barge = 'no' where allow_barge IS NULL;
alter table contact alter column allow_barge set default 'no';
alter table contact alter column allow_barge set not NULL;
alter table contact add constraint contact_allow_barge CHECK (allow_barge IN ('yes', 'no'));

-- 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;
       uid ALIAS for $1;
       ext ALIAS for $2;
   BEGIN

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

       select into bargeable contact.bargeable
       from   phoneline left join contact on contact.id = phoneline.contact
       where  phoneline.extension = ext 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;

