-
Notifications
You must be signed in to change notification settings - Fork 39
/
cashLetter.go
340 lines (291 loc) · 9.74 KB
/
cashLetter.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
// Copyright 2020 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package imagecashletter
import (
"errors"
"fmt"
)
// CashLetterError is an Error that describes CashLetter validation issues
type CashLetterError struct {
CashLetterID string
FieldName string
Msg string
}
func (e *CashLetterError) Error() string {
return fmt.Sprintf("CashLetterNumber %s %s %s", e.CashLetterID, e.FieldName, e.Msg)
}
// Errors specific to parsing a CashLetter
var (
msgCashLetterBundleEntries = "%v cannot have bundle entries"
msgCashLetterRoutingNumber = "%v cannot have a Routing Number Summary"
msgMandatoryRecord = "record is mandatory"
)
// CashLetter contains CashLetterHeader, CashLetterControl and Bundle records.
type CashLetter struct {
// ID is a client defined string used as a reference to this record.
ID string `json:"id"`
// CashLetterHeader is a Cash Letter Header Record
CashLetterHeader *CashLetterHeader `json:"cashLetterHeader,omitempty"`
// Bundles is an array of Bundle
Bundles []*Bundle `json:"bundles,omitempty"`
// Credits is an array of Credit
Credits []*Credit `json:"credit,omitempty"`
// CreditItems is an array of CreditItem
CreditItems []*CreditItem `json:"creditItem,omitempty"`
// RoutingNumberSummary is an array of RoutingNumberSummary
RoutingNumberSummary []*RoutingNumberSummary `json:"routingNumberSummary,omitempty"`
// currentBundle is the currentBundle being parsed
currentBundle *Bundle
// RoutingNumberSummary is an imagecashletter RoutingNumberSummary
currentRoutingNumberSummary *RoutingNumberSummary
// CashLetterControl is a Cash Letter Control Record
CashLetterControl *CashLetterControl `json:"cashLetterControl,omitempty"`
}
// NewCashLetter takes a CashLetterHeader and returns a CashLetter
func NewCashLetter(clh *CashLetterHeader) CashLetter {
cl := CashLetter{}
cl.SetControl(NewCashLetterControl())
cl.SetHeader(clh)
return cl
}
func (cl *CashLetter) setRecordType() {
if cl == nil {
return
}
cl.CashLetterHeader.setRecordType()
for i := range cl.Bundles {
cl.Bundles[i].setRecordType()
}
for i := range cl.CreditItems {
cl.CreditItems[i].setRecordType()
}
for i := range cl.RoutingNumberSummary {
cl.RoutingNumberSummary[i].setRecordType()
}
cl.CashLetterControl.setRecordType()
}
// Validate performs ImageCashLetter validations and format rule checks and returns an error if not Validated
func (cl *CashLetter) Validate() error {
if cl.CashLetterHeader == nil {
return errors.New("nil CashLetterHeader")
}
if cl.CashLetterHeader.RecordTypeIndicator == "N" {
if cl.GetBundles() != nil {
return &CashLetterError{
CashLetterID: cl.CashLetterHeader.CashLetterID,
FieldName: "RecordTypeIndicator",
Msg: fmt.Sprintf(msgCashLetterBundleEntries, cl.CashLetterHeader.RecordTypeIndicator),
}
}
}
switch cl.CashLetterHeader.CollectionTypeIndicator {
case
"00", "01", "02":
default:
if cl.GetRoutingNumberSummary() != nil {
return &CashLetterError{
CashLetterID: cl.CashLetterHeader.CashLetterID,
FieldName: "CollectionTypeIndicator",
Msg: fmt.Sprintf(msgCashLetterRoutingNumber, cl.CashLetterHeader.CollectionTypeIndicator),
}
}
}
if cl.CashLetterControl == nil {
return &CashLetterError{
CashLetterID: cl.CashLetterHeader.CashLetterID,
FieldName: "CashLetterControl",
Msg: msgMandatoryRecord,
}
}
if err := cl.CashLetterControl.Validate(); err != nil {
return err
}
return nil
}
// build a valid CashLetter by building a CashLetterControl. An error is returned if
// the CashLetter being built has invalid records.
func (cl *CashLetter) build() error {
// Requires a valid CashLetterHeader
if err := cl.CashLetterHeader.Validate(); err != nil {
return err
}
// CashLetterControl Counts
cashLetterBundleCount := len(cl.Bundles)
cashLetterItemsCount := 0
cashLetterTotalAmount := 0
cashLetterImagesCount := 0
// Sequence Numbers
bundleSequenceNumber := 1
// creditIndicator
creditIndicator := 0
if len(cl.GetCreditItems()) > 0 {
cashLetterItemsCount = cashLetterItemsCount + len(cl.GetCreditItems())
creditIndicator = 1
}
// Bundles
for _, b := range cl.Bundles {
// Set Bundle Sequence Numbers
b.BundleHeader.SetBundleSequenceNumber(bundleSequenceNumber)
// Sequence Number
cdSequenceNumber := 1
// Check Items
for _, cd := range b.Checks {
if cd.EceInstitutionItemSequenceNumber != "" {
i := cd.parseNumField(cd.EceInstitutionItemSequenceNumber)
cdSequenceNumber = i
}
// Record Numbers
cdAddendumARecordNumber := 1
cdAddendumCRecordNumber := 1
// Set CheckDetail Sequence Numbers
cd.SetEceInstitutionItemSequenceNumber(cdSequenceNumber)
// Set Addenda SequenceNumber and RecordNumber
for i := range cd.CheckDetailAddendumA {
cd.CheckDetailAddendumA[i].SetBOFDItemSequenceNumber(cdSequenceNumber)
cd.CheckDetailAddendumA[i].RecordNumber = cdAddendumARecordNumber
cdAddendumARecordNumber++
if cdAddendumARecordNumber > 9 {
cdAddendumARecordNumber = 1
}
}
for x := range cd.CheckDetailAddendumC {
cd.CheckDetailAddendumC[x].SetEndorsingBankItemSequenceNumber(cdSequenceNumber)
cd.CheckDetailAddendumC[x].RecordNumber = cdAddendumARecordNumber
cdAddendumCRecordNumber++
if cdAddendumCRecordNumber > 99 {
cdAddendumCRecordNumber = 1
}
}
cdSequenceNumber++
cashLetterItemsCount = cashLetterItemsCount + 1
cashLetterTotalAmount = cashLetterTotalAmount + cd.ItemAmount
cashLetterImagesCount = cashLetterImagesCount + len(cd.ImageViewDetail)
}
// Returns Items
for _, rd := range b.Returns {
// Sequence Number
var rdSequenceNumber int
rdSequenceNumber++
// Record Numbers
rdAddendumARecordNumber := 1
rdAddendumDRecordNumber := 1
// Set ReturnDetail Sequence Numbers
rd.SetEceInstitutionItemSequenceNumber(rdSequenceNumber)
// Set Addenda SequenceNumber and RecordNumber
for i := range rd.ReturnDetailAddendumA {
rd.ReturnDetailAddendumA[i].SetBOFDItemSequenceNumber(rdSequenceNumber)
rd.ReturnDetailAddendumA[i].RecordNumber = rdAddendumARecordNumber
rdAddendumARecordNumber++
if rdAddendumARecordNumber > 9 {
rdAddendumARecordNumber = 1
}
}
for x := range rd.ReturnDetailAddendumD {
rd.ReturnDetailAddendumD[x].SetEndorsingBankItemSequenceNumber(rdSequenceNumber)
rd.ReturnDetailAddendumD[x].RecordNumber = rdAddendumDRecordNumber
rdAddendumDRecordNumber++
if rdAddendumDRecordNumber > 99 {
rdAddendumDRecordNumber = 1
}
}
cashLetterItemsCount = cashLetterItemsCount + 1
cashLetterTotalAmount = cashLetterTotalAmount + rd.ItemAmount
cashLetterImagesCount = cashLetterImagesCount + len(rd.ImageViewDetail)
}
// Validate Bundle
if err := b.Validate(); err != nil {
return err
}
// Build Bundle
if err := b.build(); err != nil {
return err
}
bundleSequenceNumber++
}
// build a CashLetterControl record
clc := NewCashLetterControl()
clc.CashLetterBundleCount = cashLetterBundleCount
clc.CashLetterItemsCount = cashLetterItemsCount
clc.CashLetterTotalAmount = cashLetterTotalAmount
clc.CashLetterImagesCount = cashLetterImagesCount
if cl.CashLetterControl.ECEInstitutionName != "" {
clc.ECEInstitutionName = cl.CashLetterControl.ECEInstitutionName
} else {
clc.ECEInstitutionName = cl.GetHeader().ECEInstitutionRoutingNumber
}
clc.CreditTotalIndicator = creditIndicator
cl.CashLetterControl = clc
return nil
}
// Create creates a CashLetter of Bundles containing CheckDetail or ReturnDetail
func (cl *CashLetter) Create() error {
if err := cl.build(); err != nil {
return err
}
return cl.Validate()
}
// SetHeader appends a CashLetterHeader to the CashLetter
func (cl *CashLetter) SetHeader(cashLetterHeader *CashLetterHeader) {
cl.CashLetterHeader = cashLetterHeader
}
// GetHeader returns the current CashLetter header
func (cl *CashLetter) GetHeader() *CashLetterHeader {
return cl.CashLetterHeader
}
// SetControl appends a CashLetterControl to the CashLetter
func (cl *CashLetter) SetControl(cashLetterControl *CashLetterControl) {
cl.CashLetterControl = cashLetterControl
}
// GetControl returns the current CashLetter Control
func (cl *CashLetter) GetControl() *CashLetterControl {
return cl.CashLetterControl
}
// AddBundle appends a Bundle to the CashLetter
func (cl *CashLetter) AddBundle(bundle *Bundle) []*Bundle {
cl.Bundles = append(cl.Bundles, bundle)
return cl.Bundles
}
// GetBundles returns a slice of Bundle for the CashLetter
func (cl *CashLetter) GetBundles() []*Bundle {
if cl == nil {
return nil
}
return cl.Bundles
}
// AddRoutingNumberSummary appends a RoutingNumberSummary to the CashLetter
func (cl *CashLetter) AddRoutingNumberSummary(rns *RoutingNumberSummary) []*RoutingNumberSummary {
cl.RoutingNumberSummary = append(cl.RoutingNumberSummary, rns)
return cl.RoutingNumberSummary
}
// GetRoutingNumberSummary returns a slice of RoutingNumberSummary for the CashLetter
func (cl *CashLetter) GetRoutingNumberSummary() []*RoutingNumberSummary {
if cl == nil {
return nil
}
return cl.RoutingNumberSummary
}
// AddCredit appends a CreditItem to the CashLetter
func (cl *CashLetter) AddCredit(cr *Credit) []*Credit {
cl.Credits = append(cl.Credits, cr)
return cl.Credits
}
// AddCreditItem appends a CreditItem to the CashLetter
func (cl *CashLetter) AddCreditItem(ci *CreditItem) []*CreditItem {
cl.CreditItems = append(cl.CreditItems, ci)
return cl.CreditItems
}
// GetCredits returns a slice of Credit for the CashLetter
func (cl *CashLetter) GetCredits() []*Credit {
if cl == nil {
return nil
}
return cl.Credits
}
// GetCreditItems returns a slice of CreditItem for the CashLetter
func (cl *CashLetter) GetCreditItems() []*CreditItem {
if cl == nil {
return nil
}
return cl.CreditItems
}