CREATE TRIGGER TTBNATUREZA_B01I FOR TBNATUREZA
ACTIVE BEFORE INSERT POSITION 0
as
begin
  if (new.id is null) then
    new.id = gen_id(GTBNATUREZA,1);
end;

CREATE TRIGGER TTBNATUREZA_B02I FOR TBNATUREZA
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Natureza contábil incluida';
    END

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

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

    HISTORICO = '';

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

    IF ( OLD.DESCRICAO <> NEW.DESCRICAO) THEN
    BEGIN
        HISTORICO = HISTORICO||'Natureza ID: '||OLD.ID||', Descricao:'|| OLD.DESCRICAO||'->'||NEW.DESCRICAO;
    END

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

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

    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Natureza Contabil Excluida: '||old.ID;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBNATUREZA',old.ID, HISTORICO);
    END
end;
	

