SET AUTOCOMMIT=0;

USE billing;

SELECT id INTO @sip_id FROM products WHERE class = 'sipaccount';
UPDATE billing_mappings SET product_id = NULL WHERE product_id = @sip_id;

SELECT id INTO @pbx_id FROM products WHERE class = 'pbxaccount';
UPDATE billing_mappings SET product_id = NULL WHERE product_id = @pbx_id;

DELETE FROM products WHERE class IN ('sipaccount', 'pbxaccount');

USE provisioning;
DROP TABLE voip_pbx_groups;

ALTER TABLE voip_subscribers 
  DROP COLUMN is_pbx_group;
ALTER TABLE voip_subscribers 
  CHANGE COLUMN pbx_group_id autoconf_group_id INT(11) UNSIGNED DEFAULT NULL;
ALTER TABLE voip_subscribers 
  ADD COLUMN autoconf_displayname VARCHAR(255) DEFAULT NULL;


SELECT id INTO @vpg_id FROM voip_preference_groups WHERE name = 'Internals';
UPDATE voip_preferences SET 
  voip_preference_groups_id = @vpg_id, 
  internal = 0 
WHERE attribute = 'cloud_pbx';

SELECT id INTO @hunt_id FROM voip_preferences WHERE attribute = 'cloud_pbx_hunt_policy';
DELETE FROM voip_preferences_enum WHERE preference_id = @hunt_id;
DELETE FROM voip_preferences WHERE attribute IN
  ('cloud_pbx_hunt_policy', 'cloud_pbx_hunt_timeout', 
   'cloud_pbx_hunt_group', 'cloud_pbx_base_cli');

DELETE FROM voip_preference_groups WHERE name = 'Cloud PBX';

COMMIT;
