CREATE TRIGGER TTBRH_HORARIO_B01I FOR TBRH_HORARIO
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.CODIGO IS NULL) THEN
    NEW.CODIGO = GEN_ID(GTBRH_HORARIO,1);
END;

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

    HISTORICO = '';

    IF ( NEW.CODIGO > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Registro Incluido';
    END

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

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

    HISTORICO = '';

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

    IF ( OLD.HORA1 <> NEW.HORA1) THEN
    BEGIN
        HISTORICO = HISTORICO||',Horario Antes:'|| OLD.HORA1||'->'||NEW.HORA1;
    END

    IF ( OLD.HORA2 <> NEW.HORA2) THEN
    BEGIN
        HISTORICO = HISTORICO||'Horario Hora:'|| OLD.HORA2||'->'||NEW.HORA2;
    END

    IF ( OLD.HORA3 <> NEW.HORA3) THEN
    BEGIN
        HISTORICO = HISTORICO||',Horario Apos:'|| OLD.HORA3||'->'||NEW.HORA3;
    END

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

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

    HISTORICO = '';

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

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