52 lines
2.5 KiB
Go
52 lines
2.5 KiB
Go
package beancount
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/mayswind/ezbookkeeping/pkg/converters/converter"
|
|
"github.com/mayswind/ezbookkeeping/pkg/core"
|
|
"github.com/mayswind/ezbookkeeping/pkg/models"
|
|
"github.com/mayswind/ezbookkeeping/pkg/utils"
|
|
)
|
|
|
|
var beancountTransactionTypeNameMapping = map[models.TransactionType]string{
|
|
models.TRANSACTION_TYPE_MODIFY_BALANCE: utils.IntToString(int(models.TRANSACTION_TYPE_MODIFY_BALANCE)),
|
|
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)),
|
|
}
|
|
|
|
// beancountTransactionDataImporter defines the structure of Beancount importer for transaction data
|
|
type beancountTransactionDataImporter struct {
|
|
}
|
|
|
|
// Initialize a beancount transaction data importer singleton instance
|
|
var (
|
|
BeancountTransactionDataImporter = &beancountTransactionDataImporter{}
|
|
)
|
|
|
|
// ParseImportedData returns the imported data by parsing the Beancount transaction data
|
|
func (c *beancountTransactionDataImporter) ParseImportedData(ctx core.Context, user *models.User, data []byte, defaultTimezone *time.Location, additionalOptions converter.TransactionDataImporterOptions, accountMap map[string]*models.Account, expenseCategoryMap map[string]map[string]*models.TransactionCategory, incomeCategoryMap map[string]map[string]*models.TransactionCategory, transferCategoryMap map[string]map[string]*models.TransactionCategory, tagMap map[string]*models.TransactionTag) (models.ImportedTransactionSlice, []*models.Account, []*models.TransactionCategory, []*models.TransactionCategory, []*models.TransactionCategory, []*models.TransactionTag, error) {
|
|
beancountDataReader, err := createNewBeancountDataReader(ctx, data)
|
|
|
|
if err != nil {
|
|
return nil, nil, nil, nil, nil, nil, err
|
|
}
|
|
|
|
beancountData, err := beancountDataReader.read(ctx)
|
|
|
|
if err != nil {
|
|
return nil, nil, nil, nil, nil, nil, err
|
|
}
|
|
|
|
transactionDataTable, err := createNewBeancountTransactionDataTable(beancountData)
|
|
|
|
if err != nil {
|
|
return nil, nil, nil, nil, nil, nil, err
|
|
}
|
|
|
|
dataTableImporter := converter.CreateNewImporterWithTypeNameMapping(beancountTransactionTypeNameMapping, "", "", BEANCOUNT_TRANSACTION_TAG_SEPARATOR)
|
|
|
|
return dataTableImporter.ParseImportedData(ctx, user, transactionDataTable, defaultTimezone, additionalOptions, accountMap, expenseCategoryMap, incomeCategoryMap, transferCategoryMap, tagMap)
|
|
}
|