CREATE TRIGGER TTBLOTE_PECA_B01I FOR TBLOTE_PECA
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBLOTE_PECA,1);
END;

CREATE TRIGGER TTBLOTE_PECA_B02I FOR TBLOTE_PECA
ACTIVE BEFORE INSERT POSITION 9
AS
declare variable historico varchar(500);
BEGIN
  historico = '';
  IF(NEW.ID > 0) then
  BEGIN
    historico = historico||' Reg.Incluido';
  END

  IF(HISTORICO <> '') then
  BEGIN
    execute procedure spi_historico('TBLOTE_PECA',NEW.ID,historico);
  END
END;

CREATE TRIGGER TTBLOTE_PECA_B01U FOR TBLOTE_PECA
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(OLD.DESCRICAO <> NEW.DESCRICAO) THEN
    BEGIN
       HISTORICO = HISTORICO||'Desc:'||OLD.DESCRICAO||'->'||NEW.DESCRICAO;
    END

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

    IF(OLD.OBSERVACAO <> NEW.OBSERVACAO)THEN
    BEGIN
        HISTORICO = HISTORICO||'Observ. alterada.';
    END

    IF(OLD.FAMILIA_ID <> NEW.FAMILIA_ID)THEN
    BEGIN
        HISTORICO = HISTORICO||'Fam:'||OLD.FAMILIA_ID||'->'||NEW.FAMILIA_ID;
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBLOTE_PECA',NEW.ID,FN_ELIPSES(HISTORICO,500));
    END
END;

CREATE TRIGGER TTBLOTE_PECA_B01D FOR TBLOTE_PECA
ACTIVE BEFORE DELETE POSITION 9
AS
declare variable historico varchar(500);
BEGIN
  historico = '';
  IF(OLD.ID > 0) then
  BEGIN
    historico = historico||' Reg.Excluido';
  END

  IF(HISTORICO <> '') then
  BEGIN
    execute procedure spi_historico('TBLOTE_PECA',OLD.ID,historico);
  END
END;

CREATE TRIGGER TTBLOTE_PECA_ITEM_B01I FOR TBLOTE_PECA_ITEM
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBLOTE_PECA_ITEM,1);
END;

CREATE TRIGGER TTBLOTE_PECA_ITEM_B02I FOR TBLOTE_PECA_ITEM
ACTIVE BEFORE INSERT POSITION 9
AS
declare variable historico varchar(500);
BEGIN
  historico = '';
  IF(NEW.LOTE_PECA_ID > 0) then
  BEGIN
    historico = historico||' Item '||NEW.ID||' Incluido';
  END

  IF(HISTORICO <> '') then
  BEGIN
    execute procedure spi_historico('TBLOTE_PECA',NEW.LOTE_PECA_ID,historico);
  END
END;

CREATE TRIGGER TTBLOTE_PECA_ITEM_B01U FOR TBLOTE_PECA_ITEM
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(OLD.PECA_ID <> NEW.PECA_ID) THEN
    BEGIN
       HISTORICO = HISTORICO||'Pc:'||OLD.PECA_ID||'->'||NEW.PECA_ID;
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'Item '||NEW.ID||'Alterado. '||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBLOTE_PECA',NEW.LOTE_PECA_ID,FN_ELIPSES(HISTORICO,500));
    END
END;

CREATE TRIGGER TTBLOTE_PECA_ITEM_B01D FOR TBLOTE_PECA_ITEM
ACTIVE BEFORE DELETE POSITION 9
AS
declare variable historico varchar(500);
BEGIN
  historico = '';
  IF(OLD.LOTE_PECA_ID > 0) then
  BEGIN
    historico = historico||' Item '||OLD.ID||' Excluido';
  END

  IF(HISTORICO <> '') then
  BEGIN
    execute procedure spi_historico('TBLOTE_PECA',OLD.LOTE_PECA_ID,historico);
  END
END;






