CREATE TRIGGER TTBMOVCONTMESTRE_B02I FOR TBMOVCONTMESTRE
ACTIVE BEFORE INSERT POSITION 9
AS
declare variable historico varchar(500);
begin
  historico = '';
  if(new.ID > 0)then
  begin
     historico = ' REGISTRO INCLUÍDO';
  end

  IF(HISTORICO <> '')THEN
  BEGIN
     EXECUTE PROCEDURE SPI_HISTORICO('TBMOVCONTMESTRE', NEW.ID, :HISTORICO);
  END

end;

CREATE OR ALTER TRIGGER TTBMOVCONTMESTRE_B01U FOR TBMOVCONTMESTRE
ACTIVE BEFORE UPDATE POSITION 9
AS
declare variable HISTORICO VARCHAR(8000);
begin
    HISTORICO = '';

    IF(NEW.CODESTABELECIMENTO <> OLD.CODESTABELECIMENTO) THEN
    BEGIN
       HISTORICO = HISTORICO||' ESTAB:'||OLD.CODESTABELECIMENTO||'->'||NEW.CODESTABELECIMENTO;
    END 

    IF(NEW.DATA <> OLD.DATA) THEN
    BEGIN
        HISTORICO = HISTORICO||',DATA:'||FORMATDATE(OLD.DATA)||'->'||FORMATDATE(NEW.DATA);
    END

    IF(NEW.TIPOLANC <> OLD.TIPOLANC) THEN
    BEGIN
       HISTORICO = HISTORICO||',TP.LANC:'||OLD.TIPOLANC||'->'||NEW.TIPOLANC;
    END

    IF(HISTORICO <> '') THEN
    BEGIN
       EXECUTE PROCEDURE SPI_HISTORICO('TBMOVCONTMESTRE', NEW.ID, FN_ELIPSES(HISTORICO,500));
    END
END;

CREATE TRIGGER TTBMOVCONTMESTRE_B02D FOR TBMOVCONTMESTRE
ACTIVE BEFORE DELETE POSITION 9
AS
declare variable historico varchar(500);
begin
  historico = '';
  if(OLD.ID > 0)then
  begin
     historico = ' REGISTRO EXCLUÍDO';
  end

  IF(HISTORICO <> '')THEN
  BEGIN
     EXECUTE PROCEDURE SPI_HISTORICO('TBMOVCONTMESTRE', OLD.ID, :HISTORICO);
  END
end;

CREATE OR ALTER TRIGGER TTBMOVCONTABIL_B09I FOR TBMOVCONTABIL
ACTIVE BEFORE INSERT POSITION 9
AS
declare variable historico varchar(500);
begin
  historico = '';
  if(NEW.ID > 0 AND NEW.IDCONTMOV > 0)then
  begin
     historico = 'LANC. ID:'||NEW.ID||', CONTA: '||FN_MASK(NEW.CONTA,'CONTA')||', CCUSTO: '||FN_MASK(NEW.CCUSTO,'CC')||' INCLUIDO';
  end

  IF(HISTORICO <> '')THEN
  BEGIN
     EXECUTE PROCEDURE SPI_HISTORICO('TBMOVCONTMESTRE', NEW.IDCONTMOV, :HISTORICO);
  END

end;

CREATE TRIGGER TTBMOVCONTABIL_B09U FOR TBMOVCONTABIL
ACTIVE BEFORE UPDATE POSITION 9
AS
declare variable HISTORICO VARCHAR(8000);
begin
    HISTORICO = '';

    IF(NEW.CODESTABELECIMENTO <> OLD.CODESTABELECIMENTO) THEN
    BEGIN
       HISTORICO = HISTORICO||'ESTAB:'||OLD.CODESTABELECIMENTO||'->'||NEW.CODESTABELECIMENTO;
    END 

    IF(NEW.DATA <> OLD.DATA) THEN
    BEGIN
		HISTORICO = HISTORICO||',DATA:'||FORMATDATE(OLD.DATA)||'->'||FORMATDATE(NEW.DATA);
    END

    IF(NEW.IDDOCUMENTO <> OLD.IDDOCUMENTO) THEN
    BEGIN
       HISTORICO = HISTORICO||',ID.DOC:'||OLD.IDDOCUMENTO||'->'||NEW.IDDOCUMENTO;
    END  

    IF(NEW.DESCRICAO <> OLD.DESCRICAO) THEN
    BEGIN
       HISTORICO = HISTORICO||',DOC:'||OLD.DESCRICAO||'->'||NEW.DESCRICAO;
    END                         

    IF(NEW.VALOR <> OLD.VALOR) THEN
    BEGIN
       HISTORICO = HISTORICO||',VLR:'||OLD.VALOR||'->'||NEW.VALOR;
    END                         

    IF(NEW.CODORIGEM <> OLD.CODORIGEM) THEN
    BEGIN
       HISTORICO = HISTORICO||',COD.ORI:'||OLD.CODORIGEM||'->'||NEW.CODORIGEM;
    END                         

    IF(NEW.HISTORICO <> OLD.HISTORICO) THEN
    BEGIN
       HISTORICO = HISTORICO||',HIST:'||OLD.HISTORICO||'->'||NEW.HISTORICO;
    END                        

    IF(NEW.CONTA <> OLD.CONTA) THEN
    BEGIN
       HISTORICO = HISTORICO||',CONTA:'||OLD.CONTA||'->'||NEW.CONTA;
    END                        

    IF(NEW.CCUSTO <> OLD.CCUSTO) THEN
    BEGIN
       HISTORICO = HISTORICO||',CCUSTO:'||OLD.CCUSTO||'->'||NEW.CCUSTO;
    END

    IF(HISTORICO <> '') THEN
    BEGIN
       historico = 'LANC.ID:'||NEW.ID||', CONTA: '||FN_MASK(OLD.CONTA,'CONTA')||', CCUSTO: '||FN_MASK(OLD.CCUSTO,'CC')||' ALTERADO '||HISTORICO;
       EXECUTE PROCEDURE SPI_HISTORICO('TBMOVCONTMESTRE', OLD.IDCONTMOV, FN_ELIPSES(HISTORICO,500));
    END
END;

CREATE TRIGGER TTBMOVCONTABIL_B10U FOR TBMOVCONTABIL
ACTIVE BEFORE UPDATE POSITION 9
AS
declare variable HISTORICO VARCHAR(8000);
begin
    HISTORICO = '';

    IF(NEW.SITUACAO <> OLD.SITUACAO) THEN
    BEGIN
         IF(NEW.SITUACAO = 1)THEN
         BEGIN
            HISTORICO = HISTORICO||' CONCILIADO!';
         END
         IF(NEW.SITUACAO = 0)THEN
         BEGIN
            HISTORICO = HISTORICO||' CONCILIAÇÃO DESFEITA!';
         END
    END

    IF(HISTORICO <> '') THEN
    BEGIN
       historico = 'LANC.ID:'||NEW.ID||', CONTA: '||FN_MASK(NEW.CONTA,'CONTA')||', CCUSTO: '||FN_MASK(NEW.CCUSTO,'CC')||HISTORICO;
       EXECUTE PROCEDURE SPI_HISTORICO('TBMOVCONTMESTRE', NEW.IDCONTMOV, FN_ELIPSES(HISTORICO,500));
    END
END;

CREATE TRIGGER TTBMOVCONTABIL_B06 FOR TBMOVCONTABIL
ACTIVE BEFORE DELETE POSITION 9
AS
declare variable historico varchar(500);
begin
  historico = '';
  if(OLD.ID > 0 AND OLD.IDCONTMOV > 0)then
  begin
     historico = 'LANC.ID:'||OLD.ID||', CONTA: '||FN_MASK(OLD.CONTA,'CONTA')||', CCUSTO: '||FN_MASK(OLD.CCUSTO,'CC')||' EXCLUIDO.';
  end

  IF(HISTORICO <> '')THEN
  BEGIN
     EXECUTE PROCEDURE SPI_HISTORICO('TBMOVCONTMESTRE', OLD.IDCONTMOV, :HISTORICO);
  END

end;
