Debian Codegraph @

segment/account_transaction.go

raw
1 package segment
2
3 import (
4 "fmt"
5 "sort"
6 "time"
7
8 "github.com/mitch000001/go-hbci/domain"
9 "github.com/mitch000001/go-hbci/element"
10 "github.com/mitch000001/go-hbci/swift"
11 )
12
13 var accountTransactionRequests = map[int](func(account domain.AccountConnection, allAccounts bool) *AccountTransactionRequestSegment){
14 6: NewAccountTransactionRequestSegmentV6,
15 5: NewAccountTransactionRequestSegmentV5,
16 }
17
18 func AccountTransactionRequestBuilder(versions []int) (func(account domain.AccountConnection, allAccounts bool) *AccountTransactionRequestSegment, error) {
19 sort.Sort(sort.Reverse(sort.IntSlice(versions)))
20 for _, version := range versions {
21 builder, ok := accountTransactionRequests[version]
22 if ok {
23 return builder, nil
24 }
25 }
26 return nil, fmt.Errorf("unsupported versions %v", versions)
27 }
28
29 func SepaAccountTransactionRequestBuilder(versions []int) (func(account domain.InternationalAccountConnection, allAccounts bool) *AccountTransactionRequestSegment, error) {
30 sort.Sort(sort.Reverse(sort.IntSlice(versions)))
31 for _, version := range versions {
32 if version > 6 {
33 continue
34 }
35 switch version {
36 case 7:
37 return NewAccountTransactionRequestSegmentV7, nil
38 }
39 }
40 return nil, fmt.Errorf("unsupported versions %v", versions)
41 }
42
43 type AccountTransactionRequestSegment struct {
44 AccountTransactionRequest
45 }
46
47 type AccountTransactionRequest interface {
48 ClientSegment
49 SetContinuationReference(string)
50 SetTransactionRange(domain.Timeframe)
51 }
52
53 func NewAccountTransactionRequestSegmentV5(account domain.AccountConnection, allAccounts bool) *AccountTransactionRequestSegment {
54 v5 := &AccountTransactionRequestV5{
55 Account: element.NewAccountConnection(account),
56 AllAccounts: element.NewBoolean(allAccounts),
57 }
58 v5.ClientSegment = NewBasicSegment(1, v5)
59
60 segment := &AccountTransactionRequestSegment{
61 AccountTransactionRequest: v5,
62 }
63 return segment
64 }
65
66 type AccountTransactionRequestV5 struct {
67 ClientSegment
68 Account *element.AccountConnectionDataElement
69 AllAccounts *element.BooleanDataElement
70 From *element.DateDataElement
71 To *element.DateDataElement
72 MaxEntries *element.NumberDataElement
73 ContinuationReference *element.AlphaNumericDataElement
74 }
75
76 func (a *AccountTransactionRequestV5) SetContinuationReference(aufsetzpoint string) {
77 a.ContinuationReference = element.NewAlphaNumeric(aufsetzpoint, len(aufsetzpoint))
78 }
79
80 func (a *AccountTransactionRequestV5) SetTransactionRange(timeframe domain.Timeframe) {
81 from := timeframe.StartDate
82 to := timeframe.EndDate
83 if to.IsZero() {
84 to = domain.NewShortDate(time.Now())
85 }
86 if from.IsZero() { // use sane defaults
87 from = domain.NewShortDate(time.Now().AddDate(0, -1, 0))
88 }
89 a.From = element.NewDate(from.Time)
90 a.To = element.NewDate(to.Time)
91 }
92
93 func (a *AccountTransactionRequestV5) Version() int { return 5 }
94 func (a *AccountTransactionRequestV5) ID() string { return "HKKAZ" }
95 func (a *AccountTransactionRequestV5) referencedId() string { return "" }
96 func (a *AccountTransactionRequestV5) sender() string { return senderUser }
97
98 func (a *AccountTransactionRequestV5) elements() []element.DataElement {
99 return []element.DataElement{
100 a.Account,
101 a.AllAccounts,
102 a.From,
103 a.To,
104 a.MaxEntries,
105 a.ContinuationReference,
106 }
107 }
108
109 func NewAccountTransactionRequestSegmentV6(account domain.AccountConnection, allAccounts bool) *AccountTransactionRequestSegment {
110 v6 := &AccountTransactionRequestV6{
111 Account: element.NewAccountConnection(account),
112 AllAccounts: element.NewBoolean(allAccounts),
113 }
114 v6.ClientSegment = NewBasicSegment(1, v6)
115
116 segment := &AccountTransactionRequestSegment{
117 AccountTransactionRequest: v6,
118 }
119 return segment
120 }
121
122 type AccountTransactionRequestV6 struct {
123 ClientSegment
124 Account *element.AccountConnectionDataElement
125 AllAccounts *element.BooleanDataElement
126 From *element.DateDataElement
127 To *element.DateDataElement
128 MaxEntries *element.NumberDataElement
129 ContinuationReference *element.AlphaNumericDataElement
130 }
131
132 func (a *AccountTransactionRequestV6) SetContinuationReference(aufsetzpoint string) {
133 a.ContinuationReference = element.NewAlphaNumeric(aufsetzpoint, len(aufsetzpoint))
134 }
135
136 func (a *AccountTransactionRequestV6) SetTransactionRange(timeframe domain.Timeframe) {
137 from := timeframe.StartDate
138 to := timeframe.EndDate
139 if to.IsZero() {
140 to = domain.NewShortDate(time.Now())
141 }
142 if from.IsZero() { // use sane defaults
143 from = domain.NewShortDate(time.Now().AddDate(0, -1, 0))
144 }
145 a.From = element.NewDate(from.Time)
146 a.To = element.NewDate(to.Time)
147 }
148
149 func (a *AccountTransactionRequestV6) Version() int { return 6 }
150 func (a *AccountTransactionRequestV6) ID() string { return "HKKAZ" }
151 func (a *AccountTransactionRequestV6) referencedId() string { return "" }
152 func (a *AccountTransactionRequestV6) sender() string { return senderUser }
153
154 func (a *AccountTransactionRequestV6) elements() []element.DataElement {
155 return []element.DataElement{
156 a.Account,
157 a.AllAccounts,
158 a.From,
159 a.To,
160 a.MaxEntries,
161 a.ContinuationReference,
162 }
163 }
164
165 func NewAccountTransactionRequestSegmentV7(account domain.InternationalAccountConnection, allAccounts bool) *AccountTransactionRequestSegment {
166 v7 := &AccountTransactionRequestV7{
167 InternationalAccount: element.NewInternationalAccountConnection(account),
168 AllAccounts: element.NewBoolean(allAccounts),
169 }
170 v7.ClientSegment = NewBasicSegment(1, v7)
171
172 segment := &AccountTransactionRequestSegment{
173 AccountTransactionRequest: v7,
174 }
175 return segment
176 }
177
178 type AccountTransactionRequestV7 struct {
179 ClientSegment
180 InternationalAccount *element.InternationalAccountConnectionDataElement
181 AllAccounts *element.BooleanDataElement
182 From *element.DateDataElement
183 To *element.DateDataElement
184 MaxEntries *element.NumberDataElement
185 ContinuationReference *element.AlphaNumericDataElement
186 }
187
188 func (a *AccountTransactionRequestV7) SetContinuationReference(aufsetzpoint string) {
189 a.ContinuationReference = element.NewAlphaNumeric(aufsetzpoint, len(aufsetzpoint))
190 }
191
192 func (a *AccountTransactionRequestV7) SetTransactionRange(timeframe domain.Timeframe) {
193 from := timeframe.StartDate
194 to := timeframe.EndDate
195 if to.IsZero() {
196 to = domain.NewShortDate(time.Now())
197 }
198 if from.IsZero() { // use sane defaults
199 from = domain.NewShortDate(time.Now().AddDate(0, -1, 0))
200 }
201 a.From = element.NewDate(from.Time)
202 a.To = element.NewDate(to.Time)
203 }
204
205 func (a *AccountTransactionRequestV7) Version() int { return 7 }
206 func (a *AccountTransactionRequestV7) ID() string { return "HKKAZ" }
207 func (a *AccountTransactionRequestV7) referencedId() string { return "" }
208 func (a *AccountTransactionRequestV7) sender() string { return senderUser }
209
210 func (a *AccountTransactionRequestV7) elements() []element.DataElement {
211 return []element.DataElement{
212 a.InternationalAccount,
213 a.AllAccounts,
214 a.From,
215 a.To,
216 a.MaxEntries,
217 a.ContinuationReference,
218 }
219 }
220
221 type AccountTransactionResponse interface {
222 BankSegment
223 BookedSwiftTransactions() *swift.MT940Messages
224 }
225
226 //go:generate go run ../cmd/unmarshaler/unmarshaler_generator.go -segment AccountTransactionResponseSegment -segment_interface AccountTransactionResponse -segment_versions="AccountTransactionResponseSegmentV5:5:Segment,AccountTransactionResponseSegmentV6:6:Segment,AccountTransactionResponseSegmentV7:7:Segment"
227
228 type AccountTransactionResponseSegment struct {
229 AccountTransactionResponse
230 }
231
232 type AccountTransactionResponseSegmentV5 struct {
233 Segment
234 BookedTransactions *element.SwiftMT940DataElement
235 UnbookedTransactions *element.BinaryDataElement
236 }
237
238 func (a *AccountTransactionResponseSegmentV5) BookedSwiftTransactions() *swift.MT940Messages {
239 return a.BookedTransactions.Val()
240 }
241
242 func (a *AccountTransactionResponseSegmentV5) Version() int { return 5 }
243 func (a *AccountTransactionResponseSegmentV5) ID() string { return "HIKAZ" }
244 func (a *AccountTransactionResponseSegmentV5) referencedId() string { return "HKKAZ" }
245 func (a *AccountTransactionResponseSegmentV5) sender() string { return senderBank }
246
247 func (a *AccountTransactionResponseSegmentV5) elements() []element.DataElement {
248 return []element.DataElement{
249 a.BookedTransactions,
250 a.UnbookedTransactions,
251 }
252 }
253
254 type AccountTransactionResponseSegmentV6 struct {
255 Segment
256 BookedTransactions *element.SwiftMT940DataElement
257 UnbookedTransactions *element.BinaryDataElement
258 }
259
260 func (a *AccountTransactionResponseSegmentV6) BookedSwiftTransactions() *swift.MT940Messages {
261 return a.BookedTransactions.Val()
262 }
263
264 func (a *AccountTransactionResponseSegmentV6) Version() int { return 6 }
265 func (a *AccountTransactionResponseSegmentV6) ID() string { return "HIKAZ" }
266 func (a *AccountTransactionResponseSegmentV6) referencedId() string { return "HKKAZ" }
267 func (a *AccountTransactionResponseSegmentV6) sender() string { return senderBank }
268
269 func (a *AccountTransactionResponseSegmentV6) elements() []element.DataElement {
270 return []element.DataElement{
271 a.BookedTransactions,
272 a.UnbookedTransactions,
273 }
274 }
275
276 type AccountTransactionResponseSegmentV7 struct {
277 Segment
278 BookedTransactions *element.SwiftMT940DataElement
279 UnbookedTransactions *element.BinaryDataElement
280 }
281
282 func (a *AccountTransactionResponseSegmentV7) BookedSwiftTransactions() *swift.MT940Messages {
283 return a.BookedTransactions.Val()
284 }
285
286 func (a *AccountTransactionResponseSegmentV7) Version() int { return 7 }
287 func (a *AccountTransactionResponseSegmentV7) ID() string { return "HIKAZ" }
288 func (a *AccountTransactionResponseSegmentV7) referencedId() string { return "HKKAZ" }
289 func (a *AccountTransactionResponseSegmentV7) sender() string { return senderBank }
290
291 func (a *AccountTransactionResponseSegmentV7) elements() []element.DataElement {
292 return []element.DataElement{
293 a.BookedTransactions,
294 a.UnbookedTransactions,
295 }
296 }