CREATE OR ALTER trigger ttbperfil_modelo_b02i for tbperfil_modelo
active before insert position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' MODELO INCLUIDO: '||NEW.MODELO_ID||' - '||(SELECT FIRST 1 M.DESCRICAO FROM TBMODELO M WHERE M.CODIGO = NEW.MODELO_ID);
    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 = 'MOD'), HISTORICO);
    END
END;


CREATE trigger ttbperfil_modelo_b01d for tbperfil_modelo
active before delete position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' MODELO EXCLUIDO: '||OLD.MODELO_ID||' - '||(SELECT FIRST 1 M.DESCRICAO FROM TBMODELO M WHERE M.CODIGO = OLD.MODELO_ID);
    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 = 'MOD'), HISTORICO);
    END
END;


