Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Faker::Company.spanish_organisation_number #2106

Merged
merged 1 commit into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions lib/faker/default/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,17 @@ 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
def spanish_organisation_number(organization_type: nil)
# Valid leading character: A, B, C, D, E, F, G, H, J, N, P, Q, R, S, U, V, W
# 7 digit numbers
[sample(self::ULetters), format('%07d', rand(10**7))].join
# 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
end

##
Expand Down Expand Up @@ -528,6 +535,31 @@ 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: 27 additions & 3 deletions test/faker/default/test_faker_company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ def test_type
end

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

def test_swedish_organisation_number
Expand Down Expand Up @@ -211,6 +210,31 @@ 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