Class: ACH::ACHFile

Inherits:
Object
  • Object
show all
Includes:
FieldIdentifiers
Defined in:
lib/ach/ach_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FieldIdentifiers

#const_field, #field, #left_justify, #routing_field, #spaceless_routing_field

Constructor Details

#initializeACHFile

Returns a new instance of ACHFile.



9
10
11
12
13
# File 'lib/ach/ach_file.rb', line 9

def initialize
  @batches = []
  @header = Records::FileHeader.new
  @control = Records::FileControl.new
end

Instance Attribute Details

#batchesObject (readonly)

Returns the value of attribute batches.



5
6
7
# File 'lib/ach/ach_file.rb', line 5

def batches
  @batches
end

#controlObject (readonly)

Returns the value of attribute control.



7
8
9
# File 'lib/ach/ach_file.rb', line 7

def control
  @control
end

#headerObject (readonly)

Returns the value of attribute header.



6
7
8
# File 'lib/ach/ach_file.rb', line 6

def header
  @header
end

Instance Method Details

#reportObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ach/ach_file.rb', line 43

def report
  to_s # To ensure correct records
  lines = []
  
  @batches.each do | batch |
    batch.entries.each do | entry |
      lines << left_justify(entry.individual_name + ": ", 25) +
          sprintf("% 7d.%02d", entry.amount / 100, entry.amount % 100)
    end
  end
  lines.join("\r\n")
end

#to_sObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ach/ach_file.rb', line 15

def to_s
  records = []
  records << @header
  @batches.each { |b| records += b.to_ach }
  records << @control
  
  nines_needed = 10 - (records.length % 10)
  nines_needed = nines_needed % 10
  nines_needed.times { records << Records::Nines.new() }
  
  @control.batch_count = @batches.length
  @control.block_count = (records.length / 10).ceil
  
  @control.entry_count = 0
  @control.debit_total = 0
  @control.credit_total = 0
  @control.entry_hash = 0
  
  @batches.each do | batch |
    @control.entry_count += batch.entries.length
    @control.debit_total += batch.control.debit_total
    @control.credit_total += batch.control.credit_total
    @control.entry_hash += batch.control.entry_hash
  end
  
  records.collect { |r| r.to_ach }.join("\r\n") + "\r\n"
end