CREATE TRIGGER TTBLICITACAO_B0I_02 FOR TBLICITACAO
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

   HISTORICO = 'ITEM INSERIDO DE ID:'||NEW.ID;

   IF (HISTORICO <> '') THEN
       BEGIN
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
      'TBLICITACAO', 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 TRIGGER TTBLICITACAO_A0D_01 FOR TBLICITACAO
ACTIVE AFTER DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

   HISTORICO = 'ITEM EXCLUÍDO DE ID: '||OLD.ID;

   IF (HISTORICO <> '') THEN
       BEGIN
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
      'TBLICITACAO', 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 TRIGGER TTBLICITACAO_B0U_01 FOR TBLICITACAO
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.DATA_VALIDADE <> NEW.DATA_VALIDADE) THEN
       BEGIN
       HISTORICO = HISTORICO||' VAL.:'||OLD.DATA_VALIDADE||'->'||NEW.DATA_VALIDADE;
       END
   IF (OLD.OBSERVACAO <> NEW.OBSERVACAO) THEN
       BEGIN
       HISTORICO = HISTORICO||' OBS.:'||OLD.OBSERVACAO||'->'||NEW.OBSERVACAO;
       END

   IF (HISTORICO <> '') THEN
       BEGIN
       INSERT INTO TBHISTORICO
          (TABELA, TABELA_ID, USUARIO, IP, HISTORICO)
       VALUES
          ('TBLICITACAO', 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 TRIGGER TTBORCAMENTO_B0U_01 FOR TBORCAMENTO
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

   HISTORICO = '';

   IF (OLD.LICITACAO_ID <> NEW.LICITACAO_ID) THEN
       BEGIN
       HISTORICO = HISTORICO||' LIC.:'||OLD.LICITACAO_ID||'->'||NEW.LICITACAO_ID;
       END
   IF (OLD.EMPRESA_ID <> NEW.EMPRESA_ID) THEN
       BEGIN
       HISTORICO = HISTORICO||' EMP.:'||OLD.EMPRESA_ID||'->'||NEW.EMPRESA_ID;
       END
   IF (OLD.FRETE <> NEW.FRETE) THEN
       BEGIN
       HISTORICO = HISTORICO||' FRETE.:'||OLD.FRETE||'->'||NEW.FRETE;
       END
   IF (OLD.PAGAMENTO_FORMA <> NEW.PAGAMENTO_FORMA) THEN
       BEGIN
       HISTORICO = HISTORICO||' FORMA PAG.:'||OLD.PAGAMENTO_FORMA||'->'||NEW.PAGAMENTO_FORMA;
       END
   IF (OLD.PAGAMENTO_CONDICAO <> NEW.PAGAMENTO_CONDICAO) THEN
       BEGIN
       HISTORICO = HISTORICO||' COND. PAG.:'||OLD.PAGAMENTO_CONDICAO||'->'||NEW.PAGAMENTO_CONDICAO;
       END
   IF (OLD.VALIDADE_PROPOSTA <> NEW.VALIDADE_PROPOSTA) THEN
       BEGIN
       HISTORICO = HISTORICO||' VALID.:'||OLD.VALIDADE_PROPOSTA||'->'||NEW.VALIDADE_PROPOSTA;
       END
   IF (OLD.PRAZO_ENTREGA <> NEW.PRAZO_ENTREGA) THEN
       BEGIN
       HISTORICO = HISTORICO||' PRAZO:'||OLD.PRAZO_ENTREGA||'->'||NEW.PRAZO_ENTREGA;
       END
   IF (OLD.CONTATO <> NEW.CONTATO) THEN
       BEGIN
       HISTORICO = HISTORICO||' CONT.:'||OLD.CONTATO||'->'||NEW.CONTATO;
       END
   IF (OLD.OBSERVACAO <> NEW.OBSERVACAO) THEN
       BEGIN
       HISTORICO = HISTORICO||' OBS.:'||OLD.OBSERVACAO||'->'||NEW.OBSERVACAO;
       END
   IF (OLD.VINCULO_ID <> NEW.VINCULO_ID) THEN
       BEGIN
       HISTORICO = HISTORICO||' VINC.:'||OLD.VINCULO_ID||'->'||NEW.VINCULO_ID;
       END
   IF (OLD.DATAENVIO <> NEW.DATAENVIO) THEN
       BEGIN
       HISTORICO = HISTORICO||' DT ENV.:'||OLD.DATAENVIO||'->'||NEW.DATAENVIO;
       END
   IF (OLD.DATARESPOSTA <> NEW.DATARESPOSTA) THEN
       BEGIN
       HISTORICO = HISTORICO||' DT RESP.:'||OLD.DATARESPOSTA||'->'||NEW.DATARESPOSTA;
       END
   IF (OLD.FRETE_VALOR <> NEW.FRETE_VALOR) THEN
       BEGIN
       HISTORICO = HISTORICO||' VL FRETE:'||OLD.FRETE_VALOR||'->'||NEW.FRETE_VALOR;
       END

   IF (HISTORICO <> '') THEN
       BEGIN
	   HISTORICO = 'ALT. ORC. '||OLD.ID||': '||HISTORICO;
       INSERT INTO TBHISTORICO
          (TABELA, TABELA_ID, USUARIO, IP, HISTORICO)
       VALUES
          ('TBLICITACAO', OLD.LICITACAO_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 TTBORCAMENTO_B0I_02 FOR TBORCAMENTO
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

   HISTORICO = 'INSERIDO ORC: '||NEW.ID;

   IF (HISTORICO <> '') THEN
       BEGIN
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
      'TBLICITACAO', NEW.LICITACAO_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 TRIGGER TTBORCAMENTO_A0D_01 FOR TBORCAMENTO
ACTIVE AFTER DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

   HISTORICO = 'EXCLUÍDO ORC: '||OLD.ID;

   IF (HISTORICO <> '') THEN
       BEGIN
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
      'TBLICITACAO', OLD.LICITACAO_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 TRIGGER TTBORCAMENTO_ITEM_B0I_02 FOR TBORCAMENTO_ITEM
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

   HISTORICO = 'INSERIDO ITEM DE ORC: '||NEW.ID||' PROD: '||NEW.PRODUTO_ID;

   IF (HISTORICO <> '') THEN
       BEGIN
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
      'TBLICITACAO',
      (SELECT FIRST 1 LICITACAO_ID FROM TBORCAMENTO WHERE ID = NEW.ORCAMENTO_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 TRIGGER TTBORCAMENTO_ITEM_A0D_01 FOR TBORCAMENTO_ITEM
ACTIVE AFTER DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

   HISTORICO = 'EXCLUÍDO ITEM DE ORC: '||OLD.ID||' PROD: '||OLD.PRODUTO_ID;

   IF (HISTORICO <> '') THEN
       BEGIN
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
      'TBLICITACAO',
      (SELECT FIRST 1 LICITACAO_ID FROM TBORCAMENTO WHERE ID = OLD.ORCAMENTO_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 TRIGGER TTBORCAMENTO_ITEM_B0U_02 FOR TBORCAMENTO_ITEM
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

   HISTORICO = '';

   IF (OLD.ORCAMENTO_ID <> NEW.ORCAMENTO_ID) THEN
       BEGIN
       HISTORICO = HISTORICO||' ORC.:'||OLD.ORCAMENTO_ID||'->'||NEW.ORCAMENTO_ID;
       END
   IF (OLD.OC <> NEW.OC) THEN
       BEGIN
       HISTORICO = HISTORICO||' OC:'||OLD.OC||'->'||NEW.OC;
       END
   IF (OLD.PRODUTO_ID <> NEW.PRODUTO_ID) THEN
       BEGIN
       HISTORICO = HISTORICO||' PROD.:'||OLD.PRODUTO_ID||'->'||NEW.PRODUTO_ID;
       END
   IF (OLD.UM <> NEW.UM) THEN
       BEGIN
       HISTORICO = HISTORICO||' UM:'||OLD.UM||'->'||NEW.UM;
       END
   IF (OLD.TAMANHO <> NEW.TAMANHO) THEN
       BEGIN
       HISTORICO = HISTORICO||' TAM.:'||OLD.TAMANHO||'->'||NEW.TAMANHO;
       END
   IF (OLD.QUANTIDADE <> NEW.QUANTIDADE) THEN
       BEGIN
       HISTORICO = HISTORICO||' QTD.:'||OLD.QUANTIDADE||'->'||NEW.QUANTIDADE;
       END
   IF (OLD.VALOR_UNITARIO <> NEW.VALOR_UNITARIO) THEN
       BEGIN
       HISTORICO = HISTORICO||' VL UNIT.:'||OLD.VALOR_UNITARIO||'->'||NEW.VALOR_UNITARIO;
       END
   IF (OLD.PERCENTUAL_IPI <> NEW.PERCENTUAL_IPI) THEN
       BEGIN
       HISTORICO = HISTORICO||' IPI:'||OLD.PERCENTUAL_IPI||'->'||NEW.PERCENTUAL_IPI;
       END
   IF (OLD.REQUISICAO_ID <> NEW.REQUISICAO_ID) THEN
       BEGIN
       HISTORICO = HISTORICO||' REQ.:'||OLD.REQUISICAO_ID||'->'||NEW.REQUISICAO_ID;
       END
   IF (OLD.OBS_PRODUTO <> NEW.OBS_PRODUTO) THEN
       BEGIN
       HISTORICO = HISTORICO||' OBS PROD:'||OLD.OBS_PRODUTO||'->'||NEW.OBS_PRODUTO;
       END

   IF (HISTORICO <> '') THEN
       BEGIN
       HISTORICO = 'ALT. ORC. '||OLD.ORCAMENTO_ID||' ITEM '||OLD.ID||': '||HISTORICO;
       INSERT INTO TBHISTORICO
          (TABELA, TABELA_ID, USUARIO, IP, HISTORICO)
       VALUES
          ('TBLICITACAO',
          (SELECT FIRST 1 LICITACAO_ID FROM TBORCAMENTO WHERE ID = OLD.ORCAMENTO_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;