CREATE TRIGGER TTBCLASSIFICACAO_CONT_B01I FOR TBCLASSIFICACAO_CONTABIL
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBCLASSIFICACAO_CONTABIL,1);
END;

CREATE OR ALTER TRIGGER TTBCLASSIFICACAO_CONT_B02I FOR TBCLASSIFICACAO_CONTABIL
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

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

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 (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBCLASSIFICACAO_CONTABIL', NEW.ID, FN_ELIPSES(HISTORICO,500));
    END
END;

CREATE OR ALTER TRIGGER TTBCLASSIFICACAO_CONT_B02D FOR TBCLASSIFICACAO_CONTABIL
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

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