SET TERM ^ ;

CREATE OR ALTER trigger ttbprogramacao_setor_b01u for tbprogramacao_setor
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

  HISTORICO = '';

  IF (OLD.PREVISAO <> NEW.PREVISAO) THEN
       BEGIN
       HISTORICO = HISTORICO||' GP: '||NEW.ESTEIRA_CODIGO||' - '||(SELECT FIRST 1 GP.DESCRICAO FROM TBESTEIRA GP WHERE GP.CODIGO = NEW.ESTEIRA_CODIGO)
       ||' QTD: '||CAST(OLD.PREVISAO AS NUMERIC(10,5))||'->'||CAST(NEW.PREVISAO AS NUMERIC(10,5));
       END

  IF(HISTORICO <> '') THEN
  BEGIN
    HISTORICO = ' REGISTRO ALTERADO: '||HISTORICO;
    EXECUTE PROCEDURE SPI_HISTORICO('TBPROGRAMACAO_SETOR', NEW.ESTEIRA_CODIGO, HISTORICO);
  END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbprogramacao_setor_hora_b01u for tbprogramacao_setor_horario
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

  HISTORICO = '';

  IF (OLD.PREVISAO <> NEW.PREVISAO) THEN
       BEGIN
       HISTORICO = HISTORICO||' GP: '||NEW.GP_ID||' - '||(SELECT FIRST 1 GP.DESCRICAO FROM TBESTEIRA GP WHERE GP.CODIGO = NEW.GP_ID)
       ||' HORÁRIO: '||(SELECT FIRST 1 FORMATTIME(H.HORA_1)||' - '||FORMATTIME(H.HORA_2) FROM TBHORARIO H WHERE H.ID = NEW.HORARIO_ID)
       ||' QTD: '||CAST(OLD.PREVISAO AS NUMERIC(10,5))||'->'||CAST(NEW.PREVISAO AS NUMERIC(10,5));
       END

  IF(HISTORICO <> '') THEN
  BEGIN

    HISTORICO = ' REGISTRO ALTERADO: '||HISTORICO;
    EXECUTE PROCEDURE SPI_HISTORICO('TBPROGRAMACAO_SETOR', NEW.GP_ID, HISTORICO);
  END
END
^

SET TERM ; ^

