-- TBCASO_CAMPOS_AGRUPAMENTO
DROP TRIGGER TTBCASO_CAMPOS_AGRUPAMENTO_B01D;

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_CAMPOS_AGRUPAMENTO_B02D FOR TBCASO_CAMPOS_AGRUPAMENTO
ACTIVE BEFORE DELETE POSITION 1
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 ; ^

SET TERM ^ ;

CREATE TRIGGER TTBCASO_CAMPOS_AGRUPAMENTO_B01D FOR TBCASO_CAMPOS_AGRUPAMENTO
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE CONT1 INTEGER;
DECLARE VARIABLE CONT2 INTEGER;
BEGIN
  CONT1 = COALESCE((SELECT COUNT(C.ID) FROM TBCASO_PAINEL_CONF C WHERE C.GRUPO_ID = OLD.ID), 0);
  CONT2 = COALESCE((SELECT COUNT(C.ID) FROM TBCASO_CONTATO_CONF C WHERE C.GRUPO_ID = OLD.ID), 0);

  IF(CONT1 > 0 OR CONT2 > 0)THEN
  BEGIN
     EXCEPTION "..." 'Exclusão não permitida! Existem registros vinculados a este Agrupamento.';
  END
END
^

SET TERM ; ^

-- TBCASO_CONTATO_CONF

DROP TRIGGER TBCASO_CONTATO_CONF_BI;

SET TERM ^ ;

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

SET TERM ; ^

SET TERM ^ ;

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

    HISTORICO = '';

    IF(NEW.ID > 0) THEN
    BEGIN
       HISTORICO = HISTORICO||'CONTATO: '|| 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_CONTATO_CONF_B01U FOR TBCASO_CONTATO_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.CONSULTA <> NEW.CONSULTA)THEN
    BEGIN
       HISTORICO = HISTORICO||' CONSULTA:'||OLD.CONSULTA||'->'||NEW.CONSULTA;
    END

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

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

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

    IF(OLD."DEFAULT" <> NEW."DEFAULT")THEN
    BEGIN
       HISTORICO = HISTORICO||' PADRAO:'||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||' TAM:'||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||' GRUPO_ID:'||OLD.GRUPO_ID||'->'||NEW.GRUPO_ID;
    END

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

IF(HISTORICO <> '') THEN
   BEGIN
     HISTORICO = 'CONTATO:'|| 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_CONTATO_CONF_B01D FOR TBCASO_CONTATO_CONF
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(OLD.ID > 0) THEN
    BEGIN
       HISTORICO = HISTORICO||'CONTATO: '|| 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_CONTATO_CONF_B02D FOR TBCASO_CONTATO_CONF
ACTIVE BEFORE DELETE POSITION 1
AS
BEGIN
   IF(OLD.ID > 0) THEN
   BEGIN
      DELETE FROM TBCASO_CONTATO_CONF_ITENS WHERE CAMPO_ID = OLD.ID;
   END
END
^

SET TERM ; ^


--TBCASO_CONTATO_CONF_ITENS

DROP TRIGGER TBCASO_CONTATO_CONF_ITENS_BI;

SET TERM ^ ;

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

SET TERM ; ^

SET TERM ^ ;

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

    HISTORICO = '';
    CONTATO_CONF = COALESCE((SELECT FIRST 1 FN_LPAD(A.ID,4,0)||' - '||A.DESCRICAO FROM TBCASO_CONTATO_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 CONTATO: '||CONTATO_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 OR ALTER TRIGGER TTBCASO_CONTATO_CONF_ITENS_B01U FOR TBCASO_CONTATO_CONF_ITENS
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE CONTATO_CONF VARCHAR(500);
BEGIN

    HISTORICO = '';
    CONTATO_CONF = COALESCE((SELECT FIRST 1 FN_LPAD(A.ID,4,0)||' - '||A.DESCRICAO FROM TBCASO_CONTATO_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 CONTATO: '||CONTATO_CONF||' ALTERADO'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBCASO_PAINEL', OLD.PAINEL_ID,FN_ELIPSES(:HISTORICO,500));
    END
END
^

SET TERM ; ^

SET TERM ^ ;

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

    HISTORICO = '';
    CONTATO_CONF = COALESCE((SELECT FIRST 1 FN_LPAD(A.ID,4,0)||' - '||A.DESCRICAO FROM TBCASO_CONTATO_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 CONTATO: '||CONTATO_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

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_PAINEL_B02D FOR TBCASO_PAINEL
ACTIVE BEFORE DELETE POSITION 1
AS
BEGIN
   IF(OLD.ID > 0) THEN
   BEGIN
      DELETE FROM TBCASO_VISUALIZACAO_CONF V WHERE V.PAINEL_ID = OLD.ID;    -- VISUALIZAÇÃO ESTÁ VINCULADA COM PAINEL DURANTE INCLUSÃO.

      DELETE FROM TBCASO_PAINEL_CONF A WHERE A.PAINEL_ID = OLD.ID;          -- CAMPOS (ALT+2)
      DELETE FROM TBCASO_USUARIOS B WHERE B.PAINEL_ID = OLD.ID;             -- USUÁRIOS (ALT+3)
      DELETE FROM TBCASO_PARAMETROS_USUARIO C WHERE C.PAINEL_ID = OLD.ID;   -- PARÂMETROS (ALT+4)
      DELETE FROM TBCASO_STATUS D WHERE D.PAINEL_ID = OLD.ID;               -- STATUS (ALT+5)
      DELETE FROM TBCASO_MOTIVO E WHERE E.PAINEL_ID = OLD.ID;               -- MOTIVO (ALT+6)
      DELETE FROM TBCASO_VISUALIZACAO_CAMPO F WHERE F.PAINEL_ID = OLD.ID;   -- VISUALIZAÇÃO (ALT+8)
      DELETE FROM TBCASO_VALIDACAO G WHERE G.PAINEL_ID = OLD.ID;            -- VALIDAÇÃO (ALT+9)
      DELETE FROM TBCASO_CONTATO_CONF H WHERE H.PAINEL_ID = OLD.ID;         -- CONTATO (ALT+0)
      DELETE FROM TBCASO_CAMPOS_AGRUPAMENTO I WHERE I.PAINEL_ID = OLD.ID;   -- AGRUPAMENTOS (ALT+7)
   END
END
^

SET TERM ; ^

