CREATE OR ALTER TRIGGER TTBNFS_B01IU FOR TBNFS
ACTIVE BEFORE INSERT OR UPDATE POSITION 0
AS
DECLARE VARIABLE CNPJ VARCHAR(100);
BEGIN
  IF(NEW.EMPRESA_CNPJ IS NOT NULL) THEN
  BEGIN
     CNPJ = FN_ELIPSES(FN_REPLACE_CHAR(NEW.EMPRESA_CNPJ),14);
     NEW.EMPRESA_CNPJ = CNPJ;
  END

  /* SETAR CHAVE ACESSO PARA NATUREZA LANCAMENTO */
  IF(CHAR_LENGTH(COALESCE(NEW.CHAVE_ACESSO,  '')) = 0) THEN
  BEGIN
    IF(NEW.NATUREZA = 2) THEN
    BEGIN
        NEW.CHAVE_ACESSO = FN_LPAD(NEW.CONTROLE, 44, '0');
    END
  END
END;