CREATE OR ALTER TRIGGER TTBFOLPAG_CONTABIL_B02I FOR TBFOLPAG_CONTABIL
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Reg. Incluído.';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_CONTABIL',NEW.ID,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFOLPAG_CONTABIL_B01U FOR TBFOLPAG_CONTABIL
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(NEW.DESCRICAO <> OLD.DESCRICAO) THEN
    BEGIN
         HISTORICO = HISTORICO||'Descrição:'||OLD.DESCRICAO||'->'||NEW.DESCRICAO;
    END            

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

    IF(NEW.DIA_CONTABIL <> OLD.DIA_CONTABIL) THEN
    BEGIN
        HISTORICO = HISTORICO||'Dia:'||OLD.DIA_CONTABIL||'->'||NEW.DIA_CONTABIL;
    END 
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_CONTABIL',NEW.ID,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFOLPAG_CONTABIL_B01D FOR TBFOLPAG_CONTABIL
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Reg. Excluído.';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_CONTABIL',OLD.ID,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFOLPAG_CONTABIL_CCUSTO_B02I FOR TBFOLPAG_CONTABIL_CCUSTO
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Reg. Incluído.';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_CONTABIL_CCUSTO',NEW.ID,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFOLPAG_CONTABIL_CCUSTO_B01U FOR TBFOLPAG_CONTABIL_CCUSTO
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(OLD.CCUSTO_CONTABILIZACAO <> NEW.CCUSTO_CONTABILIZACAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' C.custo:'||OLD.CCUSTO_CONTABILIZACAO||'->'||NEW.CCUSTO_CONTABILIZACAO;
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_CONTABIL_CCUSTO',NEW.ID,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFOLPAG_CONTABIL_CCUSTO_B01D FOR TBFOLPAG_CONTABIL_CCUSTO
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Reg. Excluído.';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_CONTABIL_CCUSTO',OLD.ID,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFOLPAG_CONTABIL_CONTA_B02I FOR TBFOLPAG_CONTABIL_CONTA
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Reg. Incluído.';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_CONTABIL_CONTA',NEW.ID,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFOLPAG_CONTABIL_CONTA_B01U FOR TBFOLPAG_CONTABIL_CONTA
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(OLD.CONTACONTABIL <> NEW.CONTACONTABIL) THEN
    BEGIN
        HISTORICO = HISTORICO||'C.Contab:'||OLD.CONTACONTABIL||'->'||NEW.CONTACONTABIL;
    END

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

    IF(OLD.HISTORICO <> NEW.HISTORICO) THEN
    BEGIN
       HISTORICO = HISTORICO||'Histórico alterado.';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_CONTABIL_CONTA',NEW.ID,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFOLPAG_CONTABIL_CONTA_B01D FOR TBFOLPAG_CONTABIL_CONTA
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Reg. Excluído.';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_CONTABIL_CONTA',OLD.ID,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFOLPAG_ENCARGO_CONTA_B02I FOR TBFOLPAG_ENCARGO_CONTA
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Reg. Incluído.';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_ENCARGO_CONTA',NEW.ID,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFOLPAG_ENCARGO_CONTA_B01U FOR TBFOLPAG_ENCARGO_CONTA
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(OLD.CONTACONTABIL <> NEW.CONTACONTABIL) THEN
    BEGIN
        HISTORICO = HISTORICO||'C.Contab:'||OLD.CONTACONTABIL||'->'||NEW.CONTACONTABIL;
    END

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

    IF(OLD.HISTORICO <> NEW.HISTORICO) THEN
    BEGIN
       HISTORICO = HISTORICO||'Histórico alterado.';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_ENCARGO_CONTA',NEW.ID,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFOLPAG_ENCARGO_CONTA_B01D FOR TBFOLPAG_ENCARGO_CONTA
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Reg. Excluído.';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_ENCARGO_CONTA',OLD.ID,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFOLPAG_PROVISAO_CONTA_B02I FOR TBFOLPAG_PROVISAO_CONTA
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Reg. Incluído.';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_PROVISAO_CONTA',NEW.ID,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFOLPAG_PROVISAO_CONTA_B01U FOR TBFOLPAG_PROVISAO_CONTA
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(OLD.CONTACONTABIL <> NEW.CONTACONTABIL) THEN
    BEGIN
        HISTORICO = HISTORICO||'C.Contab:'||OLD.CONTACONTABIL||'->'||NEW.CONTACONTABIL;
    END

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

    IF(OLD.HISTORICO <> NEW.HISTORICO) THEN
    BEGIN
       HISTORICO = HISTORICO||'Historico alterado.';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_PROVISAO_CONTA',NEW.ID,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFOLPAG_PROVISAO_CONTA_B01D FOR TBFOLPAG_PROVISAO_CONTA
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Reg. Excluído.';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_PROVISAO_CONTA',OLD.ID,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFOLPAG_CONTABIL_VERBA_B02I FOR TBFOLPAG_CONTABIL_VERBA
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Reg. Incluído.';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_CONTABIL_VERBA',NEW.ID,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFOLPAG_CONTABIL_VERBA_B01U FOR TBFOLPAG_CONTABIL_VERBA
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(OLD.VERBA_CODIGO <> NEW.VERBA_CODIGO) THEN
    BEGIN
        HISTORICO = HISTORICO||'Verba:'||OLD.VERBA_CODIGO||'->'||NEW.VERBA_CODIGO;
    END 

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

    IF(OLD.OPERACAO <> NEW.OPERACAO) THEN
    BEGIN
        HISTORICO = HISTORICO||'Operacao:'||'"'||OLD.OPERACAO||'"'||'->'||'"'||NEW.OPERACAO||'"';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_CONTABIL_VERBA',NEW.ID,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFOLPAG_CONTABIL_VERBA_B01D FOR TBFOLPAG_CONTABIL_VERBA
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Reg. Excluído.';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_CONTABIL_VERBA',OLD.ID,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFOLPAG_CONTABIL_ENCARGO_B02I FOR TBFOLPAG_CONTABIL_ENCARGO
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Reg. Incluído.';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_CONTABIL_ENCARGO',NEW.ID,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFOLPAG_CONTABIL_ENCARGO_B01U FOR TBFOLPAG_CONTABIL_ENCARGO
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(OLD.ENCARGO_CODIGO <> NEW.ENCARGO_CODIGO) THEN
    BEGIN
        HISTORICO = HISTORICO||'Encargo:'||OLD.ENCARGO_CODIGO||'->'||NEW.ENCARGO_CODIGO;
    END

    IF(OLD.OPERACAO <> NEW.OPERACAO) THEN
    BEGIN
        HISTORICO = HISTORICO||'Operacao:'||'"'||OLD.OPERACAO||'"'||'->'||'"'||NEW.OPERACAO||'"';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_CONTABIL_ENCARGO',NEW.ID,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFOLPAG_CONTABIL_ENCARGO_B02D FOR TBFOLPAG_CONTABIL_ENCARGO
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Reg. Excluído.';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_CONTABIL_ENCARGO',OLD.ID,HISTORICO);
    END
END;

CREATE TRIGGER TTBFOLPAG_VERBA_B02D FOR TBFOLPAG_VERBA
ACTIVE BEFORE DELETE POSITION 0
AS
declare variable ID INTEGER;
begin
  /* Trigger text */
  ID = COALESCE((SELECT FIRST 1 A.ID FROM TBFOLPAG_CONTABIL_VERBA A WHERE A.VERBA_CODIGO = OLD.CODIGO),0);
  IF(ID > 0)THEN
  BEGIN
     EXCEPTION "..." 'REGISTRO VINCULADO A FOLHA DE PAGAMENTO OPERAÇÃO CANCELADA!';
  END
end;

CREATE TRIGGER TTBFOLPAG_ENCARGO_B02D FOR TBFOLPAG_ENCARGO
ACTIVE BEFORE DELETE POSITION 0
AS
declare variable ID INTEGER;
begin
  /* Trigger text */
  ID = COALESCE((SELECT FIRST 1 A.ID FROM TBFOLPAG_CONTABIL_ENCARGO A WHERE A.ENCARGO_CODIGO = OLD.CODIGO),0);
  IF(ID > 0)THEN
  BEGIN
     EXCEPTION "..." 'REGISTRO VINCULADO A FOLHA DE PAGAMENTO OPERAÇÃO CANCELADA!';
  END
end;






