Skip to content

Commit

Permalink
Merge pull request #30 from williamgueiros/special-cnpj
Browse files Browse the repository at this point in the history
Casos especiais de cnpj
  • Loading branch information
VitorTrin authored Jan 11, 2021
2 parents 0e8f95d + 0578a9e commit a4b9e7e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
elixir 1.11.0-otp-23
elixir 1.11.3-otp-23
erlang 23.0.4
71 changes: 57 additions & 14 deletions lib/cpfcnpj.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,36 @@ defmodule Cpfcnpj do
"""
@spec valid?({:cpf | :cnpj, String.t()}) :: boolean()
def valid?(number_in) do
if check_number(number_in) != :error, do: type_checker(number_in), else: false
check_number(number_in) and type_checker(number_in) and special_checker(number_in)
end

defp check_number({_, nil}) do
:error
false
end

# Checks length and if all are equal
defp check_number(tp_cpfcnpj) do
cpfcnpj = String.replace(elem(tp_cpfcnpj, 1), ~r/[\.\/-]/, "")

all_equal =
String.replace(cpfcnpj, String.at(cpfcnpj, 0), "")
all_equal? =
cpfcnpj
|> String.replace(String.at(cpfcnpj, 0), "")
|> String.length()
|> Kernel.==(0)

case tp_cpfcnpj do
{:cpf, _} ->
if String.length(cpfcnpj) != @cpf_length or all_equal == 0 do
:error
end
correct_length? =
case tp_cpfcnpj do
{:cpf, _} ->
String.length(cpfcnpj) == @cpf_length

{:cnpj, _} ->
if String.length(cpfcnpj) != @cnpj_length or all_equal == 0 do
:error
end
end
{:cnpj, _} ->
String.length(cpfcnpj) == @cnpj_length
end

correct_length? and not all_equal?
end

# Checks validation digits
defp type_checker(tp_cpfcnpj) do
cpfcnpj = String.replace(elem(tp_cpfcnpj, 1), ~r/[^0-9]/, "")
first_char_valid = character_valid(cpfcnpj, {elem(tp_cpfcnpj, 0), :first})
Expand All @@ -69,6 +72,46 @@ defmodule Cpfcnpj do
verif == String.slice(cpfcnpj, -2, 2)
end

# Checks special cases
defp special_checker({:cpf, _}) do
true
end

defp special_checker({:cnpj, _} = tp_cpfcnpj) do
cnpj = String.replace(elem(tp_cpfcnpj, 1), ~r/[\.\/-]/, "")

order = String.slice(cnpj, 8..11)

first_three_digits = String.slice(cnpj, 0..2)

basic = String.slice(cnpj, 0..7)

not_allowed_basics = basic in ~w<
11111111
22222222
33333333
44444444
55555555
66666666
77777777
88888888
99999999 >

cond do
not_allowed_basics ->
false

order == "0000" ->
false

String.to_integer(order) > 300 and first_three_digits == "000" and basic != "00000000" ->
false

true ->
true
end
end

defp mult_sum(algs, cpfcnpj) do
mult =
cpfcnpj
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Brcpfcnpj.Mixfile do
def project do
[
app: :brcpfcnpj,
version: "0.2.1",
version: "0.2.2",
elixir: "~> 1.7",
description: description(),
package: package(),
Expand Down
18 changes: 18 additions & 0 deletions test/cnpj_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,22 @@ defmodule CnpjTest do
test "should be invalid with nil input" do
assert Brcpfcnpj.cnpj_valid?(%Cnpj{number: nil}) == false
end

test "should be invalid when starting with zeroes and order > 0300" do
assert Brcpfcnpj.cnpj_valid?(%Cnpj{number: "00010000030160"}) == false
assert Brcpfcnpj.cnpj_valid?(%Cnpj{number: "00000898246954"}) == false
assert Brcpfcnpj.cnpj_valid?(%Cnpj{number: "00000136747140"}) == false
end

test "should be invalid for basic orders" do
assert Brcpfcnpj.cnpj_valid?(%Cnpj{number: "11111111030180"}) == false
assert Brcpfcnpj.cnpj_valid?(%Cnpj{number: "22222222030180"}) == false
assert Brcpfcnpj.cnpj_valid?(%Cnpj{number: "33333333030180"}) == false
assert Brcpfcnpj.cnpj_valid?(%Cnpj{number: "44444444030180"}) == false
assert Brcpfcnpj.cnpj_valid?(%Cnpj{number: "55555555030180"}) == false
assert Brcpfcnpj.cnpj_valid?(%Cnpj{number: "66666666030180"}) == false
assert Brcpfcnpj.cnpj_valid?(%Cnpj{number: "77777777030180"}) == false
assert Brcpfcnpj.cnpj_valid?(%Cnpj{number: "88888888030180"}) == false
assert Brcpfcnpj.cnpj_valid?(%Cnpj{number: "99999999030180"}) == false
end
end

0 comments on commit a4b9e7e

Please sign in to comment.