Skip to content

Commit

Permalink
Create validar_cnpj.R
Browse files Browse the repository at this point in the history
  • Loading branch information
jtrecenti authored Jan 16, 2023
1 parent 3f210f8 commit b8e7967
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions R/validar_cnpj.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#' Valida um CNPJ
#'
#' Recebe um CNPJ e retorna TRUE ou FALSE. Nao vetorizado.
#'
#' @param cnpj string com o CNPJ a ser validado
#'
#' @return Retorna TRUE ou FALSE
#' @export
validar_cnpj <- function(cnpj) {
cnpj <- gsub("[^[:digit:]]", "", cnpj)
if(cnpj == '') return(FALSE)

if (nchar(cnpj) != 14)
return(FALSE)

# Elimina CNPJs invalidos conhecidos
invalidos <- c(
"00000000000000", "11111111111111", "22222222222222", "33333333333333",
"44444444444444", "55555555555555", "66666666666666", "77777777777777",
"88888888888888", "99999999999999"
)
if(cnpj %in% invalidos) return(FALSE)

# Valida DVs
tamanho <- nchar(cnpj) - 2
numeros <- substr(cnpj, 1, tamanho)
digitos <- substr(cnpj, tamanho + 1, nchar(cnpj))
soma <- 0
pos <- tamanho - 7
for (i in tamanho:1) {
soma <- soma + as.numeric(substr(numeros, tamanho - i + 1, tamanho - i + 1)) * pos
pos <- pos - 1
if (pos < 2)
pos <- 9
}
resultado <- ifelse(soma %% 11 < 2, 0, 11 - soma %% 11)
if (resultado != as.numeric(substr(digitos, 1, 1)))
return(FALSE)

tamanho <- tamanho + 1
numeros <- substr(cnpj, 1, tamanho)
soma <- 0
pos <- tamanho - 7
for (i in tamanho:1) {
soma <- soma + as.numeric(substr(numeros, tamanho - i + 1, tamanho - i + 1)) * pos
pos <- pos - 1
if (pos < 2)
pos <- 9
}
resultado <- ifelse(soma %% 11 < 2, 0, 11 - soma %% 11)
if (resultado != as.numeric(substr(digitos, 2, 2)))
return(FALSE)
return(TRUE)
}

0 comments on commit b8e7967

Please sign in to comment.