Class: ACH::ACHFile
- Inherits:
-
Object
- Object
- ACH::ACHFile
- Includes:
- FieldIdentifiers
- Defined in:
- lib/ach/ach_file.rb
Constant Summary
Constants included from FieldIdentifiers
FieldIdentifiers::ENCODING_OPTIONS
Instance Attribute Summary collapse
-
#batches ⇒ Object
readonly
Returns the value of attribute batches.
-
#control ⇒ Object
readonly
Returns the value of attribute control.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
Instance Method Summary collapse
-
#initialize(data = nil) ⇒ ACHFile
constructor
A new instance of ACHFile.
- #parse(data) ⇒ Object
- #report ⇒ Object
- #to_s ⇒ Object
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 |
# File 'lib/ach/ach_file.rb', line 11 def initialize data=nil @batches = [] @header = Records::FileHeader.new @control = Records::FileControl.new if data parse(data) end end |
Instance Attribute Details
#batches ⇒ Object (readonly)
Returns the value of attribute batches.
7 8 9 |
# File 'lib/ach/ach_file.rb', line 7 def batches @batches end |
#control ⇒ Object (readonly)
Returns the value of attribute control.
9 10 11 |
# File 'lib/ach/ach_file.rb', line 9 def control @control end |
#header ⇒ Object (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
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 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 |
# File 'lib/ach/ach_file.rb', line 69 def parse data trace_number = 0 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_identification = line[40..49].strip.gsub(/\A1/, '') 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.effective_entry_date = 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.account_number = 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' # skip else raise "Didn't recognize type code #{type} for this line:\n#{line}" end end self.batches << batch unless batch.nil? batch.entries.each{ |entry| entry.trace_number = (trace_number += 1) } to_s end |
#report ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ach/ach_file.rb', line 50 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 << "" 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("\r\n") end |
#to_s ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ach/ach_file.rb', line 21 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 |