ALTER TABLE TBCLASSIFICACAO_CONTABIL
    ADD PERFIL VARCHAR(10);
SET TERM ^ ;

CREATE OR ALTER trigger ttbclassificacao_cont_b01u for tbclassificacao_contabil
active before update position 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(1000);
BEGIN

    HISTORICO = '';

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

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

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

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

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

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

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

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

    IF (OLD.TIPO_CUSTO IS DISTINCT FROM NEW.TIPO_CUSTO) THEN
    BEGIN
         HISTORICO = HISTORICO||' CONTR.ORCAM: '||COALESCE(OLD.TIPO_CUSTO, 0)||'->'||COALESCE(NEW.TIPO_CUSTO, 0);
    END

     IF (OLD.PERFIL IS DISTINCT FROM NEW.PERFIL) THEN
    BEGIN
         HISTORICO = HISTORICO||' CONTR.ORCAM: '||COALESCE(OLD.PERFIL, '')||'->'||COALESCE(NEW.PERFIL, '');
    END

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

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbclassificacao_contabil_b04u for tbclassificacao_contabil
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

  HISTORICO = '';

  if (OLD.PERFIL IS NULL AND NEW.PERFIL IS NOT NULL) then
       BEGIN
       HISTORICO = HISTORICO||' CLASS.FIN.INCLUIDA: '||NEW.CODIGO||' - '||NEW.DESCRICAO;
       END
       
  if (OLD.PERFIL IS NOT NULL AND NEW.PERFIL IS NOT NULL AND OLD.PERFIL <> NEW.PERFIL) then
       BEGIN
       HISTORICO = HISTORICO||' CLASS.FIN.INCLUIDA: '||NEW.CODIGO||' - '||NEW.DESCRICAO;
       END
  IF(HISTORICO <> '') THEN
  BEGIN
    EXECUTE PROCEDURE SPI_HISTORICO('TBPERFIL', (SELECT FIRST 1 P.CONTROLE FROM TBPERFIL P WHERE P.ID = NEW.PERFIL AND P.TABELA = 'CFIN'), HISTORICO);
  END
END
^

SET TERM ; ^



SET TERM ^ ;

CREATE OR ALTER trigger ttbclassificacao_contabil_b05u for tbclassificacao_contabil
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

  HISTORICO = '';

  if (OLD.PERFIL IS NOT NULL AND NEW.PERFIL = '') then
       BEGIN
       HISTORICO = HISTORICO||' CLASS.FIN.EXCLUIDA: '||OLD.CODIGO||' - '||OLD.DESCRICAO;
       END

  IF(HISTORICO <> '') THEN
  BEGIN
    EXECUTE PROCEDURE SPI_HISTORICO('TBPERFIL', (SELECT FIRST 1 P.CONTROLE FROM TBPERFIL P WHERE P.ID = OLD.PERFIL AND P.TABELA = 'CFIN'), HISTORICO);
  END
END
^

SET TERM ; ^






