

CREATE OR ALTER trigger tbwhatsapp_b02i for tbwhatsapp
active before insert position 2
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.id > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Registro Incluido';
    END

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


CREATE OR ALTER trigger tbwhatsapp_b01u for tbwhatsapp
active before update position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

    IF ( OLD.NOME <> NEW.NOME) THEN
    BEGIN
        HISTORICO = HISTORICO||'NOME:'|| OLD.NOME||'->'||NEW.NOME;
    END
    
    IF ( OLD.MENU <> NEW.MENU) THEN
    BEGIN
        HISTORICO = HISTORICO||', MENU:'|| OLD.MENU||'->'||NEW.NUMERO;
    END

    IF ( OLD.FUNCAO <> NEW.FUNCAO) THEN
    BEGIN
        HISTORICO = HISTORICO||', FUNCAO:'|| OLD.FUNCAO||'->'||NEW.FUNCAO;
    END
    
    IF ( OLD.NIVEL <> NEW.NIVEL) THEN
    BEGIN
        HISTORICO = HISTORICO||', NIVEL:'|| OLD.NIVEL||'->'||NEW.NIVEL;
    END

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

    IF ( OLD.SUPERVISOR_ID <> NEW.SUPERVISOR_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||', SUPERVISOR:'|| OLD.SUPERVISOR_ID||'->'||NEW.SUPERVISOR_ID;
    END

    IF ( OLD.REPRESENTANTE_ID <> NEW.REPRESENTANTE_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||', REPRESENTANTE:'|| OLD.REPRESENTANTE_ID||'->'||NEW.REPRESENTANTE_ID;
    END

    IF ( OLD.WHATSAPP_COMANDOS_ID <> NEW.WHATSAPP_COMANDOS_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||', WHATSAPP COMANDOS:'|| OLD.WHATSAPP_COMANDOS_ID||'->'||NEW.WHATSAPP_COMANDOS_ID;
    END

    IF ( OLD.OBSERVACAO <> NEW.OBSERVACAO) THEN
    BEGIN
        HISTORICO = HISTORICO||', OBSERVAÇÃO:'|| OLD.OBSERVACAO||'->'||NEW.OBSERVACAO;
    END

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


CREATE OR ALTER trigger tbwhatsapp_b01d for tbwhatsapp
active before delete position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.id > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Registro Excluido';
    END

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



CREATE trigger tbwhatsapp_permissoes_b02i for tbwhatsapp_permissoes
active before insert position 2
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

        HISTORICO = HISTORICO||'PERMISSAO('||NEW.ID||'):'|| NEW.STATUS;

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


CREATE trigger tbwhatsapp_permissoes_b01u for tbwhatsapp_permissoes
active after update position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.STATUS <> NEW.STATUS) THEN
    BEGIN
        HISTORICO = HISTORICO||'PERMISSAO('||NEW.ID||'):'|| OLD.STATUS||'->'||NEW.STATUS;
    END
    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBWHATSAPP',NEW.whatsapp_id, HISTORICO);
    END
end;






