-- 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 ; ^

