-
Notifications
You must be signed in to change notification settings - Fork 40
/
fileControl.go
219 lines (199 loc) · 7.47 KB
/
fileControl.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
// 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 (
"encoding/json"
"fmt"
"strings"
"unicode/utf8"
)
// FileControl Record
type FileControl struct {
// ID is a client defined string used as a reference to this record.
ID string `json:"id"`
// RecordType defines the type of record
recordType string
// CashLetterCount identifies the total number of cash letters within the file.
CashLetterCount int `json:"cashLetterCount"`
// TotalRecordCount identifies the total number of records of all types sent in the file, including the FileControl.
TotalRecordCount int `json:"totalRecordCount"`
// TotalItemCount identifies the total number of Items sent within the file.
TotalItemCount int `json:"totalItemCount"`
// FileTotalAmount identifies the total Item amount of the complete file.
FileTotalAmount int `json:"fileTotalAmount"`
// ImmediateOriginContactName identifies contact at the institution that creates the ECE file.
ImmediateOriginContactName string `json:"immediateOriginContactName"`
// ImmediateOriginContactPhoneNumber is the phone number of the contact at the institution that creates the
// file.
ImmediateOriginContactPhoneNumber string `json:"immediateOriginContactPhoneNumber"`
// CreditTotalIndicator isa code that indicates whether Credits Items are included in this record’s totals.
// If so they will be included in TotalItemCount and FileTotal Amount.
// TotalRecordCount includes all records of all types regardless of the value of this field.
// Values:
// 0: Credit Items are not included in totals
// 1: Credit Items are included in totals
CreditTotalIndicator int `json:"creditTotalIndicator"`
// reserved is a field reserved for future use. Reserved should be blank.
reserved string
// validator is composed for image cash letter data validation
validator
// converters is composed for image cash letter to golang Converters
converters
}
// NewFileControl returns a new FileControl with default values for non exported fields
func NewFileControl() FileControl {
fc := FileControl{}
fc.setRecordType()
return fc
}
func (fc *FileControl) setRecordType() {
if fc == nil {
return
}
fc.recordType = "99"
fc.reserved = " "
}
// Parse takes the input record string and parses the FileControl values
func (fc *FileControl) Parse(record string) {
if utf8.RuneCountInString(record) < 65 {
return
}
// Character position 1-2, Always "99"
fc.setRecordType()
// 03-08
fc.CashLetterCount = fc.parseNumField(record[2:8])
// 09-16
fc.TotalRecordCount = fc.parseNumField(record[8:16])
// 17-24
fc.TotalItemCount = fc.parseNumField(record[16:24])
// 25-40
fc.FileTotalAmount = fc.parseNumField(record[24:40])
// 41-54
fc.ImmediateOriginContactName = fc.parseStringField(record[40:54])
// 55-64
fc.ImmediateOriginContactPhoneNumber = fc.parseStringField(record[54:64])
// 65-65
fc.CreditTotalIndicator = fc.parseNumField(record[64:65])
// 66-80 reserved - Leave blank
fc.reserved = " "
}
func (fc *FileControl) UnmarshalJSON(data []byte) error {
type Alias FileControl
aux := struct {
*Alias
}{
(*Alias)(fc),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
fc.setRecordType()
return nil
}
// String writes the FileControl struct to a string.
func (fc *FileControl) String() string {
if fc == nil {
return ""
}
var buf strings.Builder
buf.Grow(80)
buf.WriteString(fc.recordType)
buf.WriteString(fc.CashLetterCountField())
buf.WriteString(fc.TotalRecordCountField())
buf.WriteString(fc.TotalItemCountField())
buf.WriteString(fc.FileTotalAmountField())
buf.WriteString(fc.ImmediateOriginContactNameField())
buf.WriteString(fc.ImmediateOriginContactPhoneNumberField())
buf.WriteString(fc.CreditTotalIndicatorField())
buf.WriteString(fc.reservedField())
return buf.String()
}
// Validate performs image cash letter format rule checks on the record and returns an error if not Validated
// The first error encountered is returned and stops the parsing.
func (fc *FileControl) Validate() error {
if err := fc.fieldInclusion(); err != nil {
return err
}
if fc.recordType != "99" {
msg := fmt.Sprintf(msgRecordType, 99)
return &FieldError{FieldName: "recordType", Value: fc.recordType, Msg: msg}
}
if err := fc.isAlphanumericSpecial(fc.ImmediateOriginContactName); err != nil {
return &FieldError{FieldName: "ImmediateOriginContactName",
Value: fc.ImmediateOriginContactName, Msg: err.Error()}
}
if err := fc.isNumeric(fc.ImmediateOriginContactPhoneNumber); err != nil {
return &FieldError{FieldName: "ImmediateOriginContactPhoneNumber",
Value: fc.ImmediateOriginContactPhoneNumber, Msg: err.Error()}
}
// Conditional
if fc.CreditTotalIndicatorField() != "" {
if err := fc.isCreditTotalIndicator(fc.CreditTotalIndicator); err != nil {
return &FieldError{FieldName: "CreditTotalIndicator", Value: fc.CreditTotalIndicatorField(), Msg: err.Error()}
}
}
return nil
}
// fieldInclusion validate mandatory fields are not default values. If fields are
// invalid the Electronic Exchange will be returned.
func (fc *FileControl) fieldInclusion() error {
if fc.recordType == "" {
return &FieldError{FieldName: "recordType",
Value: fc.recordType,
Msg: msgFieldInclusion + ", did you use FileControl()?"}
}
if fc.CashLetterCount == 0 {
return &FieldError{FieldName: "CashLetterCount",
Value: fc.CashLetterCountField(),
Msg: msgFieldInclusion + ", did you use FileControl()?"}
}
if fc.TotalRecordCount == 0 {
return &FieldError{FieldName: "TotalRecordCount",
Value: fc.TotalRecordCountField(),
Msg: msgFieldInclusion + ", did you use FileControl()?"}
}
if fc.TotalItemCount == 0 {
return &FieldError{FieldName: "TotalItemCount",
Value: fc.TotalItemCountField(),
Msg: msgFieldInclusion + ", did you use FileControl()?"}
}
if fc.FileTotalAmount == 0 {
return &FieldError{FieldName: "FileTotalAmount",
Value: fc.FileTotalAmountField(),
Msg: msgFieldInclusion + ", did you use FileControl()?"}
}
return nil
}
// CashLetterCountField gets a string of the CashLetterCount zero padded
func (fc *FileControl) CashLetterCountField() string {
return fc.numericField(fc.CashLetterCount, 6)
}
// TotalRecordCountField gets a string of the TotalRecordCount zero padded
func (fc *FileControl) TotalRecordCountField() string {
return fc.numericField(fc.TotalRecordCount, 8)
}
// TotalItemCountField gets a string of TotalItemCount zero padded
func (fc *FileControl) TotalItemCountField() string {
return fc.numericField(fc.TotalItemCount, 8)
}
// FileTotalAmountField gets a string of FileTotalAmount zero padded
func (fc *FileControl) FileTotalAmountField() string {
return fc.numericField(fc.FileTotalAmount, 16)
}
// ImmediateOriginContactNameField gets the ImmediateOriginContactName field padded
func (fc *FileControl) ImmediateOriginContactNameField() string {
return fc.alphaField(fc.ImmediateOriginContactName, 14)
}
// ImmediateOriginContactPhoneNumberField gets the ImmediateOriginContactPhoneNumber field padded
func (fc *FileControl) ImmediateOriginContactPhoneNumberField() string {
return fc.alphaField(fc.ImmediateOriginContactPhoneNumber, 10)
}
// CreditTotalIndicatorField gets a string of the CreditTotalIndicator field
func (fc *FileControl) CreditTotalIndicatorField() string {
return fc.numericField(fc.CreditTotalIndicator, 1)
}
// reservedField gets reserved - blank space
func (fc *FileControl) reservedField() string {
return fc.alphaField(fc.reserved, 15)
}