CREATE TRIGGER TTBESCALA_B01I FOR TBESCALA
ACTIVE BEFORE INSERT POSITION 0
AS
begin
  IF(NEW.CODIGO IS NULL) THEN
     NEW.CODIGO = GEN_ID(GTBESCALA,1);
end;

CREATE OR ALTER TRIGGER TTBESCALA_B02I FOR TBESCALA
ACTIVE BEFORE INSERT POSITION 9
AS
declare variable historico varchar(500);
BEGIN
  historico = '';
  IF(NEW.CODIGO > 0) then
  BEGIN
    historico = historico||' Reg.Incluido';
  END

  IF(HISTORICO <> '') then
  BEGIN
    execute procedure spi_historico('TBESCALA',NEW.CODIGO,historico);
  END
END;

CREATE OR ALTER TRIGGER TTBESCALA_B01U FOR TBESCALA
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(OLD.DESCRICAO <> NEW.DESCRICAO) THEN
    BEGIN
       HISTORICO = HISTORICO||'Desc:'||OLD.DESCRICAO||'->'||NEW.DESCRICAO;
    END

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

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

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

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

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

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

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

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

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

    IF(OLD.TURNO <> NEW.TURNO)THEN
    BEGIN
        HISTORICO = HISTORICO||'Turno:'||OLD.TURNO||'->'||NEW.TURNO;
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBESCALA',NEW.CODIGO,FN_ELIPSES(HISTORICO,500));
    END
END;

CREATE TRIGGER TTBESCALA_B01D FOR TBESCALA
ACTIVE BEFORE DELETE POSITION 9
AS
declare variable historico varchar(500);
BEGIN
  historico = '';
  IF(OLD.CODIGO > 0) then
  BEGIN
    historico = historico||' Reg.Excluido';
  END

  IF(HISTORICO <> '') then
  BEGIN
    execute procedure spi_historico('TBESCALA',OLD.CODIGO,historico);
  END
END;

CREATE TRIGGER TTBESCALA_HORARIO_B01I FOR TBESCALA_HORARIO
ACTIVE BEFORE INSERT POSITION 0
AS
declare variable HORARIO_CODIGO VARCHAR(10);
begin
    HORARIO_CODIGO = '';
    IF(NEW.HORARIO_CODIGO IS NOT NULL) THEN
    BEGIN
       HORARIO_CODIGO = COALESCE((SELECT FIRST 1 A.HORARIO_CODIGO FROM TBESCALA_HORARIO A
                            WHERE A.HORARIO_CODIGO = NEW.HORARIO_CODIGO
                             AND A.ESCALA_CODIGO = NEW.ESCALA_CODIGO
                             AND A.PROXIMA_JORNADA = NEW.PROXIMA_JORNADA
                             AND A.DIA_SEMANA = NEW.DIA_SEMANA),'');
    END
    IF(HORARIO_CODIGO <> '') THEN
    BEGIN
        EXCEPTION "..." 'HORARIO COD.'||HORARIO_CODIGO||' JA CADASTRADO PARA ESTE DIA';
    END
end;

CREATE OR ALTER TRIGGER TTBESCALA_HORARIO_B02I FOR TBESCALA_HORARIO
ACTIVE BEFORE INSERT POSITION 9
AS
declare variable historico varchar(500);
BEGIN
  historico = '';
  IF(NEW.HORARIO_CODIGO > 0 AND NEW.ESCALA_CODIGO > 0) then
  BEGIN
    historico = historico||'Horario cod.'||NEW.HORARIO_CODIGO||' Incluido';
  END

  IF(HISTORICO <> '') then
  BEGIN
    execute procedure spi_historico('TBESCALA',NEW.ESCALA_CODIGO,historico);
  END
END;

CREATE OR ALTER TRIGGER TTBESCALA_HORARIO_B01U FOR TBESCALA_HORARIO
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(OLD.GERA_PROXIMA <> NEW.GERA_PROXIMA) THEN
    BEGIN
       HISTORICO = HISTORICO||'Gera.Prox:'||OLD.GERA_PROXIMA||'->'||NEW.GERA_PROXIMA;
    END

    IF(OLD.PROXIMA_JORNADA <> NEW.PROXIMA_JORNADA)THEN
    BEGIN
        HISTORICO = HISTORICO||'Prox.Jorn:'||OLD.PROXIMA_JORNADA||'->'||NEW.PROXIMA_JORNADA;
    END

    IF(OLD.GERA_ANTERIOR <> NEW.GERA_ANTERIOR)THEN
    BEGIN
        HISTORICO = HISTORICO||'Gera.Ant:'||OLD.GERA_ANTERIOR||'->'||NEW.GERA_ANTERIOR;
    END

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

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

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

    IF(OLD.HORARIO_CODIGO <> NEW.HORARIO_CODIGO)THEN
    BEGIN
        HISTORICO = HISTORICO||'Horario:'||OLD.HORARIO_CODIGO||'->'||NEW.HORARIO_CODIGO;
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'Horario cod.'||old.HORARIO_CODIGO||' Alterado '||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBESCALA',OLD.ESCALA_CODIGO,FN_ELIPSES(HISTORICO,500));
    END
END;

CREATE OR ALTER TRIGGER TTBESCALA_HORARIO_B01D FOR TBESCALA_HORARIO
ACTIVE BEFORE DELETE POSITION 9
AS
declare variable historico varchar(500);
BEGIN
  historico = '';
  IF(OLD.HORARIO_CODIGO > 0 AND OLD.ESCALA_CODIGO > 0) then
  BEGIN
    historico = historico||'Horario cod.'||OLD.HORARIO_CODIGO||' Excluido';
  END

  IF(HISTORICO <> '') then
  BEGIN
    execute procedure spi_historico('TBESCALA',OLD.ESCALA_CODIGO,historico);
  END
END;






