-- TBCASO_STATUS

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_STATUS_B01I FOR TBCASO_STATUS
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBCASO_STATUS,1);
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_STATUS_B02I FOR TBCASO_STATUS
ACTIVE BEFORE INSERT POSITION 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(NEW.ID > 0) THEN
    BEGIN
       HISTORICO = HISTORICO||'STATUS: '|| FN_LPAD(NEW.ID,4,0)||' - '||NEW.DESCRICAO ||' INCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', NEW.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_STATUS_B01U FOR TBCASO_STATUS
ACTIVE BEFORE UPDATE POSITION 0
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.TIPO <> NEW.TIPO)THEN
    BEGIN
       HISTORICO = HISTORICO||' TIPO:'||OLD.TIPO||'->'||NEW.TIPO;
    END

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

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

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

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

IF(HISTORICO <> '') THEN
   BEGIN
     HISTORICO = 'STATUS:'|| FN_LPAD(NEW.ID,4,0)||' - '||NEW.DESCRICAO ||' ALTERADO'||HISTORICO;
     EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', NEW.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
   END
END
^

SET TERM ; ^

SET TERM ^ ;

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

    HISTORICO = '';

    IF(OLD.ID > 0) THEN
    BEGIN
       HISTORICO = HISTORICO||'STATUS: '|| FN_LPAD(OLD.ID,4,0)||' - '||OLD.DESCRICAO ||' EXCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', OLD.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
    END
END
^

SET TERM ; ^


-- TBCASO_PAINEL

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_PAINEL_B01I FOR TBCASO_PAINEL
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBCASO_PAINEL,1);
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_PAINEL_B02I FOR TBCASO_PAINEL
ACTIVE BEFORE INSERT POSITION 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

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

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_PAINEL_B01U FOR TBCASO_PAINEL
ACTIVE BEFORE UPDATE POSITION 0
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.TITULO <> NEW.TITULO) THEN
    BEGIN
        HISTORICO = HISTORICO||' TITULO:'|| OLD.TITULO||'->'||NEW.TITULO;
    END

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

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

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

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

     IF ( OLD.CONTATO_CADASTRO <> NEW.CONTATO_CADASTRO) THEN
    BEGIN
        HISTORICO = HISTORICO||' CONTATO CADASTRO:'|| OLD.CONTATO_CADASTRO||'->'||NEW.CONTATO_CADASTRO;
    END

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

     IF ( OLD.EMAIL_PASS <> NEW.EMAIL_PASS) THEN
    BEGIN
        HISTORICO = HISTORICO||' EMAIL SENHA:'||'SENHA ALTERADA';
    END

     IF ( OLD.EMAIL_SMTP <> NEW.EMAIL_SMTP) THEN
    BEGIN
        HISTORICO = HISTORICO||' EMAIL SMTP:'|| OLD.EMAIL_SMTP||'->'||NEW.EMAIL_SMTP;
    END

     IF ( OLD.PORTA_SMTP <> NEW.PORTA_SMTP) THEN
    BEGIN
        HISTORICO = HISTORICO||' PORTA SMTP:'|| OLD.PORTA_SMTP||'->'||NEW.PORTA_SMTP;
    END

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

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

      IF ( OLD.EMAIL_MONITOR <> NEW.EMAIL_MONITOR) THEN
    BEGIN
        HISTORICO = HISTORICO||' EMAIL MONITOR:'|| OLD.EMAIL_MONITOR||'->'||NEW.EMAIL_MONITOR;
    END

     IF ( OLD.SQL_EMAIL_CONTATO <> NEW.SQL_EMAIL_CONTATO) THEN
    BEGIN
        HISTORICO = HISTORICO||' SQL EMAIL CONTATO:'|| OLD.SQL_EMAIL_CONTATO||'->'||NEW.SQL_EMAIL_CONTATO;
    END

    IF ( OLD.CAMPO_NOME_CONTATO <> NEW.CAMPO_NOME_CONTATO) THEN
    BEGIN
        HISTORICO = HISTORICO||' CAMPO NOME CONTATO:'|| OLD.CAMPO_NOME_CONTATO||'->'||NEW.CAMPO_NOME_CONTATO;
    END

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

SET TERM ; ^

SET TERM ^ ;

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

    HISTORICO = '';

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

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL',OLD.ID, HISTORICO);
    END
END
^

SET TERM ; ^


-- TBCASO_USUARIOS

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_USUARIOS_B01I FOR TBCASO_USUARIOS
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBCASO_USUARIOS,1);
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_USUARIOS_B02I FOR TBCASO_USUARIOS
ACTIVE BEFORE INSERT POSITION 1
AS
DECLARE VARIABLE PARAMETRO_ID INTEGER;
BEGIN

  FOR
    SELECT
      U.ID
    FROM
        TBCASO_PARAMETROS_USUARIO U
    WHERE U.PAINEL_ID = NEW.PAINEL_ID
  INTO
    :PARAMETRO_ID
  DO
  BEGIN
    INSERT INTO TBCASO_USUARIO_PARAMETRO (USUARIO_ID, PARAMETRO_ID, VALOR)
    VALUES (NEW.USUARIO_ID, :PARAMETRO_ID, 0);
  END

END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_USUARIOS_B03I FOR TBCASO_USUARIOS
ACTIVE BEFORE INSERT POSITION 2
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE USUARIO VARCHAR(500);
BEGIN

HISTORICO = '';
USUARIO = COALESCE((SELECT FIRST 1 FN_LPAD(A.CODIGO,4,0)||' - '||A.USUARIO FROM TBUSUARIO A WHERE A.CODIGO = NEW.USUARIO_ID),'');

IF(NEW.ID > 0) THEN
BEGIN
   HISTORICO = HISTORICO||'USUARIO: '||USUARIO||' INCLUIDO';
END

IF (HISTORICO <> '') THEN
   BEGIN
    EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', NEW.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
   END
END
^

SET TERM ; ^

SET TERM ^ ;

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

HISTORICO = '';
USUARIO = COALESCE((SELECT FIRST 1 FN_LPAD(A.CODIGO,4,0)||'-'||A.USUARIO FROM TBUSUARIO A WHERE A.CODIGO = NEW.USUARIO_ID),'');

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

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

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

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

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

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

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

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

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

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


IF(HISTORICO <> '') THEN
   BEGIN
     HISTORICO = 'USUARIO:'||USUARIO||' ALTERADO'||HISTORICO;
     EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', NEW.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
   END
END
^

SET TERM ; ^

SET TERM ^ ;

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

HISTORICO = '';
USUARIO = COALESCE((SELECT FIRST 1 FN_LPAD(A.CODIGO,4,0)||' - '||A.USUARIO FROM TBUSUARIO A WHERE A.CODIGO = OLD.USUARIO_ID),'');

IF(OLD.ID > 0) THEN
BEGIN
   HISTORICO = HISTORICO||'USUARIO:'||USUARIO||' EXCLUIDO';
END

IF (HISTORICO <> '') THEN
   BEGIN
    EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', OLD.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
   END
END
^

SET TERM ; ^


-- TBCASO_USUARIO_PARAMETRO

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_USUARIO_PARAMETRO_B01I FOR TBCASO_USUARIO_PARAMETRO
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBCASO_USUARIO_PARAMETRO,1);
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_USUARIO_PARAMETRO_B02I FOR TBCASO_USUARIO_PARAMETRO
ACTIVE BEFORE INSERT POSITION 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE USUARIO VARCHAR(500);
DECLARE VARIABLE PARAM VARCHAR(500);
BEGIN

HISTORICO = '';
USUARIO = COALESCE((SELECT FIRST 1 FN_LPAD(A.CODIGO,4,0)||' - '||A.USUARIO FROM TBUSUARIO A WHERE A.CODIGO = NEW.USUARIO_ID),'');
PARAM = COALESCE((SELECT FIRST 1 FN_LPAD(B.ID,4,0)||' - '||B.DESCRICAO FROM TBCASO_PARAMETROS_USUARIO B WHERE B.ID = NEW.PARAMETRO_ID),'');

IF(NEW.ID > 0) THEN
  BEGIN
     HISTORICO = HISTORICO||'PARAM: '||PARAM||', INCLUIDO NO USUARIO: '||USUARIO;
  END

IF(HISTORICO <> '') THEN
   BEGIN
      EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', NEW.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
   END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_USUARIO_PARAMETRO_B01U FOR TBCASO_USUARIO_PARAMETRO
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE USUARIO VARCHAR(500);
DECLARE VARIABLE PARAM VARCHAR(500);
BEGIN

HISTORICO = '';
USUARIO = COALESCE((SELECT FIRST 1 FN_LPAD(A.CODIGO,4,0)||' - '||A.USUARIO FROM TBUSUARIO A WHERE A.CODIGO = NEW.USUARIO_ID),'');
PARAM = COALESCE((SELECT FIRST 1 FN_LPAD(B.ID,4,0)||' - '||B.DESCRICAO FROM TBCASO_PARAMETROS_USUARIO B WHERE B.ID = NEW.PARAMETRO_ID),'');

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

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

IF(HISTORICO <> '') THEN
   BEGIN
      HISTORICO = 'PARAM:'||PARAM||' DO USUARIO:'||USUARIO||' ALTERADO'||HISTORICO;
      EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', NEW.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
   END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_USUARIO_PARAMETRO_B01D FOR TBCASO_USUARIO_PARAMETRO
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE USUARIO VARCHAR(500);
DECLARE VARIABLE PARAM VARCHAR(500);
BEGIN

HISTORICO = '';
USUARIO = COALESCE((SELECT FIRST 1 FN_LPAD(A.CODIGO,4,0)||' - '||A.USUARIO FROM TBUSUARIO A WHERE A.CODIGO = OLD.USUARIO_ID),'');
PARAM = COALESCE((SELECT FIRST 1 FN_LPAD(B.ID,4,0)||' - '||B.DESCRICAO FROM TBCASO_PARAMETROS_USUARIO B WHERE B.ID = OLD.PARAMETRO_ID),'');

IF(OLD.ID > 0) THEN
  BEGIN
     HISTORICO = HISTORICO||'PARAM: '||PARAM||', EXCLUIDO NO USUARIO: '||USUARIO;
  END

IF(HISTORICO <> '') THEN
   BEGIN
      EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', OLD.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
   END
END
^

SET TERM ; ^


-- TBCASO_PARAMETROS_USUARIO

ALTER TABLE TBCASO_PARAMETROS_USUARIO ADD UNIQUE (ID);

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_PARAMETROS_USUARIO_B01I FOR TBCASO_PARAMETROS_USUARIO
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBCASO_PARAMETROS_USUARIO,1);
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_PARAMETROS_USUARIO_B02I FOR TBCASO_PARAMETROS_USUARIO
ACTIVE BEFORE INSERT POSITION 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(NEW.ID > 0) THEN
    BEGIN
       HISTORICO = HISTORICO||'PARAM: '|| FN_LPAD(NEW.ID,4,0)||' - '||NEW.DESCRICAO ||' INCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', NEW.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_PARAMETROS_USUARIO_A01I FOR TBCASO_PARAMETROS_USUARIO
ACTIVE AFTER INSERT POSITION 0
AS
DECLARE VARIABLE USUARIO_ID INTEGER;
BEGIN

  FOR
    SELECT
      U.USUARIO_ID
    FROM
        TBCASO_USUARIOS U
    WHERE U.PAINEL_ID = NEW.PAINEL_ID
  INTO
    :USUARIO_ID
  DO
  BEGIN
    INSERT INTO TBCASO_USUARIO_PARAMETRO (USUARIO_ID, PARAMETRO_ID, PAINEL_ID, VALOR)
    VALUES (:USUARIO_ID, NEW.ID, NEW.PAINEL_ID, 0);
  END

END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_PARAMETROS_USUARIO_B01U FOR TBCASO_PARAMETROS_USUARIO
ACTIVE BEFORE UPDATE POSITION 0
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.NOME <> NEW.NOME)THEN
    BEGIN
       HISTORICO = HISTORICO||' VARIAVEL:'||OLD.NOME||'->'||NEW.NOME;
    END

IF(HISTORICO <> '') THEN
   BEGIN
     HISTORICO = 'PARAM:'|| FN_LPAD(NEW.ID,4,0)||' - '||NEW.DESCRICAO ||' ALTERADO'||HISTORICO;
     EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', NEW.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
   END
END
^

SET TERM ; ^

SET TERM ^ ;

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

    HISTORICO = '';

    IF(OLD.ID > 0) THEN
    BEGIN
       HISTORICO = HISTORICO||'PARAM: '|| FN_LPAD(OLD.ID,4,0)||' - '||OLD.DESCRICAO ||' EXCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', OLD.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE TRIGGER TTBCASO_PARAMETROS_USUARIO_B02D FOR TBCASO_PARAMETROS_USUARIO
ACTIVE BEFORE DELETE POSITION 1
AS
BEGIN
   IF(OLD.ID > 0) THEN
   BEGIN
      DELETE FROM TBCASO_USUARIO_PARAMETRO WHERE PARAMETRO_ID = OLD.ID;
   END
END^

SET TERM ; ^


-- TBCASO_MOTIVO


SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_MOTIVO_B01I FOR TBCASO_MOTIVO
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBCASO_MOTIVO, 1);
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_MOTIVO_B02I FOR TBCASO_MOTIVO
ACTIVE BEFORE INSERT POSITION 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(NEW.ID > 0) THEN
    BEGIN
       HISTORICO = HISTORICO||'MOTIVO: '|| FN_LPAD(NEW.ID,4,0)||' - '||NEW.DESCRICAO ||' INCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', NEW.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_MOTIVO_B01U FOR TBCASO_MOTIVO
ACTIVE BEFORE UPDATE POSITION 0
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.STATUS <> NEW.STATUS)THEN
    BEGIN
       HISTORICO = HISTORICO||' STATUS:'||OLD.STATUS||'->'||NEW.STATUS;
    END

IF(HISTORICO <> '') THEN
   BEGIN
     HISTORICO = 'MOTIVO:'|| FN_LPAD(NEW.ID,4,0)||' - '||NEW.DESCRICAO ||' ALTERADO'||HISTORICO;
     EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', NEW.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
   END
END
^

SET TERM ; ^

SET TERM ^ ;

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

    HISTORICO = '';

    IF(OLD.ID > 0) THEN
    BEGIN
       HISTORICO = HISTORICO||'MOTIVO: '|| FN_LPAD(OLD.ID,4,0)||' - '||OLD.DESCRICAO ||' EXCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', OLD.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_MOTIVO_B02D FOR TBCASO_MOTIVO
ACTIVE BEFORE DELETE POSITION 1
AS
BEGIN
   IF(OLD.ID > 0) THEN
   BEGIN
      DELETE FROM TBCASO_TIPO WHERE MOTIVO_ID = OLD.ID;
   END
END
^

SET TERM ; ^


-- TBCASO_TIPO

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_TIPO_B01I FOR TBCASO_TIPO
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBCASO_TIPO,1);
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_TIPO_B02I FOR TBCASO_TIPO
ACTIVE BEFORE INSERT POSITION 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE PAINEL_ID INTEGER;
DECLARE VARIABLE MOTIVO VARCHAR(500);
BEGIN
     HISTORICO = '';
     MOTIVO = COALESCE((SELECT FIRST 1 FN_LPAD(A.ID,4,0)||' - '||A.DESCRICAO FROM TBCASO_MOTIVO A WHERE A.ID = NEW.MOTIVO_ID),'');
     PAINEL_ID = COALESCE((SELECT FIRST 1 A.ID FROM TBCASO_PAINEL A WHERE A.ID = (SELECT FIRST 1 B.PAINEL_ID FROM TBCASO_MOTIVO B WHERE B.ID = NEW.MOTIVO_ID)),0);
     IF(PAINEL_ID > 0)THEN
     HISTORICO = HISTORICO||'TIPO: '||FN_LPAD(NEW.ID,4,0)||' - '||NEW.DESCRICAO||', INCLUIDO NO MOTIVO: '||MOTIVO;
     BEGIN
         EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
     END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_TIPO_B01U FOR TBCASO_TIPO
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE PAINEL_ID INTEGER;
DECLARE VARIABLE MOTIVO VARCHAR(500);
BEGIN

HISTORICO = '';
MOTIVO = COALESCE((SELECT FIRST 1 FN_LPAD(A.ID,4,0)||' - '||A.DESCRICAO FROM TBCASO_MOTIVO A WHERE A.ID = NEW.MOTIVO_ID),'');
PAINEL_ID = COALESCE((SELECT FIRST 1 A.ID FROM TBCASO_PAINEL A WHERE A.ID = (SELECT FIRST 1 B.PAINEL_ID FROM TBCASO_MOTIVO B WHERE B.ID = NEW.MOTIVO_ID)),0);

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

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

IF(HISTORICO > '')THEN
    BEGIN
        HISTORICO = 'TIPO: '||FN_LPAD(NEW.ID,4,0)||' - '||NEW.DESCRICAO||' DO MOTIVO: '||MOTIVO||' ALTERADO'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_TIPO_B01D FOR TBCASO_TIPO
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE PAINEL_ID INTEGER;
DECLARE VARIABLE MOTIVO VARCHAR(500);
BEGIN
     HISTORICO = '';
     MOTIVO = COALESCE((SELECT FIRST 1 FN_LPAD(A.ID,4,0)||' - '||A.DESCRICAO FROM TBCASO_MOTIVO A WHERE A.ID = OLD.MOTIVO_ID),'');
     PAINEL_ID = COALESCE((SELECT FIRST 1 A.ID FROM TBCASO_PAINEL A WHERE A.ID = (SELECT FIRST 1 B.PAINEL_ID FROM TBCASO_MOTIVO B WHERE B.ID = OLD.MOTIVO_ID)),0);
     IF(PAINEL_ID > 0)THEN
     HISTORICO = HISTORICO||'TIPO: '||FN_LPAD(OLD.ID,4,0)||' - '||OLD.DESCRICAO||', EXCLUIDO NO MOTIVO: '||MOTIVO;
     BEGIN
         EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
     END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_TIPO_B02D FOR TBCASO_TIPO
ACTIVE BEFORE DELETE POSITION 1
AS
BEGIN
   IF(OLD.ID > 0) THEN
   BEGIN
      DELETE FROM TBCASO_ORIGEM WHERE TIPO_ID = OLD.ID;
   END
END
^

SET TERM ; ^


-- TBCASO_ORIGEM

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_ORIGEM_B01I FOR TBCASO_ORIGEM
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBCASO_ORIGEM,1);
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_ORIGEM_B02I FOR TBCASO_ORIGEM
ACTIVE BEFORE INSERT POSITION 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE PAINEL_ID INTEGER;
DECLARE VARIABLE MOTIVO_ID INTEGER;
BEGIN
HISTORICO = '';
MOTIVO_ID = 0;
PAINEL_ID = 0;
  MOTIVO_ID = COALESCE((SELECT FIRST 1 A.ID FROM TBCASO_MOTIVO A WHERE A.ID = (SELECT FIRST 1 B.MOTIVO_ID FROM TBCASO_TIPO B WHERE B.ID = NEW.TIPO_ID)),0);
  IF(MOTIVO_ID > 0)THEN
    BEGIN
       PAINEL_ID = COALESCE((SELECT FIRST 1 B.PAINEL_ID FROM TBCASO_MOTIVO B WHERE B.ID = :MOTIVO_ID),0);
    END


IF(PAINEL_ID > 0 AND MOTIVO_ID > 0)THEN
   HISTORICO = HISTORICO||'ORIGEM: '||FN_LPAD(NEW.ID,4,0)||' - '||NEW.DESCRICAO||', INCLUIDO NO TIPO: '||NEW.TIPO_ID||' DO MOTIVO:'||MOTIVO_ID;
    BEGIN
       EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_ORIGEM_B01U FOR TBCASO_ORIGEM
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE PAINEL_ID INTEGER;
DECLARE VARIABLE MOTIVO_ID INTEGER;
BEGIN
HISTORICO = '';
MOTIVO_ID = 0;
PAINEL_ID = 0;
  MOTIVO_ID = COALESCE((SELECT FIRST 1 A.ID FROM TBCASO_MOTIVO A WHERE A.ID = (SELECT FIRST 1 B.MOTIVO_ID FROM TBCASO_TIPO B WHERE B.ID = NEW.TIPO_ID)),0);
  IF(MOTIVO_ID > 0)THEN
    BEGIN
       PAINEL_ID = COALESCE((SELECT FIRST 1 B.PAINEL_ID FROM TBCASO_MOTIVO B WHERE B.ID = :MOTIVO_ID),0);
    END

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

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


IF(HISTORICO > '')THEN
    BEGIN
       HISTORICO = HISTORICO||'ORIGEM: '||FN_LPAD(NEW.ID,4,0)||' - '||NEW.DESCRICAO||' DO TIPO: '||NEW.TIPO_ID||' DO MOTIVO:'||MOTIVO_ID||' ALTERADO'||HISTORICO;
       EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_ORIGEM_B01D FOR TBCASO_ORIGEM
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE PAINEL_ID INTEGER;
DECLARE VARIABLE MOTIVO_ID INTEGER;
BEGIN
HISTORICO = '';
MOTIVO_ID = 0;
PAINEL_ID = 0;
  MOTIVO_ID = COALESCE((SELECT FIRST 1 A.ID FROM TBCASO_MOTIVO A WHERE A.ID = (SELECT FIRST 1 B.MOTIVO_ID FROM TBCASO_TIPO B WHERE B.ID = OLD.TIPO_ID)),0);
  IF(MOTIVO_ID > 0)THEN
    BEGIN
       PAINEL_ID = COALESCE((SELECT FIRST 1 B.PAINEL_ID FROM TBCASO_MOTIVO B WHERE B.ID = :MOTIVO_ID),0);
    END


IF(PAINEL_ID > 0 AND MOTIVO_ID > 0)THEN
   HISTORICO = HISTORICO||'ORIGEM: '||FN_LPAD(OLD.ID,4,0)||' - '||OLD.DESCRICAO||', INCLUIDO NO TIPO: '||OLD.TIPO_ID||' DO MOTIVO:'||MOTIVO_ID;
    BEGIN
       EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
    END
END
^

SET TERM ; ^

-- --------------------------------------------------------------------------------------------------------------------------------------------------------------- --
-- TBCASO_CAMPOS_AGRUPAMENTO

DROP TRIGGER TBCASO_CAMPOS_AGRUPAMENTO_BI;

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_CAMPOS_AGRUPAMENTO_B01I FOR TBCASO_CAMPOS_AGRUPAMENTO
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBCASO_CAMPOS_AGRUPAMENTO,1);
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_CAMPOS_AGRUPAMENTO_B02I FOR TBCASO_CAMPOS_AGRUPAMENTO
ACTIVE BEFORE INSERT POSITION 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(NEW.ID > 0) THEN
    BEGIN
       HISTORICO = HISTORICO||'AGRUP: '|| FN_LPAD(NEW.ID,4,0)||' - '||NEW.DESCRICAO ||' INCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', NEW.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
    END
END
^

SET TERM ; ^

SET TERM ^ ;

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

HISTORICO = '';

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

IF(HISTORICO <> '') THEN
   BEGIN
     HISTORICO = 'AGRUP:'|| FN_LPAD(NEW.ID,4,0)||' - '||NEW.DESCRICAO ||' ALTERADO'||HISTORICO;
     EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', NEW.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
   END
END
^

SET TERM ; ^

SET TERM ^ ;

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

    HISTORICO = '';

    IF(OLD.ID > 0) THEN
    BEGIN
       HISTORICO = HISTORICO||'AGRUP: '|| FN_LPAD(OLD.ID,4,0)||' - '||OLD.DESCRICAO ||' EXCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', OLD.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
    END
END
^

SET TERM ; ^

-- TBCASO_PAINEL_CONF

DROP TRIGGER TBCASO_PAINEL_CONF_BI;

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_PAINEL_CONF_B01I FOR TBCASO_PAINEL_CONF
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBCASO_PAINEL_CONF,1);
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE TRIGGER TTBCASO_PAINEL_CONF_B02I FOR TBCASO_PAINEL_CONF
ACTIVE BEFORE INSERT POSITION 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(NEW.ID > 0) THEN
    BEGIN
       HISTORICO = HISTORICO||'CAMPO: '|| FN_LPAD(NEW.ID,4,0)||' - '||NEW.DESCRICAO ||' INCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', NEW.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
    END
END
^

SET TERM ; ^

SET TERM ^ ;

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

HISTORICO = '';

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

IF(HISTORICO <> '') THEN
   BEGIN
     HISTORICO = 'CAMPO:'|| FN_LPAD(NEW.ID,4,0)||' - '||NEW.DESCRICAO ||' ALTERADO'||HISTORICO;
     EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', NEW.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
   END
END
^

SET TERM ; ^

SET TERM ^ ;

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

    HISTORICO = '';

    IF(OLD.ID > 0) THEN
    BEGIN
       HISTORICO = HISTORICO||'CAMPO: '|| FN_LPAD(OLD.ID,4,0)||' - '||OLD.DESCRICAO ||' EXCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', OLD.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE TRIGGER TTBCASO_PAINEL_CONF_B02D FOR TBCASO_PAINEL_CONF
ACTIVE BEFORE DELETE POSITION 1
AS
BEGIN
   IF(OLD.ID > 0) THEN
   BEGIN
      DELETE FROM TBCASO_PAINEL_CONF_ITENS WHERE CAMPO_ID = OLD.ID;
   END
END
^

SET TERM ; ^


--TBCASO_PAINEL_CONF_ITENS

DROP TRIGGER TBCASO_PAINEL_CONF_ITENS_BI;

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_PAINEL_CONF_ITENS_B01I FOR TBCASO_PAINEL_CONF_ITENS
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBCASO_PAINEL_CONF_ITENS,1);
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE TRIGGER TTBCASO_PAINEL_CONF_ITENS_B02I FOR TBCASO_PAINEL_CONF_ITENS
ACTIVE BEFORE INSERT POSITION 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE PAINEL_CONF VARCHAR(500);
BEGIN

    HISTORICO = '';
    PAINEL_CONF = COALESCE((SELECT FIRST 1 FN_LPAD(A.ID,4,0)||' - '||A.DESCRICAO FROM TBCASO_PAINEL_CONF A WHERE A.ID = NEW.CAMPO_ID),'');

    IF(NEW.ID > 0) THEN
    BEGIN
       HISTORICO = HISTORICO||'ITEM: '||FN_LPAD(NEW.ID,4,0)||' - '||NEW.DESCRICAO||', INCLUIDO NO CAMPO: '||PAINEL_CONF;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', NEW.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE TRIGGER TTBCASO_PAINEL_CONF_ITENS_B01U FOR TBCASO_PAINEL_CONF_ITENS
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE PAINEL_CONF VARCHAR(500);
BEGIN

    HISTORICO = '';
    PAINEL_CONF = COALESCE((SELECT FIRST 1 FN_LPAD(A.ID,4,0)||' - '||A.DESCRICAO FROM TBCASO_PAINEL_CONF A WHERE A.ID = NEW.CAMPO_ID),'');

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

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

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

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

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'ITEM: '||FN_LPAD(NEW.ID,4,0)||' - '||NEW.DESCRICAO||' DO CAMPO: '||PAINEL_CONF||' ALTERADO'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', OLD.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE TRIGGER TTBCASO_PAINEL_CONF_ITENS_B01D FOR TBCASO_PAINEL_CONF_ITENS
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE PAINEL_CONF VARCHAR(500);
BEGIN

    HISTORICO = '';
    PAINEL_CONF = COALESCE((SELECT FIRST 1 FN_LPAD(A.ID,4,0)||' - '||A.DESCRICAO FROM TBCASO_PAINEL_CONF A WHERE A.ID = OLD.CAMPO_ID),'');

    IF(OLD.ID > 0) THEN
    BEGIN
       HISTORICO = HISTORICO||'ITEM: '||FN_LPAD(OLD.ID,4,0)||' - '||OLD.DESCRICAO||', EXCLUIDO NO CAMPO: '||PAINEL_CONF;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', OLD.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
    END
END
^

SET TERM ; ^


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

-- TBCASO_PAINEL_CONF

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_PAINEL_CONF_B02D FOR TBCASO_PAINEL_CONF
ACTIVE BEFORE DELETE POSITION 1
AS
BEGIN
   IF(OLD.ID > 0) THEN
   BEGIN
      DELETE FROM TBCASO_PAINEL_CONF_ITENS I WHERE I.CAMPO_ID = OLD.ID;
      DELETE FROM TBCASO_CAMPO_DEPENDENCIA D WHERE D.CAMPO_ID = OLD.ID;
   END
END
^

SET TERM ; ^

-- TBCASO_CAMPO_DEPENDENCIA

DROP TRIGGER TBCASO_CAMPO_DEPENDENCIA_BI;

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_CAMPO_DEPENDENCIA_B01I FOR TBCASO_CAMPO_DEPENDENCIA
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBCASO_CAMPO_DEPENDENCIA,1);
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE TRIGGER TTBCASO_CAMPO_DEPENDENCIA_B02I FOR TBCASO_CAMPO_DEPENDENCIA
ACTIVE BEFORE INSERT POSITION 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE PAINEL_CONF VARCHAR(500);
BEGIN

    HISTORICO = '';
    PAINEL_CONF = COALESCE((SELECT FIRST 1 FN_LPAD(A.ID,4,0)||' - '||A.DESCRICAO FROM TBCASO_PAINEL_CONF A WHERE A.ID = NEW.CAMPO_ID),'');

    IF(NEW.ID > 0) THEN
    BEGIN
       HISTORICO = HISTORICO||'DEPEND: '||FN_LPAD(NEW.ID,4,0)||', INCLUIDO NO CAMPO: '||PAINEL_CONF;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', NEW.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE TRIGGER TTBCASO_CAMPO_DEPENDENCIA_B01D FOR TBCASO_CAMPO_DEPENDENCIA
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE PAINEL_CONF VARCHAR(500);
BEGIN

    HISTORICO = '';
    PAINEL_CONF = COALESCE((SELECT FIRST 1 FN_LPAD(A.ID,4,0)||' - '||A.DESCRICAO FROM TBCASO_PAINEL_CONF A WHERE A.ID = OLD.CAMPO_ID),'');

    IF(OLD.ID > 0) THEN
    BEGIN
       HISTORICO = HISTORICO||'DEPEND: '||FN_LPAD(OLD.ID,4,0)||', EXCLUIDA NO CAMPO: '||PAINEL_CONF;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', OLD.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
    END
END
^

SET TERM ; ^

