CREATE OR ALTER trigger ttbusuario_b01u for tbusuario
active before update position 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(1000);
BEGIN

    HISTORICO = '';

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

    IF(OLD.NOME <> NEW.NOME) THEN
    BEGIN
       HISTORICO = HISTORICO||', Nome Alterado';
    END

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

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

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

    IF(OLD.SKYPE <> NEW.SKYPE) THEN
    BEGIN
       HISTORICO = HISTORICO||', Skype Alterado';
    END

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

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

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

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

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

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

    IF(OLD.EMAIL_USERNAME <> NEW.EMAIL_USERNAME) THEN
    BEGIN
       HISTORICO = HISTORICO||', User Alterado';
    END

    IF(OLD.EMAIL_PASS <> NEW.EMAIL_PASS) THEN
    BEGIN
       HISTORICO = HISTORICO||', Password Alterado';
    END

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

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

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

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

    IF(OLD.VENDEDOR_ID IS DISTINCT FROM NEW.VENDEDOR_ID) THEN
    BEGIN
       HISTORICO = HISTORICO||', VENDEDOR: '||COALESCE(OLD.VENDEDOR_ID, 0)||' -> '||COALESCE(NEW.VENDEDOR_ID, 0);
    END

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

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

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

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

    IF (OLD.HABILITA_INCLUIR_CHAMADO <> NEw.HABILITA_INCLUIR_CHAMADO) THEN
    BEGIN
        HISTORICO = HISTORICO||', Inc.Chamdo: '||OLD.HABILITA_INCLUIR_CHAMADO||' -> '||NEW.HABILITA_INCLUIR_CHAMADO;
    END

    IF(HISTORICO <> '') THEN
    BEGIN
       historico = FN_ELIPSES(historico,500);
       EXECUTE PROCEDURE SPI_HISTORICO('TBUSUARIO',NEW.CODIGO, HISTORICO);
    END
END;
