-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathprimaryRemittanceDocument.go
198 lines (172 loc) · 7.21 KB
/
primaryRemittanceDocument.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
// 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 wire
import (
"encoding/json"
"strings"
"unicode/utf8"
)
// PrimaryRemittanceDocument is primary remittance document
type PrimaryRemittanceDocument struct {
// tag
tag string
// DocumentTypeCode * `AROI` - Accounts Receivable Open Item * `BOLD` - Bill of Lading Shipping Notice * `CINV` - Commercial Invoice * `CMCN` - Commercial Contract * `CNFA` - Credit Note Related to Financial Adjustment * `CREN` - Credit Note * `DEBN` - Debit Note * `DISP` - Dispatch Advice * `DNFA` - Debit Note Related to Financial Adjustment HIRI Hire Invoice * `MSIN` - Metered Service Invoice * `PROP` - Proprietary Document Type * `PUOR` - Purchase Order * `SBIN` - Self Billed Invoice * `SOAC` - Statement of Account * `TSUT` - Trade Services Utility Transaction VCHR Voucher
DocumentTypeCode string `json:"documentTypeCode,omitempty"`
// ProprietaryDocumentTypeCode
ProprietaryDocumentTypeCode string `json:"proprietaryDocumentTypeCode,omitempty"`
// DocumentIdentificationNumber
DocumentIdentificationNumber string `json:"documentIdentificationNumber,omitempty"`
// Issuer
Issuer string `json:"issuer,omitempty"`
// validator is composed for data validation
validator
// converters is composed for WIRE to GoLang Converters
converters
}
// NewPrimaryRemittanceDocument returns a new PrimaryRemittanceDocument
func NewPrimaryRemittanceDocument() *PrimaryRemittanceDocument {
prd := &PrimaryRemittanceDocument{
tag: TagPrimaryRemittanceDocument,
}
return prd
}
// Parse takes the input string and parses the PrimaryRemittanceDocument values
//
// Parse provides no guarantee about all fields being filled in. Callers should make a Validate() call to confirm
// successful parsing and data validity.
func (prd *PrimaryRemittanceDocument) Parse(record string) error {
if utf8.RuneCountInString(record) < 13 {
return NewTagMinLengthErr(13, len(record))
}
prd.tag = record[:6]
prd.DocumentTypeCode = record[6:10]
length := 10
value, read, err := prd.parseVariableStringField(record[length:], 35)
if err != nil {
return fieldError("ProprietaryDocumentTypeCode", err)
}
prd.ProprietaryDocumentTypeCode = value
length += read
value, read, err = prd.parseVariableStringField(record[length:], 35)
if err != nil {
return fieldError("DocumentIdentificationNumber", err)
}
prd.DocumentIdentificationNumber = value
length += read
value, read, err = prd.parseVariableStringField(record[length:], 35)
if err != nil {
return fieldError("Issuer", err)
}
prd.Issuer = value
length += read
if err := prd.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}
return nil
}
func (prd *PrimaryRemittanceDocument) UnmarshalJSON(data []byte) error {
type Alias PrimaryRemittanceDocument
aux := struct {
*Alias
}{
(*Alias)(prd),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
prd.tag = TagPrimaryRemittanceDocument
return nil
}
// String returns a fixed-width PrimaryRemittanceDocument record
func (prd *PrimaryRemittanceDocument) String() string {
return prd.Format(FormatOptions{
VariableLengthFields: false,
})
}
// Format returns a PrimaryRemittanceDocument record formatted according to the FormatOptions
func (prd *PrimaryRemittanceDocument) Format(options FormatOptions) string {
var buf strings.Builder
buf.Grow(115)
buf.WriteString(prd.tag)
buf.WriteString(prd.DocumentTypeCodeField())
buf.WriteString(prd.FormatProprietaryDocumentTypeCode(options) + Delimiter)
buf.WriteString(prd.FormatDocumentIdentificationNumber(options) + Delimiter)
buf.WriteString(prd.FormatIssuer(options) + Delimiter)
if options.VariableLengthFields {
return prd.stripDelimiters(buf.String())
} else {
return buf.String()
}
}
// Validate performs WIRE format rule checks on PrimaryRemittanceDocument and returns an error if not Validated
// The first error encountered is returned and stops that parsing.
// Document Type Code and Document Identification Number are mandatory for each set of remittance data.
// Proprietary Document Type Code is mandatory for Document Type Code PROP; otherwise not permitted.
func (prd *PrimaryRemittanceDocument) Validate() error {
if err := prd.fieldInclusion(); err != nil {
return err
}
if prd.tag != TagPrimaryRemittanceDocument {
return fieldError("tag", ErrValidTagForType, prd.tag)
}
if err := prd.isDocumentTypeCode(prd.DocumentTypeCode); err != nil {
return fieldError("DocumentTypeCode", err, prd.DocumentTypeCode)
}
if err := prd.isAlphanumeric(prd.ProprietaryDocumentTypeCode); err != nil {
return fieldError("ProprietaryDocumentTypeCode", err, prd.ProprietaryDocumentTypeCode)
}
if err := prd.isAlphanumeric(prd.DocumentIdentificationNumber); err != nil {
return fieldError("DocumentIdentificationNumber", err, prd.DocumentIdentificationNumber)
}
if err := prd.isAlphanumeric(prd.Issuer); err != nil {
return fieldError("Issuer", err, prd.Issuer)
}
return nil
}
// fieldInclusion validate mandatory fields. If fields are
// invalid the WIRE will return an error.
func (prd *PrimaryRemittanceDocument) fieldInclusion() error {
if prd.DocumentIdentificationNumber == "" {
return fieldError("DocumentIdentificationNumber", ErrFieldRequired)
}
switch prd.DocumentTypeCode {
case ProprietaryDocumentType:
if prd.ProprietaryDocumentTypeCode == "" {
return fieldError("ProprietaryDocumentTypeCode", ErrFieldRequired)
}
default:
if strings.TrimSpace(prd.ProprietaryDocumentTypeCode) != "" {
return fieldError("ProprietaryDocumentTypeCode", ErrInvalidProperty, prd.ProprietaryDocumentTypeCode)
}
}
return nil
}
// DocumentTypeCodeField gets a string of the DocumentTypeCode field
func (prd *PrimaryRemittanceDocument) DocumentTypeCodeField() string {
return prd.alphaField(prd.DocumentTypeCode, 4)
}
// ProprietaryDocumentTypeCodeField gets a string of the ProprietaryDocumentTypeCode field
func (prd *PrimaryRemittanceDocument) ProprietaryDocumentTypeCodeField() string {
return prd.alphaField(prd.ProprietaryDocumentTypeCode, 35)
}
// DocumentIdentificationNumberField gets a string of the DocumentIdentificationNumber field
func (prd *PrimaryRemittanceDocument) DocumentIdentificationNumberField() string {
return prd.alphaField(prd.DocumentIdentificationNumber, 35)
}
// IssuerField gets a string of the Issuer field
func (prd *PrimaryRemittanceDocument) IssuerField() string {
return prd.alphaField(prd.Issuer, 35)
}
// FormatProprietaryDocumentTypeCode returns ProprietaryDocumentTypeCode formatted according to the FormatOptions
func (prd *PrimaryRemittanceDocument) FormatProprietaryDocumentTypeCode(options FormatOptions) string {
return prd.formatAlphaField(prd.ProprietaryDocumentTypeCode, 35, options)
}
// FormatDocumentIdentificationNumber returns DocumentIdentificationNumber formatted according to the FormatOptions
func (prd *PrimaryRemittanceDocument) FormatDocumentIdentificationNumber(options FormatOptions) string {
return prd.formatAlphaField(prd.DocumentIdentificationNumber, 35, options)
}
// FormatIssuer returns Issuer formatted according to the FormatOptions
func (prd *PrimaryRemittanceDocument) FormatIssuer(options FormatOptions) string {
return prd.formatAlphaField(prd.Issuer, 35, options)
}