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

Company spanish_organisation_number invalid #2345

Closed
rjurado01 opened this issue Jun 30, 2021 · 2 comments
Closed

Company spanish_organisation_number invalid #2345

rjurado01 opened this issue Jun 30, 2021 · 2 comments

Comments

@rjurado01
Copy link

rjurado01 commented Jun 30, 2021

Describe the bug

Some spanish company CIF generated are invalid. For example: N6043409

spanish_cif_control_digit is missing

To Reproduce

Faker::Company.spanish_organisation_number

Expected behavior

LETTERS_CIF = %w[A B C D E F G H J N P Q R S U V W]
LETTERS_CIF_NUMBER = %w[P Q S W]
LETTERS_CIF_CONTROL = %w[J A B C D E F G H I]
REGEX_CIF = /^(#{LETTERS_CIF.join('|')})\-?(\d{7})\-?(\d|#{LETTERS_CIF_CONTROL.join('|')})$/

`N6043409` =~ REGEX_CIF # invalid CIF generated by faker
=> nil

'U59881409' =~ REGEX_CIF # valid CIF generated by faker
=> 0

Additional context

Example of validation method:

def cif_valid?(cif)
  if cif =~ REGEX_CIF
    number = Regexp.last_match(2)
    first_letter = Regexp.last_match(1)
    province_code = number[0..1]
    actual_control = Regexp.last_match(3)

    total = number.split('').each_with_index.inject(0) do |acc, (element, index)|
      acc + if index.even?
              (element.to_i * 2).digits.inject(:+)
            else
              element.to_i
            end
    end

    decimal = total.digits.first
    expected_control = decimal != 0 ? 10 - decimal : decimal

    # Control code must be a letter
    return LETTERS_CIF_CONTROL[expected_control] if LETTERS_CIF_NUMBER.include?(first_letter) ||
                                                    province_code == '00'

    # Control code will be a number or a letter
    return [expected_control.to_s,
            LETTERS_CIF_CONTROL[expected_control]].include?(actual_control)
  end

  false
end
@laulujan
Copy link
Contributor

Hello, is it ok if I give it a try on this issue?

@thdaraujo
Copy link
Contributor

fixed by #2351

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants