SET TERM ^ ;

CREATE trigger ttbusuario_menus_app_b02i for tbusuario_menus_app
active before insert position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE CONTROLE INTEGER;
DECLARE VARIABLE MENU_DESCRICAO VARCHAR(200);

BEGIN

    HISTORICO = '';

    CONTROLE = COALESCE((SELECT M.CONTROLE FROM TBMENU M WHERE M.ID = NEW.MENU_ID), '');
    MENU_DESCRICAO = COALESCE((SELECT M.DESCRICAO FROM TBMENU M WHERE M.ID = NEW.MENU_ID), '');

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'MENU APP INCLUIDO:'||CONTROLE||' - '||MENU_DESCRICAO;
    END

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

SET TERM ; ^

SET TERM ^ ;

CREATE trigger ttbusuario_menus_app_b01d for tbusuario_menus_app
active before delete position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE CONTROLE INTEGER;
DECLARE VARIABLE MENU_DESCRICAO VARCHAR(200);

BEGIN

    HISTORICO = '';

    CONTROLE = COALESCE((SELECT M.CONTROLE FROM TBMENU M WHERE M.ID = OLD.MENU_ID), '');
    MENU_DESCRICAO = COALESCE((SELECT M.DESCRICAO FROM TBMENU M WHERE M.ID = OLD.MENU_ID), '');

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'MENU APP EXCLUIDO:'||CONTROLE||' - '||MENU_DESCRICAO;
    END

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

SET TERM ; ^

