-- Copyright (c) 1997-now, NeoNova BV, The Netherlands. All rights Reserved.
--
-- This software is the copyrighted work of NeoNova BV.
-- Use of the software is subject to the general business terms and conditions of NeoNova BV
-- and its related transaction documents like license sheets or other written notices.#
-- Any reproduction or redistribution of the software not in accordance with the license terms
-- is expressly prohibited by law, and may result in severe civil and criminally penalties.
-- Violators will be prosecuted to the maximum extent possible.

CREATE OR REPLACE FUNCTION insert_company() RETURNS trigger SECURITY DEFINER AS '
  DECLARE
    list record;
  BEGIN
    insert into phonetypebutton (company, phonetype, button, number, label, function) VALUES 
                                (NEW.id, ''110'', 1, '''', '''', ''HOLD'');
    insert into phonetypebutton (company, phonetype, button, number, label, function) VALUES 
                                (NEW.id, ''110'', 2, '''', '''', ''TRANSFER'');
    insert into phonetypebutton (company, phonetype, button, number, label, function) VALUES 
                                (NEW.id, ''110'', 3, '''', '''', ''MUTE'');
    insert into phonetypebutton (company, phonetype, button, number, label, function) VALUES 
                                (NEW.id, ''110'', 4, '''', '''', ''DND'');

    insert into phonetypebutton (company, phonetype, button, number, label, function) VALUES (NEW.id, ''46'', 1, '''', '''', ''mynumber'');
    insert into phonetypebutton (company, phonetype, button, number, label, function) VALUES (NEW.id, ''46'', 2, '''', '''', ''myextra'');
    insert into phonetypebutton (company, phonetype, button, number, label, function) VALUES (NEW.id, ''46'', 3, '''', '''', ''F_CONFERENCE'');
    insert into phonetypebutton (company, phonetype, button, number, label, function) VALUES (NEW.id, ''46'', 4, '''', '''', ''F_REDIAL'');
    insert into phonetypebutton (company, phonetype, button, number, label, function) VALUES (NEW.id, ''46'', 5, '''', '''', ''F_TRANSFER'');
    insert into phonetypebutton (company, phonetype, button, number, label, function) VALUES (NEW.id, ''46'', 6, '''', '''', ''none'');

    -- Aastra 6753i
    insert into phonetypebutton (company, phonetype, button, number, label, function) VALUES (NEW.id, ''93'', 1, '''', '''', ''record'');
    insert into phonetypebutton (company, phonetype, button, number, label, function) VALUES (NEW.id, ''93'', 2, '''', '''', ''F_DIRECTORY'');
    insert into phonetypebutton (company, phonetype, button, number, label, function) VALUES (NEW.id, ''93'', 3, '''', '''', ''callers'');
    insert into phonetypebutton (company, phonetype, button, number, label, function) VALUES (NEW.id, ''93'', 4, ''*25'', '''', ''speed'');
    insert into phonetypebutton (company, phonetype, button, number, label, function) VALUES (NEW.id, ''93'', 5, '''', '''', ''F_DND'');
    insert into phonetypebutton (company, phonetype, button, number, label, function) VALUES (NEW.id, ''93'', 6, '''', '''', ''F_TRANSFER'');

    insert into contacttype (name, company, description) values (''default'', NEW.id, ''Default'');
    insert into contacttype (name, company, description) values (''internal'', NEW.id, ''Internal Call'');

    insert into profile (name, company) values (''Default'', NEW.id);
    insert into presence (name, company) values (''Available'', NEW.id);
    insert into role(name, company) values (''Default'', NEW.id);

    -- give this new company all system extensions
    IF NEW.id > 3 THEN
      FOR list in SELECT extension, function, callerid from phoneline where phoneline.company = 3 and function = ''system'' LOOP
        insert into phoneline (company, extension, function, callerid) VALUES (NEW.id, list.extension, ''system'', list.callerid);
      END LOOP;
    END IF;
    -- and phonetype defaults
    IF NEW.id > 2 THEN
      FOR list in SELECT id, hash_xfer_in, hash_xfer_out, qualify, dtmfmode, vol_speaker, vol_speaker_mic, 
                         vol_handset, vol_handset_mic, vol_headset, vol_headset_mic from phonetype LOOP
        insert into phonetypephone (company, phonetype,  hash_xfer_in, hash_xfer_out, qualify, dtmfmode, vol_speaker, 
                                    vol_speaker_mic, vol_handset, vol_handset_mic, vol_headset, vol_headset_mic) VALUES 
                                   (NEW.id, list.id,  list.hash_xfer_in, list.hash_xfer_out, list.qualify, list.dtmfmode, list.vol_speaker,
                                   list.vol_speaker_mic, list.vol_handset, list.vol_handset_mic, list.vol_headset, list.vol_headset_mic);
      END LOOP;
    END IF;

    RETURN NEW;
  END;
' LANGUAGE plpgsql;

drop trigger insert_company on company;
CREATE TRIGGER insert_company AFTER INSERT ON company FOR EACH ROW EXECUTE PROCEDURE insert_company();

