Class: Valvat::Checksum::BG

Inherits:
Base
  • Object
show all
Defined in:
lib/valvat/checksum/bg.rb

Instance Attribute Summary

Attributes inherited from Base

#vat

Instance Method Summary collapse

Methods inherited from Base

check_digit_length, #initialize, #validate

Constructor Details

This class inherits a constructor from Valvat::Checksum::Base

Instance Method Details

#build_prod(add = 1) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/valvat/checksum/bg.rb', line 48

def build_prod(add = 1)
  prod = 0
  figures.each_with_index do |fig, index|
    prod += (index + add) * fig.to_i
  end
  prod
end

#check_digitObject



6
7
8
# File 'lib/valvat/checksum/bg.rb', line 6

def check_digit
  natural_person? ? check_digit_natural_person : check_digit_legal_person
end

#check_digit_foreign_natural_personObject



29
30
31
32
33
34
35
# File 'lib/valvat/checksum/bg.rb', line 29

def check_digit_foreign_natural_person
  weight = [21, 19, 17, 13, 11, 9, 7, 3, 1]

  figures.map do |fig|
    fig * weight.shift
  end.inject(:+).modulo(10)
end


37
38
39
40
41
42
43
44
45
46
# File 'lib/valvat/checksum/bg.rb', line 37

def check_digit_legal_person
  chk = build_prod % 11

  if chk == 10
    chk = build_prod(3) % 11
    chk = 0 if chk == 10
  end

  chk
end

#check_digit_local_natural_personObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/valvat/checksum/bg.rb', line 18

def check_digit_local_natural_person
  weight = [2, 4, 8, 5, 10, 9, 7, 3, 6]
  chk = figures.map do |fig|
    fig * weight.shift
  end.inject(:+).modulo(11)

  return chk if chk < 10

  0
end

#check_digit_natural_personObject



10
11
12
13
14
15
16
# File 'lib/valvat/checksum/bg.rb', line 10

def check_digit_natural_person
  local_person_chk = check_digit_local_natural_person

  return local_person_chk if given_check_digit == local_person_chk

  check_digit_foreign_natural_person
end

#natural_person?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/valvat/checksum/bg.rb', line 56

def natural_person?
  vat.to_s_wo_country.length == 10
end