Skip to content

Commit

Permalink
Revert "Improve Faker::Company.spanish_organisation_number (faker-rub…
Browse files Browse the repository at this point in the history
…y#2106)"

This reverts commit c57f585.
  • Loading branch information
droznyk authored Oct 23, 2020
1 parent 834d04f commit cbfbd41
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 62 deletions.
38 changes: 3 additions & 35 deletions lib/faker/default/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,10 @@ def profession
# @faker.version 1.8.5
#
# Get a random Spanish organization number. See more here https://es.wikipedia.org/wiki/N%C3%BAmero_de_identificaci%C3%B3n_fiscal
def spanish_organisation_number(organization_type: nil)
def spanish_organisation_number
# Valid leading character: A, B, C, D, E, F, G, H, J, N, P, Q, R, S, U, V, W
# format: 1 digit letter (organization type) + 7 digit numbers + 1 digit control (letter or number based on
# organization type)
letters = %w[A B C D E F G H J N P Q R S U V W]

organization_type = sample(letters) unless letters.include?(organization_type)
code = format('%07d', rand(10**7))
control = spanish_cif_control_digit(organization_type, code)

[organization_type, code, control].join
# 7 digit numbers
[sample(self::ULetters), format('%07d', rand(10**7))].join
end

##
Expand Down Expand Up @@ -535,31 +528,6 @@ def inn_checksum(factor, number)
end % 11 % 10
).to_s
end

def spanish_cif_control_digit(organization_type, code)
letters = %w[J A B C D E F G H]

control = code.split('').each_with_index.inject(0) do |sum, (value, index)|
if (index + 1).even?
sum + value.to_i
else
sum + spanish_b_algorithm(value.to_i)
end
end

control = control.to_s[-1].to_i
control = control.zero? ? control : 10 - control

%w[A B C D E F G H J U V].include?(organization_type) ? control : letters[control]
end

def spanish_b_algorithm(value)
result = value.to_i * 2

return result if result < 10

result.to_s[0].to_i + result.to_s[1].to_i
end
end
end
end
30 changes: 3 additions & 27 deletions test/faker/default/test_faker_company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ def test_type
end

def test_spanish_organisation_number
cif = @tester.spanish_organisation_number(organization_type: 'A')
assert @tester.send(:spanish_cif_control_digit, 'A', cif[1..7]) == cif[-1].to_i
org_no = @tester.spanish_organisation_number
assert org_no.match(/\D\d{7}/)
assert Faker::Base::ULetters.include?(org_no[0].to_s)
end

def test_swedish_organisation_number
Expand Down Expand Up @@ -210,31 +211,6 @@ def test_sic_code
assert @tester.sic_code.match(/\d\d\d\d/)
end

def test_spanish_cif_control_digit
assert @tester.send(:spanish_cif_control_digit, 'A', '2217680') == 4
assert @tester.send(:spanish_cif_control_digit, 'B', '4031315') == 7
assert @tester.send(:spanish_cif_control_digit, 'C', '7191088') == 9
assert @tester.send(:spanish_cif_control_digit, 'D', '3178686') == 6
assert @tester.send(:spanish_cif_control_digit, 'E', '4484441') == 3
assert @tester.send(:spanish_cif_control_digit, 'F', '4830511') == 4
assert @tester.send(:spanish_cif_control_digit, 'G', '7676903') == 3
assert @tester.send(:spanish_cif_control_digit, 'H', '8888075') == 2
assert @tester.send(:spanish_cif_control_digit, 'J', '6840041') == 5
assert @tester.send(:spanish_cif_control_digit, 'N', '5350867') == 'G'
assert @tester.send(:spanish_cif_control_digit, 'P', '5669582') == 'H'
assert @tester.send(:spanish_cif_control_digit, 'Q', '5182823') == 'D'
assert @tester.send(:spanish_cif_control_digit, 'R', '1099088') == 'E'
assert @tester.send(:spanish_cif_control_digit, 'S', '2210399') == 'H'
assert @tester.send(:spanish_cif_control_digit, 'U', '3957325') == 8
assert @tester.send(:spanish_cif_control_digit, 'V', '7536342') == 4
assert @tester.send(:spanish_cif_control_digit, 'W', '6793772') == 'B'
end

def test_spanish_b_algorithm
assert @tester.send(:spanish_b_algorithm, 2) == 4
assert @tester.send(:spanish_b_algorithm, 6) == 3
end

private

def czech_o_n_checksum(org_no)
Expand Down

0 comments on commit cbfbd41

Please sign in to comment.