CREATE TRIGGER TTBCENTRO_NEGOCIO_B02I FOR TBCENTRO_NEGOCIO
ACTIVE BEFORE INSERT POSITION 9
AS
declare variable HISTORICO VARCHAR(500);
BEGIN
   HISTORICO = '';
   IF(NEW.ID IS NOT NULL)then
   BEGIN
       HISTORICO = HISTORICO||'Reg. Incluido.';
   END

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

CREATE TRIGGER TTBCENTRO_NEGOCIO_B01U FOR TBCENTRO_NEGOCIO
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

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

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

CREATE TRIGGER TTBCENTRO_NEGOCIO_B01D FOR TBCENTRO_NEGOCIO
ACTIVE BEFORE DELETE POSITION 9
AS
declare variable HISTORICO VARCHAR(500);
BEGIN
   HISTORICO = '';
   IF(OLD.ID IS NOT NULL)then
   BEGIN
       HISTORICO = HISTORICO||'Reg. Excluido.';
   END

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

CREATE TRIGGER TTBCENTRO_NEGOCIO_FAMILIA_B02I FOR TBCENTRO_NEGOCIO_FAMILIA
ACTIVE BEFORE INSERT POSITION 9
AS
declare variable HISTORICO VARCHAR(500);
BEGIN
   HISTORICO = '';
   IF(NEW.CENTRO_NEGOCIO_ID IS NOT NULL)then
   BEGIN
       HISTORICO = HISTORICO||'Fam.'||NEW.FAMILIA_ID||' Incluida.';
   END

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

CREATE TRIGGER TTBCENTRO_NEGOCIO_FAMILIA_B01D FOR TBCENTRO_NEGOCIO_FAMILIA
ACTIVE BEFORE DELETE POSITION 9
AS
declare variable HISTORICO VARCHAR(500);
BEGIN
   HISTORICO = '';
   IF(OLD.CENTRO_NEGOCIO_ID IS NOT NULL)then
   BEGIN
       HISTORICO = HISTORICO||'Fam.'||OLD.FAMILIA_ID||' Excluida.';
   END

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

CREATE TRIGGER TTBCENTRO_NEGOCIO_CCUSTO_B02I FOR TBCENTRO_NEGOCIO_CCUSTO
ACTIVE BEFORE INSERT POSITION 9
AS
declare variable HISTORICO VARCHAR(500);
BEGIN
   HISTORICO = '';
   IF(NEW.CENTRO_NEGOCIO_ID IS NOT NULL)then
   BEGIN
       HISTORICO = HISTORICO||'C.Custo:'||NEW.CCUSTO_ID||' Incluido.';
   END

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

CREATE TRIGGER TTBCENTRO_NEGOCIO_CCUSTO_B01D FOR TBCENTRO_NEGOCIO_CCUSTO
ACTIVE BEFORE DELETE POSITION 9
AS
declare variable HISTORICO VARCHAR(500);
BEGIN
   HISTORICO = '';
   IF(OLD.CENTRO_NEGOCIO_ID IS NOT NULL)then
   BEGIN
       HISTORICO = HISTORICO||'C.Custo:'||OLD.CCUSTO_ID||' Excluido.';
   END

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

CREATE TRIGGER TTBCENTRO_NEGOCIO_FAT_B02I FOR TBCENTRO_NEGOCIO_FATURAMENTO
ACTIVE BEFORE INSERT POSITION 9
AS
declare variable HISTORICO VARCHAR(500);
BEGIN
   HISTORICO = '';
   IF(NEW.CENTRO_NEGOCIO_ID IS NOT NULL)then
   BEGIN
       HISTORICO = HISTORICO||'Fat.'||NEW.ID||' Incluido.';
   END

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

CREATE TRIGGER TTBCENTRO_NEGOCIO_FAT_B01U FOR TBCENTRO_NEGOCIO_FATURAMENTO
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

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

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

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

CREATE TRIGGER TTBCENTRO_NEGOCIO_FAT_B01D FOR TBCENTRO_NEGOCIO_FATURAMENTO
ACTIVE BEFORE DELETE POSITION 9
AS
declare variable HISTORICO VARCHAR(500);
BEGIN
   HISTORICO = '';
   IF(OLD.CENTRO_NEGOCIO_ID IS NOT NULL)then
   BEGIN
       HISTORICO = HISTORICO||'Fat.'||OLD.ID||' Excluido.';
   END

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


 
 




