| 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 |
) |
| 11 |
|
| 12 |
var accountBalanceRequests = map[int]func(account domain.AccountConnection, allAccounts bool) AccountBalanceRequest{ |
| 13 |
5: NewAccountBalanceRequestV5, |
| 14 |
6: NewAccountBalanceRequestV6, |
| 15 |
} |
| 16 |
|
| 17 |
// AccountBalanceRequestBuilder returns the highest matching versioned segment |
| 18 |
func AccountBalanceRequestBuilder(versions []int) (func(account domain.AccountConnection, allAccounts bool) AccountBalanceRequest, error) { |
| 19 |
sort.Sort(sort.Reverse(sort.IntSlice(versions))) |
| 20 |
for _, version := range versions { |
| 21 |
builder, ok := accountBalanceRequests[version] |
| 22 |
if ok { |
| 23 |
return builder, nil |
| 24 |
} |
| 25 |
} |
| 26 |
return nil, fmt.Errorf("unsupported versions %v", versions) |
| 27 |
} |
| 28 |
|
| 29 |
type AccountBalanceRequest interface { |
| 30 |
ClientSegment |
| 31 |
SetContinuationMark(continuationMark string) |
| 32 |
} |
| 33 |
|
| 34 |
func NewAccountBalanceRequestV5(account domain.AccountConnection, allAccounts bool) AccountBalanceRequest { |
| 35 |
a := &AccountBalanceRequestSegmentV5{ |
| 36 |
AccountConnection: element.NewAccountConnection(account), |
| 37 |
AllAccounts: element.NewBoolean(allAccounts), |
| 38 |
} |
| 39 |
a.ClientSegment = NewBasicSegment(1, a) |
| 40 |
return a |
| 41 |
} |
| 42 |
|
| 43 |
type AccountBalanceRequestSegmentV5 struct { |
| 44 |
ClientSegment |
| 45 |
AccountConnection *element.AccountConnectionDataElement |
| 46 |
AllAccounts *element.BooleanDataElement |
| 47 |
MaxEntries *element.NumberDataElement |
| 48 |
ContinuationReference *element.AlphaNumericDataElement |
| 49 |
} |
| 50 |
|
| 51 |
func (a *AccountBalanceRequestSegmentV5) Version() int { return 5 } |
| 52 |
func (a *AccountBalanceRequestSegmentV5) ID() string { return "HKSAL" } |
| 53 |
func (a *AccountBalanceRequestSegmentV5) referencedId() string { return "" } |
| 54 |
func (a *AccountBalanceRequestSegmentV5) sender() string { return senderUser } |
| 55 |
|
| 56 |
func (a *AccountBalanceRequestSegmentV5) elements() []element.DataElement { |
| 57 |
return []element.DataElement{ |
| 58 |
a.AccountConnection, |
| 59 |
a.AllAccounts, |
| 60 |
a.MaxEntries, |
| 61 |
a.ContinuationReference, |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func (a *AccountBalanceRequestSegmentV5) SetContinuationMark(continuationMark string) { |
| 66 |
a.ContinuationReference = element.NewAlphaNumeric(continuationMark, 35) |
| 67 |
} |
| 68 |
|
| 69 |
func NewAccountBalanceRequestV6(account domain.AccountConnection, allAccounts bool) AccountBalanceRequest { |
| 70 |
a := &AccountBalanceRequestSegmentV6{ |
| 71 |
AccountConnection: element.NewAccountConnection(account), |
| 72 |
AllAccounts: element.NewBoolean(allAccounts), |
| 73 |
} |
| 74 |
a.ClientSegment = NewBasicSegment(1, a) |
| 75 |
return a |
| 76 |
} |
| 77 |
|
| 78 |
type AccountBalanceRequestSegmentV6 struct { |
| 79 |
ClientSegment |
| 80 |
AccountConnection *element.AccountConnectionDataElement |
| 81 |
AllAccounts *element.BooleanDataElement |
| 82 |
MaxEntries *element.NumberDataElement |
| 83 |
ContinuationReference *element.AlphaNumericDataElement |
| 84 |
} |
| 85 |
|
| 86 |
func (a *AccountBalanceRequestSegmentV6) Version() int { return 6 } |
| 87 |
func (a *AccountBalanceRequestSegmentV6) ID() string { return "HKSAL" } |
| 88 |
func (a *AccountBalanceRequestSegmentV6) referencedId() string { return "" } |
| 89 |
func (a *AccountBalanceRequestSegmentV6) sender() string { return senderUser } |
| 90 |
|
| 91 |
func (a *AccountBalanceRequestSegmentV6) elements() []element.DataElement { |
| 92 |
return []element.DataElement{ |
| 93 |
a.AccountConnection, |
| 94 |
a.AllAccounts, |
| 95 |
a.MaxEntries, |
| 96 |
a.ContinuationReference, |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
func (a *AccountBalanceRequestSegmentV6) SetContinuationMark(continuationMark string) { |
| 101 |
a.ContinuationReference = element.NewAlphaNumeric(continuationMark, 35) |
| 102 |
} |
| 103 |
|
| 104 |
//go:generate go run ../cmd/unmarshaler/unmarshaler_generator.go -segment AccountBalanceResponseSegment |
| 105 |
|
| 106 |
type AccountBalanceResponse interface { |
| 107 |
BankSegment |
| 108 |
AccountBalance() domain.AccountBalance |
| 109 |
} |
| 110 |
|
| 111 |
type AccountBalanceResponseSegment struct { |
| 112 |
Segment |
| 113 |
AccountConnection *element.AccountConnectionDataElement |
| 114 |
AccountProductName *element.AlphaNumericDataElement |
| 115 |
AccountCurrency *element.CurrencyDataElement |
| 116 |
BookedBalance *element.BalanceDataElement |
| 117 |
EarmarkedBalance *element.BalanceDataElement |
| 118 |
CreditLimit *element.AmountDataElement |
| 119 |
AvailableAmount *element.AmountDataElement |
| 120 |
UsedAmount *element.AmountDataElement |
| 121 |
BookingDate *element.DateDataElement |
| 122 |
BookingTime *element.TimeDataElement |
| 123 |
DueDate *element.DateDataElement |
| 124 |
} |
| 125 |
|
| 126 |
func (a *AccountBalanceResponseSegment) Version() int { return 5 } |
| 127 |
func (a *AccountBalanceResponseSegment) ID() string { return "HISAL" } |
| 128 |
func (a *AccountBalanceResponseSegment) referencedId() string { return "HKSAL" } |
| 129 |
func (a *AccountBalanceResponseSegment) sender() string { return senderBank } |
| 130 |
|
| 131 |
func (a *AccountBalanceResponseSegment) AccountBalance() domain.AccountBalance { |
| 132 |
balance := domain.AccountBalance{ |
| 133 |
Account: a.AccountConnection.Val(), |
| 134 |
ProductName: a.AccountProductName.Val(), |
| 135 |
Currency: a.AccountCurrency.Val(), |
| 136 |
BookedBalance: a.BookedBalance.Balance(), |
| 137 |
} |
| 138 |
if earmarked := a.EarmarkedBalance; earmarked != nil { |
| 139 |
val := earmarked.Balance() |
| 140 |
balance.EarmarkedBalance = &val |
| 141 |
} |
| 142 |
if credit := a.CreditLimit; credit != nil { |
| 143 |
val := credit.Val() |
| 144 |
balance.CreditLimit = &val |
| 145 |
} |
| 146 |
if available := a.AvailableAmount; available != nil { |
| 147 |
val := available.Val() |
| 148 |
balance.AvailableAmount = &val |
| 149 |
} |
| 150 |
if used := a.UsedAmount; used != nil { |
| 151 |
val := used.Val() |
| 152 |
balance.UsedAmount = &val |
| 153 |
} |
| 154 |
if date := a.BookingDate; date != nil { |
| 155 |
val := date.Val() |
| 156 |
balance.BookingDate = &val |
| 157 |
} |
| 158 |
if t := a.BookingTime; t != nil { |
| 159 |
val := t.Val() |
| 160 |
balance.BookingDate.Add(val.Sub(time.Time{})) |
| 161 |
} |
| 162 |
if dueDate := a.DueDate; dueDate != nil { |
| 163 |
val := dueDate.Val() |
| 164 |
balance.DueDate = &val |
| 165 |
} |
| 166 |
return balance |
| 167 |
} |
| 168 |
|
| 169 |
func (a *AccountBalanceResponseSegment) elements() []element.DataElement { |
| 170 |
return []element.DataElement{ |
| 171 |
a.AccountConnection, |
| 172 |
a.AccountProductName, |
| 173 |
a.AccountCurrency, |
| 174 |
a.BookedBalance, |
| 175 |
a.EarmarkedBalance, |
| 176 |
a.CreditLimit, |
| 177 |
a.AvailableAmount, |
| 178 |
a.UsedAmount, |
| 179 |
a.BookingDate, |
| 180 |
a.BookingTime, |
| 181 |
a.DueDate, |
| 182 |
} |
| 183 |
} |