Skip to content
Open
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
5 changes: 3 additions & 2 deletions lib/faker/default/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,9 @@ def indian_gst_number(state_code: nil)

gen.letter(name: :z_char, length: 1, ranges: [['Z']])

gen.computed(deps: %i[state_code_param taxpayer_number registration_number]) do |state_code_param, taxpayer_number, registration_number|
gst_base = "#{state_code_param}#{taxpayer_number}#{registration_number}"
# The check character covers all 14 preceding characters, including the "Z"
gen.computed(deps: %i[state_code_param taxpayer_number registration_number z_char]) do |state_code_param, taxpayer_number, registration_number, z_char|
gst_base = "#{state_code_param}#{taxpayer_number}#{registration_number}#{z_char}"
chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'.chars
values = gst_base.chars
sum = values.map.with_index do |char, index|
Expand Down
14 changes: 14 additions & 0 deletions test/faker/default/test_faker_company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,20 @@ 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_check_character
chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'.chars

10.times do
gst = @tester.indian_gst_number
sum = gst[0, 14].chars.each_with_index.sum do |char, index|
product = chars.index(char) * (index.odd? ? 2 : 1)
(product / 36) + (product % 36)
end

assert_equal chars[(36 - (sum % 36)) % 36], gst[14], gst
end
end

def test_state_code_in_indian_gst_number
assert_raise ArgumentError do
@tester.indian_gst_number(state_code: '01')
Expand Down