-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadjustment.go
152 lines (136 loc) · 5.16 KB
/
adjustment.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
// 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"
)
// Adjustment is adjustment
type Adjustment struct {
// tag
tag string
// Adjustment * `01` - Pricing Error * `03` - Extension Error * `04` - Item Not Accepted (Damaged) * `05` - Item Not Accepted (Quality) * `06` - Quantity Contested 07 Incorrect Product * `11` - Returns (Damaged) * `12` - Returns (Quality) * `59` - Item Not Received * `75` - Total Order Not Received * `81` - Credit as Agreed * `CM` - Covered by Credit Memo
AdjustmentReasonCode string `json:"adjustmentReasonCode,omitempty"`
// CreditDebitIndicator * `CRDT` - Credit * `DBIT` - Debit
CreditDebitIndicator string `json:"creditDebitIndicator,omitempty"`
// RemittanceAmount is remittance amounts
RemittanceAmount RemittanceAmount `json:"remittanceAmount,omitempty"`
// AdditionalInfo is additional information
AdditionalInfo string `json:"additionalInfo,omitempty"`
// validator is composed for data validation
validator
// converters is composed for WIRE to GoLang Converters
converters
}
// NewAdjustment returns a new Adjustment
func NewAdjustment() *Adjustment {
adj := &Adjustment{
tag: TagAdjustment,
}
return adj
}
// Parse takes the input string and parses the Adjustment 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 (adj *Adjustment) Parse(record string) error {
adj.tag = record[:6]
adj.AdjustmentReasonCode = adj.parseStringField(record[6:8])
adj.CreditDebitIndicator = adj.parseStringField(record[8:12])
adj.RemittanceAmount.CurrencyCode = adj.parseStringField(record[12:15])
optionalFields := strings.Split(record[15:], "*")
if len(optionalFields) >= 1 {
adj.RemittanceAmount.Amount = adj.parseStringField(optionalFields[0])
}
if len(optionalFields) >= 2 {
adj.AdditionalInfo = adj.parseStringField(optionalFields[1])
}
return nil
}
func (adj *Adjustment) UnmarshalJSON(data []byte) error {
type Alias Adjustment
aux := struct {
*Alias
}{
(*Alias)(adj),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
adj.tag = TagAdjustment
return nil
}
// String writes Adjustment
func (adj *Adjustment) String() string {
var buf strings.Builder
buf.Grow(168)
buf.WriteString(adj.tag)
buf.WriteString(adj.AdjustmentReasonCodeField())
buf.WriteString(adj.CreditDebitIndicatorField())
buf.WriteString(adj.CurrencyCodeField())
buf.WriteString(strings.TrimSpace(adj.AmountField()) + "*")
buf.WriteString(strings.TrimSpace(adj.AdditionalInfoField()) + "*")
return adj.cleanupDelimiters(buf.String())
}
// Validate performs WIRE format rule checks on Adjustment and returns an error if not Validated
// The first error encountered is returned and stops that parsing.
// Adjustment Reason, Credit Debit Indicator, Currency Code and Amount are mandatory.
func (adj *Adjustment) Validate() error {
if err := adj.fieldInclusion(); err != nil {
return err
}
if adj.tag != TagAdjustment {
return fieldError("tag", ErrValidTagForType, adj.tag)
}
if err := adj.isAdjustmentReasonCode(adj.AdjustmentReasonCode); err != nil {
return fieldError("AdjustmentReasonCode", err, adj.AdjustmentReasonCode)
}
if err := adj.isCreditDebitIndicator(adj.CreditDebitIndicator); err != nil {
return fieldError("CreditDebitIndicator", err, adj.CreditDebitIndicator)
}
if err := adj.isCurrencyCode(adj.RemittanceAmount.CurrencyCode); err != nil {
return fieldError("CurrencyCode", err, adj.RemittanceAmount.CurrencyCode)
}
if err := adj.isAmount(adj.RemittanceAmount.Amount); err != nil {
return fieldError("Amount", err, adj.RemittanceAmount.Amount)
}
return nil
}
// fieldInclusion validate mandatory fields. If fields are
// invalid the WIRE will return an error.
func (adj *Adjustment) fieldInclusion() error {
if adj.AdjustmentReasonCode == "" {
return fieldError("AdjustmentReasonCode", ErrFieldRequired)
}
if adj.CreditDebitIndicator == "" {
return fieldError("CreditDebitIndicator", ErrFieldRequired)
}
if adj.RemittanceAmount.Amount == "" {
return fieldError("Amount", ErrFieldRequired)
}
if adj.RemittanceAmount.CurrencyCode == "" {
return fieldError("CurrencyCode", ErrFieldRequired)
}
return nil
}
// AdjustmentReasonCodeField gets a string of the AdjustmentReasonCode field
func (adj *Adjustment) AdjustmentReasonCodeField() string {
return adj.alphaField(adj.AdjustmentReasonCode, 2)
}
// CreditDebitIndicatorField gets a string of the CreditDebitIndicator field
func (adj *Adjustment) CreditDebitIndicatorField() string {
return adj.alphaField(adj.CreditDebitIndicator, 4)
}
// CurrencyCodeField gets a string of the CurrencyCode field
func (adj *Adjustment) CurrencyCodeField() string {
return adj.alphaField(adj.RemittanceAmount.CurrencyCode, 3)
}
// AmountField gets a string of the Amount field
func (adj *Adjustment) AmountField() string {
return adj.alphaField(adj.RemittanceAmount.Amount, 19)
}
// AdditionalInfoField gets a string of the AdditionalInfo field
func (adj *Adjustment) AdditionalInfoField() string {
return adj.alphaField(adj.AdditionalInfo, 140)
}