CREATE OR ALTER trigger ttbwhatsapp_tarefa_perm_b03i for tbwhatsapp_tarefa_perm
active before insert position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE DESC_USUARIO VARCHAR(100);
DECLARE VARIABLE DESC_PERMISSAO VARCHAR(100);
BEGIN

    HISTORICO = '';
    SELECT TRIM(W.NOME) FROM TBWHATSAPP W WHERE W.ID = NEW.WHATSAPP_ID INTO DESC_USUARIO;
    SELECT TRIM(T.DESCRICAO) FROM TBWHATSAPP_TAREFAS T WHERE T.ID = NEW.WHATSAPP_COMANDOS_ID INTO DESC_PERMISSAO;

    HISTORICO = HISTORICO||' REGISTRO ALTERADO: '||NEW.WHATSAPP_COMANDOS_ID||' - '||DESC_PERMISSAO||', USUARIO INCLUIDO: '||NEW.WHATSAPP_ID||' - '||DESC_USUARIO;

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



CREATE OR ALTER trigger ttbwhatsapp_tarefa_perm_b01d for tbwhatsapp_tarefa_perm
active before delete position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE DESC_USUARIO VARCHAR(100);
DECLARE VARIABLE DESC_PERMISSAO VARCHAR(100);
BEGIN

    HISTORICO = '';
    SELECT TRIM(W.NOME) FROM TBWHATSAPP W WHERE W.ID = OLD.WHATSAPP_ID INTO DESC_USUARIO;
    SELECT TRIM(T.DESCRICAO) FROM TBWHATSAPP_TAREFAS T WHERE T.ID = OLD.WHATSAPP_COMANDOS_ID INTO DESC_PERMISSAO;

    HISTORICO = HISTORICO||' REGISTRO ALTERADO: '||OLD.WHATSAPP_COMANDOS_ID||' - '||DESC_PERMISSAO||', USUARIO EXCLUIDO: '||OLD.WHATSAPP_ID||' - '||DESC_USUARIO;

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

