SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBOPERACAO_FRETE_B1I FOR TBOPERACAO_FRETE
ACTIVE BEFORE INSERT POSITION 1
AS
--*****************************
--*                           *
--*       REGISTRA LOG        *
--*       DE  GRAVAÇÃO        *
--*                           *
--*****************************

DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

   HISTORICO = 'REGISTRO ' || FN_LPAD(NEW.ID,4,0) || ' INCLUÍDO.';

   IF (HISTORICO <> '') THEN
   BEGIN
       INSERT INTO TBHISTORICO
              (
              TABELA,
              TABELA_ID,
              USUARIO, IP,
              HISTORICO
              )
       VALUES(
              'TBOPERACAO_FRETE',
              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 TTBOPERACAO_FRETE_B0U FOR TBOPERACAO_FRETE
ACTIVE BEFORE UPDATE POSITION 0
AS
--*****************************
--*                           *
--*       REGISTRA LOG        *
--*       DE ALTERAÇÃO        *
--*                           *
--*****************************

DECLARE VARIABLE HISTORICO VARCHAR(5000);
BEGIN

    HISTORICO = '';

    IF (OLD.ESTABELECIMENTO_ID  <> NEW.ESTABELECIMENTO_ID  ) THEN BEGIN HISTORICO = HISTORICO||' ESTAB.: '         ||OLD.ESTABELECIMENTO_ID  ||'->'||NEW.ESTABELECIMENTO_ID  ; END
    IF (OLD.CFOP_CODIGO         <> NEW.CFOP_CODIGO         ) THEN BEGIN HISTORICO = HISTORICO||' CFOP CÓD.: '      ||OLD.CFOP_CODIGO         ||'->'||NEW.CFOP_CODIGO         ; END
    IF (OLD.CFOP_DOC_ORIGEM     <> NEW.CFOP_DOC_ORIGEM     ) THEN BEGIN HISTORICO = HISTORICO||' CFOP DOC. ORIG.: '||OLD.CFOP_DOC_ORIGEM     ||'->'||NEW.CFOP_DOC_ORIGEM     ; END
    IF (OLD.PRODUTO_ID          <> NEW.PRODUTO_ID          ) THEN BEGIN HISTORICO = HISTORICO||' PROD. ID: '       ||OLD.PRODUTO_ID          ||'->'||NEW.PRODUTO_ID          ; END
    IF (OLD.DESCRICAO           <> NEW.DESCRICAO           ) THEN BEGIN HISTORICO = HISTORICO||' DESC.: '    ||OLD.DESCRICAO           ||'->'||NEW.DESCRICAO           ; END
    IF (OLD.CST_ICMS            <> NEW.CST_ICMS            ) THEN BEGIN HISTORICO = HISTORICO||' ICMS: '           ||OLD.CST_ICMS            ||'->'||NEW.CST_ICMS            ; END
    IF (OLD.CST_ICMS_SIMPLES    <> NEW.CST_ICMS_SIMPLES    ) THEN BEGIN HISTORICO = HISTORICO||' ICMS SIMP.: '     ||OLD.CST_ICMS_SIMPLES    ||'->'||NEW.CST_ICMS_SIMPLES    ; END
    IF (OLD.CST_PIS             <> NEW.CST_PIS             ) THEN BEGIN HISTORICO = HISTORICO||' PIS: '            ||OLD.CST_PIS             ||'->'||NEW.CST_PIS             ; END
    IF (OLD.CST_COFINS          <> NEW.CST_COFINS          ) THEN BEGIN HISTORICO = HISTORICO||' COFINS: '         ||OLD.CST_COFINS          ||'->'||NEW.CST_COFINS          ; END
    IF (OLD.SPED_PISCOF         <> NEW.SPED_PISCOF         ) THEN BEGIN HISTORICO = HISTORICO||' SPED: '           ||OLD.SPED_PISCOF         ||'->'||NEW.SPED_PISCOF         ; END
    IF (OLD.IND_NAT_FRETE       <> NEW.IND_NAT_FRETE       ) THEN BEGIN HISTORICO = HISTORICO||' NAT. FRETE: '     ||OLD.IND_NAT_FRETE       ||'->'||NEW.IND_NAT_FRETE       ; END
    IF (OLD.TIPO_DOC            <> NEW.TIPO_DOC            ) THEN BEGIN HISTORICO = HISTORICO||' TIPO DOC.: '      ||OLD.TIPO_DOC            ||'->'||NEW.TIPO_DOC            ; END
    IF (OLD.CONTA_PAGAR_OPERACAO<> NEW.CONTA_PAGAR_OPERACAO) THEN BEGIN HISTORICO = HISTORICO||' OP. CPA: '        ||OLD.CONTA_PAGAR_OPERACAO||'->'||NEW.CONTA_PAGAR_OPERACAO; END
    IF (OLD.PAGAMENTO_FORMA     <> NEW.PAGAMENTO_FORMA     ) THEN BEGIN HISTORICO = HISTORICO||' FORMA PAG.: '     ||OLD.PAGAMENTO_FORMA     ||'->'||NEW.PAGAMENTO_FORMA     ; END
    IF (OLD.DIAS_VENCIMENTO     <> NEW.DIAS_VENCIMENTO     ) THEN BEGIN HISTORICO = HISTORICO||' DIAS VENC.: '     ||OLD.DIAS_VENCIMENTO     ||'->'||NEW.DIAS_VENCIMENTO     ; END


    IF (HISTORICO <> '') THEN
    BEGIN
    HISTORICO = 'REGISTRO: ' ||FN_LPAD(OLD.ID,4,0)|| ' - ' ||OLD.DESCRICAO||' ALTERADO '||HISTORICO;
    END


    IF (HISTORICO <> '') THEN
    BEGIN
        INSERT INTO TBHISTORICO
        (
        TABELA,
        TABELA_ID,
        USUARIO, IP,
        HISTORICO
        )VALUES(
        'TBOPERACAO_FRETE',
        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),
        FN_ELIPSES(:HISTORICO,500)
        );
    END
END^

CREATE OR ALTER TRIGGER TTBOPERACAO_FRETE_B0D FOR TBOPERACAO_FRETE
ACTIVE BEFORE DELETE POSITION 0
AS
--*****************************
--*                           *
--*       REGISTRA LOG        *
--*       DE  EXCLUSÃO        *
--*                           *
--*****************************

DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

   HISTORICO = 'REGISTRO ' ||FN_LPAD(OLD.ID,4,0) || ' EXCLUÍDO.';

   IF (HISTORICO <> '') THEN
   BEGIN
       INSERT INTO TBHISTORICO
              (
              TABELA,
              TABELA_ID,
              USUARIO, IP,
              HISTORICO
              )
       VALUES(
              'TBOPERACAO_FRETE',
              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 ; ^

