CREATE TRIGGER TTBFOLPAG_VERBA_B02I FOR TBFOLPAG_VERBA
ACTIVE BEFORE INSERT POSITION 2
AS
declare variable CODIGO INTEGER;
begin
if(NEW.CODIGO > 0 AND NEW.ID > 0) THEN
    BEGIN
        CODIGO = COALESCE((SELECT FIRST 1 A.CODIGO FROM TBFOLPAG_VERBA A WHERE A.CODIGO = NEW.CODIGO AND A.ID <> NEW.ID),0);
    END
IF(CODIGO > 0)THEN
    BEGIN
       EXCEPTION "..." 'VERBA CODIGO: '||CODIGO||' JÁ CADASTRADA! ';
    END
END;

CREATE TRIGGER TTBFOLPAG_VERBA_B03I FOR TBFOLPAG_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_VERBA',NEW.ID,HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBFOLPAG_VERBA_B01U FOR TBFOLPAG_VERBA
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE INTZOLD VARCHAR(10);
DECLARE VARIABLE INTZNEW VARCHAR(10);
DECLARE VARIABLE CODIGO INTEGER;
BEGIN
    HISTORICO = '';
    IF(OLD.CONTACONTABIL <> NEW.CONTACONTABIL) THEN
    BEGIN
        HISTORICO = HISTORICO||' C.Contabil: '||coalesce((select first 1 T.CONTA||' - '||T.DESCRICAO FROM TBCONTACONTABIL T WHERE T.CONTA = OLD.CONTACONTABIL),'')
        ||' ->'||coalesce((SELECT FIRST 1 T.CONTA||' - '||T.DESCRICAO FROM TBCONTACONTABIL T WHERE T.CONTA = OLD.CONTACONTABIL),'');
    END

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

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

    IF(OLD.NATUREZA <> NEW.NATUREZA)THEN
    BEGIN
        INTZNEW = CASE WHEN NEW.NATUREZA = 'C' THEN 'Credora'
                    WHEN NEW.NATUREZA = 'D' THEN 'Devedora'
                    ELSE ''
                    END;
        INTZOLD = CASE WHEN OLD.NATUREZA = 'C' THEN 'Credora'
                    WHEN OLD.NATUREZA = 'D' THEN 'Devedora'
                    ELSE ''
                    END;

        HISTORICO = HISTORICO||' Ntz: '||INTZOLD||' -> '||INTZNEW;
    END

    IF(OLD.CCUSTO <> NEW.CCUSTO)THEN
    BEGIN
        HISTORICO = HISTORICO||' C.Custo: '||coalesce((select first 1 U.CODIGO||' - '||U.DESCRICAO FROM TBCENTRO_DE_CUSTO U WHERE U.CODIGO = OLD.CCUSTO),'')
        ||' ->'||coalesce((SELECT FIRST 1 U.CODIGO||' - '||U.DESCRICAO FROM TBCENTRO_DE_CUSTO U WHERE U.CODIGO = NEW.CCUSTO),'');
    END
    if(OLD.CODIGO <> NEW.CODIGO) THEN
    BEGIN
        HISTORICO = HISTORICO||' Codigo: '||OLD.CODIGO||' -> '||NEW.CODIGO;
        CODIGO = COALESCE((SELECT FIRST 1 A.CODIGO FROM TBFOLPAG_VERBA A WHERE A.CODIGO = NEW.CODIGO AND A.ID <> NEW.ID),0);
    END
    IF(CODIGO > 0)THEN
        BEGIN
           EXCEPTION "..." 'VERBA CODIGO: '||CODIGO||' JÁ CADASTRADA! ';
        END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFOLPAG_VERBA',NEW.ID,HISTORICO);
    END
END;

CREATE TRIGGER TTBFOLPAG_VERBA_B01D FOR TBFOLPAG_VERBA
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('TBFOLPAG_VERBA',OLD.ID,HISTORICO);
    END
END;

