Class: Faker::Invoice

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/default/invoice.rb

Constant Summary

Constants inherited from Base

Base::LLetters, Base::Letters, Base::NOT_GIVEN, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, generate, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, translate, unique, with_locale

Class Method Details

.amount_between(from: 0, to: 0) ⇒ Integer

Produces a random amount between values with 2 decimals

Examples:

Faker::Finance.amount_between #=> 0
Faker::Finance.amount_between(0, 10) #=> 4.33

Parameters:

  • from (Integer) (defaults to: 0)

    Specifies lower limit.

  • to (Integer) (defaults to: 0)

    Specifies upper limit.

Returns:

  • (Integer)


20
21
22
# File 'lib/faker/default/invoice.rb', line 20

def amount_between(from: 0, to: 0)
  Faker::Base.rand_in_range(from, to).round(2)
end

.creditor_reference(ref: '') ⇒ String

Produces a random valid reference accoring to the International bank slip reference en.wikipedia.org/wiki/Creditor_Reference

Examples:

Faker::Invoice.creditor_reference #=> "RF34118592570724925498"

Parameters:

  • ref (String) (defaults to: '')

    Specifies reference base.

Returns:



34
35
36
37
38
# File 'lib/faker/default/invoice.rb', line 34

def creditor_reference(ref: '')
  ref = reference if ref.empty?

  "RF#{iban_checksum('RF', ref)}#{ref}"
end

.reference(ref: '') ⇒ String

Produces a random valid reference.

Examples:

Faker::Invoice.reference #=> "45656646957845"

Parameters:

  • ref (String) (defaults to: '')

    Specifies reference base.

Returns:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/faker/default/invoice.rb', line 50

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