---------------------------------------------------------------------------------------------------------------------------------
-- TBPATRIMONIO_MODELO

DROP TRIGGER TBPATRIMONIO_MODELO_BI;

SET TERM ^ ;

CREATE OR ALTER trigger ttbpatrimonio_modelo_b01i for tbpatrimonio_modelo
active before insert position 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBPATRIMONIO_MODELO,1);
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbpatrimonio_modelo_b1i for tbpatrimonio_modelo
inactive before insert position 1
AS
--*****************************
--*                           *
--*       Registra log        *
--*       de  gravação        *
--*                           *
--*****************************

DECLARE VARIABLE HISTORICO VARCHAR(500);
begin

   HISTORICO = 'Reg. incluído.';

   if (HISTORICO <> '') Then
   Begin
       INSERT INTO TBHISTORICO
              (
              TABELA,
              TABELA_ID,
              USUARIO, IP,
              HISTORICO
              )
       VALUES(
              'TBPATRIMONIO_MODELO',
              NEW.ID,
              (Select First 1 MON$USER from mon$attachments where mon$attachment_id = current_connection),
              (Select First 1 MON$REMOTE_ADDRESS from mon$attachments where mon$attachment_id = current_connection),
              :Historico
              );
   End
end
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbpatrimonio_modelo_b02i for tbpatrimonio_modelo
active before insert position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'REGISTRO INCLUIDO';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBPATRIMONIO_MODELO', NEW.ID, HISTORICO);
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger tbpatrimonio_modelo_bu0 for tbpatrimonio_modelo
inactive before update position 0
AS
--*****************************
--*                           *
--*       Registra log        *
--*       de alteração        *
--*                           *
--*****************************

DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

      HISTORICO = '';

   IF (OLD.descricao <> NEW.descricao) THEN
       BEGIN
       historico = historico||' Descrição: '||OLD.descricao||'->'||NEW.descricao;
       END
   IF (OLD.status <> NEW.status) THEN
       BEGIN
       historico = historico||' Status.: '||OLD.status||'->'||NEW.status;
       END


   IF (HISTORICO <> '') THEN
   BEGIN
       INSERT INTO TBHISTORICO
              (
              TABELA,
              TABELA_ID,
              USUARIO, IP,
              HISTORICO
              )
       VALUES(
              'TBPATRIMONIO_MODELO',
              NEW.ID,
              (SELECT FIRST 1 MON$USER FROM mon$attachments WHERE mon$attachment_id = CURRENT_CONNECTION),
              (SELECT FIRST 1 MON$REMOTE_ADDRESS FROM mon$attachments WHERE mon$attachment_id = CURRENT_CONNECTION),
              :Historico
              );
   END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbpatrimonio_modelo_b01u for tbpatrimonio_modelo
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(2000);
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 (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'REGISTRO ALTERADO:'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBPATRIMONIO_MODELO', OLD.ID, HISTORICO);
    END
END
^

SET TERM ; ^

DROP TRIGGER TTBPATRIMONIO_MODELO_B1D;

SET TERM ^ ;

CREATE OR ALTER trigger ttbpatrimonio_modelo_b01d for tbpatrimonio_modelo
active before delete position 0
As
/*
*************************************
*                                   *
* 1 - VerIfica se há vínculos com   *
*     patrimonio ou componentes     *
*                                   *
*************************************
*/
Declare Variable Vinculo1 Integer;
Declare Variable Vinculo2 Integer;
Begin
    For
        select
        (select first 1 p.id from tbpatrimonio p  where p.modelo_id = OLD.id),
        (select first 1 c.id from tbpatrimonio_componentes c  where c.modelo_id = OLD.id)
        from
        RDB$DATABASE
    Into :Vinculo1, :Vinculo2
    Do
    Begin
        If ((Vinculo1 > 0) or (vinculo2 > 0))  Then
        Begin
             Exception "..." 'EXCLUSÃO NÃO PERMITIDA. JÁ EXISTEM REGISTROS VINCULADOS A ESTE ITEM'||
            ' . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ';  
        End
    End
End
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger tbpatrimonio_modelo_bd0 for tbpatrimonio_modelo
inactive before delete position 0
AS
--*****************************
--*                           *
--*       Registra log        *
--*       de  exclusão        *
--*                           *
--*****************************

DECLARE VARIABLE HISTORICO VARCHAR(500);
begin

   HISTORICO = 'Reg. excluído.';

   if (HISTORICO <> '') Then
   Begin
       INSERT INTO TBHISTORICO
              (
              TABELA,
              TABELA_ID,
              USUARIO, IP,
              HISTORICO
              )
       VALUES(
              'TBPATRIMONIO_MODELO',
              OLD.ID,
              (Select First 1 MON$USER from mon$attachments where mon$attachment_id = current_connection),
              (Select First 1 MON$REMOTE_ADDRESS from mon$attachments where mon$attachment_id = current_connection),
              :Historico
              );
   End
end
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbpatrimonio_modelo_b02d for tbpatrimonio_modelo
active before delete position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF(OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'REGISTRO EXCLUIDO';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBPATRIMONIO_MODELO', OLD.ID, HISTORICO);
    END
END
^

SET TERM ; ^