| 1 |
// Copyright © 2015 Michael Wagner <[email protected]> |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package main |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"os" |
| 20 |
"time" |
| 21 |
|
| 22 |
"github.com/mitch000001/go-hbci/domain" |
| 23 |
"github.com/mitch000001/go-hbci/iban" |
| 24 |
"github.com/spf13/cobra" |
| 25 |
) |
| 26 |
|
| 27 |
var daysToFetch int |
| 28 |
var transactionsAccount string |
| 29 |
var disableSepa bool |
| 30 |
|
| 31 |
// transactionsCmd represents the transactions command |
| 32 |
var transactionsCmd = &cobra.Command{ |
| 33 |
Use: "transactions", |
| 34 |
Short: "fetch transactions for an account", |
| 35 |
Long: `This command allows to fetch transactions for a specific account. By |
| 36 |
default it will fetch the transactions for the account used to authenticate |
| 37 |
for the last ten days. For example: |
| 38 |
|
| 39 |
banking transactions --accountID=123456789 --daysToFetch=30 |
| 40 |
|
| 41 |
will fetch booked transactions for account 123456789 for the last 30 days.`, |
| 42 |
Run: func(cmd *cobra.Command, args []string) { |
| 43 |
if transactionsAccount == "" { |
| 44 |
transactionsAccount = clientConfig.AccountID |
| 45 |
} |
| 46 |
timeframe := domain.Timeframe{ |
| 47 |
StartDate: domain.NewShortDate(time.Now().AddDate(0, 0, -daysToFetch)), |
| 48 |
} |
| 49 |
|
| 50 |
i, err := iban.NewGerman(clientConfig.BankID, transactionsAccount) |
| 51 |
if err != nil { |
| 52 |
fmt.Println(err) |
| 53 |
os.Exit(1) |
| 54 |
} |
| 55 |
account = domain.InternationalAccountConnection{ |
| 56 |
IBAN: string(i), |
| 57 |
AccountID: transactionsAccount, |
| 58 |
BankID: domain.BankID{CountryCode: 280, ID: clientConfig.BankID}, |
| 59 |
} |
| 60 |
|
| 61 |
if disableSepa { |
| 62 |
fetchTransactions(account, timeframe) |
| 63 |
return |
| 64 |
} |
| 65 |
|
| 66 |
fetchSepaTransactions(account, timeframe) |
| 67 |
}, |
| 68 |
} |
| 69 |
|
| 70 |
func fetchTransactions(account domain.InternationalAccountConnection, timeframe domain.Timeframe) { |
| 71 |
transactions, err := hbciClient.AccountTransactions(account.ToAccountConnection(), timeframe, false, "") |
| 72 |
if err != nil { |
| 73 |
fmt.Println(err) |
| 74 |
os.Exit(1) |
| 75 |
} |
| 76 |
fmt.Print(domain.AccountTransactions(transactions)) |
| 77 |
} |
| 78 |
|
| 79 |
func fetchSepaTransactions(account domain.InternationalAccountConnection, timeframe domain.Timeframe) { |
| 80 |
transactions, err := hbciClient.SepaAccountTransactions(account, timeframe, false, "") |
| 81 |
if err != nil { |
| 82 |
fmt.Println(err) |
| 83 |
os.Exit(1) |
| 84 |
} |
| 85 |
fmt.Print(domain.AccountTransactions(transactions)) |
| 86 |
} |
| 87 |
|
| 88 |
func init() { |
| 89 |
rootCmd.AddCommand(transactionsCmd) |
| 90 |
|
| 91 |
transactionsCmd.Flags().BoolVar( |
| 92 |
&disableSepa, "disableSepa", false, |
| 93 |
"whether the library should not handle account data as sepa compliant", |
| 94 |
) |
| 95 |
transactionsCmd.Flags().IntVar( |
| 96 |
&daysToFetch, "daysToFetch", 10, |
| 97 |
"the number of days to fetch transactions for (10)", |
| 98 |
) |
| 99 |
transactionsCmd.Flags().StringVar( |
| 100 |
&transactionsAccount, "accountID", "", |
| 101 |
"the accountID to fetch transactions for (defaults to the UserID)", |
| 102 |
) |
| 103 |
} |