Debian Codegraph @

element/group_data_element_group.go

raw
1 package element
2
3 import (
4 "fmt"
5 "math"
6 "strconv"
7 "time"
8
9 "github.com/mitch000001/go-hbci/charset"
10 "github.com/mitch000001/go-hbci/domain"
11 )
12
13 // NewAmount returns a new AmountDataElement
14 func NewAmount(value float64, currency string) *AmountDataElement {
15 a := &AmountDataElement{
16 Amount: NewValue(value),
17 Currency: NewCurrency(currency),
18 }
19 a.DataElement = NewGroupDataElementGroup(amountGDEG, 2, a)
20 return a
21 }
22
23 // An AmountDataElement represents a value with a currency
24 type AmountDataElement struct {
25 DataElement
26 Amount *ValueDataElement
27 Currency *CurrencyDataElement
28 }
29
30 // Elements returns the child elements of the group
31 func (a *AmountDataElement) Elements() []DataElement {
32 return []DataElement{
33 a.Amount,
34 a.Currency,
35 }
36 }
37
38 // Val returns the value of a as a domain.Amount
39 func (a *AmountDataElement) Val() domain.Amount {
40 return domain.Amount{
41 Amount: a.Amount.Val(),
42 Currency: a.Currency.Val(),
43 }
44 }
45
46 // UnmarshalHBCI unmarshals value into a
47 func (a *AmountDataElement) UnmarshalHBCI(value []byte) error {
48 elements, err := ExtractElements(value)
49 if err != nil {
50 return err
51 }
52 if len(elements) != 2 {
53 return fmt.Errorf("Malformed marshaled value")
54 }
55 a.Amount = &ValueDataElement{}
56 err = a.Amount.UnmarshalHBCI(elements[0])
57 if err != nil {
58 return err
59 }
60 a.Currency = &CurrencyDataElement{}
61 err = a.Currency.UnmarshalHBCI(elements[1])
62 if err != nil {
63 return err
64 }
65 a.DataElement = NewGroupDataElementGroup(amountGDEG, 2, a)
66 return nil
67 }
68
69 // NewBankIdentification returns a new BankIndentificationDataElement
70 func NewBankIdentification(bankID domain.BankID) *BankIdentificationDataElement {
71 b := &BankIdentificationDataElement{
72 CountryCode: NewCountryCode(bankID.CountryCode),
73 BankID: NewAlphaNumeric(bankID.ID, 30),
74 }
75 b.DataElement = NewGroupDataElementGroup(bankIdentificationGDEG, 2, b)
76 return b
77 }
78
79 // A BankIdentificationDataElement represents the identification for a bank institute
80 type BankIdentificationDataElement struct {
81 DataElement
82 CountryCode *CountryCodeDataElement
83 BankID *AlphaNumericDataElement
84 }
85
86 // Val returns the value of b as domain.BankID
87 func (b *BankIdentificationDataElement) Val() domain.BankID {
88 return domain.BankID{
89 CountryCode: b.CountryCode.Val(),
90 ID: b.BankID.Val(),
91 }
92 }
93
94 // Elements returns all child elements of b
95 func (b *BankIdentificationDataElement) Elements() []DataElement {
96 return []DataElement{
97 b.CountryCode,
98 b.BankID,
99 }
100 }
101
102 // UnmarshalHBCI unmarshals value into b
103 func (b *BankIdentificationDataElement) UnmarshalHBCI(value []byte) error {
104 elements, err := ExtractElements(value)
105 if err != nil {
106 return err
107 }
108 if len(elements) < 2 {
109 return fmt.Errorf("Malformed marshaled value")
110 }
111 countryCode := &CountryCodeDataElement{}
112 err = countryCode.UnmarshalHBCI(elements[0])
113 if err != nil {
114 return err
115 }
116 b.CountryCode = countryCode
117 b.BankID = NewAlphaNumeric(charset.ToUTF8(elements[1]), 30)
118 return nil
119 }
120
121 // NewAccountConnection returns a new AccountConnectionDataElement
122 func NewAccountConnection(conn domain.AccountConnection) *AccountConnectionDataElement {
123 a := &AccountConnectionDataElement{
124 AccountID: NewIdentification(conn.AccountID),
125 SubAccountCharacteristics: NewIdentification(conn.SubAccountCharacteristics),
126 CountryCode: NewCountryCode(conn.CountryCode),
127 BankID: NewAlphaNumeric(conn.BankID, 30),
128 }
129 a.DataElement = NewGroupDataElementGroup(accountConnectionGDEG, 4, a)
130 return a
131 }
132
133 // AccountConnectionDataElement represents a bank account
134 type AccountConnectionDataElement struct {
135 DataElement
136 AccountID *IdentificationDataElement
137 SubAccountCharacteristics *IdentificationDataElement
138 CountryCode *CountryCodeDataElement
139 BankID *AlphaNumericDataElement
140 }
141
142 // Elements returns all child elements of a
143 func (a *AccountConnectionDataElement) Elements() []DataElement {
144 return []DataElement{
145 a.AccountID,
146 a.SubAccountCharacteristics,
147 a.CountryCode,
148 a.BankID,
149 }
150 }
151
152 // UnmarshalHBCI unmarshals value into a
153 func (a *AccountConnectionDataElement) UnmarshalHBCI(value []byte) error {
154 elements, err := ExtractElements(value)
155 if err != nil {
156 return err
157 }
158 if len(elements) < 4 {
159 return fmt.Errorf("Malformed AccountConnection")
160 }
161 countryCode, err := strconv.Atoi(charset.ToUTF8(elements[2]))
162 if err != nil {
163 return fmt.Errorf("%T: Malformed CountryCode: %q", a, elements[2])
164 }
165 accountConnection := domain.AccountConnection{
166 AccountID: charset.ToUTF8(elements[0]),
167 SubAccountCharacteristics: charset.ToUTF8(elements[1]),
168 CountryCode: countryCode,
169 BankID: charset.ToUTF8(elements[3]),
170 }
171 *a = *NewAccountConnection(accountConnection)
172 return nil
173 }
174
175 // Val returns the value of a as domain.AccountConnection
176 func (a *AccountConnectionDataElement) Val() domain.AccountConnection {
177 return domain.AccountConnection{
178 AccountID: a.AccountID.Val(),
179 SubAccountCharacteristics: a.SubAccountCharacteristics.Val(),
180 CountryCode: a.CountryCode.Val(),
181 BankID: a.BankID.Val(),
182 }
183 }
184
185 // NewInternationalAccountConnection returns a new InternationalAccountConnectionDataElement
186 func NewInternationalAccountConnection(conn domain.InternationalAccountConnection) *InternationalAccountConnectionDataElement {
187 i := &InternationalAccountConnectionDataElement{
188 IBAN: NewAlphaNumeric(conn.IBAN, 34),
189 BIC: NewAlphaNumeric(conn.BIC, 11),
190 AccountID: NewIdentification(conn.AccountID),
191 SubAccountCharacteristics: NewIdentification(conn.SubAccountCharacteristics),
192 BankID: NewBankIdentification(conn.BankID),
193 }
194 i.DataElement = NewGroupDataElementGroup(internationalAccountConnectionGDEG, 5, i)
195 return i
196 }
197
198 // An InternationalAccountConnectionDataElement represents an international bank account
199 type InternationalAccountConnectionDataElement struct {
200 DataElement
201 IBAN *AlphaNumericDataElement
202 BIC *AlphaNumericDataElement
203 AccountID *IdentificationDataElement
204 SubAccountCharacteristics *IdentificationDataElement
205 BankID *BankIdentificationDataElement
206 }
207
208 // Elements returns the child elements of i
209 func (i *InternationalAccountConnectionDataElement) Elements() []DataElement {
210 return []DataElement{
211 i.IBAN,
212 i.BIC,
213 i.AccountID,
214 i.SubAccountCharacteristics,
215 i.BankID,
216 }
217 }
218
219 // UnmarshalHBCI unmarshals value into i
220 func (i *InternationalAccountConnectionDataElement) UnmarshalHBCI(value []byte) error {
221 elements, err := ExtractElements(value)
222 if err != nil {
223 return err
224 }
225 if len(elements) == 0 {
226 return fmt.Errorf("Malformed AccountConnection")
227 }
228 i.DataElement = NewGroupDataElementGroup(internationalAccountConnectionGDEG, 5, i)
229 if len(elements) > 0 && len(elements[0]) > 0 {
230 i.IBAN = &AlphaNumericDataElement{}
231 err = i.IBAN.UnmarshalHBCI(elements[0])
232 if err != nil {
233 return err
234 }
235 }
236 if len(elements) > 1 && len(elements[1]) > 0 {
237 i.BIC = &AlphaNumericDataElement{}
238 err = i.BIC.UnmarshalHBCI(elements[1])
239 if err != nil {
240 return err
241 }
242 }
243 if len(elements) > 2 && len(elements[2]) > 0 {
244 i.AccountID = &IdentificationDataElement{}
245 err = i.AccountID.UnmarshalHBCI(elements[2])
246 if err != nil {
247 return err
248 }
249 }
250 if len(elements) > 3 && len(elements[3]) > 0 {
251 i.SubAccountCharacteristics = &IdentificationDataElement{}
252 err = i.SubAccountCharacteristics.UnmarshalHBCI(elements[3])
253 if err != nil {
254 return err
255 }
256 }
257 if len(elements) > 4 && len(elements[4]) > 0 {
258 i.BankID = &BankIdentificationDataElement{}
259 err = i.BankID.UnmarshalHBCI(elements[4])
260 if err != nil {
261 return err
262 }
263 }
264 return nil
265 }
266
267 // Val returns the value of i as domain.InternationalAccountConnection
268 func (i *InternationalAccountConnectionDataElement) Val() domain.InternationalAccountConnection {
269 conn := domain.InternationalAccountConnection{}
270 if i.IBAN != nil {
271 conn.IBAN = i.IBAN.Val()
272 }
273 if i.BIC != nil {
274 conn.BIC = i.BIC.Val()
275 }
276 if i.AccountID != nil {
277 conn.AccountID = i.AccountID.Val()
278 }
279 if i.SubAccountCharacteristics != nil {
280 conn.SubAccountCharacteristics = i.SubAccountCharacteristics.Val()
281 }
282 if i.BankID != nil {
283 conn.BankID = i.BankID.Val()
284 }
285 return conn
286 }
287
288 // NewBalance returns a new BalanceDataElement
289 func NewBalance(amount domain.Amount, date time.Time, withTime bool) *BalanceDataElement {
290 var debitCredit string
291 if amount.Amount < 0 {
292 debitCredit = "D"
293 } else {
294 debitCredit = "C"
295 }
296 b := &BalanceDataElement{
297 DebitCreditIndicator: NewAlphaNumeric(debitCredit, 1),
298 Amount: NewValue(math.Abs(amount.Amount)),
299 Currency: NewCurrency(amount.Currency),
300 TransmissionDate: NewDate(date),
301 }
302 if withTime {
303 b.TransmissionTime = NewTime(date)
304 }
305 b.DataElement = NewGroupDataElementGroup(balanceGDEG, 5, b)
306 return b
307 }
308
309 // A BalanceDataElement represents an account balance to a given date
310 type BalanceDataElement struct {
311 DataElement
312 DebitCreditIndicator *AlphaNumericDataElement
313 Amount *ValueDataElement
314 Currency *CurrencyDataElement
315 TransmissionDate *DateDataElement
316 TransmissionTime *TimeDataElement
317 }
318
319 // Elements returns all child elements of b
320 func (b *BalanceDataElement) Elements() []DataElement {
321 return []DataElement{
322 b.DebitCreditIndicator,
323 b.Amount,
324 b.Currency,
325 b.TransmissionDate,
326 b.TransmissionTime,
327 }
328 }
329
330 // Balance returns the balance as domain.Balance
331 func (b *BalanceDataElement) Balance() domain.Balance {
332 sign := b.DebitCreditIndicator.Val()
333 val := b.Amount.Val()
334 if sign == "D" {
335 val = -val
336 }
337 currency := b.Currency.Val()
338 amount := domain.Amount{
339 Amount: val,
340 Currency: currency,
341 }
342 balance := domain.Balance{
343 Amount: amount,
344 TransmissionDate: b.TransmissionDate.Val(),
345 }
346 if transmissionTime := b.TransmissionTime; transmissionTime != nil {
347 val := transmissionTime.Val()
348 balance.TransmissionTime = &val
349 }
350 return balance
351 }
352
353 // UnmarshalHBCI unmarshals value into b
354 func (b *BalanceDataElement) UnmarshalHBCI(value []byte) error {
355 elements, err := ExtractElements(value)
356 if err != nil {
357 return err
358 }
359 if len(elements) < 4 {
360 return fmt.Errorf("%T: Malformed marshaled value", b)
361 }
362 b.DebitCreditIndicator = &AlphaNumericDataElement{}
363 err = b.DebitCreditIndicator.UnmarshalHBCI(elements[0])
364 if err != nil {
365 return err
366 }
367 b.Amount = &ValueDataElement{}
368 err = b.Amount.UnmarshalHBCI(elements[1])
369 if err != nil {
370 return err
371 }
372 b.Currency = &CurrencyDataElement{}
373 err = b.Currency.UnmarshalHBCI(elements[2])
374 if err != nil {
375 return err
376 }
377 b.TransmissionDate = &DateDataElement{}
378 err = b.TransmissionDate.UnmarshalHBCI(elements[3])
379 if err != nil {
380 return err
381 }
382 if len(elements) == 5 {
383 b.TransmissionTime = &TimeDataElement{}
384 err = b.TransmissionTime.UnmarshalHBCI(elements[4])
385 if err != nil {
386 return err
387 }
388 }
389 b.DataElement = NewGroupDataElementGroup(balanceGDEG, 5, b)
390 return nil
391 }
392
393 // Date returns the transmission date of the balance
394 func (b *BalanceDataElement) Date() time.Time {
395 return b.TransmissionDate.Val()
396 }
397
398 // NewAddress creates a new AddressDataElement from address
399 func NewAddress(address domain.Address) *AddressDataElement {
400 a := &AddressDataElement{
401 Name1: NewAlphaNumeric(address.Name1, 35),
402 Name2: NewAlphaNumeric(address.Name2, 35),
403 Street: NewAlphaNumeric(address.Street, 35),
404 PLZ: NewAlphaNumeric(address.PLZ, 10),
405 City: NewAlphaNumeric(address.City, 35),
406 CountryCode: NewCountryCode(address.CountryCode),
407 Phone: NewAlphaNumeric(address.Phone, 35),
408 Fax: NewAlphaNumeric(address.Fax, 35),
409 Email: NewAlphaNumeric(address.Email, 35),
410 }
411 a.DataElement = NewGroupDataElementGroup(addressGDEG, 9, a)
412 return a
413 }
414
415 // AddressDataElement represents an address
416 type AddressDataElement struct {
417 DataElement
418 Name1 *AlphaNumericDataElement
419 Name2 *AlphaNumericDataElement
420 Street *AlphaNumericDataElement
421 PLZ *AlphaNumericDataElement
422 City *AlphaNumericDataElement
423 CountryCode *CountryCodeDataElement
424 Phone *AlphaNumericDataElement
425 Fax *AlphaNumericDataElement
426 Email *AlphaNumericDataElement
427 }
428
429 // Elements returns all child elements of a
430 func (a *AddressDataElement) Elements() []DataElement {
431 return []DataElement{
432 a.Name1,
433 a.Name2,
434 a.Street,
435 a.PLZ,
436 a.City,
437 a.CountryCode,
438 a.Phone,
439 a.Fax,
440 a.Email,
441 }
442 }
443
444 // Address returns the address as a domain.Address
445 func (a *AddressDataElement) Address() domain.Address {
446 return domain.Address{
447 Name1: a.Name1.Val(),
448 Name2: a.Name2.Val(),
449 Street: a.Street.Val(),
450 PLZ: a.PLZ.Val(),
451 City: a.City.Val(),
452 CountryCode: a.CountryCode.Val(),
453 Phone: a.Phone.Val(),
454 Fax: a.Fax.Val(),
455 Email: a.Email.Val(),
456 }
457 }