CREATE TRIGGER TTBUSUARIO_CCUSTO_B01I FOR TBUSUARIO_CCUSTO
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(NEW.CCUSTO is not null) THEN
    BEGIN
        HISTORICO = HISTORICO||'CCUSTO: '||NEW.CCUSTO||' Incluido';
    END

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

CREATE TRIGGER TTBUSUARIO_CCUSTO_B01D FOR TBUSUARIO_CCUSTO
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(OLD.CCUSTO is not null) THEN
    BEGIN
        HISTORICO = HISTORICO||'CCUSTO: '||OLD.CCUSTO||' Excluido';
    END

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

CREATE TRIGGER TTBUSUARIO_FAMILIA_B01I FOR TBUSUARIO_FAMILIA
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(NEW.FAMILIA_ID is not null) THEN
    BEGIN
        HISTORICO = HISTORICO||'Familia: '||NEW.FAMILIA_ID||' Incluida';
    END

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

CREATE TRIGGER TTBUSUARIO_FAMILIA_B01D FOR TBUSUARIO_FAMILIA
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(OLD.FAMILIA_ID is not null) THEN
    BEGIN
        HISTORICO = HISTORICO||'Familia: '||OLD.FAMILIA_ID||' Excluida';
    END

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

CREATE TRIGGER TTBCONSUMO_REQ_PERMISSAO_B02I FOR TBCONSUMO_REQ_PERMISSAO
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(NEW.USUARIO_ID is not null) THEN
    BEGIN
        HISTORICO = HISTORICO||'Requisição Família: '||NEW.FAMILIA||', CCusto: '||NEW.CCUSTO_REQUISICAO||' Incluida.';
    END

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

CREATE TRIGGER TTBCONSUMO_REQ_PERMISSAO_B02U FOR TBCONSUMO_REQ_PERMISSAO
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

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

    IF(HISTORICO <> '') THEN
    BEGIN
       HISTORICO = 'Requisição Família: '||NEW.FAMILIA||', CCusto: '||NEW.CCUSTO_REQUISICAO||' '||HISTORICO;
       EXECUTE PROCEDURE SPI_HISTORICO('TBUSUARIO',NEW.USUARIO_ID, HISTORICO);
    END
END;

CREATE TRIGGER TTBCONSUMO_REQ_PERMISSAO_B02D FOR TBCONSUMO_REQ_PERMISSAO
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(OLD.USUARIO_ID is not null) THEN
    BEGIN
       HISTORICO = HISTORICO||'Requisição Família: '||OLD.FAMILIA||', CCusto: '||OLD.CCUSTO_REQUISICAO||' Excluida.';
    END

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

CREATE OR ALTER TRIGGER TTBUSUARIO_PERFIL_DETALHE_B01I FOR TBUSUARIO_PERFIL_DETALHE
ACTIVE BEFORE INSERT POSITION 2
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE HISTORICO2 VARCHAR(500);
BEGIN
    HISTORICO = '';
    HISTORICO2 = '';

    IF(new.perfil_id > 0) THEN
    BEGIN
        HISTORICO  = HISTORICO||' Usuário vinculado: '||coalesce((select u.usuario from tbusuario u where u.codigo = new.usuario_id),'');
        HISTORICO2 = HISTORICO2||' Perfil: '||new.PERFIL_ID||' Incluido';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBUSUARIO_PERFIL',new.perfil_id, HISTORICO);
    END
    
    IF (HISTORICO2 <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBUSUARIO',new.USUARIO_ID, HISTORICO2);
    END

end;

CREATE OR ALTER TRIGGER TBUSUARIO_PERFIL_DETALHE_BD0 FOR TBUSUARIO_PERFIL_DETALHE
ACTIVE BEFORE DELETE POSITION 2
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE HISTORICO2 VARCHAR(500);
BEGIN
    HISTORICO2 = '';

    IF(old.perfil_id > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Usuário removido: '||coalesce((select u.usuario from tbusuario u where u.codigo = old.usuario_id),'');
        HISTORICO2 = HISTORICO2||' Perfil: '||old.PERFIL_ID||' Excluido';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBUSUARIO_PERFIL',old.perfil_id, HISTORICO);
    END
	
    IF (HISTORICO2 <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBUSUARIO',old.USUARIO_ID, HISTORICO2);
    END
end;

CREATE TRIGGER TTBRELATORIO_WEB_USUARIO_B02I FOR TBRELATORIO_WEB_USUARIO
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(new.USUARIO_ID is not null) THEN
    BEGIN
       HISTORICO = HISTORICO||'Relatorio: '||new.RELATORIO_ID||' Incluido.';
    END

    IF(HISTORICO <> '') THEN
    BEGIN
       EXECUTE PROCEDURE SPI_HISTORICO('TBUSUARIO',new.USUARIO_ID, HISTORICO);
    END
END;

CREATE TRIGGER TTBRELATORIO_WEB_USUARIO_B02D FOR TBRELATORIO_WEB_USUARIO
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(OLD.USUARIO_ID is not null) THEN
    BEGIN
       HISTORICO = HISTORICO||'Relatorio: '||old.RELATORIO_ID||' Excluido.';
    END

    IF(HISTORICO <> '') THEN
    BEGIN
       EXECUTE PROCEDURE SPI_HISTORICO('TBUSUARIO',old.USUARIO_ID, HISTORICO);
    END
END;

CREATE TRIGGER TTBMENU_DETALHE_B02I FOR TBMENU_DETALHE
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(new.USUARIO_ID is not null) THEN
    BEGIN
       HISTORICO = HISTORICO||'Menu: '||new.MENU_ID||' Incluido.';
    END

    IF(HISTORICO <> '') THEN
    BEGIN
       EXECUTE PROCEDURE SPI_HISTORICO('TBUSUARIO',new.USUARIO_ID, HISTORICO);
    END
END;

CREATE TRIGGER TTBMENU_DETALHE_B01D FOR TBMENU_DETALHE
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(OLD.USUARIO_ID is not null) THEN
    BEGIN
       HISTORICO = HISTORICO||'Menu: '||OLD.MENU_ID||' Excluido.';
    END

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

CREATE TRIGGER TTBMENU_DETALHE_B01U FOR TBMENU_DETALHE
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

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

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

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

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

    IF(HISTORICO <> '') THEN
    BEGIN
       HISTORICO = 'Menu: '||NEW.MENU_ID||' '||HISTORICO;
       EXECUTE PROCEDURE SPI_HISTORICO('TBUSUARIO',NEW.USUARIO_ID, HISTORICO);
    END
END;

CREATE TRIGGER TTBUSUARIO_BI_B02I FOR TBUSUARIO_BI
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(new.USUARIO_ID is not null) THEN
    BEGIN
       HISTORICO = HISTORICO||'Painel: '||new.ID||' Incluido.';
    END

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

CREATE TRIGGER TTBUSUARIO_BI_B01D FOR TBUSUARIO_BI
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(OLD.USUARIO_ID is not null) THEN
    BEGIN
       HISTORICO = HISTORICO||'Painel: '||OLD.ID||' Excluido.';
    END

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

CREATE 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.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(HISTORICO <> '') THEN
    BEGIN
       historico = FN_ELIPSES(historico,500);
       EXECUTE PROCEDURE SPI_HISTORICO('TBUSUARIO',NEW.CODIGO, HISTORICO);
    END
END;

CREATE TRIGGER TTBUSUARIO_ESTABELECIMENTO_B01I FOR TBUSUARIO_ESTABELECIMENTO
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(NEW.USUARIO_CODIGO is not null) THEN
    BEGIN
       HISTORICO = HISTORICO||'Estab: '||NEW.ESTABELECIMENTO_CODIGO||' Incluido.';
    END

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

CREATE OR ALTER TRIGGER TTBUSUARIO_ESTABELECIMENTO_B01D FOR TBUSUARIO_ESTABELECIMENTO
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(OLD.USUARIO_CODIGO is not null) THEN
    BEGIN
       HISTORICO = HISTORICO||'Estab: '||OLD.ESTABELECIMENTO_CODIGO||' Excluido.';
    END

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

CREATE OR ALTER TRIGGER TTBUSUARIO_CCUSTO_B01I FOR TBUSUARIO_CCUSTO
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(NEW.CCUSTO is not null) THEN
    BEGIN
        HISTORICO = HISTORICO||'CCUSTO: '||NEW.CCUSTO||' Incluido';
    END

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

CREATE OR ALTER TRIGGER TTBUSUARIO_CCUSTO_B01D FOR TBUSUARIO_CCUSTO
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(OLD.CCUSTO is not null) THEN
    BEGIN
        HISTORICO = HISTORICO||'CCUSTO: '||OLD.CCUSTO||' Excluido';
    END

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

CREATE OR ALTER TRIGGER TTBUSUARIO_ESTABELECIMENTO_B01I FOR TBUSUARIO_ESTABELECIMENTO
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(NEW.USUARIO_CODIGO is not null) THEN
    BEGIN
       HISTORICO = HISTORICO||'Estab: '||NEW.ESTABELECIMENTO_CODIGO||' Incluido.';
    END

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

CREATE OR ALTER TRIGGER TTBUSUARIO_ESTABELECIMENTO_B01D FOR TBUSUARIO_ESTABELECIMENTO
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(OLD.USUARIO_CODIGO is not null) THEN
    BEGIN
       HISTORICO = HISTORICO||'Estab: '||OLD.ESTABELECIMENTO_CODIGO||' Excluido.';
    END

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

CREATE OR ALTER TRIGGER TTBUSUARIO_FAMILIA_B01I FOR TBUSUARIO_FAMILIA
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(NEW.FAMILIA_ID is not null) THEN
    BEGIN
        HISTORICO = HISTORICO||'Familia: '||NEW.FAMILIA_ID||' Incluida';
    END

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

CREATE OR ALTER TRIGGER TTBUSUARIO_FAMILIA_B01D FOR TBUSUARIO_FAMILIA
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(OLD.FAMILIA_ID is not null) THEN
    BEGIN
        HISTORICO = HISTORICO||'Familia: '||OLD.FAMILIA_ID||' Excluida';
    END

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

CREATE OR ALTER TRIGGER TTBUSUARIO_PERFIL_DETALHE_B01I FOR TBUSUARIO_PERFIL_DETALHE
ACTIVE BEFORE INSERT POSITION 2
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE HISTORICO2 VARCHAR(500);
BEGIN
    HISTORICO = '';
    HISTORICO2 = '';

    IF(new.perfil_id > 0) THEN
    BEGIN
        HISTORICO  = HISTORICO||' Usuário vinculado: '||coalesce((select u.usuario from tbusuario u where u.codigo = new.usuario_id),'');
        HISTORICO2 = HISTORICO2||' Perfil: '||new.PERFIL_ID||' Incluido';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBUSUARIO_PERFIL',new.perfil_id, HISTORICO);
    END
    
    IF (HISTORICO2 <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBUSUARIO',new.USUARIO_ID, HISTORICO2);
    END

end;

CREATE OR ALTER TRIGGER TBUSUARIO_PERFIL_DETALHE_BD0 FOR TBUSUARIO_PERFIL_DETALHE
ACTIVE BEFORE DELETE POSITION 2
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE HISTORICO2 VARCHAR(500);
BEGIN
    HISTORICO2 = '';

    IF(old.perfil_id > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' Usuário removido: '||coalesce((select u.usuario from tbusuario u where u.codigo = old.usuario_id),'');
        HISTORICO2 = HISTORICO2||' Perfil: '||old.PERFIL_ID||' Excluido';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBUSUARIO_PERFIL',old.perfil_id, HISTORICO);
    END 
    IF (HISTORICO2 <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBUSUARIO',old.USUARIO_ID, HISTORICO2);
    END

end;

CREATE OR ALTER TRIGGER TTBRELATORIO_WEB_USUARIO_B02I FOR TBRELATORIO_WEB_USUARIO
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(new.USUARIO_ID is not null) THEN
    BEGIN
       HISTORICO = HISTORICO||'Relatorio: '||new.RELATORIO_ID||' Incluido.';
    END

    IF(HISTORICO <> '') THEN
    BEGIN
       EXECUTE PROCEDURE SPI_HISTORICO('TBUSUARIO',new.USUARIO_ID, HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBRELATORIO_WEB_USUARIO_B02D FOR TBRELATORIO_WEB_USUARIO
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(OLD.USUARIO_ID is not null) THEN
    BEGIN
       HISTORICO = HISTORICO||'Relatorio: '||old.RELATORIO_ID||' Excluido.';
    END

    IF(HISTORICO <> '') THEN
    BEGIN
       EXECUTE PROCEDURE SPI_HISTORICO('TBUSUARIO',old.USUARIO_ID, HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBCONSUMO_REQ_PERMISSAO_B02I FOR TBCONSUMO_REQ_PERMISSAO
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(NEW.USUARIO_ID is not null) THEN
    BEGIN
        HISTORICO = HISTORICO||'Requisição Família: '||NEW.FAMILIA||', CCusto: '||NEW.CCUSTO_REQUISICAO||' Incluida.';
    END

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

CREATE OR ALTER TRIGGER TTBCONSUMO_REQ_PERMISSAO_B02U FOR TBCONSUMO_REQ_PERMISSAO
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

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

    IF(HISTORICO <> '') THEN
    BEGIN
       HISTORICO = 'Requisição Família: '||NEW.FAMILIA||', CCusto: '||NEW.CCUSTO_REQUISICAO||' '||HISTORICO;
       EXECUTE PROCEDURE SPI_HISTORICO('TBUSUARIO',NEW.USUARIO_ID, HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBCONSUMO_REQ_PERMISSAO_B02D FOR TBCONSUMO_REQ_PERMISSAO
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(OLD.USUARIO_ID is not null) THEN
    BEGIN
       HISTORICO = HISTORICO||'Requisição Família: '||OLD.FAMILIA||', CCusto: '||OLD.CCUSTO_REQUISICAO||' Excluida.';
    END

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

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.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(HISTORICO <> '') THEN
    BEGIN
       historico = FN_ELIPSES(historico,500);
       EXECUTE PROCEDURE SPI_HISTORICO('TBUSUARIO',NEW.CODIGO, HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBMENU_DETALHE_B02I FOR TBMENU_DETALHE
ACTIVE BEFORE INSERT POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(new.USUARIO_ID is not null) THEN
    BEGIN
       HISTORICO = HISTORICO||'Menu: '||new.MENU_ID||' Incluido.';
    END

    IF(HISTORICO <> '') THEN
    BEGIN
       EXECUTE PROCEDURE SPI_HISTORICO('TBUSUARIO',new.USUARIO_ID, HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBMENU_DETALHE_B01U FOR TBMENU_DETALHE
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

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

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

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

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

    IF(HISTORICO <> '') THEN
    BEGIN
       HISTORICO = 'Menu: '||NEW.MENU_ID||', Alterado:'||HISTORICO;
       EXECUTE PROCEDURE SPI_HISTORICO('TBUSUARIO',NEW.USUARIO_ID, HISTORICO);
    END
END;

CREATE OR ALTER TRIGGER TTBMENU_DETALHE_B01D FOR TBMENU_DETALHE
ACTIVE BEFORE DELETE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(OLD.USUARIO_ID is not null) THEN
    BEGIN
       HISTORICO = HISTORICO||'Menu: '||OLD.MENU_ID||' Excluido.';
    END

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



















