
INSERT INTO client_parameter (param, value) VALUES ('ENABLE_TIME_TRIGGERS', '1');

----------------------------------------------------------------

CREATE TRIGGER t_member_i_ctime
    AFTER INSERT ON member FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.ctime != UNIXEPOCH()
BEGIN
    UPDATE member SET ctime = UNIXEPOCH() WHERE mgnr = NEW.mgnr;
END;

CREATE TRIGGER t_member_u_ctime
    BEFORE UPDATE ON member FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND OLD.ctime != NEW.ctime
BEGIN
    SELECT RAISE(ABORT, 'It is not allowed to change ctime');
END;

CREATE TRIGGER t_member_i_mtime
    AFTER INSERT ON member FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.mtime != UNIXEPOCH()
BEGIN
    UPDATE member SET mtime = UNIXEPOCH() WHERE mgnr = NEW.mgnr;
END;

CREATE TRIGGER t_member_u_mtime
    AFTER UPDATE ON member FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.mtime != UNIXEPOCH()
BEGIN
    UPDATE member SET mtime = UNIXEPOCH() WHERE mgnr = NEW.mgnr;
END;

----------------------------------------------------------------

CREATE TRIGGER t_member_billing_address_i_mtime_member
    AFTER INSERT ON member_billing_address FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE member SET mtime = UNIXEPOCH() WHERE mgnr = NEW.mgnr;
END;

CREATE TRIGGER t_member_billing_address_u_mtime_member
    AFTER UPDATE ON member_billing_address FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE member SET mtime = UNIXEPOCH() WHERE mgnr = OLD.mgnr OR mgnr = NEW.mgnr;
END;

CREATE TRIGGER t_member_billing_address_d_mtime_member
    AFTER DELETE ON member_billing_address FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE member SET mtime = UNIXEPOCH() WHERE mgnr = OLD.mgnr;
END;

----------------------------------------------------------------

CREATE TRIGGER t_member_telephone_number_i_mtime_member
    AFTER INSERT ON member_telephone_number FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE member SET mtime = UNIXEPOCH() WHERE mgnr = NEW.mgnr;
END;

CREATE TRIGGER t_member_telephone_number_u_mtime_member
    AFTER UPDATE ON member_telephone_number FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE member SET mtime = UNIXEPOCH() WHERE mgnr = OLD.mgnr OR mgnr = NEW.mgnr;
END;

CREATE TRIGGER t_member_telephone_number_d_mtime_member
    AFTER DELETE ON member_telephone_number FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE member SET mtime = UNIXEPOCH() WHERE mgnr = OLD.mgnr;
END;

----------------------------------------------------------------

CREATE TRIGGER t_area_commitment_contract_i_ctime
    AFTER INSERT ON area_commitment_contract FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.ctime != UNIXEPOCH()
BEGIN
    UPDATE area_commitment_contract SET ctime = UNIXEPOCH() WHERE fbnr = NEW.fbnr;
END;

CREATE TRIGGER t_area_commitment_contract_u_ctime
    BEFORE UPDATE ON area_commitment_contract FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND OLD.ctime != NEW.ctime
BEGIN
    SELECT RAISE(ABORT, 'It is not allowed to change ctime');
END;

CREATE TRIGGER t_area_commitment_contract_i_mtime
    AFTER INSERT ON area_commitment_contract FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.mtime != UNIXEPOCH()
BEGIN
    UPDATE area_commitment_contract SET mtime = UNIXEPOCH() WHERE fbnr = NEW.fbnr;
END;

CREATE TRIGGER t_area_commitment_contract_u_mtime
    AFTER UPDATE ON area_commitment_contract FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.mtime != UNIXEPOCH()
BEGIN
    UPDATE area_commitment_contract SET mtime = UNIXEPOCH() WHERE fbnr = NEW.fbnr;
END;

----------------------------------------------------------------

CREATE TRIGGER t_area_commitment_i_ctime
    AFTER INSERT ON area_commitment FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.ctime != UNIXEPOCH()
BEGIN
    UPDATE area_commitment SET ctime = UNIXEPOCH() WHERE (fbnr, revnr) = (NEW.fbnr, NEW.revnr);
END;

CREATE TRIGGER t_area_commitment_u_ctime
    BEFORE UPDATE ON area_commitment FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND OLD.ctime != NEW.ctime
BEGIN
    SELECT RAISE(ABORT, 'It is not allowed to change ctime');
END;

CREATE TRIGGER t_area_commitment_i_mtime
    AFTER INSERT ON area_commitment FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.mtime != UNIXEPOCH()
BEGIN
    UPDATE area_commitment SET mtime = UNIXEPOCH() WHERE (fbnr, revnr) = (NEW.fbnr, NEW.revnr);
END;

CREATE TRIGGER t_area_commitment_u_mtime
    AFTER UPDATE ON area_commitment FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.mtime != UNIXEPOCH()
BEGIN
    UPDATE area_commitment SET mtime = UNIXEPOCH() WHERE (fbnr, revnr) = (NEW.fbnr, NEW.revnr);
END;

CREATE TRIGGER t_area_commitment_i_mtime_contract
    AFTER INSERT ON area_commitment FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE area_commitment_contract SET mtime = UNIXEPOCH() WHERE fbnr = NEW.fbnr;
END;

CREATE TRIGGER t_area_commitment_u_mtime_contract
    AFTER UPDATE ON area_commitment FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE area_commitment_contract SET mtime = UNIXEPOCH() WHERE fbnr = NEW.fbnr OR fbnr = OLD.fbnr;
END;

CREATE TRIGGER t_area_commitment_d_mtime_contract
    AFTER DELETE ON area_commitment FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE area_commitment_contract SET mtime = UNIXEPOCH() WHERE fbnr = OLD.fbnr;
END;

----------------------------------------------------------------

CREATE TRIGGER t_delivery_i_ctime
    AFTER INSERT ON delivery FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.ctime != UNIXEPOCH()
BEGIN
    UPDATE delivery SET ctime = UNIXEPOCH() WHERE (year, did) = (NEW.year, NEW.did);
END;

CREATE TRIGGER t_delivery_u_ctime
    AFTER UPDATE ON delivery FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND OLD.ctime != NEW.ctime
BEGIN
    SELECT RAISE(ABORT, 'It is not allowed to change ctime');
END;

CREATE TRIGGER t_delivery_i_mtime
    AFTER INSERT ON delivery FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.mtime != UNIXEPOCH()
BEGIN
    UPDATE delivery SET mtime = UNIXEPOCH() WHERE (year, did) = (NEW.year, NEW.did);
END;

CREATE TRIGGER t_delivery_u_mtime
    AFTER UPDATE ON delivery FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.mtime != UNIXEPOCH()
BEGIN
    UPDATE delivery SET mtime = UNIXEPOCH() WHERE (year, did) = (NEW.year, NEW.did);
END;

----------------------------------------------------------------

CREATE TRIGGER t_delivery_part_i_ctime
    AFTER INSERT ON delivery_part FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.ctime != UNIXEPOCH()
BEGIN
    UPDATE delivery_part SET ctime = UNIXEPOCH() WHERE (year, did, dpnr) = (NEW.year, NEW.did, NEW.dpnr);
END;

CREATE TRIGGER t_delivery_part_u_ctime
    AFTER UPDATE ON delivery_part FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND OLD.ctime != NEW.ctime
BEGIN
    SELECT RAISE(ABORT, 'It is not allowed to change ctime');
END;

CREATE TRIGGER t_delivery_part_i_mtime
    AFTER INSERT ON delivery_part FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.mtime != UNIXEPOCH()
BEGIN
    UPDATE delivery_part SET mtime = UNIXEPOCH() WHERE (year, did, dpnr) = (NEW.year, NEW.did, NEW.dpnr);
END;

CREATE TRIGGER t_delivery_part_u_mtime
    AFTER UPDATE ON delivery_part FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.mtime != UNIXEPOCH()
BEGIN
    UPDATE delivery_part SET mtime = UNIXEPOCH() WHERE (year, did, dpnr) = (NEW.year, NEW.did, NEW.dpnr);
END;

CREATE TRIGGER t_delivery_part_i_mtime_delivery
    AFTER INSERT ON delivery_part FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE delivery SET mtime = UNIXEPOCH() WHERE (year, did) = (NEW.year, NEW.did);
END;

CREATE TRIGGER t_delivery_part_u_mtime_delivery
    AFTER UPDATE ON delivery_part FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE delivery SET mtime = UNIXEPOCH() WHERE (year, did) = (NEW.year, NEW.did) OR (year, did) = (OLD.year, OLD.did);
END;

CREATE TRIGGER t_delivery_part_d_mtime_delivery
    AFTER DELETE ON delivery_part FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE delivery SET mtime = UNIXEPOCH() WHERE (year, did) = (OLD.year, OLD.did);
END;

----------------------------------------------------------------

CREATE TRIGGER t_delivery_part_modifier_i_mtime_delivery_part
    AFTER INSERT ON delivery_part_modifier FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE delivery_part SET mtime = UNIXEPOCH() WHERE (year, did, dpnr) = (NEW.year, NEW.did, NEW.dpnr);
END;

CREATE TRIGGER t_delivery_part_modifier_u_mtime_delivery_part
    AFTER UPDATE ON delivery_part_modifier FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE delivery_part SET mtime = UNIXEPOCH() WHERE (year, did, dpnr) = (NEW.year, NEW.did, NEW.dpnr) OR (year, did, dpnr) = (OLD.year, OLD.did, OLD.dpnr);
END;

CREATE TRIGGER t_delivery_part_modifier_d_mtime_delivery_part
    AFTER DELETE ON delivery_part_modifier FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE delivery_part SET mtime = UNIXEPOCH() WHERE (year, did, dpnr) = (OLD.year, OLD.did, OLD.dpnr);
END;

----------------------------------------------------------------

CREATE TRIGGER t_payment_variant_i_ctime
    AFTER INSERT ON payment_variant FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.ctime != UNIXEPOCH()
BEGIN
    UPDATE payment_variant SET ctime = UNIXEPOCH() WHERE (year, avnr) = (NEW.year, NEW.avnr);
END;

CREATE TRIGGER t_payment_variant_u_ctime
    BEFORE UPDATE ON payment_variant FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND OLD.ctime != NEW.ctime
BEGIN
    SELECT RAISE(ABORT, 'It is not allowed to change ctime');
END;

CREATE TRIGGER t_payment_variant_i_mtime
    AFTER INSERT ON payment_variant FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.mtime != UNIXEPOCH()
BEGIN
    UPDATE payment_variant SET mtime = UNIXEPOCH() WHERE (year, avnr) = (NEW.year, NEW.avnr);
END;

CREATE TRIGGER t_payment_variant_u_mtime
    AFTER UPDATE ON payment_variant FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.mtime != UNIXEPOCH()
BEGIN
    UPDATE payment_variant SET mtime = UNIXEPOCH() WHERE (year, avnr) = (NEW.year, NEW.avnr);
END;

----------------------------------------------------------------

CREATE TRIGGER t_payment_delivery_part_i_mtime_payment_variant
    AFTER INSERT ON payment_delivery_part FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE payment_variant SET mtime = UNIXEPOCH() WHERE (year, avnr) = (NEW.year, NEW.avnr);
END;

CREATE TRIGGER t_payment_delivery_part_u_mtime_payment_variant
    AFTER UPDATE ON payment_delivery_part FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE payment_variant SET mtime = UNIXEPOCH() WHERE (year, avnr) = (OLD.year, OLD.avnr) OR (year, avnr) = (NEW.year, NEW.avnr);
END;

CREATE TRIGGER t_payment_delivery_part_d_mtime_payment_variant
    AFTER DELETE ON payment_delivery_part FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE payment_variant SET mtime = UNIXEPOCH() WHERE (year, avnr) = (OLD.year, OLD.avnr);
END;

----------------------------------------------------------------

CREATE TRIGGER t_payment_member_i_mtime_payment_variant
    AFTER INSERT ON payment_member FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE payment_variant SET mtime = UNIXEPOCH() WHERE (year, avnr) = (NEW.year, NEW.avnr);
END;

CREATE TRIGGER t_payment_member_u_mtime_payment_variant
    AFTER UPDATE ON payment_member FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE payment_variant SET mtime = UNIXEPOCH() WHERE (year, avnr) = (OLD.year, OLD.avnr) OR (year, avnr) = (NEW.year, NEW.avnr);
END;

CREATE TRIGGER t_payment_member_d_mtime_payment_variant
    AFTER DELETE ON payment_member FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE payment_variant SET mtime = UNIXEPOCH() WHERE (year, avnr) = (OLD.year, OLD.avnr);
END;

----------------------------------------------------------------

CREATE TRIGGER t_credit_i_ctime
    AFTER INSERT ON credit FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.ctime != UNIXEPOCH()
BEGIN
    UPDATE credit SET ctime = UNIXEPOCH() WHERE (year, tgnr) = (NEW.year, NEW.tgnr);
END;

CREATE TRIGGER t_credit_u_ctime
    BEFORE UPDATE ON credit FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND OLD.ctime != NEW.ctime
BEGIN
    SELECT RAISE(ABORT, 'It is not allowed to change ctime');
END;

CREATE TRIGGER t_credit_i_mtime
    AFTER INSERT ON credit FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.mtime != UNIXEPOCH()
BEGIN
    UPDATE credit SET mtime = UNIXEPOCH() WHERE (year, tgnr) = (NEW.year, NEW.tgnr);
END;

CREATE TRIGGER t_credit_u_mtime
    AFTER UPDATE ON credit FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.mtime != UNIXEPOCH()
BEGIN
    UPDATE credit SET mtime = UNIXEPOCH() WHERE (year, tgnr) = (NEW.year, NEW.tgnr);
END;

----------------------------------------------------------------

CREATE TRIGGER t_delivery_announcement_i_ctime
    AFTER INSERT ON delivery_announcement FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.ctime != UNIXEPOCH()
BEGIN
    UPDATE delivery_announcement SET ctime = UNIXEPOCH() WHERE (year, dsnr, mgnr, sortid) = (NEW.year, NEW.dsnr, NEW.mgnr, NEW.sortid);
END;

CREATE TRIGGER t_delivery_announcement_u_ctime
    BEFORE UPDATE ON delivery_announcement FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND OLD.ctime != NEW.ctime
BEGIN
    SELECT RAISE(ABORT, 'It is not allowed to change ctime');
END;

CREATE TRIGGER t_delivery_announcement_i_mtime
    AFTER INSERT ON delivery_announcement FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.mtime != UNIXEPOCH()
BEGIN
    UPDATE delivery_announcement SET mtime = UNIXEPOCH() WHERE (year, dsnr, mgnr, sortid) = (NEW.year, NEW.dsnr, NEW.mgnr, NEW.sortid);
END;

CREATE TRIGGER t_delivery_announcement_u_mtime
    AFTER UPDATE ON delivery_announcement FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.mtime != UNIXEPOCH()
BEGIN
    UPDATE delivery_announcement SET mtime = UNIXEPOCH() WHERE (year, dsnr, mgnr, sortid) = (NEW.year, NEW.dsnr, NEW.mgnr, NEW.sortid);
END;
