DROP TRIGGER IF EXISTS sk_card_serie_del;
CREATE TRIGGER sk_card_serie_del AFTER DELETE ON sk_card_series FOR EACH ROW
BEGIN
DELETE FROM sk_card_data WHERE sk_card_data.sid=OLD.sid;
DELETE FROM sk_users WHERE sk_users.sid=OLD.sid;
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_tg;
CREATE TRIGGER sk_rad_user_add_tg AFTER INSERT ON sk_users FOR EACH ROW
BEGIN
	UPDATE sk_packets SET users_col = users_col + 1 WHERE sk_packets.gid=NEW.gid;
	UPDATE sk_card_series SET cards_col = cards_col + 1 WHERE sk_card_series.sid = NEW.sid;
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_acc_tg;
CREATE TRIGGER sk_rad_user_add_acc_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.account_id = 0
BEGIN
	INSERT INTO sk_accounts (service_set_id, int_property) VALUES ((SELECT service_set_id FROM sk_packets WHERE gid=NEW.gid), new.uid);
	UPDATE sk_users SET account_id=(SELECT sk_accounts.account_id FROM sk_accounts,sk_packets WHERE sk_accounts.service_set_id=sk_packets.service_set_id AND sk_packets.gid=NEW.gid AND sk_accounts.int_propert=new.uid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_credit_tg;
CREATE TRIGGER sk_rad_user_add_credit_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.credit IS NULL OR NEW.credit = 0
BEGIN
	UPDATE sk_users SET credit=(SELECT credit FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_tos_tg;
CREATE TRIGGER sk_rad_user_add_tos_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.tos IS NULL OR NEW.tos = 0
BEGIN
	UPDATE sk_users SET tos=(SELECT tos FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_dtos_tg;
CREATE TRIGGER sk_rad_user_add_dtos_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.do_with_tos IS NULL OR NEW.do_with_tos = 0
BEGIN
	UPDATE sk_users SET do_with_tos=(SELECT do_with_tos FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_dir_tg;
CREATE TRIGGER sk_rad_user_add_dir_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.direction IS NULL OR NEW.direction = 0
BEGIN
	UPDATE sk_users SET direction=(SELECT direction FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_fix_tg;
CREATE TRIGGER sk_rad_user_add_fix_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.fixed IS NULL OR NEW.fixed = 0
BEGIN
	UPDATE sk_users SET fixed=(SELECT fixed FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_fcost_tg;
CREATE TRIGGER sk_rad_user_add_fcost_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.fixed_cost IS NULL OR NEW.fixed_cost = 0
BEGIN
	UPDATE sk_users SET fixed_cost=(SELECT fixed_cost FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_atime_tg;
CREATE TRIGGER sk_rad_user_add_atime_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.activation_time IS NULL OR NEW.activation_time = 0
BEGIN
	UPDATE sk_users SET activation_time=(SELECT activation_time FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_ttimel_tg;
CREATE TRIGGER sk_rad_user_add_ttimel_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.total_time_limit IS NULL OR NEW.total_time_limit = 0
BEGIN
	UPDATE sk_users SET total_time_limit=(SELECT total_time_limit FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_ttrafl_tg;
CREATE TRIGGER sk_rad_user_add_ttrafl_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.total_traffic_limit IS NULL OR NEW.total_traffic_limit = 0
BEGIN
	UPDATE sk_users SET total_traffic_limit=(SELECT total_traffic_limit FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_tmoneyl_tg;
CREATE TRIGGER sk_rad_user_add_tmoneyl_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.total_money_limit IS NULL OR NEW.total_money_limit = 0
BEGIN
	UPDATE sk_users SET total_money_limit=(SELECT total_money_limit FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_mtimel_tg;
CREATE TRIGGER sk_rad_user_add_mtimel_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.month_time_limit IS NULL OR NEW.month_time_limit = 0
BEGIN
	UPDATE sk_users SET month_time_limit=(SELECT month_time_limit FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_mtrafl_tg;
CREATE TRIGGER sk_rad_user_add_mtrafl_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.month_traffic_limit IS NULL OR NEW.month_traffic_limit = 0
BEGIN
	UPDATE sk_users SET month_traffic_limit=(SELECT month_traffic_limit FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_mmoneyl_tg;
CREATE TRIGGER sk_rad_user_add_mmoneyl_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.month_money_limit IS NULL OR NEW.month_money_limit = 0
BEGIN
	UPDATE sk_users SET month_money_limit=(SELECT month_money_limit FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_wtimel_tg;
CREATE TRIGGER sk_rad_user_add_wtimel_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.week_time_limit IS NULL OR NEW.week_time_limit = 0
BEGIN
	UPDATE sk_users SET week_time_limit=(SELECT week_time_limit FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_wtrafl_tg;
CREATE TRIGGER sk_rad_user_add_wtrafl_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.week_traffic_limit IS NULL OR NEW.week_traffic_limit = 0
BEGIN
	UPDATE sk_users SET week_traffic_limit=(SELECT week_traffic_limit FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_wmoneyl_tg;
CREATE TRIGGER sk_rad_user_add_wmoneyl_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.week_money_limit IS NULL OR NEW.week_money_limit = 0
BEGIN
	UPDATE sk_users SET week_money_limit=(SELECT week_money_limit FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_dtimel_tg;
CREATE TRIGGER sk_rad_user_add_dtimel_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.day_time_limit IS NULL OR NEW.day_time_limit = 0
BEGIN
	UPDATE sk_users SET day_time_limit=(SELECT day_time_limit FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_dtrafl_tg;
CREATE TRIGGER sk_rad_user_add_dtrafl_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.day_traffic_limit IS NULL OR NEW.day_traffic_limit = 0
BEGIN
	UPDATE sk_users SET day_traffic_limit=(SELECT day_traffic_limit FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_dmoneyl_tg;
CREATE TRIGGER sk_rad_user_add_dmoneyl_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.day_money_limit IS NULL OR NEW.day_money_limit = 0
BEGIN
	UPDATE sk_users SET day_money_limit=(SELECT day_money_limit FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_suse_tg;
CREATE TRIGGER sk_rad_user_add_suse_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.simultanoeus_use IS NULL OR NEW.simultaneous_use = 0
BEGIN
	UPDATE sk_users SET simultaneous_use=(SELECT simultaneous_use FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_plimit_tg;
CREATE TRIGGER sk_rad_user_add_plimit_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.port_limit IS NULL OR NEW.port_limit = 0
BEGIN
	UPDATE sk_users SET port_limit=(SELECT port_limit FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_stout_tg;
CREATE TRIGGER sk_rad_user_add_stout_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.session_timeout IS NULL OR NEW.session_timeout = 0
BEGIN
	UPDATE sk_users SET session_timeout=(SELECT session_timeout FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_itout_tg;
CREATE TRIGGER sk_rad_user_add_itout_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.idle_timeout IS NULL OR NEW.idle_timeout = 0
BEGIN
	UPDATE sk_users SET idle_timeout=(SELECT idle_timeout FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_np_tg;
CREATE TRIGGER sk_rad_user_add_np_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.no_pass IS NULL OR NEW.no_pass = 0
BEGIN
	UPDATE sk_users SET no_pass=(SELECT no_pass FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_na_tg;
CREATE TRIGGER sk_rad_user_add_na_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.no_acct IS NULL OR NEW.no_acct = 0
BEGIN
	UPDATE sk_users SET no_acct=(SELECT no_acct FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_acb_tg;
CREATE TRIGGER sk_rad_user_add_acb_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.allow_callback IS NULL OR NEW.allow_callback = 0
BEGIN
	UPDATE sk_users SET allow_callback=(SELECT allow_callback FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_block_tg;
CREATE TRIGGER sk_rad_user_add_block_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.blocked IS NULL OR NEW.blocked = 0
BEGIN
	UPDATE sk_users SET blocked=(SELECT blocked FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_act_tg;
CREATE TRIGGER sk_rad_user_add_act_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.activated IS NULL OR NEW.activated = 0
BEGIN
	UPDATE sk_users SET activated=(SELECT activated FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_up_tg;
CREATE TRIGGER sk_rad_user_add_up_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.up IS NULL OR NEW.up = 0
BEGIN
	UPDATE sk_users SET up=(SELECT up FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_down_tg;
CREATE TRIGGER sk_rad_user_add_down_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.down IS NULL OR NEW.down = 0
BEGIN
	UPDATE sk_users SET down=(SELECT down FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_lt_tg;
CREATE TRIGGER sk_rad_user_add_lt_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.login_time IS NULL OR NEW.login_time = ''
BEGIN
	UPDATE sk_users SET login_time=(SELECT login_time FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_hg_tg;
CREATE TRIGGER sk_rad_user_add_hg_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.huntgroup_name IS NULL OR NEW.huntgroup_name = ''
BEGIN
	UPDATE sk_users SET huntgroup_name=(SELECT huntgroup_name FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_op_tg;
CREATE TRIGGER sk_rad_user_add_op_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.other_params IS NULL OR NEW.other_params = ''
BEGIN
	UPDATE sk_users SET other_params=(SELECT other_params FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_as_tg;
CREATE TRIGGER sk_rad_user_add_as_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.allowed_servers IS NULL OR NEW.allowed_servers = ''
BEGIN
	UPDATE sk_users SET allowed_servers=(SELECT allowed_servers FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_fip_tg;
CREATE TRIGGER sk_rad_user_add_fip_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.framed_ip IS NULL OR NEW.framed_ip = ''
BEGIN
	UPDATE sk_users SET framed_ip=(SELECT framed_ip FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_fmask_tg;
CREATE TRIGGER sk_rad_user_add_fmask_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.framed_mask IS NULL OR NEW.framed_mask = ''
BEGIN
	UPDATE sk_users SET framed_mask=(SELECT framed_mask FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_ippool_tg;
CREATE TRIGGER sk_rad_user_add_ippool_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.ippool_name IS NULL OR NEW.ippool_name = ''
BEGIN
	UPDATE sk_users SET ippool_name=(SELECT ippool_name FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_add_froute_tg;
CREATE TRIGGER sk_rad_user_add_froute_tg AFTER INSERT ON sk_users FOR EACH ROW WHEN NEW.framed_route IS NULL OR NEW.framed_route = ''
BEGIN
	UPDATE sk_users SET framed_route=(SELECT framed_route FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_upd_tos_tg;
CREATE TRIGGER sk_rad_user_upd_tos_tg AFTER UPDATE ON sk_users FOR EACH ROW WHEN NEW.tos != OLD.tos
BEGIN
	DELETE FROM sk_prices WHERE sk_prices.uid=OLD.uid;
END;

DROP TRIGGER IF EXISTS sk_rad_user_upd_gid_tg;
CREATE TRIGGER sk_rad_user_upd_gid_tg AFTER UPDATE ON sk_users FOR EACH ROW WHEN NEW.gid != OLD.gid
BEGIN
	UPDATE sk_users SET credit=(SELECT credit FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET tos=(SELECT tos FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET do_with_tos=(SELECT do_with_tos FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET direction=(SELECT direction FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET fixed=(SELECT fixed FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET fixed_cost=(SELECT fixed_cost FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET activation_time=(SELECT activation_time FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET total_time_limit=(SELECT total_time_limit FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET total_traffic_limit=(SELECT total_traffic_limit FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET total_money_limit=(SELECT total_money_limit FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET month_time_limit=(SELECT month_time_limit FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET month_traffic_limit=(SELECT month_traffic_limit FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET month_money_limit=(SELECT month_money_limit FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET week_time_limit=(SELECT week_time_limit FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET week_traffic_limit=(SELECT week_traffic_limit FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET week_money_limit=(SELECT week_money_limit FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET day_time_limit=(SELECT day_time_limit FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET day_traffic_limit=(SELECT day_traffic_limit FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET day_money_limit=(SELECT day_money_limit FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET simultaneous_use=(SELECT simultaneous_use FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET port_limit=(SELECT port_limit FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET session_timeout=(SELECT session_timeout FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET idle_timeout=(SELECT idle_timeout FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET no_pass=(SELECT no_pass FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET no_acct=(SELECT no_acct FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET allow_callback=(SELECT allow_callback FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET blocked=(SELECT blocked FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET activated=(SELECT activated FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET up=(SELECT up FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET down=(SELECT down FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET login_time=(SELECT login_time FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET huntgroup_name=(SELECT huntgroup_name FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET other_params=(SELECT other_params FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET allowed_servers=(SELECT allowed_servers FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET framed_ip=(SELECT framed_ip FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET framed_mask=(SELECT framed_mask FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET ippool_name=(SELECT ippool_name FROM sk_packets WHERE gid=NEW.gid);
	UPDATE sk_users SET framed_route=(SELECT framed_route FROM sk_packets WHERE gid=NEW.gid);
END;

DROP TRIGGER IF EXISTS sk_rad_user_upd_acc_tg;
CREATE TRIGGER sk_rad_user_upd_acc_tg AFTER UPDATE ON sk_users FOR EACH ROW WHEN NEW.deposit != OLD.deposit
BEGIN
	INSERT INTO sk_system_conf (value, attribute) VALUES (1, 'internal_account_update');
	UPDATE sk_accounts SET a_sum=a_sum-(OLD.deposit-NEW.deposit) WHERE sk_accounts.account_id=OLD.account_id;
	DELETE FROM sk_system_conf WHERE attribute='internal_account_update';
END;

DROP TRIGGER IF EXISTS sk_rad_user_upd_tg;
CREATE TRIGGER sk_rad_user_upd_tg AFTER UPDATE ON sk_users FOR EACH ROW WHEN NEW.username != OLD.username OR NEW.password != OLD.password
BEGIN
	UPDATE sk_objects SET nickname=NEW.username, password=NEW.password WHERE sk_objects.mode='P' AND sk_objects.nickname=OLD.username AND sk_objects.password=OLD.password;
END;

DROP TRIGGER IF EXISTS sk_rad_user_del_tg;
CREATE TRIGGER sk_rad_user_del_tg BEFORE DELETE ON sk_users FOR EACH ROW
BEGIN
	DELETE FROM sk_actions WHERE sk_actions.uid=old_uid OR sk_actions.username=old.username;
	DELETE FROM sk_prices WHERE sk_prices.uid=old.uid;
	UPDATE sk_packets SET users_col=users_col-1 WHERE sk_packets.gid=old.gid;
	DELETE FROM sk_accounts WHERE sk_accounts.int_property=old.uid;
	DELETE FROM sk_objects WHERE sk_objects.mode='P' AND sk_objects.nickname=old.username AND sk_objects.password=old.password;
	UPDATE sk_card_series SET cards_col=cards_col-1 WHERE sk_card_series.sid = old.sid;
END;

DROP TRIGGER IF EXISTS sk_rad_group_upd_tos_tg;
CREATE TRIGGER sk_rad_group_upd_tos_tg AFTER UPDATE ON sk_packets FOR EACH ROW WHEN new.tos != old.tos
BEGIN
	DELETE FROM sk_prices WHERE sk_prices.gid=OLD.gid;
END;

DROP TRIGGER IF EXISTS sk_rad_group_upd_tg;
CREATE TRIGGER sk_rad_group_upd_tg AFTER UPDATE ON sk_packets FOR EACH ROW
BEGIN
	UPDATE sk_users SET 
	deposit=new.deposit, credit=new.credit,
	tos=new.tos, do_with_tos=new.do_with_tos,
	direction=new.direction, fixed=new.fixed,
	fixed_cost=new.fixed_cost, activation_time=new.activation_time,
	total_time_limit=new.total_time_limit, month_time_limit=new.month_time_limit,
	week_time_limit=new.week_time_limit, day_time_limit=new.day_time_limit,
	total_traffic_limit=new.total_traffic_limit, month_traffic_limit=new.month_traffic_limit,
	week_traffic_limit=new.week_traffic_limit, day_traffic_limit=new.day_traffic_limit,
	total_money_limit=new.total_money_limit, month_money_limit=new.month_money_limit,
	week_money_limit=new.week_money_limit, day_money_limit=new.day_money_limit,
	login_time=new.login_time, huntgroup_name=new.huntgroup_name,
	simultaneous_use=new.simultaneous_use, port_limit=new.port_limit,
	session_timeout=new.session_timeout, idle_timeout=new.idle_timeout,
	no_pass=new.no_pass, no_acct=new.no_acct,
	allow_callback=new.allow_callback, other_params=new.other_params,
	allowed_servers=new.allowed_servers, blocked=new.blocked,
	framed_ip=new.framed_ip, framed_mask=new.framed_mask, up=new.up,
	down=new.down, ippool_name=new.ippool_name
	WHERE gid=old.gid AND activated=0 AND
	deposit=old.deposit AND credit=old.credit AND
	tos=old.tos AND do_with_tos=old.do_with_tos AND 
	direction=old.direction AND fixed=old.fixed AND 
	fixed_cost=old.fixed_cost AND activation_time=old.activation_time AND
	total_time_limit=old.total_time_limit AND month_time_limit=old.month_time_limit AND
	week_time_limit=old.week_time_limit AND day_time_limit=old.day_time_limit AND
	total_traffic_limit=old.total_traffic_limit AND month_traffic_limit=old.month_traffic_limit AND
	week_traffic_limit=old.week_traffic_limit AND day_traffic_limit=old.day_traffic_limit AND
	total_money_limit=old.total_money_limit AND month_money_limit=old.month_money_limit AND
	week_money_limit=old.week_money_limit AND day_money_limit=old.day_money_limit AND
	login_time=old.login_time AND huntgroup_name=old.huntgroup_name AND
	simultaneous_use=old.simultaneous_use AND port_limit=old.port_limit AND
	session_timeout=old.session_timeout AND idle_timeout=old.idle_timeout AND
	no_pass=old.no_pass AND no_acct=old.no_acct AND
	allow_callback=old.allow_callback AND other_params=old.other_params AND
	allowed_servers=old.allowed_servers AND blocked=old.blocked AND
	framed_ip=old.framed_ip AND framed_mask=old.framed_mask AND up=old.up AND
	down=old.down AND ippool_name=old.ippool_name;
END;

DROP TRIGGER IF EXISTS sk_rad_group_del_tg;
CREATE TRIGGER sk_rad_group_del_tg AFTER DELETE ON sk_packets FOR EACH ROW
BEGIN
	DELETE FROM sk_users WHERE sk_users.gid=OLD.gid;
	DELETE FROM sk_card_series WHERE sk_card_series.gid=OLD.gid;
	DELETE FROM sk_prices WHERE sk_prices.gid=OLD.gid;
	DELETE FROM sk_group_tracker WHERE sk_group_tracker.gid=OLD.gid;
	DELETE FROM sk_actions WHERE sk_actions.gid=OLD.gid;
END;

DROP TRIGGER IF EXISTS sk_acc_upd_tg;
CREATE TRIGGER sk_acc_upd_tg AFTER UPDATE ON sk_accounts FOR EACH ROW WHEN (SELECT value FROM sk_system_conf WHERE attribute='internal_account_update') != 1
BEGIN
    UPDATE sk_users SET deposit=deposit+(NEW.a_sum-OLD.a_sum) WHERE sk_users.account_id=NEW.account_id;
    UPDATE sk_users SET expired=strftime('%Y-%m-%d %H:%M:%S', sk_users.activation_time + strftime('%s', 'now')) WHERE sk_users.activation_time > 0 AND sk_users.blocked=0 AND sk_users.account_id=NEW.account_id;
END;

DROP TRIGGER IF EXISTS sk_service_set_del;
CREATE TRIGGER sk_service_set_del BEFORE DELETE ON sk_service_set FOR EACH ROW
BEGIN
	DELETE FROM sk_accounts WHERE sk_accounts.service_set_id=OLD.service_set_id;
	DELETE FROM sk_holidays WHERE sk_holidays.service_set_id=OLD.service_set_id;
	DELETE FROM sk_packets WHERE sk_packets.service_set_id=OLD.service_set_id;
END;

