-- Call Forward Unconditional Set
-- $1 = shortname
-- $2 = extension
-- $3 = when unconditional value
-- $4 = userid
CREATE or REPLACE FUNCTION  cfwd_unconditional_set(varchar, varchar, varchar, varchar) RETURNS INTEGER AS '
  DECLARE
    _id int;
    _profile int;
    _contact int;
    _when_unconditional varchar;
    _when_unconditional_number varchar;
  BEGIN
    select into _id, _profile, _contact, _when_unconditional, _when_unconditional_number                                              phoneline.id, phoneline.profile, phoneline.contact, phoneline_forwarding.when_unconditional,                               phoneline_forwarding.when_unconditional_number
      from phoneline, phoneline_forwarding
     where phoneline.company = ( select id from company  where shortname = $1 and status = ''active'') and
           extension = $2 and phoneline.status <> ''deleted''
       and phoneline.id = phoneline_forwarding.phoneline and phoneline.profile = phoneline_forwarding.profile;
    IF _when_unconditional_number = '''' AND $3 = ''forward'' THEN
      return ''0'';
    ELSE
      update phoneline_forwarding set when_unconditional = $3 where profile = _profile and phoneline = _id;
      insert into activate(action, type, which, status, created ) VALUES (''modify'', ''phoneline'', _id, ''QUEUED'', NOW() );
      insert into history (created, creator, contact, type, lookup, lookupid, value) values
             (now(), $4::text::int4, _contact, ''update'', ''phoneline'', _id,
             ''when_unconditional from '' ||  quote_literal(_when_unconditional) || ''to '' || quote_literal($3));
      return ''0'';
    END IF;
  END;
' LANGUAGE plpgsql;

CREATE or REPLACE FUNCTION  cfwd_unconditional_setnumber(varchar, varchar, varchar, varchar) RETURNS INTEGER AS '
  DECLARE
    _id int;
    _profile int;
    _contact int; 
    _when_unconditional_number varchar;
  BEGIN
    select into _id, _profile, _contact, _when_unconditional_number phoneline.id, phoneline.profile, phoneline.contact, phoneline.when_unconditional_number
      from phoneline 
     where phoneline.company = ( select id from company  where shortname = $1 and status = ''active'') and
           extension = $2 and phoneline.status <> ''deleted'';
    update phoneline_forwarding set when_unconditional_number = $3 where profile = _profile and phoneline = _id;
    insert into activate(action, type, which, status, created ) VALUES (''modify'', ''phoneline'', _id, ''QUEUED'', NOW() );
    insert into history (created, creator, contact, type, lookup, lookupid, value) values
           (now(), $4::text::int4, _contact, ''update'', ''phoneline'', _id,
           ''when_unconditional_number from '' ||  quote_literal(_when_unconditional_number) || ''to '' || quote_literal($3));
    return ''0'';
  END;
' LANGUAGE plpgsql;

-- Call Forward Busy Set
CREATE or REPLACE FUNCTION  cfwd_busy_set(varchar, varchar, varchar, varchar) RETURNS INTEGER AS '
  DECLARE
    _id int;
    _profile int;
    _contact int;
    _when_busy varchar;
  BEGIN
    select into _id, _profile, _contact, _when_busy phoneline.id, phoneline.profile, phoneline.contact, phoneline.when_busy
      from phoneline 
     where phoneline.company = ( select id from company  where shortname = $1 and status = ''active'') and
           extension = $2 and phoneline.status <> ''deleted'';
    update phoneline_forwarding set when_busy = $3 where profile = _profile and phoneline = _id;
    insert into activate(action, type, which, status, created ) VALUES (''modify'', ''phoneline'', _id, ''QUEUED'', NOW() );
    insert into history (created, creator, contact, type, lookup, lookupid, value) values 
           (now(), $4::text::int4, _contact, ''update'', ''phoneline'', _id,       
           ''when_busy from '' ||  quote_literal(_when_busy) || ''to '' || quote_literal($3));
    return ''0'';
  END; 
' LANGUAGE plpgsql;

CREATE or REPLACE FUNCTION  cfwd_busy_setnumber(varchar, varchar, varchar, varchar) RETURNS INTEGER AS '
  DECLARE 
    _id int;
    _profile int;
    _contact int; 
    _when_busy_number varchar;
  BEGIN
    select into _id, _profile, _contact, _when_busy_number phoneline.id, phoneline.profile, phoneline.contact, phoneline.when_busy_number
      from phoneline
     where phoneline.company = ( select id from company  where shortname = $1 and status = ''active'') and
           extension = $2 and phoneline.status <> ''deleted'';
    update phoneline_forwarding set when_busy_number = $3 where profile = _profile and phoneline = _id;
    insert into activate(action, type, which, status, created ) VALUES (''modify'', ''phoneline'', _id, ''QUEUED'', NOW() );
    insert into history (created, creator, contact, type, lookup, lookupid, value) values
           (now(), $4::text::int4, _contact, ''update'', ''phoneline'', _id,
           ''when_busy_number from '' ||  quote_literal(_when_busy_number) || ''to '' || quote_literal($3));
    return ''0'';
  END; 
' LANGUAGE plpgsql;

-- Call Forward No Answer Set
CREATE or REPLACE FUNCTION  cfwd_noanswer_set(varchar, varchar, varchar, varchar) RETURNS INTEGER AS '
  DECLARE
    _id int;
    _profile int;
    _contact int;
    _when_noanswer varchar;
  BEGIN
    select into _id, _profile, _contact, _when_noanswer phoneline.id, phoneline.profile, phoneline.contact, phoneline.when_noanswer
      from phoneline
     where phoneline.company = ( select id from company  where shortname = $1 and status = ''active'') and
           extension = $2 and phoneline.status <> ''deleted'';
    update phoneline_forwarding set when_noanswer = $3 where profile = _profile and phoneline = _id;
    insert into activate(action, type, which, status, created ) VALUES (''modify'', ''phoneline'', _id, ''QUEUED'', NOW() );
    insert into history (created, creator, contact, type, lookup, lookupid, value) values
           (now(), $4::text::int4, _contact, ''update'', ''phoneline'', _id,
           ''when_noanswer from '' ||  quote_literal(_when_noanswer) || ''to '' || quote_literal($3));
    return ''0'';
  END;
' LANGUAGE plpgsql;

CREATE or REPLACE FUNCTION  cfwd_noanswer_setnumber(varchar, varchar, varchar, varchar) RETURNS INTEGER AS '
  DECLARE 
    _id int;
    _profile int;
    _contact int; 
    _when_noanswer_number varchar;
  BEGIN
    select into _id, _profile, _contact, _when_noanswer_number phoneline.id, phoneline.profile, phoneline.contact, phoneline.when_noanswer_number
      from phoneline
     where phoneline.company = ( select id from company  where shortname = $1 and status = ''active'') and
           extension = $2 and phoneline.status <> ''deleted'';
    update phoneline_forwarding set when_noanswer_number = $3 where profile = _profile and phoneline = _id;
    insert into activate(action, type, which, status, created ) VALUES (''modify'', ''phoneline'', _id, ''QUEUED'', NOW() );
    insert into history (created, creator, contact, type, lookup, lookupid, value) values
           (now(), $4::text::int4, _contact, ''update'', ''phoneline'', _id,
           ''when_noanswer_number from '' ||  quote_literal(_when_noanswer_number) || ''to '' || quote_literal($3));
    return ''0''; 
  END; 
' LANGUAGE plpgsql;

-- Call Forward Logged Off Set
CREATE or REPLACE FUNCTION  cfwd_loggedoff_set(varchar, varchar, varchar, varchar) RETURNS INTEGER AS '
  DECLARE
    _id int;
    _profile int;
    _contact int;
    _when_loggedoff varchar;
  BEGIN
    select into _id, _profile, _contact, _when_loggedoff phoneline.id, phoneline.profile, phoneline.contact, phoneline.when_loggedoff
      from phoneline
     where phoneline.company = ( select id from company  where shortname = $1 and status = ''active'') and
           extension = $2 and phoneline.status <> ''deleted'';
    update phoneline_forwarding set when_loggedoff = $3 where profile = _profile and phoneline = _id;
    insert into activate(action, type, which, status, created ) VALUES (''modify'', ''phoneline'', _id, ''QUEUED'', NOW() );
    insert into history (created, creator, contact, type, lookup, lookupid, value) values
           (now(), $4::text::int4, _contact, ''update'', ''phoneline'', _id,
           ''when_loggedoff from '' ||  quote_literal(_when_loggedoff) || ''to '' || quote_literal($3));
    return ''0'';
  END;
' LANGUAGE plpgsql;

CREATE or REPLACE FUNCTION  cfwd_loggedoff_setnumber(varchar, varchar, varchar, varchar) RETURNS INTEGER AS '
  DECLARE 
    _id int;
    _profile int;
    _contact int; 
    _when_loggedoff_number varchar;
  BEGIN
    select into _id, _profile, _contact, _when_loggedoff_number phoneline.id, phoneline.profile, phoneline.contact, phoneline.when_loggedoff_number
      from phoneline
     where phoneline.company = ( select id from company  where shortname = $1 and status = ''active'') and
           extension = $2 and phoneline.status <> ''deleted'';
    update phoneline_forwarding set when_loggedoff_number = $3 where profile = _profile and phoneline = _id;
    insert into activate(action, type, which, status, created ) VALUES (''modify'', ''phoneline'', _id, ''QUEUED'', NOW() );
    insert into history (created, creator, contact, type, lookup, lookupid, value) values
           (now(), $4::text::int4, _contact, ''update'', ''phoneline'', _id,
           ''when_loggedoff_number from '' ||  quote_literal(_when_loggedoff_number) || ''to '' || quote_literal($3));
    return ''0''; 
  END; 
' LANGUAGE plpgsql;

