-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathbundleControl.go
196 lines (175 loc) · 6.29 KB
/
bundleControl.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
// 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"
)
// Errors specific to a BundleControl Record
// BundleControl Record
type BundleControl 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
// BundleItemsCount identifies the total number of items within the bundle.
BundleItemsCount int `json:"bundleitemsCount"`
// BundleTotalAmount identifies the total amount of item amounts within the bundle.
BundleTotalAmount int `json:"bundleTotalAmount"`
// MICRValidTotalAmount identifies the total amount of all CheckDetail Records within the bundle which
// contains 1 in the MICRValidIndicator .
MICRValidTotalAmount int `json:"micrValidTotalAmount"`
// BundleImagesCount identifies the total number of Image ViewDetail Records within the bundle.
BundleImagesCount int `json:"bundleImagesCount"`
// UserField is used at the discretion of users of the standard.
UserField string `json:"userField"`
// CreditTotalIndicator identifies a code that indicates whether Credits Items are included in the totals.
// If so they will be included in Items CashLetterItemsCount, CashLetterTotalAmount and
// CashLetterImagesCount.
// 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
}
// NewBundleControl returns a new BundleControl with default values for non exported fields
func NewBundleControl() *BundleControl {
bc := &BundleControl{}
bc.setRecordType()
return bc
}
func (bc *BundleControl) setRecordType() {
if bc == nil {
return
}
bc.recordType = "70"
bc.reserved = " "
}
// Parse takes the input record string and parses the BundleControl values
func (bc *BundleControl) Parse(record string) {
if utf8.RuneCountInString(record) < 56 {
return
}
// Character position 1-2, Always "70"
bc.setRecordType()
// 03-06
bc.BundleItemsCount = bc.parseNumField(record[2:6])
// 07-18
bc.BundleTotalAmount = bc.parseNumField(record[6:18])
// 19-30
bc.MICRValidTotalAmount = bc.parseNumField(record[18:30])
// 31-35
bc.BundleImagesCount = bc.parseNumField(record[30:35])
// 36-55
bc.UserField = bc.parseStringField(record[35:55])
// 56-56
bc.CreditTotalIndicator = bc.parseNumField(record[55:56])
// 57-80
bc.reserved = " "
}
func (bc *BundleControl) UnmarshalJSON(data []byte) error {
type Alias BundleControl
aux := struct {
*Alias
}{
(*Alias)(bc),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
bc.setRecordType()
return nil
}
// String writes the BundleControl struct to a string.
func (bc *BundleControl) String() string {
if bc == nil {
return ""
}
var buf strings.Builder
buf.Grow(80)
buf.WriteString(bc.recordType)
buf.WriteString(bc.BundleItemsCountField())
buf.WriteString(bc.BundleTotalAmountField())
buf.WriteString(bc.MICRValidTotalAmountField())
buf.WriteString(bc.BundleImagesCountField())
buf.WriteString(bc.UserFieldField())
buf.WriteString(bc.CreditTotalIndicatorField())
buf.WriteString(bc.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 (bc *BundleControl) Validate() error {
if err := bc.fieldInclusion(); err != nil {
return err
}
if bc.recordType != "70" {
msg := fmt.Sprintf(msgRecordType, 70)
return &FieldError{FieldName: "recordType", Value: bc.recordType, Msg: msg}
}
if err := bc.isAlphanumericSpecial(bc.UserField); err != nil {
return &FieldError{FieldName: "UserField", Value: bc.UserField, Msg: err.Error()}
}
if bc.CreditTotalIndicatorField() != "" {
if err := bc.isCreditTotalIndicator(bc.CreditTotalIndicator); err != nil {
return &FieldError{FieldName: "CreditTotalIndicator", Value: bc.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 (bc *BundleControl) fieldInclusion() error {
if bc.recordType == "" {
return &FieldError{FieldName: "recordType",
Value: bc.recordType,
Msg: msgFieldInclusion + ", did you use BundleControl()?"}
}
if bc.BundleItemsCount == 0 {
return &FieldError{FieldName: "BundleItemsCount",
Value: bc.BundleItemsCountField(),
Msg: msgFieldInclusion + ", did you use BundleControl()?"}
}
if bc.BundleTotalAmount == 0 {
return &FieldError{FieldName: "BundleTotalAmount",
Value: bc.BundleTotalAmountField(),
Msg: msgFieldInclusion + ", did you use BundleControl()?"}
}
return nil
}
// BundleItemsCountField gets a string of the BundleItemsCount zero padded
func (bc *BundleControl) BundleItemsCountField() string {
return bc.numericField(bc.BundleItemsCount, 4)
}
// BundleTotalAmountField gets a string of the BundleTotalAmount zero padded
func (bc *BundleControl) BundleTotalAmountField() string {
return bc.numericField(bc.BundleTotalAmount, 12)
}
// MICRValidTotalAmountField gets a string of the MICRValidTotalAmount zero padded
func (bc *BundleControl) MICRValidTotalAmountField() string {
return bc.numericField(bc.MICRValidTotalAmount, 12)
}
// BundleImagesCountField gets a string of the BundleImagesCount zero padded
func (bc *BundleControl) BundleImagesCountField() string {
return bc.numericField(bc.BundleImagesCount, 5)
}
// UserFieldField gets the UserField field
func (bc *BundleControl) UserFieldField() string {
return bc.alphaField(bc.UserField, 20)
}
// CreditTotalIndicatorField gets a string of the CreditTotalIndicator field
func (bc *BundleControl) CreditTotalIndicatorField() string {
return bc.numericField(bc.CreditTotalIndicator, 1)
}
// reservedField gets reserved - blank space
func (bc *BundleControl) reservedField() string {
return bc.alphaField(bc.reserved, 24)
}