Class: ACH::ACHFile

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

Constant Summary

Constants included from FieldIdentifiers

FieldIdentifiers::ENCODING_OPTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FieldIdentifiers

#const_field, #field, #left_justify, #spaceless_routing_field

Constructor Details

#initialize(data = nil) ⇒ ACHFile

Returns a new instance of ACHFile.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ach/ach_file.rb', line 11

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

  if data
    if (data.encode(Encoding.find('ASCII'), **ENCODING_OPTIONS) =~ /\n|\r\n/).nil?
      parse_fixed(data)
    else
      parse(data)
    end
  end
end

Instance Attribute Details

#batchesObject (readonly)

Returns the value of attribute batches.



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

def batches
  @batches
end

#controlObject (readonly)

Returns the value of attribute control.



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

def control
  @control
end

#headerObject (readonly)

Returns the value of attribute header.



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

def header
  @header
end

Instance Method Details

#parse(data) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/ach/ach_file.rb', line 86

def parse data
  fh =  self.header
  batch = nil
  bh = nil
  ed = nil

  data.strip.split(/\n|\r\n/).each do |line|
    type = line[0].chr
    case type
    when '1'
      fh.immediate_destination          = line[03..12].strip
      fh.immediate_origin               = line[13..22].strip
      fh.transmission_datetime          = Time.utc('20'+line[23..24], line[25..26], line[27..28], line[29..30], line[31..32])
      fh.file_id_modifier               = line[33..33]
      fh.immediate_destination_name     = line[40..62].strip
      fh.immediate_origin_name          = line[63..85].strip
      fh.reference_code                 = line[86..93].strip
    when '5'
      self.batches << batch unless batch.nil?
      batch = ACH::Batch.new
      bh = batch.header
      bh.company_name                   = line[4..19].strip
      bh.company_discretionary_data     = line[20..39].strip
      bh.company_identification         = line[40..49].gsub(/\A1/, '')

      # Does not try to guess if company identification is an EIN
      # TODO fix differently when I feel like breaking backwards
      # compatibility.
      bh.full_company_identification    = line[40..49]
      bh.standard_entry_class_code      = line[50..52].strip
      bh.company_entry_description      = line[53..62].strip
      bh.company_descriptive_date       = Date.parse(line[63..68]) rescue nil # this can be various formats
      bh.           = Date.parse(line[69..74])
      bh.originating_dfi_identification = line[79..86].strip
    when '6'
      ed = ACH::CtxEntryDetail.new
      ed.transaction_code               = line[1..2]
      ed.routing_number                 = line[3..11]
      ed.                 = line[12..28].strip
      ed.amount                         = line[29..38].to_i # cents
      ed.individual_id_number           = line[39..53].strip
      ed.individual_name                = line[54..75].strip
      ed.originating_dfi_identification = line[79..86]
      ed.trace_number                   = line[87..93].to_i
      batch.entries << ed
    when '7'
      type_code = line[1..2]
      ad = case type_code
      when '98'
        ACH::Addendum::NotificationOfChange.new
      when '99'
        ACH::Addendum::Return.new
      else
        ACH::Addendum.new
      end
      ad.type_code                      = type_code
      ad.payment_data                   = line[3..82].strip
      ad.sequence_number                = line[83..86].strip.to_i
      ad.entry_detail_sequence_number   = line[87..93].to_i
      ed.addenda << ad
    when '8'
      # skip
    when '9'
      @control = Records::FileControl.new
      @control.filler = line[55..93]
    else
      raise UnrecognizedTypeCode, "Didn't recognize type code #{type} for this line:\n#{line}"
    end
  end

  self.batches << batch unless batch.nil?
  to_s
end

#parse_fixed(data) ⇒ Object



80
81
82
83
84
# File 'lib/ach/ach_file.rb', line 80

def parse_fixed data
  # replace with a space to preserve the record-lengths
  encoded_data = data.encode(Encoding.find('ASCII'),{:invalid => :replace, :undef => :replace, :replace => ' '})
  parse encoded_data.scan(/.{94}/).join("\n")
end

#report(eol: ACH.eol) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ach/ach_file.rb', line 61

def report eol: ACH.eol
  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 << ""
  lines << left_justify("Debit Total: ", 25) +
      sprintf("% 7d.%02d", @control.debit_total / 100, @control.debit_total % 100)
  lines << left_justify("Credit Total: ", 25) +
      sprintf("% 7d.%02d", @control.credit_total / 100, @control.credit_total % 100)

  lines.join(eol)
end

#to_s(eol = ACH.eol) ⇒ Object

Parameters:

  • eol (String) (defaults to: ACH.eol)

    Line ending, default to CRLF



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ach/ach_file.rb', line 27

def to_s eol = ACH.eol
  records = []
  records << @header

  @batches.each_with_index do |batch, index|
    batch.header.batch_number ||= index + 1
    records += batch.to_ach
  end
  records << @control

  records_count = records.map(&:records_count).reduce(:+)
  nines_needed = (10 - records_count) % 10
  nines_needed = nines_needed % 10
  nines_needed.times { records << Records::Nines.new() }

  records_count = records.map(&:records_count).reduce(:+)
  @control.batch_count = @batches.length
  @control.block_count = (records_count / 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.inject(0) { |total, entry| total + entry.records_count }
    @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(eol: eol) }.join(eol) + eol
end