class Faker::Invoice
def amount_between(from: 0, to: 0)
-
(Integer)
-
Parameters:
-
to
(Integer
) -- Specifies upper limit. -
from
(Integer
) -- Specifies lower limit.
def amount_between(from: 0, to: 0) Faker::Base.rand_in_range(from, to).round(2) end
def calculate_weighted_sum(base, weight_factors)
Multipl. 1 2 1 2 1 2 1 2
Ref.num. 1 2 3 4 5 6 7 8
For example with 12345678, [1,2]
Calculates weighted sum
def calculate_weighted_sum(base, weight_factors) base.to_s.reverse.each_char.with_index.map do |digit, index| digit.to_i * weight_factors.at(index % weight_factors.length) end.reduce(:+) # reduce(:+) = sum() but with better ruby version support end
def creditor_reference(ref: '')
-
(String)
-
Parameters:
-
ref
(String
) -- Specifies reference base.
def creditor_reference(ref: '') ref = reference if ref.empty? "RF#{iban_checksum('RF', ref)}#{ref}" end
def iban_checksum(country_code, account)
Calculates the mandatory checksum in 3rd and 4th characters in IBAN format
def iban_checksum(country_code, account) # Converts letters to numbers according the iban rules, A=10..Z=35 account_to_number = "#{account}#{country_code}00".upcase.chars.map do |d| d =~ /[A-Z]/ ? (d.ord - 55).to_s : d end.join.to_i # This is answer to (iban_to_num + checksum) % 97 == 1 checksum = (1 - account_to_number) % 97 # Use leftpad to make the size always to 2 checksum.to_s.rjust(2, '0') end
def kidmod10(base)
def kidmod10(base) weighted_sum = calculate_weighted_sum(base, [1, 2]) mod10_remainder(weighted_sum) end
def method_731(base)
731 Method
def method_731(base) weighted_sum = calculate_weighted_sum(base, [7, 3, 1]) mod10_remainder(weighted_sum) end
def mod10_remainder(number)
def mod10_remainder(number) -number % 10 end
def reference(ref: '')
-
(String)
-
Parameters:
-
ref
(String
) -- Specifies reference base.
def reference(ref: '') pattern = fetch('invoice.reference.pattern') ref = Base.regexify(/#{pattern}/) if ref.empty? # If reference contains reserved '#' characters we need to calculate check_digits as well check_digit_match = ref.match(/#+/) if check_digit_match # Get the method for selected language check_digit_method = fetch('invoice.reference.check_digit_method') # Calculate the check digit with matching method name # Trim all '#' from the reference before calculating that check_digit = send(check_digit_method, ref.tr('#', '')) # Make sure that our check digit is as long as all of the '###' we found check_digit = check_digit.to_s.rjust(check_digit_match[0].length, '0') # Replace all of the ref = ref.sub(check_digit_match[0], check_digit) end ref end