Skip to content

Commit

Permalink
Added Faker::Company.indian_gst_number fixed faker-ruby#2823
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitkhadria committed Sep 6, 2023
1 parent edac6f2 commit 2ddafea
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc/default/company.md
Expand Up @@ -84,4 +84,8 @@ Faker::Company.russian_tax_number #=> "0965855857"
Faker::Company.russian_tax_number(region: '77') #=> "7717152803"
Faker::Company.russian_tax_number(type: :individual) #=> "488935903348"
Faker::Company.russian_tax_number(region: '77', type: :individual) #=> "779124694601"

# Get a random formatted Indian tax number (GST)
Faker::Company.indian_gst_number #=> "15VQPNZ2126J2ZU"
Faker::Company.indian_gst_number(state_code: "22") #=> "22ZVWEY6632K0ZN"
```
42 changes: 42 additions & 0 deletions lib/faker/default/company.rb
Expand Up @@ -464,6 +464,35 @@ def sic_code
fetch('company.sic_code')
end

##
# Get a random Indian Goods and Services Tax (GST) number.
# For more on Indian tax number here:
# https://simple.wikipedia.org/wiki/GSTIN
# @params state code [String] Any state code.
#
# @return [String]
# @example
# Faker::Company.indian_gst_number #=> "15VQPNZ2126J2ZU"
# Faker::Company.indian_gst_number(state_code: "22") #=> "22ZVWEY6632K0ZN"
#
# @faker.version 3.2.1
def indian_gst_number(state_code: nil)
state_code_list = %w[01 02 03 04 05 06 07 08 09 10 11 12 13 14 97]
state_code = state_code_list.sample(random: Faker::Config.random) if state_code.nil?
taxpayer_number = Array.new(3) { ('A'..'Z').to_a.sample(random: Faker::Config.random) } + Array.new(1) {
%w[A B C F G H L J P T
K].to_a.sample(random: Faker::Config.random)
} + Array.new(1) {
('A'..'Z').to_a.sample(random: Faker::Config.random)
} + Array.new(4) {
rand(10)
} + [('A'..'Z').to_a.sample(random: Faker::Config.random)]
taxpayer_number = taxpayer_number.join
registration_number = rand(10).to_s
checksum = calculate_gst_checksum(state_code, taxpayer_number, registration_number)
"#{state_code}#{taxpayer_number}#{registration_number}Z#{checksum}"
end

private

# Mod11 functionality from https://github.com/badmanski/mod11/blob/master/lib/mod11.rb
Expand Down Expand Up @@ -605,6 +634,19 @@ def spanish_b_algorithm(value)

result.to_s[0].to_i + result.to_s[1].to_i
end

def calculate_gst_checksum(state_code, taxpayer_number, registration_number)
gst_base = "#{state_code}#{taxpayer_number}#{registration_number}"
chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'.chars
values = gst_base.upcase.chars
sum = values.map.with_index do |char, index|
product = chars.index(char) * (index.odd? ? 2 : 1)
(product / chars.length).floor + (product % chars.length)
end.reduce(:+)

checksum = (chars.length - (sum % chars.length)) % chars.length
chars[checksum]
end
end
end
end
9 changes: 9 additions & 0 deletions test/faker/default/test_faker_company.rb
Expand Up @@ -250,6 +250,15 @@ def test_spanish_b_algorithm
assert_equal(3, @tester.send(:spanish_b_algorithm, 6))
end

def text_indian_gst_number
assert_match(/^([0-2][0-9]|[3][0-7])[A-Z]{3}[ABCFGHLJPTK][A-Z]\d{4}[A-Z][A-Z0-9][Z][A-Z0-9]$/i, @tester.indian_gst_number)
end

def test_indian_gst_number_with_state_code
assert_match(/^(22)[A-Z]{3}[ABCFGHLJPTK][A-Z]\d{4}[A-Z][A-Z0-9][Z][A-Z0-9]$/i, @tester.indian_gst_number(state_code: '22'))
assert_match(/^(01)[A-Z]{3}[ABCFGHLJPTK][A-Z]\d{4}[A-Z][A-Z0-9][Z][A-Z0-9]$/i, @tester.indian_gst_number(state_code: '01'))
end

private

def czech_o_n_checksum(org_no)
Expand Down

0 comments on commit 2ddafea

Please sign in to comment.