CREATE TRIGGER TTBFISCAL_CONTADOR_B01I FOR TBFISCAL_CONTADOR
ACTIVE BEFORE INSERT POSITION 0
AS
begin
  IF(NEW.CODIGO IS NULL) THEN
     NEW.CODIGO = GEN_ID(GTBFISCAL_CONTADOR,1);
end;

CREATE TRIGGER TTBFISCAL_CONTADOR_B02I FOR TBFISCAL_CONTADOR
ACTIVE BEFORE INSERT POSITION 9
AS
declare variable historico varchar(500);
BEGIN
  historico = '';
  IF(NEW.CODIGO > 0) then
  BEGIN
    historico = historico||' Reg.Incluido';
  END

  IF(HISTORICO <> '') then
  BEGIN
    execute procedure spi_historico('TBFISCAL_CONTADOR',NEW.CODIGO,historico);
  END
END;

CREATE OR ALTER TRIGGER TTBFISCAL_CONTADOR_B01U FOR TBFISCAL_CONTADOR
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';
    IF(OLD.ESTABELECIMENTO_CODIGO <> NEW.ESTABELECIMENTO_CODIGO) THEN
    BEGIN
        HISTORICO = HISTORICO||' Estab:'||OLD.ESTABELECIMENTO_CODIGO||' -> '||NEW.ESTABELECIMENTO_CODIGO;
    END

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

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

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

    IF(OLD.CRC_UF <> NEW.CRC_UF)THEN
    BEGIN
        HISTORICO = HISTORICO||' CRC/UF:'||OLD.CRC_UF||' -> '||NEW.CRC_UF;
    END 

    IF(OLD.CRC_ANO <> NEW.CRC_ANO)THEN
    BEGIN
        HISTORICO = HISTORICO||' CRC/ANO:'||OLD.CRC_ANO||' -> '||NEW.CRC_ANO;
    END    

    IF(OLD.CRC_VALIDADE <> NEW.CRC_VALIDADE)THEN
    BEGIN
        HISTORICO = HISTORICO||' CRC/VALID:'||OLD.CRC_VALIDADE||' -> '||NEW.CRC_VALIDADE;
    END    

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

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

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

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

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

    IF(OLD.FAX <> NEW.FAX)THEN
    BEGIN
        HISTORICO = HISTORICO||' Fax:'||OLD.FAX||' -> '||NEW.FAX;
    END
         
    IF(OLD.EMAIL <> NEW.EMAIL)THEN
    BEGIN
        HISTORICO = HISTORICO||' Email:'||OLD.EMAIL||' -> '||NEW.EMAIL;
    END  
         
    IF(OLD.SPED <> NEW.SPED)THEN
    BEGIN
        HISTORICO = HISTORICO||' SPED:'||OLD.SPED||' -> '||NEW.SPED;
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFISCAL_CONTADOR',NEW.CODIGO,FN_ELIPSES(HISTORICO,500));
    END
END;

CREATE TRIGGER TTBFISCAL_CONTADOR_B01D FOR TBFISCAL_CONTADOR
ACTIVE BEFORE DELETE POSITION 9
AS
declare variable historico varchar(500);
BEGIN
  historico = '';
  IF(OLD.CODIGO > 0) then
  BEGIN
    historico = historico||' Reg.Excluido';
  END

  IF(HISTORICO <> '') then
  BEGIN
    execute procedure spi_historico('TBFISCAL_CONTADOR',OLD.CODIGO,historico);
  END
END;