mirror of
https://github.com/internetee/registry.git
synced 2025-08-04 00:42:04 +02:00
Use Estonian reference number format instead of ISO 11649
This commit is contained in:
parent
aa6e8f8a93
commit
5738c17731
25 changed files with 244 additions and 47 deletions
48
app/models/billing/reference_no/base.rb
Normal file
48
app/models/billing/reference_no/base.rb
Normal file
|
@ -0,0 +1,48 @@
|
|||
module Billing
|
||||
class ReferenceNo
|
||||
class Base
|
||||
def self.generate
|
||||
new(SecureRandom.random_number(1..1_000_000))
|
||||
end
|
||||
|
||||
def initialize(base)
|
||||
@base = base.to_s
|
||||
end
|
||||
|
||||
def check_digit
|
||||
amount = amount_after_multiplication
|
||||
next_number_ending_with_zero(amount) - amount
|
||||
end
|
||||
|
||||
def to_s
|
||||
base
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :base
|
||||
|
||||
def next_number_ending_with_zero(number)
|
||||
next_number = number
|
||||
|
||||
loop do
|
||||
next_number = next_number.next
|
||||
return next_number if next_number.to_s.end_with?('0')
|
||||
end
|
||||
end
|
||||
|
||||
def amount_after_multiplication
|
||||
multipliers = [7, 3, 1]
|
||||
enumerator = multipliers.cycle
|
||||
amount = 0
|
||||
|
||||
base.reverse.each_char do |char|
|
||||
digit = char.to_i
|
||||
amount += (digit * enumerator.next)
|
||||
end
|
||||
|
||||
amount
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue