CREATE or REPLACE FUNCTION logoutagent(integer,integer,integer) RETURNS integer AS '
   DECLARE
       res integer;
       qid integer;
       plid integer;
       qcid integer;
       compid integer;
       uid ALIAS for $1;
       qline ALIAS for $2;
       fromext ALIAS for $3;
   BEGIN
-- get the company id
       select into compid company from contact where id = uid;

       select into qid queue.id
       from   queue left join phoneline on phoneline.id = queue.phoneline
       where  extension = qline and queue.status != ''deleted'' and queue.company = compid;

       IF qid IS NULL THEN
           RAISE NOTICE ''queue % not found'', qline;
           return ''1''; -- error: queue not found, but still logged off
       END IF;

-- okay, the queue is real, now log us out
       if fromext = 1 THEN
         SELECT into plid id
         FROM   phoneline where function = ''contact'' and lookupid = uid and status != ''deleted''
         limit  1;
       else
         SELECT into plid id
         FROM   phoneline where extension = fromext and function = ''contact'' and lookupid = uid and status != ''deleted''
         limit  1;
       end if;

       select into qcid queue_contact.id from queue_contact where (contact = uid and phoneline = plid and queue = qid);
       IF qcid IS NULL THEN
           RAISE NOTICE ''user % alreaddy logged out of queue %'', uid, qline;
           return ''1'';
       END IF;

       delete from queue_contact where id = qcid;
       RAISE NOTICE ''user % logged out of queue %'', uid, qline;
       INSERT INTO history (created, creator, contact, type, lookup, lookupid, value) values
             (NOW(), uid, uid, ''update'', ''queue'', qid, ''logged off this queue'');
       INSERT INTO history (created, creator, contact, type, lookup, lookupid, value) values
             (NOW(), uid, uid, ''update'', ''contact'', uid, ''logged out of queue '' || qline);
       -- note: must be in this order: first queue_contact, then queue
       INSERT INTO activate(action, type, which, status, created )
              VALUES (''delete'', ''queue_contact'', qcid, ''QUEUED'', NOW() );
       INSERT INTO activate(action, type, which, status, created )
              VALUES (''modify'', ''queue'', qid, ''QUEUED'', NOW() );
       return ''1'';
   END;
' LANGUAGE plpgsql;

CREATE or REPLACE FUNCTION loginagent(integer,integer,integer) RETURNS integer AS '
   DECLARE
       res integer;
       qid integer;
       plid integer;
       dep_only varchar(3);
       cdid integer;
       qcid integer;
       compid integer;
       uid ALIAS for $1;
       qline ALIAS for $2;
       fromext ALIAS for $3;
   BEGIN

-- get the company id
       select into compid company from contact where id = uid;

-- see if we already logged in
       SELECT INTO res queue_contact.id
       from  queue_contact left join queue on queue.id = queue_contact.queue
                         left join phoneline on phoneline.id = queue.phoneline
       where phoneline.extension = qline and phoneline.company = compid and 
             queue_contact.contact = uid and queue.status != ''deleted'';

       IF res IS NOT NULL THEN
           RAISE NOTICE ''user % already logged into queue %, queue_contact.id = %'', uid, qline, res;
           return ''1''; -- success
       END IF;

-- not already logged in. Find the proper queue from the phoneline id.
       select into qid queue.id
       from   queue left join phoneline on phoneline.id = queue.phoneline
       where  phoneline.extension = qline and queue.company = compid and queue.status != ''deleted'';

       IF qid IS NULL THEN
           RAISE NOTICE ''queue % not found'', qline;
           return ''0''; -- error: queue not found
       END IF;
-- okay, the queue is real, now log us in.
       if fromext = 1 THEN
         SELECT into plid id
         FROM   phoneline where function = ''contact'' and lookupid = uid and status != ''deleted''
         limit  1;
       else
         SELECT into plid id
         FROM   phoneline where extension = fromext and function = ''contact'' and lookupid = uid and phoneline.status != ''deleted''
         limit  1;
       end if;

       IF plid IS NULL THEN
           RAISE NOTICE ''phoneline % not found for user %'', fromext, uid;
           return ''0''; -- our phoneline is not found
       END IF;

       SELECT into dep_only queue.department_only
         FROM queue
        WHERE id = qid and department_only = ''no'';

       IF dep_only IS NULL THEN
         SELECT into cdid contact_department.id
           FROM contact_department, queue
          WHERE queue.id = qid AND queue.department = contact_department.department
            AND contact_department.contact = uid;

         IF cdid IS NULL THEN
             RAISE NOTICE ''contact % not in same department as queue %'', uid, qid;
             return ''0'';
         END IF;
       END IF;

       insert into queue_contact(queue, contact, phoneline) values (qid, uid, plid);
       select into qcid queue_contact.id from queue_contact where (contact=uid and queue=qid);
       RAISE NOTICE ''user % logged in to queue %'', uid, qline;
       INSERT INTO activate(action, type, which, status, created ) 
              VALUES (''add'', ''queue_contact'', qcid, ''QUEUED'', NOW() );
       INSERT INTO history (created, creator, contact, type, lookup, lookupid, value) values
             (NOW(), uid, uid, ''update'', ''queue'', qid, ''logged on to this queue'');
       INSERT INTO history (created, creator, contact, type, lookup, lookupid, value) values
             (NOW(), uid, uid, ''update'', ''contact'', uid, ''logged on to queue '' || qline);
       INSERT INTO activate(action, type, which, status, created )
              VALUES (''modify'', ''queue'', qid, ''QUEUED'', NOW() );
       return ''1'';
   END;
' LANGUAGE plpgsql;
