ALTER TABLE contact add column canlogout varchar(3);
UPDATE contact SET canlogout = 'yes' where canlogout is NULL;
ALTER TABLE contact ALTER column canlogout SET not NULL;
ALTER TABLE contact ALTER column canlogout SET default 'yes';
alter table contact add constraint contact_canlogout CHECK (canlogout IN ('yes','no'));

-- logout a user from a given phone
-- logoutuser(phone)
CREATE or REPLACE FUNCTION  logoutuser(integer) RETURNS integer AS '
   DECLARE
       phonereboot varchar;
       _canlogout varchar; 
       contactid integer;
       pw integer;
       worktime integer;
       pevent integer;
       pcontact integer;
       ch record; -- channel
   BEGIN
       -- should phones be rebooted? Normally yes.
       SELECT INTO phonereboot value from astglobals where name=''phonereboot'';
       
       -- who is currently logged in to this phone?
       SELECT INTO contactid, _canlogout id, canlogout from contact where phone = $1 and status <> ''deleted'' limit 1;
       
       IF _canlogout <> ''yes'' THEN
         RAISE NOTICE ''user % can not logout'', contactid;
         return ''0'';
       END IF;

       update contact set phone = 1 where phone = $1;
       update channel set inuse = ''no'' where phone = $1;
       update channel set inuse = ''yes'' where phone = $1 and line = ''1'';
       IF contactid IS NOT NULL THEN
         SELECT INTO pevent, worktime event, cast(extract(epoch FROM CURRENT_TIMESTAMP(0) - stamp) as integer) from contacthist
               WHERE contact = contactid and event in (''40'', ''41'') order by stamp desc limit 1;
         if pevent = ''40'' THEN
           INSERT INTO contacthist (contact, stamp, event, int2, parm1)
                  VALUES (contactid, CURRENT_TIMESTAMP(0), ''41'', worktime, ''Logoff'');
         END IF;
         RAISE NOTICE ''user % logged out'', contactid;
         INSERT INTO activate(action, type, which, status, created )
                VALUES (''modify'', ''contact'', contactid, ''QUEUED'', NOW() );
         INSERT INTO history (created, creator, contact, type, lookup, lookupid, value) values
               (NOW(), contactid, contactid, ''update'', ''phone'', $1, ''logged off'');
       END IF;
       INSERT INTO activate(action, type, which, status, created )
              VALUES (''modify'', ''phone'', $1, ''QUEUED'', NOW() );
       FOR ch IN select id from channel where phone = $1 LOOP
         INSERT INTO activate(action, type, which, status, created )
                VALUES (''modify'', ''channel'', ch.id, ''QUEUED'', NOW() );
       END LOOP;
       IF phonereboot = ''yes'' THEN
         INSERT INTO activate(action, type, which, status, created )
                VALUES (''module'', ''phone-check-sync'', $1, ''QUEUED'', NOW() );
       END IF;

       SELECT INTO pw pairedwith from phone where id = $1;
       SELECT INTO pcontact id from contact where phone = pw and status <> ''deleted'' limit 1;

       --IF pcontact > ''1'' THEN
       --  PERFORM logoutuser(pw);
       --END IF;
       return ''1'';
   END;
' LANGUAGE plpgsql;

