----------------------------------------------------------------------------------------------------------
-- TBPATRIMONIO_TIPO

DROP TRIGGER TBPATRIMONIO_TIPO_BI;

SET TERM ^ ;

CREATE OR ALTER trigger ttbpatrimonio_tipo_b01i for tbpatrimonio_tipo
active before insert position 0
as
begin
  if (new.id is null) then
    new.id = gen_id(GTBPATRIMONIO_TIPO,1);
end
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbpatrimonio_tipo_b1i for tbpatrimonio_tipo
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_TIPO',
              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_tipo_b02i for tbpatrimonio_tipo
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_TIPO', NEW.ID, HISTORICO);
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbpatrimonio_tipo_b0u for tbpatrimonio_tipo
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||' 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
       INSERT INTO TBHISTORICO
              (
              TABELA,
              TABELA_ID,
              USUARIO, IP,
              HISTORICO
              )
       VALUES(
              'TBPATRIMONIO_TIPO',
              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_tipo_b01u for tbpatrimonio_tipo
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_TIPO', OLD.ID, HISTORICO);
    END
END
^

SET TERM ; ^

DROP TRIGGER TBPATRIMONIO_TIPO_B1D;

SET TERM ^ ;

CREATE OR ALTER trigger ttbpatrimonio_tipo_b01d for tbpatrimonio_tipo
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.tipo_id = OLD.id),
        (select first 1 c.id from tbpatrimonio_componentes c  where c.tipo_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 ttbpatrimonio_tipo_b0d for tbpatrimonio_tipo
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_TIPO',
              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_tipo_b02d for tbpatrimonio_tipo
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_TIPO', OLD.ID, HISTORICO);
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbpatrimonio_tipo_b03d for tbpatrimonio_tipo
active before delete position 2
AS
BEGIN
  DELETE FROM TBPATRIMONIO_PARAMETRO WHERE TIPO_ID = OLD.ID;        -- Deleta registros vinculados
END
^

SET TERM ; ^

---------------------------------------------------------------------------------------------------------------------------------
-- TBPATRIMONIO_PARAMETRO

DROP TRIGGER TBPATRIMONIO_PARAMETRO_BI;

SET TERM ^ ;

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

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbpatrimonio_parametro_b1i for tbpatrimonio_parametro
inactive before insert position 1
AS
--*****************************
--*                           *
--*       Registra log        *
--*       de  gravação        *
--*    Obs.:Registro filho    *
--*                           *
--*****************************

DECLARE VARIABLE HISTORICO VARCHAR(500);
begin

   HISTORICO = 'Item incluído: ' ||new.id;

   if (HISTORICO <> '') Then
   Begin
       INSERT INTO TBHISTORICO
              (
              TABELA,
              TABELA_ID,
              USUARIO, IP,
              HISTORICO
              )
       VALUES(
              'TBPATRIMONIO_TIPO',
              NEW.tipo_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_parametro_b02i for tbpatrimonio_parametro
active before insert position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);

BEGIN

    HISTORICO = '';

    IF(NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'DADO COMPLEMENTAR:'||FN_LPAD(NEW.ID, 4, '0')||' INCLUIDO';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBPATRIMONIO_TIPO', NEW.TIPO_ID, HISTORICO);
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger tbpatrimonio_parametro_bu0 for tbpatrimonio_parametro
inactive before update position 0
AS
--*****************************
--*                           *
--*       Registra log        *
--*       de alteração        *
--*    Obs.:Registro filho    *
--*                           *
--*****************************

DECLARE VARIABLE HISTORICO VARCHAR(500);
begin

      HISTORICO = '';

   if (OLD.Descricao <> NEW.Descricao) then
       BEGIN
       HISTORICO = HISTORICO||' Desc.'||OLD.descricao||'->'||NEW.descricao;
       END
   if (OLD.sequencia <> NEW.sequencia) then
       BEGIN
       HISTORICO = HISTORICO||' Seq.'||OLD.sequencia||'->'||NEW.sequencia;
       END


   if (HISTORICO <> '') Then
   Begin
   historico = 'Alt.Item: '||Old.Id||' '||Historico;
       INSERT INTO TBHISTORICO
              (
              TABELA,
              TABELA_ID,
              USUARIO, IP,
              HISTORICO
              )
       VALUES(
              'TBPATRIMONIO_TIPO',
              NEW.tipo_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_parametro_b01u for tbpatrimonio_parametro
active before update position 0
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.SEQUENCIA <> NEW.SEQUENCIA)THEN
    BEGIN
       HISTORICO = HISTORICO||' SEQUENCIA:'||OLD.SEQUENCIA||'->'||NEW.SEQUENCIA;
    END

    IF(HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'DADO COMPLEMENTAR:'||FN_LPAD(NEW.ID, 4, '0')||' ALTERADO'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBPATRIMONIO_TIPO', OLD.TIPO_ID, HISTORICO);
    END
END
^

SET TERM ; ^

DROP TRIGGER TTBPATRIMONIO_PARAMETRO_B1D;

SET TERM ^ ;

CREATE OR ALTER trigger ttbpatrimonio_parametro_b01d for tbpatrimonio_parametro
active before delete position 0
As
/*
*********************************************
*                                           *
* 1 - VerIfica se há configurações já       *
*     registradas para o "PARAMETRO" atual. *
*                                           *
*********************************************
*/
Declare Variable Vinculo Integer;
Begin
    For
        select first 1 c.id from tbpatrimonio_configuracao c where c.parametro_id = Old.Id Or c.componente_id = Old.Id
    Into :Vinculo
    Do
    Begin
        If (Vinculo > 0) Then
        Begin
             Exception "..." 'EXCLUSÃO NÃO PERMITIDA. JÁ EXISTEM CONFIGURAÇÕES VINCULADOS A ESTE ITEM'||
            ' . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ';  
        End
    End
End
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbpatrimonio_parametro_b0d for tbpatrimonio_parametro
inactive before delete position 0
AS
--*****************************
--*                           *
--*       Registra log        *
--*       de  exclusão        *
--*    Obs.:Registro filho    *
--*                           *
--*****************************

DECLARE VARIABLE HISTORICO VARCHAR(500);
begin

   HISTORICO = 'Item excluído: ' ||old.id;

   if (HISTORICO <> '') Then
   Begin
       INSERT INTO TBHISTORICO
              (
              TABELA,
              TABELA_ID,
              USUARIO, IP,
              HISTORICO
              )
       VALUES(
              'TBPATRIMONIO_TIPO',
              OLD.tipo_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_parametro_b02d for tbpatrimonio_parametro
active before delete position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);

BEGIN

    HISTORICO = '';

    IF(OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'DADO COMPLEMENTAR:'||FN_LPAD(OLD.ID, 4, '0')||' EXCLUIDO';
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBPATRIMONIO_TIPO', OLD.TIPO_ID, HISTORICO);
    END
END
^

SET TERM ; ^