CREATE TRIGGER TTBLOTE_B01I FOR TBLOTE
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF(NEW.LOTE IS NULL) THEN
    NEW.LOTE = GEN_ID(GTBLOTE,1);
END;

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

  HISTORICO = '';

  IF(NEW.LOTE > 0) THEN
  BEGIN
    HISTORICO = HISTORICO||'Reg.incluido';
  END

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

CREATE TRIGGER TTBLOTE_B01U FOR TBLOTE
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

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

    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBLOTE',NEW.LOTE,FN_ELIPSES(HISTORICO,500));
    END
END;

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

  HISTORICO = '';

  IF(OLD.LOTE > 0) THEN
  BEGIN
    HISTORICO = HISTORICO||'Reg.excluido';
  END

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

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

  HISTORICO = '';

  IF(NEW.LOTE > 0 AND NEW.COLABORADOR_CODIGO > 0) THEN
  BEGIN
    HISTORICO = HISTORICO||'Colab.'||NEW.COLABORADOR_CODIGO||' Incluido.';
  END

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

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

  HISTORICO = '';

  IF(OLD.LOTE > 0 AND OLD.COLABORADOR_CODIGO > 0) THEN
  BEGIN
    HISTORICO = HISTORICO||'Colab.'||OLD.COLABORADOR_CODIGO||' Excluido.';
  END

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





