CREATE OR ALTER trigger ttbgp_etapa_pmp_b02i for tbgp_etapa_pmp
active before insert position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' ETAPA DO PLANO MESTRE INCLUIDA: '||NEW.ID||' - '||NEW.DESCRICAO;
    END

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


CREATE OR ALTER trigger ttbgp_etapa_pmp_b01u for tbgp_etapa_pmp
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

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

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

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = ' REG.ALTERADO: ETAPA: '||NEW.ID||' ALTERADA: '||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBESTEIRA',NEW.GP_ID, HISTORICO);
    END
END;



CREATE trigger ttbgp_etapa_pmp_b01d for tbgp_etapa_pmp
active before delete position 1
AS
BEGIN
   IF(OLD.ID > 0) THEN
   BEGIN
      DELETE FROM TBGP_ETAPA_PMP_COLUNA T WHERE T.GP_ETAPA_PMP_ID = OLD.ID;
   END
END;


CREATE trigger ttbgp_etapa_pmp_b02d for tbgp_etapa_pmp
active before delete position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' ETAPA DO PLANO MESTRE EXCLUIDA: '||OLD.ID||' - '||OLD.DESCRICAO;
    END

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





