USE `billing`;

ALTER TABLE `billing`.`billing_fees`
ADD COLUMN (`onpeak_extra_rate` DOUBLE NOT NULL DEFAULT 0,
            `onpeak_extra_second` INT(5) UNSIGNED NULL DEFAULT NULL,
            `offpeak_extra_rate` DOUBLE NOT NULL DEFAULT 0,
            `offpeak_extra_second` INT(5) UNSIGNED NULL DEFAULT NULL);

ALTER TABLE `billing`.`billing_fees_history`
ADD COLUMN (`onpeak_extra_rate` DOUBLE NOT NULL DEFAULT 0,
            `onpeak_extra_second` INT(5) UNSIGNED NULL DEFAULT NULL,
            `offpeak_extra_rate` DOUBLE NOT NULL DEFAULT 0,
            `offpeak_extra_second` INT(5) UNSIGNED NULL DEFAULT NULL);

ALTER TABLE `billing`.`billing_fees_raw`
ADD COLUMN (`onpeak_extra_rate` DOUBLE NOT NULL DEFAULT 0,
            `onpeak_extra_second` INT(5) UNSIGNED NULL DEFAULT NULL,
            `offpeak_extra_rate` DOUBLE NOT NULL DEFAULT 0,
            `offpeak_extra_second` INT(5) UNSIGNED NULL DEFAULT NULL);

DELIMITER $$

DROP TRIGGER IF EXISTS billing.bill_fees_crepl_trig;

CREATE trigger billing.bill_fees_crepl_trig after insert on billing_fees
  for each row
  begin
  declare my_bzh_id int unsigned;

  select id into my_bzh_id from billing.billing_zones_history where bz_id = NEW.billing_zone_id;

  insert into billing.billing_fees_history
    values(NULL, NEW.id, NEW.billing_profile_id, my_bzh_id,
     NEW.source, NEW.destination, NEW.direction,
     NEW.type, NEW.onpeak_init_rate, NEW.onpeak_init_interval, NEW.onpeak_follow_rate,
     NEW.onpeak_follow_interval, NEW.offpeak_init_rate, NEW.offpeak_init_interval,
     NEW.offpeak_follow_rate, NEW.offpeak_follow_interval, NEW.use_free_time, NEW.match_mode,
     NEW.onpeak_extra_rate, NEW.onpeak_extra_second, NEW.offpeak_extra_rate, NEW.offpeak_extra_second);

  end;
$$

DROP TRIGGER IF EXISTS billing.bill_fees_urepl_trig;

CREATE trigger billing.bill_fees_urepl_trig after update on billing_fees
  for each row
  begin
  declare my_bzh_id int unsigned;

  select id into my_bzh_id from billing.billing_zones_history where bz_id = NEW.billing_zone_id;

  update billing.billing_fees_history
    set bf_id = NEW.id, billing_profile_id = NEW.billing_profile_id,
      billing_zones_history_id = my_bzh_id,
      source = NEW.source, destination = NEW.destination, direction = NEW.direction,
      type = NEW.type,
      onpeak_init_rate = NEW.onpeak_init_rate, onpeak_init_interval = NEW.onpeak_init_interval,
      onpeak_follow_rate = NEW.onpeak_follow_rate, onpeak_follow_interval = NEW.onpeak_follow_interval,
      offpeak_init_rate = NEW.offpeak_init_rate, offpeak_init_interval = NEW.offpeak_init_interval,
      offpeak_follow_rate = NEW.offpeak_follow_rate, offpeak_follow_interval = NEW.offpeak_follow_interval,
      use_free_time = NEW.use_free_time, match_mode = NEW.match_mode, onpeak_extra_rate = NEW.onpeak_extra_rate,
      onpeak_extra_second = NEW.onpeak_extra_second, offpeak_extra_rate = NEW.offpeak_extra_rate,
      offpeak_extra_second = NEW.offpeak_extra_second
    where bf_id = OLD.id;

  end;
$$

DELIMITER ;