-
Notifications
You must be signed in to change notification settings - Fork 0
/
ofxexport.go
66 lines (60 loc) · 2.14 KB
/
ofxexport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package ofx
import (
"encoding/xml"
"fmt"
"io"
"time"
)
type BankTransaction struct {
PostedDate time.Time
InterestDate time.Time
Ref string
DestinationAccount string
TxType string
Memo string
Amount float64
}
type OfxTransactionList struct {
XMLName xml.Name `xml:"OFX"`
CurDef string `xml:"BANKMSGSRSV1>STMTTRNRS>STMTRS>CURDEF"`
PayerBank string `xml:"BANKMSGSRSV1>STMTTRNRS>STMTRS>BANKACCTFROM>BANKID"`
PayerAccount string `xml:"BANKMSGSRSV1>STMTTRNRS>STMTRS>BANKACCTFROM>ACCTID"`
PayerAccountType string `xml:"BANKMSGSRSV1>STMTTRNRS>STMTRS>BANKACCTFROM>ACCTTYPE"`
Transactions []OfxTransaction `xml:"BANKMSGSRSV1>STMTTRNRS>STMTRS>BANKTRANLIST>STMTTRN"`
}
// WriteOFX creates an XML document from a OfxTransactionList, and writes it to
// the Writer passed in as an argument
func (txs *OfxTransactionList) WriteOFX(w io.Writer) {
header := `<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?OFX OFXHEADER="200" VERSION="211" SECURITY="NONE" OLDFILEUID="NONE" NEWFILEUID="NONE"?>
`
w.Write([]byte(header))
enc := xml.NewEncoder(w)
enc.Indent("", " ")
if err := enc.Encode(txs); err != nil {
fmt.Printf("error: %v\n", err)
}
}
type OfxTransaction struct {
XMLName xml.Name `xml:"STMTTRN"`
Type string `xml:"TRNTYPE"`
DatePosted string `xml:"DTPOSTED"`
DateUser string `xml:"DTUSER"`
Amount float64 `xml:"TRNAMT"`
ID string `xml:"FITID"`
Payee string `xml:"NAME"`
Memo string `xml:"MEMO"`
PayeeBank string `xml:"BANKACCTTO>BANKID"`
PayeeAccount string `xml:"BANKACCTTO>ACCTID"`
PayeeAccountType string `xml:"BANKACCTTO>ACCTTYPE"`
}
// ToOfx converts a single bank transaction (BankTransaction) to a single OfxTransaction
func (btx *BankTransaction) ToOfx() OfxTransaction {
var otx OfxTransaction
otx.Type = btx.TxType
otx.DatePosted = btx.InterestDate.Format("20060102")
otx.DateUser = btx.PostedDate.Format("20060102")
otx.Amount = btx.Amount
otx.Memo = btx.Memo
return otx
}