CREATE OR ALTER TRIGGER TTBCUSTO_SUBPADRAO_B02I FOR TBCUSTO_SUBPADRAO
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_SUBPADRAO',
              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_SUBPADRAO_B01U FOR TBCUSTO_SUBPADRAO
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.QUANTIDADE <> old.QUANTIDADE)then
   begin
      HISTORICO = HISTORICO||' Qtd: ' ||old.QUANTIDADE||' -> '||new.QUANTIDADE;
   end

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

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

   if (HISTORICO <> '') Then
   Begin
       INSERT INTO TBHISTORICO
              (
              TABELA,
              TABELA_ID,
              USUARIO, IP,
              HISTORICO
              )
       VALUES(
              'TBCUSTO_SUBPADRAO',
              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_SUBPADRAO_B01D FOR TBCUSTO_SUBPADRAO
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_SUBPADRAO',
              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_SUBPADRAO_ITEM_B02I FOR TBCUSTO_SUBPADRAO_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_SUBPADRAO',
              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_SUBPADRAO_ITEM_B01U FOR TBCUSTO_SUBPADRAO_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.'||new.ID||HISTORICO;
   END

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

