class Faker::Invoice
Public Class Methods
Produces a random amount between values with 2 decimals
@param from [Integer] Specifies lower limit. @param to [Integer] Specifies upper limit. @return [Integer]
@example
Faker::Finance.amount_between #=> 0 Faker::Finance.amount_between(0, 10) #=> 4.33
@faker.version 1.9.0
# File lib/faker/default/invoice.rb, line 20 def amount_between(legacy_from = NOT_GIVEN, legacy_to = NOT_GIVEN, from: 0, to: 0) warn_for_deprecated_arguments do |keywords| keywords << :from if legacy_from != NOT_GIVEN keywords << :to if legacy_to != NOT_GIVEN end Faker::Base.rand_in_range(from, to).round(2) end
Produces a random valid reference accoring to the International bank slip reference en.wikipedia.org/wiki/Creditor_Reference
@param ref [String] Specifies reference base. @return [String]
@example
Faker::Invoice.creditor_reference #=> "RF34118592570724925498"
@faker.version 1.9.0
# File lib/faker/default/invoice.rb, line 39 def creditor_reference(legacy_ref = NOT_GIVEN, ref: '') warn_for_deprecated_arguments do |keywords| keywords << :ref if legacy_ref != NOT_GIVEN end ref = reference if ref.empty? "RF#{iban_checksum('RF', ref)}#{ref}" end
Produces a random valid reference.
@param ref [String] Specifies reference base. @return [String]
@example
Faker::Invoice.reference #=> "45656646957845"
@faker.version 1.9.0
# File lib/faker/default/invoice.rb, line 59 def reference(legacy_ref = NOT_GIVEN, ref: '') warn_for_deprecated_arguments do |keywords| keywords << :ref if legacy_ref != NOT_GIVEN end 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
Private Class Methods
Calculates weigthed sum
For example with 12345678, [1,2] Ref.num. 1 2 3 4 5 6 7 8 Multipl. 1 2 1 2 1 2 1 2 Total 1+ 4+ 3+ 8+ 5+1+2+ 7+1+6 = 38
# File lib/faker/default/invoice.rb, line 124 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
Calculates the mandatory checksum in 3rd and 4th characters in IBAN format source: en.wikipedia.org/wiki/International_Bank_Account_Number#Validating_the_IBAN
# File lib/faker/default/invoice.rb, line 92 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
Norsk Modulus 10 - KIDMOD10
# File lib/faker/default/invoice.rb, line 113 def kidmod10(base) weighted_sum = calculate_weighted_sum(base, [1, 2]) mod10_remainder(weighted_sum) end
731 Method Source: wiki.xmldation.com/support/fk/finnish_reference_number
# File lib/faker/default/invoice.rb, line 107 def method_731(base) weighted_sum = calculate_weighted_sum(base, [7, 3, 1]) mod10_remainder(weighted_sum) end
MOD-10 - remainder
# File lib/faker/default/invoice.rb, line 131 def mod10_remainder(number) -number % 10 end