2017-03-22 20:29:08 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"github.com/aclindsa/ofxgo"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2017-04-16 20:50:06 -04:00
|
|
|
var bankTransactionsCommand = command{
|
2017-03-22 20:29:08 -04:00
|
|
|
Name: "transactions-bank",
|
|
|
|
Description: "Print bank transactions and balance",
|
|
|
|
Flags: flag.NewFlagSet("transactions-bank", flag.ExitOnError),
|
|
|
|
CheckFlags: checkServerFlags,
|
|
|
|
Do: bankTransactions,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
defineServerFlags(bankTransactionsCommand.Flags)
|
2017-04-12 21:40:42 -04:00
|
|
|
bankTransactionsCommand.Flags.StringVar(&bankID, "bankid", "", "BankID (from `get-accounts` subcommand)")
|
|
|
|
bankTransactionsCommand.Flags.StringVar(&acctID, "acctid", "", "AcctID (from `get-accounts` subcommand)")
|
2017-03-22 20:29:08 -04:00
|
|
|
bankTransactionsCommand.Flags.StringVar(&acctType, "accttype", "CHECKING", "AcctType (from `get-accounts` subcommand)")
|
|
|
|
}
|
|
|
|
|
|
|
|
func bankTransactions() {
|
2017-04-16 20:50:06 -04:00
|
|
|
client, query := newRequest()
|
2017-03-22 20:29:08 -04:00
|
|
|
|
2017-04-06 05:58:22 -04:00
|
|
|
acctTypeEnum, err := ofxgo.NewAcctType(acctType)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error parsing accttype:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2017-03-22 20:29:08 -04:00
|
|
|
uid, err := ofxgo.RandomUID()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error creating uid for transaction:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
statementRequest := ofxgo.StatementRequest{
|
|
|
|
TrnUID: *uid,
|
|
|
|
BankAcctFrom: ofxgo.BankAcct{
|
2017-04-12 21:40:42 -04:00
|
|
|
BankID: ofxgo.String(bankID),
|
|
|
|
AcctID: ofxgo.String(acctID),
|
2017-04-06 05:58:22 -04:00
|
|
|
AcctType: acctTypeEnum,
|
2017-03-22 20:29:08 -04:00
|
|
|
},
|
|
|
|
Include: true,
|
|
|
|
}
|
2017-04-06 05:58:22 -04:00
|
|
|
|
2017-03-31 11:54:43 -04:00
|
|
|
query.Bank = append(query.Bank, &statementRequest)
|
2017-03-22 20:29:08 -04:00
|
|
|
|
|
|
|
response, err := client.Request(query)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error requesting account statement:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.Signon.Status.Code != 0 {
|
2017-03-25 06:14:52 -04:00
|
|
|
meaning, _ := response.Signon.Status.CodeMeaning()
|
|
|
|
fmt.Printf("Nonzero signon status (%d: %s) with message: %s\n", response.Signon.Status.Code, meaning, response.Signon.Status.Message)
|
2017-03-22 20:29:08 -04:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2017-03-31 11:54:43 -04:00
|
|
|
if len(response.Bank) < 1 {
|
2017-03-22 20:29:08 -04:00
|
|
|
fmt.Println("No banking messages received")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-31 11:54:43 -04:00
|
|
|
if stmt, ok := response.Bank[0].(*ofxgo.StatementResponse); ok {
|
2017-03-22 20:29:08 -04:00
|
|
|
fmt.Printf("Balance: %s %s (as of %s)\n", stmt.BalAmt, stmt.CurDef, stmt.DtAsOf)
|
|
|
|
fmt.Println("Transactions:")
|
|
|
|
for _, tran := range stmt.BankTranList.Transactions {
|
|
|
|
printTransaction(stmt.CurDef, &tran)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 20:20:22 -04:00
|
|
|
func printTransaction(defCurrency ofxgo.CurrSymbol, tran *ofxgo.Transaction) {
|
2017-03-22 20:29:08 -04:00
|
|
|
currency := defCurrency
|
2021-10-17 15:49:08 -04:00
|
|
|
if tran.Currency != nil {
|
2017-04-18 20:17:44 -04:00
|
|
|
currency = tran.Currency.CurSym
|
2017-03-22 20:29:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var name string
|
|
|
|
if len(tran.Name) > 0 {
|
|
|
|
name = string(tran.Name)
|
2017-03-29 05:31:01 -04:00
|
|
|
} else if tran.Payee != nil {
|
2017-03-22 20:29:08 -04:00
|
|
|
name = string(tran.Payee.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tran.Memo) > 0 {
|
|
|
|
name = name + " - " + string(tran.Memo)
|
|
|
|
}
|
|
|
|
|
2017-04-17 20:20:22 -04:00
|
|
|
fmt.Printf("%s %-15s %-11s %s\n", tran.DtPosted, tran.TrnAmt.String()+" "+currency.String(), tran.TrnType, name)
|
2017-03-22 20:29:08 -04:00
|
|
|
}
|