forked from moov-io/ach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.go
1156 lines (1068 loc) · 39.5 KB
/
batch.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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"unicode/utf8"
)
// Batch holds the Batch Header and Batch Control and all Entry Records
type Batch struct {
// id is a client defined string used as a reference to this record. accessed via ID/SetID
id string
Header *BatchHeader `json:"batchHeader,omitempty"`
Entries []*EntryDetail `json:"entryDetails,omitempty"`
Control *BatchControl `json:"batchControl,omitempty"`
ADVEntries []*ADVEntryDetail `json:"advEntryDetails,omitempty"`
ADVControl *ADVBatchControl `json:"advBatchControl,omitempty"`
// offset holds the information to build an EntryDetail record which
// balances the batch by debiting or crediting the sum of amounts in the batch.
offset *Offset
// category defines if the entry is a Forward, Return, or NOC
category string
// Converters is composed for ACH to GoLang Converters
converters
}
const (
// ACK ACH Payment Acknowledgment - A code that indicates acknowledgment of receipt of a corporate credit payment
// (CCD).
ACK = "ACK"
// ADV Automated Accounting Advice – A code that provide accounting information regarding an Entry. It is an
// optional service.
ADV = "ADV"
// ARC Accounts Receivable Entry – A code that indicates a consumer check converted to a one-time ACH debit.
// The Accounts Receivable (ARC) Entry provides initiates a single-entry ACH debit to customer accounts by
// converting checks at the point of receipt through the U.S. mail, at a drop box location or in-person for
// payment of a bill at a manned location.
ARC = "ARC"
// ATX Financial EDI Acknowledgment - A code that indicates acknowledgement by the Receiving Depository Financial
// Institution (RDFI) that a Corporate Credit Exchange (CTX) has been received.
ATX = "ATX"
// BOC Back Office Conversion Entry - A code that indicates single entry debit initiated at the point of purchase
// or at a manned bill payment location to transfer funds through conversion to an ACH debit entry during back
// office processing.
BOC = "BOC"
// CCD Corporate Credit or Debit Entry - A code that indicates an entry initiated by an Organization to transfer
// funds to or from an account of that Organization or another Organization. For commercial accounts only.
CCD = "CCD"
// CIE Customer Initiated Entry - A code that indicates a credit entry initiated on behalf of, and upon the
// instruction of, a consumer to transfer funds to a non-consumer Receiver.
CIE = "CIE"
// COR Notification of Change or Refused Notification of Change - A code used by an RDFI or ODFI when originating a
// Notification of Change or Refused Notification of Change in automated format.
COR = "COR"
// CTX Corporate Trade Exchange - A code that indicates the ability to collect and disburse funds and information
// between companies. Generally it is used by businesses paying one another for goods or services.
CTX = "CTX"
// DNE Death Notification Entry - A code that United States Federal agencies (e.g. Social Security) use to notify
// depository financial institutions that the recipient of government benefit payments has died.
DNE = "DNE"
// ENR Automated Enrollment Entry - A code indicating enrollment of a person with an agency of the US government
// for a depository financial institution.
ENR = "ENR"
// IAT International ACH Transaction - A code IAT indicating a credit or debit ACH entry that is part of a payment
// transaction involving a financial agency’s office (i.e., depository financial institution or business issuing
// money orders) that is not located in the territorial jurisdiction of the United States. IAT entries can be made
// to or from a corporate or consumer account and must be accompanied by seven (7) mandatory addenda records
// identifying the name and physical address of the Originator, name and physical address of the Receiver,
// Receiver’s account number, Receiver’s bank identity and reason for the payment.
IAT = "IAT"
// MTE Machine Transfer Entry - A code that indicates when a consumer uses their debit card at an Automated Teller
// Machine (ATM) to withdraw cash. MTE transactions cannot be aggregated together under a single Entry.
MTE = "MTE"
// POP Point of Purchase Entry - A code that indicates a check presented in-person to a merchant for purchase
// is presented as an ACH entry instead of a physical check.
POP = "POP"
// POS Point of Sale Entry - A code that indicates a debit entry initiated at an “electronic terminal” to a
// consumer account of the receiver to pay an obligation incurred in a point-of-sale transaction, or to effect a
// point-of-sale terminal cash withdrawal.
POS = "POS"
// PPD Prearranged Payment and Deposit Entry - A code tha indicates a an entry initiated by an organization based
// on a standing or a single entry authorization to transfer funds.
PPD = "PPD"
// RCK Re-presented Check Entry - A code that indicates a physical check that was presented but returned because of
//// insufficient funds may be represented as an ACH entry.
RCK = "RCK"
// SHR Shared Network Transaction - A code that indicates a debit Entry initiated at an “electronic terminal,” as
// that term is defined in Regulation E, to a Consumer Account of the Receiver to pay an obligation incurred in a
// point-of-sale transaction, or to effect a point-of-sale terminal cash withdrawal. Also an adjusting or other
// credit Entry related to such debit Entry, transfer of funds, or obligation. SHR Entries are initiated in a
// shared network where the ODFI and RDFI have an agreement in addition to these Rules to process such Entries.
SHR = "SHR"
// TEL Telephone Initiated Entry - A code indicating a Telephone-Initiated consumer debit transactions. The NACHA
// Operating Rules permit TEL entries when the originator obtains the Receiver’s authorization for the debit entry
// orally via the telephone. An entry based upon a Receiver’s oral authorization must utilize the TEL
// Standard Entry Class (SEC) Code.
TEL = "TEL"
// TRC Check Truncation Entry - is a code used to identify a debit entry of a truncated check.
TRC = "TRC"
// TRX Check Truncation Entries Exchange - used to identify a debit entry exchange of a truncated checks (multiple).
TRX = "TRX"
// WEB Internet-Initiated/Mobile Entry - A code indicating an entry submitted pursuant to an authorization obtained
// solely via the Internet or a mobile network. For consumer accounts only.
WEB = "WEB"
// XCK Destroyed Check Entry - A code indicating a debit entry initiated for a a destroyed check eligible items
XCK = "XCK"
)
func (batch *Batch) UnmarshalJSON(p []byte) error {
if batch != nil {
batch.Header = NewBatchHeader()
batch.Control = NewBatchControl()
batch.ADVControl = NewADVBatchControl()
}
type Alias Batch
aux := struct {
*Alias
}{
(*Alias)(batch),
}
if err := json.Unmarshal(p, &aux); err != nil {
return err
}
return nil
}
// NewBatch takes a BatchHeader and returns a matching SEC code batch type that is a batcher. Returns an error if the SEC code is not supported.
func NewBatch(bh *BatchHeader) (Batcher, error) {
if bh == nil {
return nil, errors.New("nil BatchHeader provided")
}
switch bh.StandardEntryClassCode {
case ACK:
return NewBatchACK(bh), nil
case ADV:
return NewBatchADV(bh), nil
case ARC:
return NewBatchARC(bh), nil
case ATX:
return NewBatchATX(bh), nil
case BOC:
return NewBatchBOC(bh), nil
case CCD:
return NewBatchCCD(bh), nil
case CIE:
return NewBatchCIE(bh), nil
case COR:
return NewBatchCOR(bh), nil
case CTX:
return NewBatchCTX(bh), nil
case DNE:
return NewBatchDNE(bh), nil
case ENR:
return NewBatchENR(bh), nil
case IAT:
return nil, ErrFileIATSEC
case MTE:
return NewBatchMTE(bh), nil
case POP:
return NewBatchPOP(bh), nil
case POS:
return NewBatchPOS(bh), nil
case PPD:
return NewBatchPPD(bh), nil
case RCK:
return NewBatchRCK(bh), nil
case SHR:
return NewBatchSHR(bh), nil
case TEL:
return NewBatchTEL(bh), nil
case TRC:
return NewBatchTRC(bh), nil
case TRX:
return NewBatchTRX(bh), nil
case WEB:
return NewBatchWEB(bh), nil
case XCK:
return NewBatchXCK(bh), nil
default:
}
return nil, NewErrFileUnknownSEC(bh.StandardEntryClassCode)
}
// ConvertBatchType will take a batch object and convert it into one of the correct batch type
func ConvertBatchType(b Batch) Batcher {
switch b.Header.StandardEntryClassCode {
case ACK:
return &BatchACK{b}
case ADV:
return &BatchADV{b}
case ARC:
return &BatchARC{b}
case ATX:
return &BatchATX{b}
case BOC:
return &BatchBOC{b}
case CCD:
return &BatchCCD{b}
case CIE:
return &BatchCIE{b}
case COR:
return &BatchCOR{b}
case CTX:
return &BatchCTX{b}
case DNE:
return &BatchDNE{b}
case ENR:
return &BatchENR{b}
case MTE:
return &BatchMTE{b}
case POP:
return &BatchPOP{b}
case POS:
return &BatchPOS{b}
case PPD:
return &BatchPPD{b}
case RCK:
return &BatchRCK{b}
case SHR:
return &BatchSHR{b}
case TEL:
return &BatchTEL{b}
case TRC:
return &BatchTRC{b}
case TRX:
return &BatchTRX{b}
case WEB:
return &BatchWEB{b}
case XCK:
return &BatchXCK{b}
default:
return &b
}
}
// Create will tabulate and assemble an ACH batch into a valid state. This includes
// setting any posting dates, sequence numbers, counts, and sums.
//
// Create implementations are free to modify computable fields in a file and should
// call the Batch's Validate() function at the end of their execution.
func (batch *Batch) Create() error {
return errors.New("use an implementation of batch or NewBatch")
}
// Validate checks properties of the ACH batch to ensure they match NACHA guidelines.
// This includes computing checksums, totals, and sequence orderings.
//
// Validate will never modify the batch.
func (batch *Batch) Validate() error {
return errors.New("use an implementation of batch or NewBatch")
}
// verify checks basic valid NACHA batch rules. Assumes properly parsed records. This does not mean it is a valid batch as validity is tied to each batch type
func (batch *Batch) verify() error {
// No entries in batch
if len(batch.Entries) <= 0 && len(batch.ADVEntries) <= 0 {
return batch.Error("entries", ErrBatchNoEntries)
}
// verify field inclusion in all the records of the batch.
if err := batch.isFieldInclusion(); err != nil {
// convert the field error in to a batch error for a consistent api
return batch.Error("FieldError", err)
}
if !batch.IsADV() {
// validate batch header and control codes are the same
if batch.Header.ServiceClassCode != batch.Control.ServiceClassCode {
return batch.Error("ServiceClassCode",
NewErrBatchHeaderControlEquality(batch.Header.ServiceClassCode, batch.Control.ServiceClassCode))
}
// Company Identification must match the Company ID from the batch header record
if batch.Header.CompanyIdentification != batch.Control.CompanyIdentification {
return batch.Error("CompanyIdentification",
NewErrBatchHeaderControlEquality(batch.Header.CompanyIdentification, batch.Control.CompanyIdentification))
}
// Control ODFIIdentification must be the same as batch header
if batch.Header.ODFIIdentification != batch.Control.ODFIIdentification {
return batch.Error("ODFIIdentification",
NewErrBatchHeaderControlEquality(batch.Header.ODFIIdentification, batch.Control.ODFIIdentification))
}
// batch number header and control must match
if batch.Header.BatchNumber != batch.Control.BatchNumber {
return batch.Error("BatchNumber",
NewErrBatchHeaderControlEquality(batch.Header.BatchNumber, batch.Control.BatchNumber))
}
} else {
if batch.Header.ServiceClassCode != batch.ADVControl.ServiceClassCode {
return batch.Error("ServiceClassCode",
NewErrBatchHeaderControlEquality(batch.Header.ServiceClassCode, batch.ADVControl.ServiceClassCode))
}
// Control ODFIIdentification must be the same as batch header
if batch.Header.ODFIIdentification != batch.ADVControl.ODFIIdentification {
return batch.Error("ODFIIdentification",
NewErrBatchHeaderControlEquality(batch.Header.ODFIIdentification, batch.ADVControl.ODFIIdentification))
}
// batch number header and control must match
if batch.Header.BatchNumber != batch.ADVControl.BatchNumber {
return batch.Error("BatchNumber",
NewErrBatchHeaderControlEquality(batch.Header.BatchNumber, batch.ADVControl.BatchNumber))
}
}
if err := batch.isBatchEntryCount(); err != nil {
return err
}
if err := batch.isSequenceAscending(); err != nil {
return err
}
if err := batch.isBatchAmount(); err != nil {
return err
}
if err := batch.isEntryHash(); err != nil {
return err
}
if err := batch.isOriginatorDNE(); err != nil {
return err
}
if err := batch.isTraceNumberODFI(); err != nil {
return err
}
if err := batch.isAddendaSequence(); err != nil {
return err
}
if err := batch.isCategory(); err != nil {
return err
}
return nil
}
// Build creates valid batch by building sequence numbers and batch batch control. An error is returned if
// the batch being built has invalid records.
func (batch *Batch) build() error {
// Requires a valid BatchHeader
if err := batch.Header.Validate(); err != nil {
return err
}
if len(batch.Entries) <= 0 && len(batch.ADVEntries) <= 0 {
return batch.Error("entries", ErrBatchNoEntries)
}
// Create record sequence numbers
entryCount := 0
seq := 1
if !batch.IsADV() {
for i, entry := range batch.Entries {
entryCount += 1 + entry.addendaCount()
currentTraceNumberODFI, err := strconv.Atoi(entry.TraceNumberField()[:8])
if err != nil {
return err
}
batchHeaderODFI, err := strconv.Atoi(batch.Header.ODFIIdentificationField()[:8])
if err != nil {
return err
}
// Add a sequenced TraceNumber if one is not already set. Have to keep original trance number Return and NOC entries
if currentTraceNumberODFI != batchHeaderODFI {
entry.SetTraceNumber(batch.Header.ODFIIdentification, seq)
}
seq++
addendaSeq := 1
for _, a := range entry.Addenda05 {
// sequences don't exist in NOC or Return addenda
a.SequenceNumber = addendaSeq
a.EntryDetailSequenceNumber = batch.parseNumField(batch.Entries[i].TraceNumberField()[8:])
addendaSeq++
}
}
// build a BatchControl record
bc := NewBatchControl()
bc.ServiceClassCode = batch.Header.ServiceClassCode
bc.CompanyIdentification = batch.Header.CompanyIdentification
bc.ODFIIdentification = batch.Header.ODFIIdentification
bc.BatchNumber = batch.Header.BatchNumber
bc.EntryAddendaCount = entryCount
bc.EntryHash = batch.calculateEntryHash()
bc.TotalCreditEntryDollarAmount, bc.TotalDebitEntryDollarAmount = batch.calculateBatchAmounts()
batch.Control = bc
} else {
for i, entry := range batch.ADVEntries {
entryCount++
if entry.Addenda99 != nil {
entryCount++
}
// Set Sequence Number
batch.ADVEntries[i].SequenceNumber = seq
seq++
if seq > 9999 {
return batch.Error("SequenceNumber", ErrBatchADVCount)
}
}
// build a BatchADVControl record
bcADV := NewADVBatchControl()
bcADV.ServiceClassCode = batch.Header.ServiceClassCode
bcADV.ACHOperatorData = batch.Header.CompanyName
bcADV.ODFIIdentification = batch.Header.ODFIIdentification
bcADV.BatchNumber = batch.Header.BatchNumber
bcADV.EntryAddendaCount = entryCount
bcADV.EntryHash = batch.calculateEntryHash()
bcADV.TotalCreditEntryDollarAmount, bcADV.TotalDebitEntryDollarAmount = batch.calculateADVBatchAmounts()
batch.ADVControl = bcADV
}
return batch.upsertOffsets()
}
// SetHeader appends an BatchHeader to the Batch
func (batch *Batch) SetHeader(batchHeader *BatchHeader) {
batch.Header = batchHeader
}
// GetHeader returns the current Batch header
func (batch *Batch) GetHeader() *BatchHeader {
return batch.Header
}
// SetControl appends an BatchControl to the Batch
func (batch *Batch) SetControl(batchControl *BatchControl) {
batch.Control = batchControl
}
// GetControl returns the current Batch Control
func (batch *Batch) GetControl() *BatchControl {
return batch.Control
}
// SetADVControl appends an BatchADVControl to the Batch
func (batch *Batch) SetADVControl(batchADVControl *ADVBatchControl) {
batch.ADVControl = batchADVControl
}
// GetADVControl returns the current Batch ADVControl
func (batch *Batch) GetADVControl() *ADVBatchControl {
return batch.ADVControl
}
// GetEntries returns a slice of entry details for the batch
func (batch *Batch) GetEntries() []*EntryDetail {
return batch.Entries
}
// AddEntry appends an EntryDetail to the Batch
func (batch *Batch) AddEntry(entry *EntryDetail) {
if entry == nil {
return
}
batch.category = entry.Category
batch.Entries = append(batch.Entries, entry)
}
// AddADVEntry appends an ADV EntryDetail to the Batch
func (batch *Batch) AddADVEntry(entry *ADVEntryDetail) {
batch.category = entry.Category
batch.ADVEntries = append(batch.ADVEntries, entry)
}
// GetADVEntries returns a slice of entry details for the batch
func (batch *Batch) GetADVEntries() []*ADVEntryDetail {
return batch.ADVEntries
}
// Category returns batch category
func (batch *Batch) Category() string {
if len(batch.Entries) == 0 && batch.category != "" {
return batch.category
}
// If an Entry has NOC or Return that's the Batch's category
for i := range batch.Entries {
switch batch.Entries[i].Category {
case CategoryReturn, CategoryNOC:
return batch.Entries[i].Category
}
}
for i := range batch.ADVEntries {
switch batch.ADVEntries[i].Category {
case CategoryReturn, CategoryNOC:
return batch.ADVEntries[i].Category
}
}
return CategoryForward
}
// ID returns the id of the batch
func (batch *Batch) ID() string {
return batch.id
}
// SetID sets the batch id
func (batch *Batch) SetID(id string) {
batch.id = id
}
// isFieldInclusion iterates through all the records in the batch and verifies against default fields
func (batch *Batch) isFieldInclusion() error {
if err := batch.Header.Validate(); err != nil {
return err
}
if !batch.IsADV() {
for _, entry := range batch.Entries {
if err := entry.Validate(); err != nil {
return err
}
if entry.Addenda02 != nil {
if err := entry.Addenda02.Validate(); err != nil {
return err
}
}
for _, addenda05 := range entry.Addenda05 {
if err := addenda05.Validate(); err != nil {
return err
}
}
if entry.Addenda98 != nil {
if err := entry.Addenda98.Validate(); err != nil {
return err
}
}
if entry.Addenda99 != nil {
if err := entry.Addenda99.Validate(); err != nil {
return err
}
}
}
return batch.Control.Validate()
}
// ADV File/Batch
for _, entry := range batch.ADVEntries {
if err := entry.Validate(); err != nil {
return err
}
if entry.Addenda99 != nil {
if err := entry.Addenda99.Validate(); err != nil {
return err
}
}
}
return batch.ADVControl.Validate()
}
// isBatchEntryCount validate Entry count is accurate
// The Entry/Addenda Count Field is a tally of each Entry Detail and Addenda
// Record processed within the batch
func (batch *Batch) isBatchEntryCount() error {
entryCount := 0
if !batch.IsADV() {
for _, entry := range batch.Entries {
entryCount += 1 + entry.addendaCount()
}
if entryCount != batch.Control.EntryAddendaCount {
return batch.Error("EntryAddendaCount",
NewErrBatchCalculatedControlEquality(entryCount, batch.Control.EntryAddendaCount))
}
} else {
for _, entry := range batch.ADVEntries {
entryCount++
if entry.Addenda99 != nil {
entryCount++
}
}
if entryCount != batch.ADVControl.EntryAddendaCount {
return batch.Error("EntryAddendaCount",
NewErrBatchCalculatedControlEquality(entryCount, batch.ADVControl.EntryAddendaCount))
}
}
return nil
}
// isBatchAmount validate Amount is the same as what is in the Entries
// The Total Debit and Credit Entry Dollar Amount fields contain accumulated
// Entry Detail debit and credit totals within a given batch
func (batch *Batch) isBatchAmount() error {
credit := 0
debit := 0
//ToDo: Consider going back to one function for calculating BatchAmounts, but I'm not sure I want to have
// calculateBatchAmounts with ADV TransactionCodes. In addition the smaller functions help keep the -over for
// gocyclo lower, although since we are currently at 25 (originally it was 18 or 19) it probably won't matter now
// in this case. Based on what I see in other github go code, I'm not sure 25 is a high enough number either.
// Balancing easy to understand functions without having to create functions just for the purpose of meeting the
// -over number convinces me that it should be higher than 25.
if !batch.IsADV() {
credit, debit = batch.calculateBatchAmounts()
if debit != batch.Control.TotalDebitEntryDollarAmount {
return batch.Error("TotalDebitEntryDollarAmount",
NewErrBatchCalculatedControlEquality(debit, batch.Control.TotalDebitEntryDollarAmount))
}
if credit != batch.Control.TotalCreditEntryDollarAmount {
return batch.Error("TotalCreditEntryDollarAmount",
NewErrBatchCalculatedControlEquality(credit, batch.Control.TotalCreditEntryDollarAmount))
}
} else {
credit, debit = batch.calculateADVBatchAmounts()
if debit != batch.ADVControl.TotalDebitEntryDollarAmount {
return batch.Error("TotalDebitEntryDollarAmount",
NewErrBatchCalculatedControlEquality(debit, batch.ADVControl.TotalDebitEntryDollarAmount))
}
if credit != batch.ADVControl.TotalCreditEntryDollarAmount {
return batch.Error("TotalCreditEntryDollarAmount",
NewErrBatchCalculatedControlEquality(credit, batch.ADVControl.TotalCreditEntryDollarAmount))
}
}
return nil
}
func (batch *Batch) calculateBatchAmounts() (credit int, debit int) {
for _, entry := range batch.Entries {
switch entry.TransactionCode {
case CheckingCredit, CheckingReturnNOCCredit, CheckingPrenoteCredit, CheckingZeroDollarRemittanceCredit,
SavingsCredit, SavingsReturnNOCCredit, SavingsPrenoteCredit, SavingsZeroDollarRemittanceCredit, GLCredit,
GLReturnNOCCredit, GLPrenoteCredit, GLZeroDollarRemittanceCredit, LoanCredit, LoanReturnNOCCredit,
LoanPrenoteCredit, LoanZeroDollarRemittanceCredit:
credit = credit + entry.Amount
case CheckingDebit, CheckingReturnNOCDebit, CheckingPrenoteDebit, CheckingZeroDollarRemittanceDebit,
SavingsDebit, SavingsReturnNOCDebit, SavingsPrenoteDebit, SavingsZeroDollarRemittanceDebit, GLDebit,
GLReturnNOCDebit, GLPrenoteDebit, GLZeroDollarRemittanceDebit, LoanDebit, LoanReturnNOCDebit:
debit = debit + entry.Amount
}
}
return credit, debit
}
func (batch *Batch) calculateADVBatchAmounts() (credit int, debit int) {
for _, entry := range batch.ADVEntries {
if entry.TransactionCode == CreditForDebitsOriginated ||
entry.TransactionCode == CreditForCreditsReceived ||
entry.TransactionCode == CreditForCreditsRejected ||
entry.TransactionCode == CreditSummary {
credit = credit + entry.Amount
}
if entry.TransactionCode == DebitForCreditsOriginated ||
entry.TransactionCode == DebitForDebitsReceived ||
entry.TransactionCode == DebitForDebitsRejectedBatches ||
entry.TransactionCode == DebitSummary {
debit = debit + entry.Amount
}
}
return credit, debit
}
// isSequenceAscending Individual Entry Detail Records within individual batches must
// be in ascending Trace Number order (although Trace Numbers need not necessarily be consecutive).
func (batch *Batch) isSequenceAscending() error {
if !batch.IsADV() {
lastSeq := "0"
for _, entry := range batch.Entries {
if entry.TraceNumber <= lastSeq {
return batch.Error("TraceNumber", NewErrBatchAscending(lastSeq, entry.TraceNumber))
}
lastSeq = entry.TraceNumber
}
}
return nil
}
// isEntryHash validates the hash by recalculating the result
func (batch *Batch) isEntryHash() error {
hashField := batch.calculateEntryHash()
if !batch.IsADV() {
if hashField != batch.Control.EntryHash {
return batch.Error("EntryHash",
NewErrBatchCalculatedControlEquality(hashField, batch.Control.EntryHash))
}
} else {
if hashField != batch.ADVControl.EntryHash {
return batch.Error("EntryHash",
NewErrBatchCalculatedControlEquality(hashField, batch.ADVControl.EntryHash))
}
}
return nil
}
// calculateEntryHash This field is prepared by hashing the 8-digit Routing Number in each entry.
// The Entry Hash provides a check against inadvertent alteration of data
func (batch *Batch) calculateEntryHash() int {
hash := 0
if !batch.IsADV() {
for _, entry := range batch.Entries {
entryRDFI, _ := strconv.Atoi(entry.RDFIIdentification)
hash = hash + entryRDFI
}
} else {
for _, entry := range batch.ADVEntries {
entryRDFI, _ := strconv.Atoi(entry.RDFIIdentification)
hash = hash + entryRDFI
}
}
// This follows what is done in BatchControl.Parse()
// EntryHash is essentially the sum of all the RDFI routing numbers in the batch. If the sum exceeds 10 digits
// (because you have lots of Entry Detail Records), lop off the most significant digits of the sum until there
// are only 10.
s := batch.numericField(hash, 10)
hash, _ = strconv.Atoi(s)
return hash
}
// "Only an agency of the United States Government may originate a DNE entry" - NACHA Operating Rules
// Origination code '2' is for government agencies. Codes 21, 23, 31, and 33 are the only transaction codes
// allowed for DNEs. Tranaction codes 21 and 31 are just for returns or NOCs of the 23 and 33 codes.
// So we check that the Originator Status Code is not equal to “2” for DNE if the Transaction Code is 23 or 33
func (batch *Batch) isOriginatorDNE() error {
if batch.Header.OriginatorStatusCode != 2 && batch.Header.StandardEntryClassCode == DNE {
for _, entry := range batch.Entries {
if entry.TransactionCode == CheckingPrenoteCredit || entry.TransactionCode == SavingsPrenoteCredit {
return batch.Error("OriginatorStatusCode", ErrBatchOriginatorDNE, batch.Header.OriginatorStatusCode)
}
}
}
return nil
}
// isTraceNumberODFI checks if the first 8 positions of the entry detail trace number
// match the batch header ODFI
func (batch *Batch) isTraceNumberODFI() error {
for _, entry := range batch.Entries {
if batch.Header.ODFIIdentificationField() != entry.TraceNumberField()[:8] {
return batch.Error("ODFIIdentificationField",
NewErrBatchTraceNumberNotODFI(batch.Header.ODFIIdentificationField(), entry.TraceNumberField()[:8]))
}
}
return nil
}
// isAddendaSequence check multiple errors on addenda records in the batch entries
func (batch *Batch) isAddendaSequence() error {
for _, entry := range batch.Entries {
if entry.Addenda02 != nil {
if entry.AddendaRecordIndicator != 1 {
return batch.Error("AddendaRecordIndicator", ErrBatchAddendaIndicator)
}
}
if len(entry.Addenda05) > 0 {
// addenda without indicator flag of 1
if entry.AddendaRecordIndicator != 1 {
return batch.Error("AddendaRecordIndicator", ErrBatchAddendaIndicator)
}
lastSeq := -1
// check if sequence is ascending
for _, a := range entry.Addenda05 {
// sequences don't exist in NOC or Return addenda
if a.SequenceNumber < lastSeq {
return batch.Error("SequenceNumber", NewErrBatchAscending(lastSeq, a.SequenceNumber))
}
lastSeq = a.SequenceNumber
// check that we are in the correct Entry Detail
if !(a.EntryDetailSequenceNumberField() == entry.TraceNumberField()[8:]) {
return batch.Error("TraceNumber", NewErrBatchAscending(lastSeq, a.SequenceNumber))
}
}
}
if entry.Addenda98 != nil {
if entry.AddendaRecordIndicator != 1 {
return batch.Error("AddendaRecordIndicator", ErrBatchAddendaIndicator)
}
}
if entry.Addenda99 != nil {
if entry.AddendaRecordIndicator != 1 {
return batch.Error("AddendaRecordIndicator", ErrBatchAddendaIndicator)
}
}
}
return nil
}
// isCategory verifies that a Forward and Return Category are not in the same batch
func (batch *Batch) isCategory() error {
if !batch.IsADV() {
category := batch.GetEntries()[0].Category
if len(batch.Entries) > 1 {
for i := 0; i < len(batch.Entries); i++ {
if batch.Entries[i].Category == CategoryNOC {
continue
}
if batch.Entries[i].Category != category {
return batch.Error("Category", NewErrBatchCategory(batch.Entries[i].Category, category))
}
}
}
} else {
category := batch.GetADVEntries()[0].Category
if len(batch.ADVEntries) > 1 {
for i := 0; i < len(batch.ADVEntries); i++ {
if batch.ADVEntries[i].Category != category {
return batch.Error("Category", NewErrBatchCategory(batch.ADVEntries[i].Category, category))
}
}
}
}
return nil
}
// addendaFieldInclusion verifies Addenda* Field Inclusion based on entry.Category and
// batchHeader.StandardEntryClassCode
// Forward Entries:
// MTE, POS, and SHR can only have Addenda02
// ACK, ATX, CCD, CIE, CTX, DNE, ENR, WEB, PPD, TRX can only have Addenda05
// ARC, BOC, POP, RCK, TEL, TRC, XCK cannot have Addenda02 or Addenda05
// Notification of Change:
// COR and Addenda98
// Return:
// Addenda99
//
func (batch *Batch) addendaFieldInclusion(entry *EntryDetail) error {
switch entry.Category {
case CategoryForward:
if err := batch.addendaFieldInclusionForward(entry); err != nil {
return err
}
case CategoryNOC:
if err := batch.addendaFieldInclusionNOC(entry); err != nil {
return err
}
case CategoryReturn, CategoryDishonoredReturn, CategoryDishonoredReturnContested:
if err := batch.addendaFieldInclusionReturn(entry); err != nil {
return err
}
}
return nil
}
// addendaFieldInclusionForward verifies Addenda* Field Inclusion for entry.Category Forward
func (batch *Batch) addendaFieldInclusionForward(entry *EntryDetail) error {
switch batch.Header.StandardEntryClassCode {
case MTE, POS, SHR:
if entry.Addenda02 == nil {
return batch.Error("Addenda02", ErrFieldInclusion)
}
if entry.Addenda05 != nil {
return batch.Error("Addenda05", ErrBatchAddendaCategory, entry.Category)
}
// ACK, ATX, CCD, CIE, CTX, DNE, ENR WEB, PPD, TRX can only have Addenda05
case ACK, ATX, CCD, CIE, CTX, DNE, ENR, WEB, PPD, TRX:
if entry.Addenda02 != nil {
return batch.Error("Addenda02", ErrBatchAddendaCategory, entry.Category)
}
case ARC, BOC, COR, POP, RCK, TEL, TRC, XCK:
if entry.Addenda02 != nil {
return batch.Error("Addenda02", ErrBatchAddendaCategory, entry.Category)
}
if entry.Addenda05 != nil {
return batch.Error("Addenda05", ErrBatchAddendaCategory, entry.Category)
}
}
if batch.Header.StandardEntryClassCode != COR {
if entry.Addenda98 != nil {
return batch.Error("Addenda98", ErrBatchAddendaCategory, entry.Category)
}
}
if entry.Addenda99 != nil {
return batch.Error("Addenda99", ErrBatchAddendaCategory, entry.Category)
}
return nil
}
// addendaFieldInclusionNOC verifies Addenda* Field Inclusion for entry.Category NOC
func (batch *Batch) addendaFieldInclusionNOC(entry *EntryDetail) error {
if entry.Addenda02 != nil {
return batch.Error("Addenda02", ErrBatchAddendaCategory, entry.Category)
}
if entry.Addenda05 != nil {
return batch.Error("Addenda05", ErrBatchAddendaCategory, entry.Category)
}
if batch.Header.StandardEntryClassCode != COR {
if entry.Addenda98 != nil {
return batch.Error("Addenda98", ErrFieldInclusion)
}
}
if entry.Addenda99 != nil {
return batch.Error("Addenda99", ErrBatchAddendaCategory, entry.Category)
}
return nil
}
// addendaFieldInclusionReturn verifies Addenda* Field Inclusion for entry.Category Return
func (batch *Batch) addendaFieldInclusionReturn(entry *EntryDetail) error {
if entry.Addenda02 != nil {
return batch.Error("Addenda02", ErrBatchAddendaCategory, entry.Category)
}
if entry.Addenda05 != nil {
return batch.Error("Addenda05", ErrBatchAddendaCategory, entry.Category)
}
if entry.Addenda98 != nil {
return batch.Error("Addenda98", ErrBatchAddendaCategory, entry.Category)
}
if entry.Addenda99 == nil {
return batch.Error("Addenda99", ErrFieldInclusion)
}
return nil
}
// IsADV determines if a batch is batch type ADV - BatchADV
func (batch *Batch) IsADV() bool {
ok := batch.GetHeader().StandardEntryClassCode == ADV
return ok
}
// ValidTranCodeForServiceClassCode validates a TransactionCode is valid for a ServiceClassCode
func (batch *Batch) ValidTranCodeForServiceClassCode(entry *EntryDetail) error {
// ADV should use ADVEntryDetail
switch entry.TransactionCode {
case CreditForDebitsOriginated, CreditForCreditsReceived, CreditForCreditsRejected, CreditSummary,
DebitForCreditsOriginated, DebitForDebitsReceived, DebitForDebitsRejectedBatches, DebitSummary:
return batch.Error("TransactionCode", ErrBatchTransactionCode, entry.TransactionCode)
}
switch batch.Header.ServiceClassCode {
case AutomatedAccountingAdvices:
return batch.Error("ServiceClassCode", ErrBatchServiceClassCode, batch.Header.ServiceClassCode)
case MixedDebitsAndCredits:
case CreditsOnly:
if entry.CreditOrDebit() != "C" {
return batch.Error("TransactionCode", NewErrBatchServiceClassTranCode(batch.Header.ServiceClassCode, entry.TransactionCode))
}
case DebitsOnly:
if entry.CreditOrDebit() != "D" {
return batch.Error("TransactionCode", NewErrBatchServiceClassTranCode(batch.Header.ServiceClassCode, entry.TransactionCode))
}
}
return nil
}
// Equal returns true only if two Batch (or any Batcher) objects are equal. Equality is determined by
// many of the ACH Batch and EntryDetail properties.
func (batch *Batch) Equal(other Batcher) bool {
// Some fields are intentionally not compared as they could vary between batches that would otherwise be the same.
if batch == nil || other == nil || batch.Header == nil || other.GetHeader() == nil {
return false
}
ohead := other.GetHeader()
if batch.Header.ServiceClassCode != ohead.ServiceClassCode {
return false
}
if !strings.EqualFold(batch.Header.CompanyName, ohead.CompanyName) {
return false
}
if batch.Header.CompanyIdentification != ohead.CompanyIdentification {
return false
}
if batch.Header.StandardEntryClassCode != ohead.StandardEntryClassCode {
return false
}
if batch.Header.EffectiveEntryDate != ohead.EffectiveEntryDate {
return false
}
if batch.Header.ODFIIdentification != ohead.ODFIIdentification {
return false
}
oentries := other.GetEntries()
if len(batch.Entries) != len(oentries) {
return false
}
equalEntries := 0
for i := range batch.Entries {
for j := range oentries {