| 1 |
package element |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
|
| 7 |
"github.com/mitch000001/go-hbci/domain" |
| 8 |
) |
| 9 |
|
| 10 |
// NewAccountLimit creates a new Account limit |
| 11 |
func NewAccountLimit(kind string, amount float64, currency string, days int) *AccountLimitDataElement { |
| 12 |
a := &AccountLimitDataElement{ |
| 13 |
Kind: NewAlphaNumeric(kind, 1), |
| 14 |
Amount: NewAmount(amount, currency), |
| 15 |
Days: NewNumber(days, 3), |
| 16 |
} |
| 17 |
a.DataElement = NewDataElementGroup(accountLimitDEG, 3, a) |
| 18 |
return a |
| 19 |
} |
| 20 |
|
| 21 |
// AccountLimitDataElement defines an Account Limit |
| 22 |
type AccountLimitDataElement struct { |
| 23 |
DataElement |
| 24 |
// Code | Beschreibung |
| 25 |
// -------------------------- |
| 26 |
// E | Einzelauftragslimit |
| 27 |
// T | Tageslimit |
| 28 |
// W | Wochenlimit |
| 29 |
// M | Monatslimit |
| 30 |
// Z  | Zeitlimit |
| 31 |
Kind *AlphaNumericDataElement |
| 32 |
Amount *AmountDataElement |
| 33 |
Days *NumberDataElement |
| 34 |
} |
| 35 |
|
| 36 |
// Val returns the limit as domain.AccountLimit |
| 37 |
func (a *AccountLimitDataElement) Val() domain.AccountLimit { |
| 38 |
return domain.AccountLimit{ |
| 39 |
Kind: a.Kind.Val(), |
| 40 |
Amount: a.Amount.Val(), |
| 41 |
Days: a.Days.Val(), |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
// GroupDataElements returns the grouped DataElements |
| 46 |
func (a *AccountLimitDataElement) GroupDataElements() []DataElement { |
| 47 |
return []DataElement{ |
| 48 |
a.Kind, |
| 49 |
a.Amount, |
| 50 |
a.Days, |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
// NewAllowedBusinessTransactions creates a new |
| 55 |
// AllowedBusinessTransactionDataElement for the given transactions |
| 56 |
func NewAllowedBusinessTransactions(transactions ...domain.BusinessTransaction) *AllowedBusinessTransactionsDataElement { |
| 57 |
var transactionDEs []DataElement |
| 58 |
for _, tr := range transactions { |
| 59 |
transactionDEs = append(transactionDEs, NewAllowedBusinessTransaction(tr)) |
| 60 |
} |
| 61 |
a := &AllowedBusinessTransactionsDataElement{ |
| 62 |
arrayElementGroup: newArrayElementGroup(allowedBusinessTransactionDEG, 0, 999, transactionDEs), |
| 63 |
} |
| 64 |
return a |
| 65 |
} |
| 66 |
|
| 67 |
// AllowedBusinessTransactionsDataElement represents allowed business |
| 68 |
// transactions for a bank institute |
| 69 |
type AllowedBusinessTransactionsDataElement struct { |
| 70 |
*arrayElementGroup |
| 71 |
} |
| 72 |
|
| 73 |
// UnmarshalHBCI unmarshals value into a |
| 74 |
func (a *AllowedBusinessTransactionsDataElement) UnmarshalHBCI(value []byte) error { |
| 75 |
elements := bytes.Split(value, []byte("+")) |
| 76 |
transactions := make([]DataElement, len(elements)) |
| 77 |
for i, elem := range elements { |
| 78 |
tr := &AllowedBusinessTransactionDataElement{} |
| 79 |
err := tr.UnmarshalHBCI(elem) |
| 80 |
if err != nil { |
| 81 |
return err |
| 82 |
} |
| 83 |
transactions[i] = tr |
| 84 |
} |
| 85 |
*a = AllowedBusinessTransactionsDataElement{ |
| 86 |
arrayElementGroup: newArrayElementGroup(allowedBusinessTransactionDEG, 0, 999, transactions), |
| 87 |
} |
| 88 |
return nil |
| 89 |
} |
| 90 |
|
| 91 |
// AllowedBusinessTransactions returns all allowed business transactions |
| 92 |
func (a *AllowedBusinessTransactionsDataElement) AllowedBusinessTransactions() []domain.BusinessTransaction { |
| 93 |
businessTransactions := make([]domain.BusinessTransaction, len(a.array)) |
| 94 |
for i, de := range a.array { |
| 95 |
businessTransactions[i] = de.(*AllowedBusinessTransactionDataElement).Val() |
| 96 |
} |
| 97 |
return businessTransactions |
| 98 |
} |
| 99 |
|
| 100 |
// NewAllowedBusinessTransaction creates a new allowed business transaction |
| 101 |
func NewAllowedBusinessTransaction(businessTransaction domain.BusinessTransaction) *AllowedBusinessTransactionDataElement { |
| 102 |
a := &AllowedBusinessTransactionDataElement{ |
| 103 |
BusinessTransactionID: NewAlphaNumeric(businessTransaction.ID, 6), |
| 104 |
NeededSignatures: NewNumber(businessTransaction.NeededSignatures, 2), |
| 105 |
} |
| 106 |
if businessTransaction.Limit != nil { |
| 107 |
a.Kind = NewAlphaNumeric(businessTransaction.Limit.Kind, 1) |
| 108 |
a.Amount = NewAmount(businessTransaction.Limit.Amount.Amount, businessTransaction.Limit.Amount.Currency) |
| 109 |
a.Days = NewNumber(businessTransaction.Limit.Days, 3) |
| 110 |
} |
| 111 |
a.DataElement = NewDataElementGroup(allowedBusinessTransactionDEG, 5, a) |
| 112 |
return a |
| 113 |
} |
| 114 |
|
| 115 |
// AllowedBusinessTransactionDataElement represents an allowed business |
| 116 |
// transaction for a bank institute |
| 117 |
type AllowedBusinessTransactionDataElement struct { |
| 118 |
DataElement |
| 119 |
BusinessTransactionID *AlphaNumericDataElement |
| 120 |
NeededSignatures *NumberDataElement |
| 121 |
// Code | Beschreibung |
| 122 |
// -------------------------- |
| 123 |
// E | Einzelauftragslimit |
| 124 |
// T | Tageslimit |
| 125 |
// W | Wochenlimit |
| 126 |
// M | Monatslimit |
| 127 |
// Z  | Zeitlimit |
| 128 |
Kind *AlphaNumericDataElement |
| 129 |
Amount *AmountDataElement |
| 130 |
Days *NumberDataElement |
| 131 |
} |
| 132 |
|
| 133 |
// UnmarshalHBCI unmarshals value into a |
| 134 |
func (a *AllowedBusinessTransactionDataElement) UnmarshalHBCI(value []byte) error { |
| 135 |
elements, err := ExtractElements(value) |
| 136 |
if err != nil { |
| 137 |
return err |
| 138 |
} |
| 139 |
if len(elements) < 2 { |
| 140 |
return fmt.Errorf("Malformed marshaled value") |
| 141 |
} |
| 142 |
a.DataElement = NewDataElementGroup(allowedBusinessTransactionDEG, 5, a) |
| 143 |
a.BusinessTransactionID = &AlphaNumericDataElement{} |
| 144 |
err = a.BusinessTransactionID.UnmarshalHBCI(elements[0]) |
| 145 |
if err != nil { |
| 146 |
return err |
| 147 |
} |
| 148 |
a.NeededSignatures = &NumberDataElement{} |
| 149 |
err = a.NeededSignatures.UnmarshalHBCI(elements[1]) |
| 150 |
if err != nil { |
| 151 |
return err |
| 152 |
} |
| 153 |
if len(elements) > 2 && len(elements[2]) > 0 { |
| 154 |
a.Kind = &AlphaNumericDataElement{} |
| 155 |
err = a.Kind.UnmarshalHBCI(elements[2]) |
| 156 |
if err != nil { |
| 157 |
return err |
| 158 |
} |
| 159 |
} |
| 160 |
if len(elements) > 3 && len(elements[3]) > 0 { |
| 161 |
a.Amount = &AmountDataElement{} |
| 162 |
err = a.Amount.UnmarshalHBCI(elements[3]) |
| 163 |
if err != nil { |
| 164 |
return err |
| 165 |
} |
| 166 |
} |
| 167 |
if len(elements) > 4 && len(elements[4]) > 0 { |
| 168 |
a.Days = &NumberDataElement{} |
| 169 |
err = a.Days.UnmarshalHBCI(elements[4]) |
| 170 |
if err != nil { |
| 171 |
return err |
| 172 |
} |
| 173 |
} |
| 174 |
return nil |
| 175 |
} |
| 176 |
|
| 177 |
// Val returns the allowed business transaction |
| 178 |
func (a *AllowedBusinessTransactionDataElement) Val() domain.BusinessTransaction { |
| 179 |
tr := domain.BusinessTransaction{ |
| 180 |
ID: a.BusinessTransactionID.Val(), |
| 181 |
NeededSignatures: a.NeededSignatures.Val(), |
| 182 |
} |
| 183 |
if a.Kind != nil { |
| 184 |
tr.Limit = &domain.AccountLimit{} |
| 185 |
tr.Limit.Kind = a.Kind.Val() |
| 186 |
} |
| 187 |
if a.Amount != nil && tr.Limit != nil { |
| 188 |
tr.Limit.Amount = a.Amount.Val() |
| 189 |
} |
| 190 |
if a.Days != nil && tr.Limit != nil { |
| 191 |
tr.Limit.Days = a.Days.Val() |
| 192 |
} |
| 193 |
return tr |
| 194 |
} |
| 195 |
|
| 196 |
// GroupDataElements returns the grouped DataElements |
| 197 |
func (a *AllowedBusinessTransactionDataElement) GroupDataElements() []DataElement { |
| 198 |
return []DataElement{ |
| 199 |
a.BusinessTransactionID, |
| 200 |
a.NeededSignatures, |
| 201 |
a.Kind, |
| 202 |
a.Amount, |
| 203 |
a.Days, |
| 204 |
} |
| 205 |
} |