CREATE TABLE phone_client_item_$mid
(
	id int(11) NOT NULL auto_increment,
	cid int(11) NOT NULL default '0',
	object_id INT NOT NULL,
	type tinyint(4) NOT NULL default '0',
	source_id int(11) NOT NULL default '0',
	date1 date default NULL,
	date2 date default NULL,
	alias varchar(100) NOT NULL default '',
	comment text NOT NULL,
	PRIMARY KEY  (id),
	KEY date1 (date1),
	KEY date2 (date2),
	KEY cid (cid),
	KEY object_id (object_id),
	KEY source_id (source_id)
);

CREATE TABLE phone_dest_$mid
(
	id int(11) NOT NULL auto_increment, 
	title varchar(100) NOT NULL default '',
	PRIMARY KEY  (id)
);

CREATE TABLE phone_item_number_$mid
(
	id int(11) NOT NULL auto_increment,
	item_id int(11) NOT NULL default '0',
	number varchar(50) NOT NULL default '',
	PRIMARY KEY  (id),
	KEY number (number),
	KEY item_id (item_id)
);

CREATE TABLE phone_item_port_$mid
(
	id int(11) NOT NULL auto_increment,
	item_id int(11) NOT NULL default '0',
	port varchar(10) NOT NULL default '',
	PRIMARY KEY  (id),
	KEY item_id (item_id),
	KEY port (port)
);

CREATE TABLE time_range_$mid
( 
	id int(11) NOT NULL auto_increment, 
	title varchar(200) NOT NULL default '',
	PRIMARY KEY  (id) 
);

CREATE TABLE IF NOT EXISTS phone_tariff_zone_$mid
(
	id int(11) NOT NULL auto_increment,
	title varchar(100) NOT NULL default '',
	PRIMARY KEY  (id) 
);

CREATE TABLE IF NOT EXISTS phone_geographic_code_$mid
(
	code varchar(20) NOT NULL,
	dest_id int(11) NOT NULL,
	start_level TINYINT NOT NULL,
	end_level TINYINT NOT NULL,
	PRIMARY KEY  (code) 
);

CREATE TABLE IF NOT EXISTS phone_zone_map_$mid
(
	id int(11) NOT NULL auto_increment,
	title varchar(200) NOT NULL,
	PRIMARY KEY (id) 
);

CREATE TABLE IF NOT EXISTS phone_zone_map_item_$mid
(
	map_id int(11) NOT NULL,
	code varchar(20) NOT NULL,
	zone_id int(11) NOT NULL,
	UNIQUE KEY map_code (map_id,code),
	KEY zone_id (zone_id) 
);

CREATE TABLE time_range_$mid ( 
	id int(11) NOT NULL auto_increment,
	title varchar(200) NOT NULL default '',
	PRIMARY KEY  (id) 
);

ALTER TABLE phone_client_item_$mid ADD alias VARCHAR(100) NOT NULL DEFAULT '';

ALTER TABLE phone_geographic_code_$mid ADD start_level TINYINT NOT NULL NULL DEFAULT 0;
ALTER TABLE phone_geographic_code_$mid ADD end_level TINYINT NOT NULL NULL DEFAULT 0;

CREATE TABLE  number_resource_$mid (
  id int(11) NOT NULL auto_increment,
  date1 date,
  date2 date,
  category_id int(11) NOT NULL,
  number varchar(100) NOT NULL,
  PRIMARY KEY  (id),
  KEY category_id (category_id),
  KEY number (number) );

CREATE TABLE  number_resource_category_$mid (
  id int(11) NOT NULL auto_increment,
  title varchar(200) NOT NULL,
  comment varchar(250) NOT NULL,
  parent_id int(11) NOT NULL,
  PRIMARY KEY  (id) );

CREATE TABLE  number_resource_use_$mid (
  resource_id int(11) NOT NULL,
  date1 date default NULL,
  date2 date default NULL,
  cid int(11) NOT NULL,
  KEY resource_id (resource_id) );
  
CREATE TABLE phone_item_rule_data_$mid (
  `item_id` int(11) NOT NULL,
  `oper_fromports` text,
  `oper_toports` text,
  `oper_fromprefs` text,
  `oper_toprefs` text,
  `oper_service_id` int(11) NOT NULL default '0',
  `oper_not_fromprefs` text NOT NULL,
  `oper_not_toprefs` text NOT NULL,
  `oper_from_range` text NOT NULL,
  `oper_to_range` text NOT NULL,
  `oper_not_from_range` text NOT NULL,
  `oper_not_to_range` text NOT NULL,
  `fromports_regexp` text NOT NULL,
  `toports_regexp` text NOT NULL,
  UNIQUE KEY `item_id` (`item_id`)
);

-- #BLOCK#
DELETE FROM phone_item_rule_data_$mid;

CREATE TABLE _phone_rule_data
SELECT item.id, item.oper_fromports, item.oper_toports, item.oper_fromprefs,
 item.oper_toprefs, item.oper_service_id, item.oper_not_fromprefs, item.oper_not_toprefs,
 item.oper_from_range, item.oper_to_range,
 item.oper_not_from_range, item.oper_not_to_range, item_rule.item_id
FROM phone_client_item_$mid AS item
LEFT JOIN phone_item_rule_data_$mid AS item_rule ON item.id=item_rule.item_id
WHERE item.type=2 
HAVING item_rule.item_id IS NULL;

INSERT INTO phone_item_rule_data_$mid 
(item_id, oper_fromports, oper_toports, oper_fromprefs,
 oper_toprefs, oper_service_id, oper_not_fromprefs, oper_not_toprefs,
 oper_from_range, oper_to_range,
 oper_not_from_range,
 oper_not_to_range )
SELECT id, oper_fromports, oper_toports, oper_fromprefs,
 oper_toprefs, oper_service_id, oper_not_fromprefs, oper_not_toprefs,
 oper_from_range, oper_to_range,
 oper_not_from_range,
 oper_not_to_range 
FROM _phone_rule_data;

DROP TABLE _phone_rule_data;
-- #ENDB#

CREATE TABLE IF NOT EXISTS `phone_cost_map_$mid` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(200) NOT NULL,
  PRIMARY KEY (`id`)
);

CREATE TABLE phone_cost_map_item_$mid (
  `map_id` int(11) NOT NULL,
  `code` varchar(20) NOT NULL,
  `cost` decimal(10,6) NOT NULL,
  UNIQUE KEY `map_code` (`map_id`,`code`)
);

CREATE TABLE  phone_gate_$mid (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `comment` varchar(255) DEFAULT NULL,
  `host` varchar(255) NOT NULL,
  `port` int(11) NOT NULL,
  `config` text,
  `type_id` int(11) NOT NULL,
  `keyword` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
);

CREATE TABLE  phone_gate_type_$mid (
  `id` tinyint(4) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `comment` varchar(255) DEFAULT NULL,
  `script` text,
  `config` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
);


CREATE TABLE phone_contract_status_$mid (
  `cid` int(10) unsigned NOT NULL DEFAULT '0',
  `status` tinyint(3) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`cid`)
);

ALTER TABLE phone_contract_status_$mid DROP INDEX `cid`;

CREATE TABLE  phone_contract_status_log_$mid (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `cid` int(11) NOT NULL,
  `status` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `dt` datetime NOT NULL,
  `log` text NOT NULL,
  `comment` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
);

CREATE TABLE  phone_contract_gate_$mid (
  `cid` int(11) NOT NULL,
  `gid` int(11) NOT NULL,
  KEY `cid_gid` (`cid`,`gid`)
);

ALTER TABLE phone_cost_map_item_$mid MODIFY code VARCHAR(255) NOT NULL;
ALTER TABLE phone_cost_map_item_$mid MODIFY cost decimal(10,6) NULL;

ALTER TABLE phone_cost_map_item_$mid ADD id INT NOT NULL AUTO_INCREMENT PRIMARY KEY;
ALTER TABLE phone_cost_map_item_$mid ADD title VARCHAR(255) NULL;
ALTER TABLE phone_cost_map_item_$mid ADD comment VARCHAR(255) NULL;

CREATE TABLE  phone_item_ip_$mid (
  `id` int(11) NOT NULL auto_increment,
  `ip` bigint(20) NOT NULL,
  `item_id` int(11) NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `item_id` (`item_id`)
);

ALTER TABLE phone_item_ip_$mid DROP COLUMN `ip`;
ALTER TABLE phone_item_ip_$mid ADD COLUMN `range_id` INTEGER  NOT NULL;

CREATE TABLE phone_item_list_mode_$mid (
	`item_id` int(11) NOT NULL,
	`list_mode` int(3) NOT NULL,
	`is_new` int(3) NOT NULL,
	UNIQUE KEY `item_id` (`item_id`)
);

CREATE TABLE phone_item_list_number_$mid (
	`item_id` int(11) NOT NULL,
	`list_number` VARCHAR(50) NOT NULL,
	`is_new` int(3) NOT NULL DEFAULT 1,
	`is_deleted` int(3) NOT NULL,
	UNIQUE KEY `item_id` (`item_id`,`list_number`)
);


ALTER TABLE number_resource_use_$mid ADD COLUMN date_reserve date NULL;

# в будущем раскомментитьб, нужны до 4.5
# ALTER TABLE phone_client_item_$mid DROP COLUMN `client_services`,
# DROP COLUMN `oper_fromports`,
# DROP COLUMN `oper_toports`,
# DROP COLUMN `oper_fromprefs`,
# DROP COLUMN `oper_toprefs`,
# DROP COLUMN `oper_service_id`,
# DROP COLUMN `oper_not_fromprefs`,
# DROP COLUMN `oper_not_toprefs`,
# DROP COLUMN `oper_from_range`,
# DROP COLUMN `oper_to_range`,
# DROP COLUMN `oper_not_from_range`,
# DROP COLUMN `oper_not_to_range`;

CREATE TABLE IF NOT EXISTS `phone_range_map_$mid` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
);

CREATE TABLE IF NOT EXISTS `phone_range_map_item_$mid` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `mapId` int(11) NOT NULL,
  `code` varchar(255) NOT NULL,
  `title` varchar(255) DEFAULT NULL,
  `dateFrom` date DEFAULT NULL,
  `dateTo` date DEFAULT NULL,
  `comment` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `mapId` (`mapId`)
);

ALTER TABLE `phone_item_rule_data_$mid` 
ADD COLUMN `oper_fromrangemap` VARCHAR(255) NOT NULL DEFAULT ''  AFTER `toports_regexp` , 
ADD COLUMN `oper_torangemap` VARCHAR(255) NOT NULL DEFAULT ''  AFTER `oper_fromrangemap` ;

ALTER TABLE `phone_item_rule_data_$mid` 
ADD COLUMN `oper_notfromrangemap` VARCHAR(255) NOT NULL DEFAULT ''  AFTER `oper_torangemap` , 
ADD COLUMN `oper_nottorangemap` VARCHAR(255) NOT NULL DEFAULT ''  AFTER `oper_notfromrangemap` ;

ALTER TABLE `phone_range_map_item_$mid` ADD COLUMN `rangeTo` VARCHAR(255) NOT NULL  AFTER `rangeFrom` ,
 CHANGE COLUMN `code` `rangeFrom` VARCHAR(255) NOT NULL ;
ALTER TABLE `number_resource_use_$mid` ADD COLUMN `comment` VARCHAR(255) NULL  ;

ALTER TABLE `number_resource_use_$mid` ADD COLUMN `reserve_user_id` INT NULL ;

ALTER TABLE `phone_item_rule_data_$mid`
	CHANGE COLUMN `oper_toports` `oper_toports` MEDIUMTEXT NULL AFTER `oper_fromports`,
	CHANGE COLUMN `oper_fromprefs` `oper_fromprefs` MEDIUMTEXT NULL AFTER `oper_toports`,
	CHANGE COLUMN `oper_toprefs` `oper_toprefs` MEDIUMTEXT NULL AFTER `oper_fromprefs`,
	CHANGE COLUMN `oper_not_fromprefs` `oper_not_fromprefs` MEDIUMTEXT NOT NULL AFTER `oper_service_id`,
	CHANGE COLUMN `oper_not_toprefs` `oper_not_toprefs` MEDIUMTEXT NOT NULL AFTER `oper_not_fromprefs`,
	CHANGE COLUMN `oper_from_range` `oper_from_range` MEDIUMTEXT NOT NULL AFTER `oper_not_toprefs`,
	CHANGE COLUMN `oper_to_range` `oper_to_range` MEDIUMTEXT NOT NULL AFTER `oper_from_range`,
	CHANGE COLUMN `oper_not_from_range` `oper_not_from_range` MEDIUMTEXT NOT NULL AFTER `oper_to_range`,
	CHANGE COLUMN `oper_not_to_range` `oper_not_to_range` MEDIUMTEXT NOT NULL AFTER `oper_not_from_range`,
	CHANGE COLUMN `fromports_regexp` `fromports_regexp` MEDIUMTEXT NOT NULL AFTER `oper_not_to_range`,
	CHANGE COLUMN `toports_regexp` `toports_regexp` MEDIUMTEXT NOT NULL AFTER `fromports_regexp`;

UPDATE `installed_modules` SET `pack_client`='ru.bitel.bgbilling.modules.phone.client', pack_server='ru.bitel.bgbilling.modules.phone.server'  WHERE name='phone';
UPDATE `scheduled_tasks` SET `class`='ru.bitel.bgbilling.modules.phone.server.task.BalanceSetter' WHERE `class`='bitel.billing.server.phone.BalanceSetter';
	
-- #BLOCK#
DELETE FROM script_event_type WHERE mid=$mid;

SET @mid=$mid, @old_event_id=1, @new_event_id='ru.bitel.bgbilling.modules.phone.server.event.ClientItemChangedEvent', @event_title='Изменён поинт/правилоы';
INSERT INTO script_event_type (mid, event_id, event_mode, title) VALUES (@mid, @new_event_id, 1, @event_title);
INSERT INTO script_function_event_type(fid, mid, event_id) SELECT sfet1.fid, sfet1.mid, @new_event_id FROM script_function_event_type AS sfet1 LEFT JOIN script_function_event_type AS sfet2 ON sfet1.fid=sfet2.fid AND sfet1.mid=sfet2.mid AND sfet2.event_id=@new_event_id WHERE sfet1.mid=@mid AND sfet1.event_id=@old_event_id AND sfet2.fid IS NULL;

-- #ENDB#
