Skip to content

Commit

Permalink
import transactions from ofx file
Browse files Browse the repository at this point in the history
  • Loading branch information
mayswind committed Oct 27, 2024
1 parent 22d653c commit 2b92e7a
Show file tree
Hide file tree
Showing 10 changed files with 498 additions and 0 deletions.
119 changes: 119 additions & 0 deletions pkg/converters/ofx/ofx_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package ofx

import "encoding/xml"

const ofxVersion1 = "100"
const ofxVersion2 = "200"

const ofxDefaultTimezoneOffset = "+00:00"

// ofxAccountType represents account type in open financial exchange (ofx) file
type ofxAccountType string

// OFX account types
const (
ofxCheckingAccount ofxAccountType = "CHECKING"
ofxSavingsAccount ofxAccountType = "SAVINGS"
ofxMoneyMarketAccount ofxAccountType = "MONEYMRKT"
ofxLineOfCreditAccount ofxAccountType = "CREDITLINE"
ofxCertificateOfDepositAccount ofxAccountType = "CD"
)

// ofxFile represents the struct of open financial exchange (ofx) file
type ofxFile struct {
XMLName xml.Name `xml:"OFX"`
FileHeader *ofxFileHeader
BankMessageResponseV1 *ofxBankMessageResponseV1 `xml:"BANKMSGSRSV1"`
CreditCardMessageResponseV1 *ofxCreditCardMessageResponseV1 `xml:"CREDITCARDMSGSRSV1"`
}

// ofxFileHeader represents the struct of open financial exchange (ofx) file header
type ofxFileHeader struct {
OFXVersion string
OFXDataVersion string
Security string
OldFileUid string
NewFileUid string
}

// ofxBankMessageResponseV1 represents the struct of open financial exchange (ofx) bank message response v1
type ofxBankMessageResponseV1 struct {
StatementTransactionResponse *ofxBankStatementTransactionResponse `xml:"STMTTRNRS"`
}

// ofxCreditCardMessageResponseV1 represents the struct of open financial exchange (ofx) credit card message response v1
type ofxCreditCardMessageResponseV1 struct {
StatementTransactionResponse *ofxCreditCardStatementTransactionResponse `xml:"CCSTMTTRNRS"`
}

// ofxBankStatementTransactionResponse represents the struct of open financial exchange (ofx) bank statement transaction response
type ofxBankStatementTransactionResponse struct {
StatementResponse *ofxBankStatementResponse `xml:"STMTRS"`
}

// ofxCreditCardStatementTransactionResponse represents the struct of open financial exchange (ofx) credit card statement transaction response
type ofxCreditCardStatementTransactionResponse struct {
StatementResponse *ofxCreditCardStatementResponse `xml:"CCSTMTRS"`
}

// ofxBankStatementResponse represents the struct of open financial exchange (ofx) bank statement response
type ofxBankStatementResponse struct {
DefaultCurrency string `xml:"CURDEF"`
AccountFrom *ofxBankAccount `xml:"BANKACCTFROM"`
TransactionList *ofxBankTransactionList `xml:"BANKTRANLIST"`
}

// ofxCreditCardStatementResponse represents the struct of open financial exchange (ofx) credit card statement response
type ofxCreditCardStatementResponse struct {
DefaultCurrency string `xml:"CURDEF"`
AccountFrom *ofxCreditCardAccount `xml:"CCACCTFROM"`
TransactionList *ofxBankTransactionList `xml:"BANKTRANLIST"`
}

// ofxBankAccount represents the struct of open financial exchange (ofx) bank account
type ofxBankAccount struct {
BankId string `xml:"BANKID"`
BranchId string `xml:"BRANCHID"`
AccountId string `xml:"ACCTID"`
AccountType ofxAccountType `xml:"ACCTTYPE"`
AccountKey string `xml:"ACCTKEY"`
}

// ofxCreditCardAccount represents the struct of open financial exchange (ofx) credit card account
type ofxCreditCardAccount struct {
AccountId string `xml:"ACCTID"`
AccountKey string `xml:"ACCTKEY"`
}

// ofxBankTransactionList represents the struct of open financial exchange (ofx) bank transaction list
type ofxBankTransactionList struct {
StartDate string `xml:"DTSTART"`
EndDate string `xml:"DTEND"`
StatementTransactions []*ofxBankStatementTransaction `xml:"STMTTRN"`
}

// ofxBankStatementTransaction represents the struct of open financial exchange (ofx) bank statement transaction
type ofxBankStatementTransaction struct {
TransactionId string `xml:"FITID"`
TransactionType string `xml:"TRNTYPE"`
PostedDate string `xml:"DTPOSTED"`
Amount string `xml:"TRNAMT"`
Name string `xml:"NAME"`
Payee *ofxPayee `xml:"PAYEE"`
Memo string `xml:"MEMO"`
Currency string `xml:"CURRENCY"`
OriginalCurrency string `xml:"ORIGCURRENCY"`
}

// ofxPayee represents the struct of open financial exchange (ofx) payee info
type ofxPayee struct {
Name string `xml:"NAME"`
Address1 string `xml:"ADDR1"`
Address2 string `xml:"ADDR2"`
Address3 string `xml:"ADDR3"`
City string `xml:"CITY"`
State string `xml:"STATE"`
PostalCode string `xml:"POSTALCODE"`
Country string `xml:"COUNTRY"`
Phone string `xml:"PHONE"`
}
48 changes: 48 additions & 0 deletions pkg/converters/ofx/ofx_transaction_data_file_importer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ofx

import (
"github.com/mayswind/ezbookkeeping/pkg/converters/datatable"
"github.com/mayswind/ezbookkeeping/pkg/core"
"github.com/mayswind/ezbookkeeping/pkg/models"
"github.com/mayswind/ezbookkeeping/pkg/utils"
)

var ofxTransactionTypeNameMapping = map[models.TransactionType]string{
models.TRANSACTION_TYPE_INCOME: utils.IntToString(int(models.TRANSACTION_TYPE_INCOME)),
models.TRANSACTION_TYPE_EXPENSE: utils.IntToString(int(models.TRANSACTION_TYPE_EXPENSE)),
models.TRANSACTION_TYPE_TRANSFER: utils.IntToString(int(models.TRANSACTION_TYPE_TRANSFER)),
}

// ofxTransactionDataImporter defines the structure of open financial exchange (ofx) file importer for transaction data
type ofxTransactionDataImporter struct {
}

// Initialize a open financial exchange (ofx) transaction data importer singleton instance
var (
OFXTransactionDataImporter = &ofxTransactionDataImporter{}
)

// ParseImportedData returns the imported data by parsing the open financial exchange (ofx) file transaction data
func (c *ofxTransactionDataImporter) ParseImportedData(ctx core.Context, user *models.User, data []byte, defaultTimezoneOffset int16, accountMap map[string]*models.Account, expenseCategoryMap map[string]*models.TransactionCategory, incomeCategoryMap map[string]*models.TransactionCategory, transferCategoryMap map[string]*models.TransactionCategory, tagMap map[string]*models.TransactionTag) (models.ImportedTransactionSlice, []*models.Account, []*models.TransactionCategory, []*models.TransactionCategory, []*models.TransactionCategory, []*models.TransactionTag, error) {
ofxDataReader, err := createNewOFXFileReader(data)

if err != nil {
return nil, nil, nil, nil, nil, nil, err
}

ofxFile, err := ofxDataReader.read(ctx)

if err != nil {
return nil, nil, nil, nil, nil, nil, err
}

transactionDataTable, err := createNewOFXTransactionDataTable(ofxFile)

if err != nil {
return nil, nil, nil, nil, nil, nil, err
}

dataTableImporter := datatable.CreateNewSimpleImporter(ofxTransactionTypeNameMapping)

return dataTableImporter.ParseImportedData(ctx, user, transactionDataTable, defaultTimezoneOffset, accountMap, expenseCategoryMap, incomeCategoryMap, transferCategoryMap, tagMap)
}
Loading

0 comments on commit 2b92e7a

Please sign in to comment.