CREATE TRIGGER TTBCHECKLIST_B02I FOR TBCHECKLIST
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Checklist incluido';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCHECKLIST',NEW.ID, HISTORICO);
    END
end;

CREATE TRIGGER TTBCHECKLIST_B01U FOR TBCHECKLIST
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.DESCRICAO <> NEW.DESCRICAO) THEN
    BEGIN
        HISTORICO = HISTORICO||'Descricao Checklist:'|| OLD.DESCRICAO||'->'||NEW.DESCRICAO;
    END

    IF ( OLD.STATUS <> NEW.STATUS) THEN
    BEGIN
        HISTORICO = HISTORICO||', Status Checklist:'|| OLD.STATUS||'->'||NEW.STATUS;
    END

    IF ( OLD.SEGUNDA <> NEW.SEGUNDA) THEN
    BEGIN
        HISTORICO = HISTORICO||', Checklist Segunda:'|| OLD.SEGUNDA||'->'||NEW.SEGUNDA;
    END

    IF ( OLD.TERCA <> NEW.TERCA) THEN
    BEGIN
        HISTORICO = HISTORICO||', Checklist Terca:'|| OLD.TERCA||'->'||NEW.TERCA;
    END

    IF ( OLD.QUARTA <> NEW.QUARTA) THEN
    BEGIN
        HISTORICO = HISTORICO||', Checklist Quarta:'|| OLD.QUARTA||'->'||NEW.QUARTA;
    END

    IF ( OLD.QUINTA <> NEW.QUINTA) THEN
    BEGIN
        HISTORICO = HISTORICO||', Checklist Quinta:'|| OLD.QUINTA||'->'||NEW.QUINTA;
    END

    IF ( OLD.SEXTA <> NEW.SEXTA) THEN
    BEGIN
        HISTORICO = HISTORICO||', Checklist Sexta:'|| OLD.SEXTA||'->'||NEW.SEXTA;
    END

    IF ( OLD.SABADO <> NEW.SABADO) THEN
    BEGIN
        HISTORICO = HISTORICO||', Checklist Sabado:'|| OLD.SABADO||'->'||NEW.SABADO;
    END

    IF ( OLD.DOMINGO <> NEW.DOMINGO) THEN
    BEGIN
        HISTORICO = HISTORICO||', Checklist Domingo:'|| OLD.DOMINGO||'->'||NEW.DOMINGO;
    END

    IF ( OLD.DIAS <> NEW.DIAS) THEN
    BEGIN
        HISTORICO = HISTORICO||', Checklist Dias:'|| OLD.DIAS||'->'||NEW.DIAS;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCHECKLIST',NEW.ID, HISTORICO);
    END
end;

CREATE TRIGGER TTBCHECKLIST_B01D FOR TBCHECKLIST
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Checklist Excluido';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCHECKLIST',old.ID, HISTORICO);
    END
end;

CREATE TRIGGER TTBCHECKLIST_TAREFAS_B02I FOR TBCHECKLIST_TAREFAS
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Tarefa incluida no Checklist: '||NEW.CHECKLIST_ID;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCHECKLIST',NEW.CHECKLIST_ID, HISTORICO);
    END
end;

CREATE OR ALTER TRIGGER TTBCHECKLIST_TAREFAS_B01U FOR TBCHECKLIST_TAREFAS
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.DESCRICAO <> NEW.DESCRICAO) THEN
    BEGIN
        HISTORICO = HISTORICO||'Descricao da tarefa '||old.ID||'no Checklist: '||OLD.CHECKLIST_ID||', '|| OLD.DESCRICAO||'->'||NEW.DESCRICAO;
    END

    IF ( OLD.STATUS <> NEW.STATUS) THEN
    BEGIN
        HISTORICO = HISTORICO||', Status tarefa '||old.ID||'Checklist: '|| OLD.STATUS||'->'||NEW.STATUS;
    END

    IF ( OLD.CHECKLIST_ID <> NEW.CHECKLIST_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||', ID Checklist da tarefa '||old.ID||'alterado: '||OLD.CHECKLIST_ID||'->'||NEW.CHECKLIST_ID;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCHECKLIST',NEW.CHECKLIST_ID, HISTORICO);
    END
end;

CREATE TRIGGER TTBCHECKLIST_TAREFAS_B01D FOR TBCHECKLIST_TAREFAS
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Tarefa '||OLD.ID||' Excluida do checklist'||OLD.CHECKLIST_ID;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCHECKLIST',old.CHECKLIST_ID, HISTORICO);
    END
end;

CREATE TRIGGER TTBCHECKLIST_USUARIO_B02I FOR TBCHECKLIST_USUARIO
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Usuario incluido no Checklist: '||NEW.CHECKLIST_ID;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCHECKLIST',NEW.CHECKLIST_ID, HISTORICO);
    END
end;

CREATE TRIGGER TTBCHECKLIST_USUARIO_B01U FOR TBCHECKLIST_USUARIO
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.CHECKLIST_ID <> NEW.CHECKLIST_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' Registro de usuario: '||OLD.ID||', Checklist ID: '||old.CHECKLIST_ID||'->'||NEW.CHECKLIST_ID;
    END

    IF ( OLD.USUARIO_ID <> NEW.USUARIO_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||', Registro de usuario: '||old.ID||', Usuario ID: '|| OLD.USUARIO_ID||'->'||NEW.USUARIO_ID;
    END

    IF ( OLD.TIPO <> NEW.TIPO) THEN
    BEGIN
        HISTORICO = HISTORICO||', Registro de usuario: '||old.ID||', Tipo: '||OLD.TIPO||'->'||NEW.TIPO;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCHECKLIST',NEW.CHECKLIST_ID, HISTORICO);
    END
end;

CREATE TRIGGER TTBCHECKLIST_USUARIO_B01D FOR TBCHECKLIST_USUARIO
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Registro de usuario: '||OLD.ID||' Excluida do checklist'||OLD.CHECKLIST_ID;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCHECKLIST',old.CHECKLIST_ID, HISTORICO);
    END
end;

CREATE TRIGGER TTBCHECKLIST_HORARIOS_B02I FOR TBCHECKLIST_HORARIOS
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Horário incluido no Checklist: '||NEW.CHECKLIST_ID;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCHECKLIST',NEW.CHECKLIST_ID, HISTORICO);
    END
end;

CREATE OR ALTER TRIGGER TTBCHECKLIST_HORARIOS_B01U FOR TBCHECKLIST_HORARIOS
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.HORA <> NEW.HORA) THEN
    BEGIN
        HISTORICO = HISTORICO||' Registro de horario: '||OLD.ID||', Horario Alterado';
    END

    IF ( OLD.STATUS <> NEW.STATUS) THEN
    BEGIN
        HISTORICO = HISTORICO||', Registro de horario: '||old.ID||', Status: '|| OLD.STATUS||'->'||NEW.STATUS;
    END

    IF ( OLD.DESCRICAO <> NEW.DESCRICAO) THEN
    BEGIN
        HISTORICO = HISTORICO||', Registro de horario: '||old.ID||', Descricao: '||OLD.DESCRICAO||'->'||NEW.DESCRICAO;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCHECKLIST',NEW.CHECKLIST_ID, HISTORICO);
    END
end;

CREATE OR ALTER TRIGGER TTBCHECKLIST_HORARIOS_B01D FOR TBCHECKLIST_HORARIOS
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Registro de horario: '||OLD.ID||', Excluido do checklist: '||OLD.CHECKLIST_ID;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCHECKLIST',old.CHECKLIST_ID, HISTORICO);
    END
end;













