CREATE OR ALTER TRIGGER TTBCUSTO_PADRAO_B02I FOR TBCUSTO_PADRAO
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
begin

   HISTORICO = 'Reg.Incluido.';

   if (HISTORICO <> '') Then
   Begin
       INSERT INTO TBHISTORICO
              (
              TABELA,
              TABELA_ID,
              USUARIO, IP,
              HISTORICO
              )
       VALUES(
              'TBCUSTO_PADRAO',
              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;

CREATE OR ALTER TRIGGER TTBCUSTO_PADRAO_B01U FOR TBCUSTO_PADRAO
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
begin

   HISTORICO = '';

   if(new.descricao <> old.descricao)then
   begin
      HISTORICO = 'Desc: ' ||old.descricao||' -> '||new.descricao;
   end

   if(new.perc_incentivo <> old.perc_incentivo)then
   begin
      HISTORICO = HISTORICO||' Perc.Incentivo: '||old.perc_incentivo||' -> '||new.perc_incentivo;
   end

   if(new.incentivo <> old.incentivo)then
   begin
      HISTORICO = HISTORICO||' Hab.Incentivo: '||old.incentivo||' -> '||new.incentivo;
   end

   if (HISTORICO <> '') Then
   Begin
       INSERT INTO TBHISTORICO
              (
              TABELA,
              TABELA_ID,
              USUARIO, IP,
              HISTORICO
              )
       VALUES(
              'TBCUSTO_PADRAO',
              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;

CREATE OR ALTER TRIGGER TTBCUSTO_PADRAO_B01D FOR TBCUSTO_PADRAO
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
begin

   HISTORICO = 'Reg.Excluido.';

   if (HISTORICO <> '') Then
   Begin
       INSERT INTO TBHISTORICO
              (
              TABELA,
              TABELA_ID,
              USUARIO, IP,
              HISTORICO
              )
       VALUES(
              'TBCUSTO_PADRAO',
              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;

CREATE OR ALTER TRIGGER TTBCUSTO_PADRAO_ITEM_B01U FOR TBCUSTO_PADRAO_ITEM
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
begin

   HISTORICO = '';

   if(new.descricao <> old.descricao)then
   begin
      HISTORICO = HISTORICO||' Desc: '||old.descricao ||' -> '||new.descricao;
   end

   if(new.percentual <> old.percentual)then
   begin
      HISTORICO = HISTORICO||' Perc: '||old.percentual||' -> '||new.percentual;
   end

   if(new.fator <> old.fator)then
   begin
      HISTORICO = HISTORICO||' Fator: '||old.fator ||' -> '||new.fator;
   end

   if(new.avos <> old.avos)then
   begin
      HISTORICO = HISTORICO||' Avos: '||old.avos||' -> '||new.avos;
   end

   if(new.usar_fator <> old.usar_fator)then
   begin
      HISTORICO = HISTORICO||' Usar Fator: '||old.usar_fator||' -> '||new.usar_fator;
   end

   if(new.editavel <> old.editavel)then
   begin
      HISTORICO = HISTORICO||' Editavel: '||old.editavel||' -> '||new.editavel;
   end

   if(new.incentivo <> old.incentivo)then
   begin
      HISTORICO = HISTORICO||' Incentivo: '||old.incentivo||' -> '||new.incentivo;
   end

   if(new.frete <> old.frete)then
   begin
      HISTORICO = HISTORICO||' Frete: '||old.frete||' -> '|| new.frete;
   end


   if (HISTORICO <> '') Then
   Begin
       HISTORICO = 'Param.'||old.id||HISTORICO;
       INSERT INTO TBHISTORICO
              (
              TABELA,
              TABELA_ID,
              USUARIO, IP,
              HISTORICO
              )
       VALUES(
              'TBCUSTO_PADRAO',
              new.padrao_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;

CREATE OR ALTER TRIGGER TTBCUSTO_PADRAO_ITEM_B02I FOR TBCUSTO_PADRAO_ITEM
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
begin

   HISTORICO = 'Param.'||new.id||' Incluido.';

   if (HISTORICO <> '') Then
   Begin
       INSERT INTO TBHISTORICO
              (
              TABELA,
              TABELA_ID,
              USUARIO, IP,
              HISTORICO
              )
       VALUES(
              'TBCUSTO_PADRAO',
              new.padrao_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;

CREATE OR ALTER TRIGGER TTBCUSTO_PADRAO_ITEM_B01D FOR TBCUSTO_PADRAO_ITEM
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
begin

   HISTORICO = 'Param. '||old.id||' Excluido.';

   if (HISTORICO <> '') Then
   Begin
       INSERT INTO TBHISTORICO
              (
              TABELA,
              TABELA_ID,
              USUARIO, IP,
              HISTORICO
              )
       VALUES(
              'TBCUSTO_PADRAO',
              old.padrao_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;



