forked from moov-io/ach
/
reader.go
733 lines (667 loc) · 19.8 KB
/
reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
// Licensed to The Moov Authors under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. The Moov Authors licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package ach
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"unicode/utf8"
"github.com/moov-io/base"
)
var (
// maxLines is the maximum number of lines a file can have. It is limited by the
// EntryAddendaCount field which has 8 digits, and the BatchCount field which has
// 6 digits in the File Control Record. So we can have at most the 2 file records,
// 2 records for each of 10^6 batches, 10^8 entry and addenda records, and 8 lines
// of 9's to round up to the nearest multiple of 10.
maxLines = 2 + 2000000 + 100000000 + 8
)
// Reader reads records from an ACH-encoded file.
type Reader struct {
// file is ach.file model being built as r is parsed.
File File
// IATCurrentBatch is the current IATBatch entries being parsed
IATCurrentBatch IATBatch
// r handles the IO.Reader sent to be parser.
scanner *bufio.Scanner
// line is the current line being parsed from the input r
line string
// currentBatch is the current Batch entries being parsed
currentBatch Batcher
// line number of the file being parsed
lineNum int
// recordName holds the current record name being parsed.
recordName string
// errors holds each error encountered when attempting to parse the file
errors base.ErrorList
}
// error returns a new ParseError based on err
func (r *Reader) parseError(err error) error {
if err == nil {
return nil
}
if _, ok := err.(*base.ParseError); ok {
return err
}
return &base.ParseError{
Line: r.lineNum,
Record: r.recordName,
Err: err,
}
}
// addCurrentBatch creates the current batch type for the file being read. A successful
// current batch will be added to r.File once parsed.
func (r *Reader) addCurrentBatch(batch Batcher) {
r.currentBatch = batch
}
// addCurrentBatch creates the current batch type for the file being read. A successful
// current batch will be added to r.File once parsed.
func (r *Reader) addIATCurrentBatch(iatBatch IATBatch) {
r.IATCurrentBatch = iatBatch
}
// SetValidation stores ValidateOpts on the Reader's underlying File which are to be used
// to override the default NACHA validation rules.
func (r *Reader) SetValidation(opts *ValidateOpts) {
if r == nil || opts == nil {
return
}
r.File.SetValidation(opts)
}
// ReadFile attempts to open a file at path and read the contents before closing
// and returning the parsed ACH File.
func ReadFile(path string) (*File, error) {
fd, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("problem reading %s: %v", path, err)
}
defer fd.Close()
file, err := NewReader(fd).Read()
return &file, err
}
// ReadFiles attempts to open files at the given paths and read the contents
// of each before closing and returning the parsed ACH Files.
func ReadFiles(paths []string) ([]*File, error) {
var out []*File
for i := range paths {
file, err := ReadFile(paths[i])
if err != nil {
return nil, err
}
if file != nil {
out = append(out, file)
}
}
return out, nil
}
// NewReader returns a new ACH Reader that reads from r.
func NewReader(r io.Reader) *Reader {
return &Reader{
scanner: bufio.NewScanner(r),
}
}
// Read reads each line in the underlying io.Reader and returns a File and any errors encountered.
//
// Read enforces ACH formatting rules and the first character of each line determines which parser is used.
//
// The returned File may not be valid. Callers should tabulate the File with File.Create followed by
// File.Validate to ensure it is Nacha compliant.
//
// Invalid files may be rejected by other financial institutions or ACH tools.
func (r *Reader) Read() (File, error) {
r.lineNum = 0
// read through the entire file
for r.scanner.Scan() {
line := r.scanner.Text()
r.lineNum++
if r.lineNum > maxLines {
r.errors.Add(ErrFileTooLong)
return r.File, r.errors
}
lineLength := utf8.RuneCountInString(line)
switch {
case r.lineNum == 1 && lineLength > RecordLength:
extraChars := lineLength % RecordLength
if extraChars != 0 {
err := fmt.Errorf(
"%d extra character(s) in ACH file: must be %d but found %d",
extraChars,
lineLength-extraChars,
lineLength,
)
r.errors.Add(r.parseError(err))
} else if err := r.processFixedWidthFile(&line); err != nil {
r.errors.Add(err)
}
case lineLength != RecordLength:
if lineLength > RecordLength {
line = trimSpacesFromLongLine(line)
}
// right-pad the line with spaces
line, err := rightPadShortLine(line)
if err != nil {
r.errors.Add(r.parseError(err))
}
r.line = line
// parse the line
if err := r.parseLine(); err != nil {
r.errors.Add(r.parseError(NewRecordWrongLengthErr(lineLength)))
r.errors.Add(err)
}
default:
r.line = line
if err := r.parseLine(); err != nil {
r.errors.Add(err)
}
}
}
// Add a lingering Batch to the file if there was no BatchControl record.
// This is common when files just contain a BatchHeader and EntryDetail records.
if r.currentBatch != nil {
r.File.AddBatch(r.currentBatch)
r.currentBatch = nil
}
// Carry through any ValidateOpts for this comparison
if (FileHeader{validateOpts: r.File.validateOpts}) == r.File.Header {
// There must be at least one File Header
r.recordName = "FileHeader"
r.errors.Add(ErrFileHeader)
}
if !r.File.IsADV() {
if (FileControl{}) == r.File.Control {
// There must be at least one File Control
r.recordName = "FileControl"
r.errors.Add(ErrFileControl)
}
} else {
if (ADVFileControl{}) == r.File.ADVControl {
// There must be at least one File Control
r.recordName = "FileControl"
r.errors.Add(ErrFileControl)
}
}
if r.errors.Empty() {
return r.File, nil
}
return r.File, r.errors
}
func trimSpacesFromLongLine(s string) string {
return strings.TrimSuffix(s[:94], " ")
}
func rightPadShortLine(s string) (string, error) {
if n := len(s); n > RecordLength {
return s, NewRecordWrongLengthErr(n)
}
return s + strings.Repeat(" ", 94-len(s)), nil
}
func (r *Reader) processFixedWidthFile(line *string) error {
// It should be safe to parse this byte by byte since ACH files are ASCII only.
record := ""
for i, c := range *line {
record = record + string(c)
if i > 0 && (i+1)%RecordLength == 0 {
r.line = record
if err := r.parseLine(); err != nil {
return err
}
record = ""
}
}
return nil
}
func (r *Reader) parseLine() error {
switch r.line[:1] {
case fileHeaderPos:
if err := r.parseFileHeader(); err != nil {
return err
}
case batchHeaderPos:
if err := r.parseBH(); err != nil {
return err
}
case entryDetailPos:
if err := r.parseED(); err != nil {
return err
}
case entryAddendaPos:
if err := r.parseEDAddenda(); err != nil {
return err
}
case batchControlPos:
if err := r.parseBatchControl(); err != nil {
return err
}
if r.currentBatch != nil {
batch := r.currentBatch
r.currentBatch = nil
batch.SetValidation(r.File.validateOpts)
r.File.AddBatch(batch)
if err := batch.Validate(); err != nil {
r.recordName = "Batches"
return r.parseError(err)
}
} else {
batch := r.IATCurrentBatch
r.IATCurrentBatch = IATBatch{}
r.File.AddIATBatch(batch)
if err := batch.Validate(); err != nil {
r.recordName = "Batches"
return r.parseError(err)
}
}
case fileControlPos:
if r.line[:2] == "99" {
// final blocking padding
break
}
if err := r.parseFileControl(); err != nil {
return err
}
default:
return NewErrUnknownRecordType(r.line[:1])
}
return nil
}
// parseBH parses determines whether to parse an IATBatchHeader or BatchHeader
func (r *Reader) parseBH() error {
if r.line[50:53] == IAT || strings.TrimSpace(r.line[04:20]) == IATCOR {
if err := r.parseIATBatchHeader(); err != nil {
return err
}
} else {
if err := r.parseBatchHeader(); err != nil {
return err
}
}
return nil
}
// parseEd parses determines whether to parse an IATEntryDetail or EntryDetail
func (r *Reader) parseED() error {
if r.IATCurrentBatch.Header != nil {
if err := r.parseIATEntryDetail(); err != nil {
return err
}
} else {
if err := r.parseEntryDetail(); err != nil {
return err
}
}
return nil
}
// parseEd parses determines whether to parse an IATEntryDetail Addenda or EntryDetail Addenda
func (r *Reader) parseEDAddenda() error {
if r.currentBatch != nil && r.currentBatch.GetHeader().CompanyName != IATCOR {
if err := r.parseAddenda(); err != nil {
return err
}
} else {
if err := r.parseIATAddenda(); err != nil {
return err
}
}
return nil
}
// parseFileHeader takes the input record string and parses the FileHeaderRecord values
func (r *Reader) parseFileHeader() error {
r.recordName = "FileHeader"
// Pass through any ValidateOpts from the Reader for this comparison
// as we need to compare the other struct fields (e.g. origin, destination)
if (FileHeader{validateOpts: r.File.validateOpts}) != r.File.Header {
// There can only be one File Header per File exit
return ErrFileHeader
}
r.File.Header.Parse(r.line)
if err := r.File.Header.Validate(); err != nil {
return r.parseError(err)
}
return nil
}
// parseBatchHeader takes the input record string and parses the FileHeaderRecord values
func (r *Reader) parseBatchHeader() error {
r.recordName = "BatchHeader"
if r.currentBatch != nil {
// batch header inside of current batch
return ErrFileBatchHeaderInsideBatch
}
// Ensure we have a valid batch header before building a batch.
bh := NewBatchHeader()
bh.Parse(r.line)
if err := bh.Validate(); err != nil {
return r.parseError(err)
}
// Passing BatchHeader into NewBatch creates a Batcher of SEC code type.
batch, err := NewBatch(bh)
if err != nil {
return r.parseError(err)
}
r.addCurrentBatch(batch)
return nil
}
// parseEntryDetail takes the input record string and parses the EntryDetailRecord values
func (r *Reader) parseEntryDetail() error {
r.recordName = "EntryDetail"
if r.currentBatch == nil {
return ErrFileEntryOutsideBatch
}
if r.currentBatch.GetHeader().StandardEntryClassCode != ADV {
ed := new(EntryDetail)
ed.Parse(r.line)
if err := ed.Validate(); err != nil {
return r.parseError(err)
}
r.currentBatch.AddEntry(ed)
} else {
ed := new(ADVEntryDetail)
ed.Parse(r.line)
if err := ed.Validate(); err != nil {
return r.parseError(err)
}
r.currentBatch.AddADVEntry(ed)
}
return nil
}
// parseAddendaRecord takes the input record string and create an Addenda Type appended to the last EntryDetail
func (r *Reader) parseAddenda() error {
r.recordName = "Addenda"
if r.currentBatch == nil {
return ErrFileAddendaOutsideBatch
}
if r.currentBatch.GetHeader().StandardEntryClassCode != ADV {
if len(r.currentBatch.GetEntries()) == 0 {
return ErrFileAddendaOutsideEntry
}
entryIndex := len(r.currentBatch.GetEntries()) - 1
entry := r.currentBatch.GetEntries()[entryIndex]
if entry.AddendaRecordIndicator == 1 {
switch r.line[1:3] {
case "02":
addenda02 := NewAddenda02()
addenda02.Parse(r.line)
if err := addenda02.Validate(); err != nil {
return r.parseError(err)
}
r.currentBatch.GetEntries()[entryIndex].Addenda02 = addenda02
case "05":
addenda05 := NewAddenda05()
addenda05.Parse(r.line)
if err := addenda05.Validate(); err != nil {
return r.parseError(err)
}
r.currentBatch.GetEntries()[entryIndex].AddAddenda05(addenda05)
case "98":
addenda98 := NewAddenda98()
addenda98.Parse(r.line)
if err := addenda98.Validate(); err != nil {
return r.parseError(err)
}
r.currentBatch.GetEntries()[entryIndex].Category = CategoryNOC
r.currentBatch.GetEntries()[entryIndex].Addenda98 = addenda98
case "99":
addenda99 := NewAddenda99()
addenda99.Parse(r.line)
addenda99.SetValidation(r.File.validateOpts)
if err := addenda99.Validate(); err != nil {
return r.parseError(err)
}
r.currentBatch.GetEntries()[entryIndex].Category = CategoryReturn
r.currentBatch.GetEntries()[entryIndex].Addenda99 = addenda99
}
} else {
return r.parseError(r.currentBatch.Error("AddendaRecordIndicator", ErrBatchAddendaIndicator))
}
} else {
if err := r.parseADVAddenda(); err != nil {
return err
}
}
return nil
}
// parseADVAddenda takes the input record string and create an Addenda99 appended to the last ADVEntryDetail
func (r *Reader) parseADVAddenda() error {
if len(r.currentBatch.GetADVEntries()) == 0 {
return ErrFileAddendaOutsideEntry
}
entryIndex := len(r.currentBatch.GetADVEntries()) - 1
entry := r.currentBatch.GetADVEntries()[entryIndex]
if entry.AddendaRecordIndicator != 1 {
return r.parseError(r.currentBatch.Error("AddendaRecordIndicator", ErrBatchAddendaIndicator))
}
addenda99 := NewAddenda99()
addenda99.Parse(r.line)
if err := addenda99.Validate(); err != nil {
return r.parseError(err)
}
r.currentBatch.GetADVEntries()[entryIndex].Category = CategoryReturn
r.currentBatch.GetADVEntries()[entryIndex].Addenda99 = addenda99
return nil
}
// parseBatchControl takes the input record string and parses the BatchControlRecord values
func (r *Reader) parseBatchControl() error {
r.recordName = "BatchControl"
if r.currentBatch == nil && r.IATCurrentBatch.GetEntries() == nil {
// batch Control without a current batch
return ErrFileBatchControlOutsideBatch
}
if r.currentBatch != nil {
if r.currentBatch.GetHeader().StandardEntryClassCode == ADV {
r.currentBatch.GetADVControl().Parse(r.line)
if err := r.currentBatch.GetADVControl().Validate(); err != nil {
return r.parseError(err)
}
} else {
r.currentBatch.GetControl().Parse(r.line)
if err := r.currentBatch.GetControl().Validate(); err != nil {
return r.parseError(err)
}
}
} else {
r.IATCurrentBatch.GetControl().Parse(r.line)
if err := r.IATCurrentBatch.GetControl().Validate(); err != nil {
return r.parseError(err)
}
}
return nil
}
// parseFileControl takes the input record string and parses the FileControlRecord values
func (r *Reader) parseFileControl() error {
r.recordName = "FileControl"
if !r.File.IsADV() {
if (FileControl{}) != r.File.Control {
// Can be only one file control per file
return ErrFileControl
}
r.File.Control.Parse(r.line)
if err := r.File.Control.Validate(); err != nil {
return r.parseError(err)
}
} else {
if (ADVFileControl{}) != r.File.ADVControl {
// Can be only one file control per file
return ErrFileControl
}
r.File.ADVControl.Parse(r.line)
if err := r.File.ADVControl.Validate(); err != nil {
return r.parseError(err)
}
}
return nil
}
// IAT specific reader functions
// parseIATBatchHeader takes the input record string and parses the FileHeaderRecord values
func (r *Reader) parseIATBatchHeader() error {
r.recordName = "BatchHeader"
if r.IATCurrentBatch.Header != nil {
// batch header inside of current batch
return ErrFileBatchHeaderInsideBatch
}
// Ensure we have a valid IAT BatchHeader before building a batch.
bh := NewIATBatchHeader()
bh.Parse(r.line)
if err := bh.Validate(); err != nil {
return r.parseError(err)
}
// Passing BatchHeader into NewBatchIAT creates a Batcher of IAT SEC code type.
iatBatch := NewIATBatch(bh)
r.addIATCurrentBatch(iatBatch)
return nil
}
// parseIATEntryDetail takes the input record string and parses the EntryDetailRecord values
func (r *Reader) parseIATEntryDetail() error {
r.recordName = "EntryDetail"
if r.IATCurrentBatch.Header == nil {
return ErrFileEntryOutsideBatch
}
ed := new(IATEntryDetail)
ed.Parse(r.line)
if err := ed.Validate(); err != nil {
return r.parseError(err)
}
r.IATCurrentBatch.AddEntry(ed)
return nil
}
// parseIATAddenda takes the input record string and create an Addenda Type appended to the last EntryDetail
func (r *Reader) parseIATAddenda() error {
r.recordName = "Addenda"
if r.IATCurrentBatch.GetEntries() == nil {
return ErrFileAddendaOutsideEntry
}
entryIndex := len(r.IATCurrentBatch.GetEntries()) - 1
entry := r.IATCurrentBatch.GetEntries()[entryIndex]
if entry.AddendaRecordIndicator == 1 {
err := r.switchIATAddenda(entryIndex)
if err != nil {
return r.parseError(err)
}
} else {
return r.parseError(fieldError("AddendaRecordIndicator", ErrIATBatchAddendaIndicator))
}
return nil
}
func (r *Reader) switchIATAddenda(entryIndex int) error {
switch r.line[1:3] {
// IAT mandatory and optional Addenda
case "10", "11", "12", "13", "14", "15", "16", "17", "18":
err := r.mandatoryOptionalIATAddenda(entryIndex)
if err != nil {
return err
}
// IATNOC
case "98":
err := r.nocIATAddenda(entryIndex)
if err != nil {
return err
}
// IAT return Addenda
case "99":
err := r.returnIATAddenda(entryIndex)
if err != nil {
return err
}
}
return nil
}
// mandatoryOptionalIATAddenda parses and validates mandatory IAT addenda records: Addenda10,
// Addenda11, Addenda12, Addenda13, Addenda14, Addenda15, Addenda16, Addenda17, Addenda18
func (r *Reader) mandatoryOptionalIATAddenda(entryIndex int) error {
switch r.line[1:3] {
case "10":
addenda10 := NewAddenda10()
addenda10.Parse(r.line)
if err := addenda10.Validate(); err != nil {
return err
}
r.IATCurrentBatch.Entries[entryIndex].Addenda10 = addenda10
case "11":
addenda11 := NewAddenda11()
addenda11.Parse(r.line)
if err := addenda11.Validate(); err != nil {
return err
}
r.IATCurrentBatch.Entries[entryIndex].Addenda11 = addenda11
case "12":
addenda12 := NewAddenda12()
addenda12.Parse(r.line)
if err := addenda12.Validate(); err != nil {
return err
}
r.IATCurrentBatch.Entries[entryIndex].Addenda12 = addenda12
case "13":
addenda13 := NewAddenda13()
addenda13.Parse(r.line)
if err := addenda13.Validate(); err != nil {
return err
}
r.IATCurrentBatch.Entries[entryIndex].Addenda13 = addenda13
case "14":
addenda14 := NewAddenda14()
addenda14.Parse(r.line)
if err := addenda14.Validate(); err != nil {
return err
}
r.IATCurrentBatch.Entries[entryIndex].Addenda14 = addenda14
case "15":
addenda15 := NewAddenda15()
addenda15.Parse(r.line)
if err := addenda15.Validate(); err != nil {
return err
}
r.IATCurrentBatch.Entries[entryIndex].Addenda15 = addenda15
case "16":
addenda16 := NewAddenda16()
addenda16.Parse(r.line)
if err := addenda16.Validate(); err != nil {
return err
}
r.IATCurrentBatch.Entries[entryIndex].Addenda16 = addenda16
case "17":
addenda17 := NewAddenda17()
addenda17.Parse(r.line)
if err := addenda17.Validate(); err != nil {
return err
}
r.IATCurrentBatch.Entries[entryIndex].AddAddenda17(addenda17)
case "18":
addenda18 := NewAddenda18()
addenda18.Parse(r.line)
if err := addenda18.Validate(); err != nil {
return err
}
r.IATCurrentBatch.Entries[entryIndex].AddAddenda18(addenda18)
}
return nil
}
// nocIATAddenda parses and validates IAT NOC record Addenda98
func (r *Reader) nocIATAddenda(entryIndex int) error {
addenda98 := NewAddenda98()
addenda98.Parse(r.line)
if err := addenda98.Validate(); err != nil {
return err
}
r.IATCurrentBatch.Entries[entryIndex].Addenda98 = addenda98
return nil
}
// returnIATAddenda parses and validates IAT return record Addenda99
func (r *Reader) returnIATAddenda(entryIndex int) error {
addenda99 := NewAddenda99()
addenda99.Parse(r.line)
if err := addenda99.Validate(); err != nil {
return err
}
r.IATCurrentBatch.Entries[entryIndex].Addenda99 = addenda99
return nil
}