class Aba::Batch

Attributes

bsb[RW]
description[RW]
entries[RW]
financial_institution[RW]
process_at[RW]
user_id[RW]
user_name[RW]

Public Class Methods

new(attrs = {}, transactions = []) { |self| ... } click to toggle source
# File lib/aba/batch.rb, line 32
def initialize(attrs = {}, transactions = [])
  attrs.each do |key, value|
    send("#{key}=", value)
  end

  @entries = []

  unless transactions.nil? || transactions.empty?
    transactions.to_a.each do |t|
      self.add_transaction(t) unless t.nil? || t.empty?
    end
  end

  yield self if block_given?
end

Public Instance Methods

add_return(attrs = {}) click to toggle source
# File lib/aba/batch.rb, line 66
def add_return(attrs = {})
  add_entry(Aba::Return, attrs)
end
add_transaction(attrs = {}) click to toggle source
# File lib/aba/batch.rb, line 62
def add_transaction(attrs = {})
  add_entry(Aba::Transaction, attrs)
end
errors() click to toggle source
# File lib/aba/batch.rb, line 82
def errors
  # Run validations
  has_errors?
  has_entry_errors?

  # Build errors
  all_errors = {}
  all_errors[:aba] = self.error_collection unless self.error_collection.empty?
  entry_error_collection = entries.each_with_index.map { |t, i| [i, t.error_collection] }.reject { |e| e[1].nil? || e[1].empty? }.to_h
  all_errors[:entries] = entry_error_collection unless entry_error_collection.empty?

  all_errors unless all_errors.empty?
end
to_s() click to toggle source
# File lib/aba/batch.rb, line 48
def to_s
  raise RuntimeError, 'No entries present - add one using `add_transaction` or `add_return`' if entries.empty?
  raise RuntimeError, 'ABA data is invalid - check the contents of `errors`' unless valid?

  # Descriptive record
  output = "#{descriptive_record}\r\n"

  # Transactions records
  output += entries.map { |t| t.to_s }.join("\r\n")

  # Batch control record
  output += "\r\n#{batch_control_record}"
end
transactions() click to toggle source
# File lib/aba/batch.rb, line 70
def transactions
  entries.select { |entry| entry.instance_of?(Aba::Transaction) }
end
transactions_valid?() click to toggle source
# File lib/aba/batch.rb, line 74
def transactions_valid?
  !transactions.map { |t| t.valid? }.include?(false)
end
valid?() click to toggle source
# File lib/aba/batch.rb, line 78
def valid?
  !has_errors? && !has_entry_errors?
end

Private Instance Methods

add_entry(type, attrs) click to toggle source
# File lib/aba/batch.rb, line 98
def add_entry(type, attrs)
  (attrs.instance_of?(type) ? attrs : type.new(attrs)).tap do |entry|
    entries << entry
  end
end
batch_control_record() click to toggle source
# File lib/aba/batch.rb, line 167
def batch_control_record
  credit_total_amount = 0
  debit_total_amount  = 0

  entries.each do |entry|
    if entry.debit?
      debit_total_amount += Integer(entry.amount).abs
    else
      credit_total_amount += Integer(entry.amount).abs
    end
  end

  # Record type
  # Max: 1
  # Char position: 1
  output = "7"

  # BSB Format Filler
  # Max: 7
  # Char position: 2-8
  output += "999-999"

  # Reserved
  # Max: 12
  # Char position: 9-20
  output += " " * 12

  # Net total
  # Max: 10
  # Char position: 21-30
  output += (credit_total_amount - debit_total_amount).abs.to_s.rjust(10, "0")

  # Credit Total Amount
  # Max: 10
  # Char position: 31-40
  output += credit_total_amount.to_s.rjust(10, "0")

  # Debit Total Amount
  # Max: 10
  # Char position: 41-50
  output += debit_total_amount.to_s.rjust(10, "0")

  # Reserved
  # Max: 24
  # Char position: 51-74
  output += " " * 24

  # Total Item Count
  # Max: 6
  # Char position: 75-80
  output += entries.size.to_s.rjust(6, "0")

  # Reserved
  # Max: 40
  # Char position: 81-120
  output += " " * 40
end
descriptive_record() click to toggle source
# File lib/aba/batch.rb, line 108
def descriptive_record
  # Record type
  # Max: 1
  # Char position: 1
  output = "0"

  # Optional branch number of the funds account with a hyphen in the 4th character position
  # Char position: 2-18
  # Max: 17
  # Blank filled
  output += self.bsb.nil? ? " " * 17 : self.bsb.to_s.ljust(17)

  # Sequence number
  # Char position: 19-20
  # Max: 2
  # Zero padded
  output += "01"

  # Name of user financial instituion
  # Max: 3
  # Char position: 21-23
  output += self.financial_institution.to_s

  # Reserved
  # Max: 7
  # Char position: 24-30
  output += " " * 7

  # Name of User supplying File
  # Char position: 31-56
  # Max: 26
  # Full BECS character set valid
  # Blank filled
  output += self.user_name.to_s.ljust(26)

  # Direct Entry User ID
  # Char position: 57-62
  # Max: 6
  # Zero padded
  output += self.user_id.to_s.rjust(6, "0")

  # Description of payments in the file (e.g. Payroll, Creditors etc.)
  # Char position: 63-74
  # Max: 12
  # Full BECS character set valid
  # Blank filled
  output += self.description.to_s.ljust(12)

  # Date on which the payment is to be processed
  # Char position: 75-80
  # Max: 6
  output += self.process_at.to_s.rjust(6, "0")

  # Reserved
  # Max: 40
  # Char position: 81-120
  output += " " * 40
end
has_entry_errors?() click to toggle source
# File lib/aba/batch.rb, line 104
def has_entry_errors?
  entries.map { |t| t.valid? }.include?(false)
end