 -- N O N   P O L Y C O M   P H O N E S
 create or replace function migrate_non_polycom_phones() returns integer as '
 declare
-  phone record;
+  my_phone record;
   my_group_id int;
   my_phone_type record;
   next_id int;

select dblink_connect('dbname=PDS');

-- todo migrate folder and folder values

select migrate_settings();
select migrate_user_groups();

  insert into phone_type_migration values (''xpressa_strongarm_vxworks'', ''genericPhone'', null);
  insert into phone_type_migration values (''ixpressa_x86_win32'', ''genericPhone'', null);

  for my_phone in select * from 
      dblink(''select p.id, p.serial_number, p.pg_id, p.usrs_id, pt.model 
      from logical_phones p, phone_types pt where p.pt_id = pt.id'') 
      as (id int, serial_number text, pg_id int, usrs_id int, pt_model text) loop

    select into my_phone_type * from phone_type_migration where pds_model = my_phone.pt_model; 

    raise notice ''importing phone %, %...'', my_phone.serial_number, my_phone_type.bean_id;

    insert into phone (phone_id, serial_number, bean_id, model_id) 
      values (my_phone.id, my_phone.serial_number, my_phone_type.bean_id, my_phone_type.model_id);

    select into my_group_id group_id from phone_group_migration where pds_group_id = my_phone.pg_id;

    insert into phone_group (phone_id, group_id) values (my_phone.id, my_group_id);

    if my_phone.usrs_id is not null 
    then
      insert into line (line_id, phone_id, position, user_id) 
        values (nextval(''line_seq''), my_phone.id, 0, my_phone.usrs_id);
    end if;

    -- todo, phone settings, only cisco? very difficult
