Make ynab export equivalent to original export #21

Merged
jacob1123 merged 7 commits from ynab-export-fixes into master 2022-02-25 16:35:18 +01:00
5 changed files with 90 additions and 23 deletions

View File

@ -129,7 +129,11 @@ func (n Numeric) String() string {
exp = -exp exp = -exp
for length <= exp { for length <= exp {
bytes = append(bytes, byte('0')) if n.Int.Int64() < 0 {
bytes = append([]byte{bytes[0], byte('0')}, bytes[1:]...)
} else {
bytes = append([]byte{byte('0')}, bytes...)
}
length++ length++
} }
@ -166,7 +170,11 @@ func (n Numeric) MarshalJSON() ([]byte, error) {
exp = -exp exp = -exp
for length <= exp { for length <= exp {
bytes = append(bytes, byte('0')) if n.Int.Int64() < 0 {
bytes = append([]byte{bytes[0], byte('0')}, bytes[1:]...)
} else {
bytes = append([]byte{byte('0')}, bytes...)
}
length++ length++
} }

View File

@ -17,6 +17,23 @@ func TestMarshalJSON(t *testing.T) {
{numeric.Zero(), `0`}, {numeric.Zero(), `0`},
{numeric.MustParse("1.23"), "1.23"}, {numeric.MustParse("1.23"), "1.23"},
{numeric.MustParse("1,24"), "1.24"}, {numeric.MustParse("1,24"), "1.24"},
{numeric.MustParse("1"), "1"},
{numeric.MustParse("10"), "10"},
{numeric.MustParse("100"), "100"},
{numeric.MustParse("1000"), "1000"},
{numeric.MustParse("0.1"), "0.1"},
{numeric.MustParse("0.01"), "0.01"},
{numeric.MustParse("0.001"), "0.001"},
{numeric.MustParse("0.0001"), "0.0001"},
{numeric.MustParse("-1"), "-1"},
{numeric.MustParse("-10"), "-10"},
{numeric.MustParse("-100"), "-100"},
{numeric.MustParse("-1000"), "-1000"},
{numeric.MustParse("-0.1"), "-0.1"},
{numeric.MustParse("-0.01"), "-0.01"},
{numeric.MustParse("-0.001"), "-0.001"},
{numeric.MustParse("-0.0001"), "-0.0001"},
{numeric.MustParse("123456789.12345"), "123456789.12345"},
{numeric.MustParse("123456789.12345"), "123456789.12345"}, {numeric.MustParse("123456789.12345"), "123456789.12345"},
{numeric.MustParse("-1.23"), "-1.23"}, {numeric.MustParse("-1.23"), "-1.23"},
{numeric.MustParse("-1,24"), "-1.24"}, {numeric.MustParse("-1,24"), "-1.24"},
@ -53,6 +70,19 @@ func TestParse(t *testing.T) {
{numeric.FromInt64WithExp(1, 0), `1`}, {numeric.FromInt64WithExp(1, 0), `1`},
{numeric.FromInt64WithExp(1, 1), `10`}, {numeric.FromInt64WithExp(1, 1), `10`},
{numeric.FromInt64WithExp(1, 2), `100`}, {numeric.FromInt64WithExp(1, 2), `100`},
{numeric.FromInt64WithExp(1, 3), `1000`},
{numeric.FromInt64WithExp(1, -1), `0.1`},
{numeric.FromInt64WithExp(1, -2), `0.01`},
{numeric.FromInt64WithExp(1, -3), `0.001`},
{numeric.FromInt64WithExp(1, -4), `0.0001`},
{numeric.FromInt64WithExp(-1, 0), `-1`},
{numeric.FromInt64WithExp(-1, 1), `-10`},
{numeric.FromInt64WithExp(-1, 2), `-100`},
{numeric.FromInt64WithExp(-1, 3), `-1000`},
{numeric.FromInt64WithExp(-1, -1), `-0.1`},
{numeric.FromInt64WithExp(-1, -2), `-0.01`},
{numeric.FromInt64WithExp(-1, -3), `-0.001`},
{numeric.FromInt64WithExp(-1, -4), `-0.0001`},
{numeric.FromInt64WithExp(123, -2), "1.23"}, {numeric.FromInt64WithExp(123, -2), "1.23"},
{numeric.FromInt64WithExp(124, -2), "1,24"}, {numeric.FromInt64WithExp(124, -2), "1,24"},
{numeric.FromInt64WithExp(12345678912345, -5), "123456789.12345"}, {numeric.FromInt64WithExp(12345678912345, -5), "123456789.12345"},

View File

@ -23,8 +23,19 @@ DELETE FROM transactions
WHERE id = $1; WHERE id = $1;
-- name: GetAllTransactionsForBudget :many -- name: GetAllTransactionsForBudget :many
SELECT transactions.id, transactions.date, transactions.memo, transactions.amount, transactions.group_id, transactions.status, SELECT transactions.id, transactions.date, transactions.memo,
accounts.name as account, COALESCE(payees.name, '') as payee, COALESCE(category_groups.name, '') as category_group, COALESCE(categories.name, '') as category transactions.amount, transactions.group_id, transactions.status,
accounts.name as account,
COALESCE(payees.name, '') as payee,
COALESCE(category_groups.name, '') as category_group,
COALESCE(categories.name, '') as category,
COALESCE((
SELECT CONCAT(otherAccounts.name)
FROM transactions otherTransactions
LEFT JOIN accounts otherAccounts ON otherAccounts.id = otherTransactions.account_id
WHERE otherTransactions.group_id = transactions.group_id
AND otherTransactions.id != transactions.id
), '')::text as transfer_account
FROM transactions FROM transactions
INNER JOIN accounts ON accounts.id = transactions.account_id INNER JOIN accounts ON accounts.id = transactions.account_id
LEFT JOIN payees ON payees.id = transactions.payee_id LEFT JOIN payees ON payees.id = transactions.payee_id
@ -40,13 +51,13 @@ SELECT transactions.id, transactions.date, transactions.memo,
COALESCE(payees.name, '') as payee, COALESCE(payees.name, '') as payee,
COALESCE(category_groups.name, '') as category_group, COALESCE(category_groups.name, '') as category_group,
COALESCE(categories.name, '') as category, COALESCE(categories.name, '') as category,
( COALESCE((
SELECT CONCAT(otherAccounts.name) SELECT CONCAT(otherAccounts.name)
FROM transactions otherTransactions FROM transactions otherTransactions
LEFT JOIN accounts otherAccounts ON otherAccounts.id = otherTransactions.account_id LEFT JOIN accounts otherAccounts ON otherAccounts.id = otherTransactions.account_id
WHERE otherTransactions.group_id = transactions.group_id WHERE otherTransactions.group_id = transactions.group_id
AND otherTransactions.id != transactions.id AND otherTransactions.id != transactions.id
) as transfer_account ), '')::text as transfer_account
FROM transactions FROM transactions
INNER JOIN accounts ON accounts.id = transactions.account_id INNER JOIN accounts ON accounts.id = transactions.account_id
LEFT JOIN payees ON payees.id = transactions.payee_id LEFT JOIN payees ON payees.id = transactions.payee_id

View File

@ -81,8 +81,19 @@ func (q *Queries) DeleteTransaction(ctx context.Context, id uuid.UUID) error {
} }
const getAllTransactionsForBudget = `-- name: GetAllTransactionsForBudget :many const getAllTransactionsForBudget = `-- name: GetAllTransactionsForBudget :many
SELECT transactions.id, transactions.date, transactions.memo, transactions.amount, transactions.group_id, transactions.status, SELECT transactions.id, transactions.date, transactions.memo,
accounts.name as account, COALESCE(payees.name, '') as payee, COALESCE(category_groups.name, '') as category_group, COALESCE(categories.name, '') as category transactions.amount, transactions.group_id, transactions.status,
accounts.name as account,
COALESCE(payees.name, '') as payee,
COALESCE(category_groups.name, '') as category_group,
COALESCE(categories.name, '') as category,
COALESCE((
SELECT CONCAT(otherAccounts.name)
FROM transactions otherTransactions
LEFT JOIN accounts otherAccounts ON otherAccounts.id = otherTransactions.account_id
WHERE otherTransactions.group_id = transactions.group_id
AND otherTransactions.id != transactions.id
), '')::text as transfer_account
FROM transactions FROM transactions
INNER JOIN accounts ON accounts.id = transactions.account_id INNER JOIN accounts ON accounts.id = transactions.account_id
LEFT JOIN payees ON payees.id = transactions.payee_id LEFT JOIN payees ON payees.id = transactions.payee_id
@ -93,16 +104,17 @@ ORDER BY transactions.date DESC
` `
type GetAllTransactionsForBudgetRow struct { type GetAllTransactionsForBudgetRow struct {
ID uuid.UUID ID uuid.UUID
Date time.Time Date time.Time
Memo string Memo string
Amount numeric.Numeric Amount numeric.Numeric
GroupID uuid.NullUUID GroupID uuid.NullUUID
Status TransactionStatus Status TransactionStatus
Account string Account string
Payee string Payee string
CategoryGroup string CategoryGroup string
Category string Category string
TransferAccount string
} }
func (q *Queries) GetAllTransactionsForBudget(ctx context.Context, budgetID uuid.UUID) ([]GetAllTransactionsForBudgetRow, error) { func (q *Queries) GetAllTransactionsForBudget(ctx context.Context, budgetID uuid.UUID) ([]GetAllTransactionsForBudgetRow, error) {
@ -125,6 +137,7 @@ func (q *Queries) GetAllTransactionsForBudget(ctx context.Context, budgetID uuid
&i.Payee, &i.Payee,
&i.CategoryGroup, &i.CategoryGroup,
&i.Category, &i.Category,
&i.TransferAccount,
); err != nil { ); err != nil {
return nil, err return nil, err
} }
@ -202,13 +215,13 @@ SELECT transactions.id, transactions.date, transactions.memo,
COALESCE(payees.name, '') as payee, COALESCE(payees.name, '') as payee,
COALESCE(category_groups.name, '') as category_group, COALESCE(category_groups.name, '') as category_group,
COALESCE(categories.name, '') as category, COALESCE(categories.name, '') as category,
( COALESCE((
SELECT CONCAT(otherAccounts.name) SELECT CONCAT(otherAccounts.name)
FROM transactions otherTransactions FROM transactions otherTransactions
LEFT JOIN accounts otherAccounts ON otherAccounts.id = otherTransactions.account_id LEFT JOIN accounts otherAccounts ON otherAccounts.id = otherTransactions.account_id
WHERE otherTransactions.group_id = transactions.group_id WHERE otherTransactions.group_id = transactions.group_id
AND otherTransactions.id != transactions.id AND otherTransactions.id != transactions.id
) as transfer_account ), '')::text as transfer_account
FROM transactions FROM transactions
INNER JOIN accounts ON accounts.id = transactions.account_id INNER JOIN accounts ON accounts.id = transactions.account_id
LEFT JOIN payees ON payees.id = transactions.payee_id LEFT JOIN payees ON payees.id = transactions.payee_id
@ -230,7 +243,7 @@ type GetTransactionsForAccountRow struct {
Payee string Payee string
CategoryGroup string CategoryGroup string
Category string Category string
TransferAccount interface{} TransferAccount string
} }
func (q *Queries) GetTransactionsForAccount(ctx context.Context, accountID uuid.UUID) ([]GetTransactionsForAccountRow, error) { func (q *Queries) GetTransactionsForAccount(ctx context.Context, accountID uuid.UUID) ([]GetTransactionsForAccountRow, error) {

View File

@ -115,12 +115,17 @@ func GetTransactionRow(transaction GetAllTransactionsForBudgetRow) []string {
transaction.Account, transaction.Account,
"", // Flag "", // Flag
transaction.Date.Format("02.01.2006"), transaction.Date.Format("02.01.2006"),
transaction.Payee, }
if transaction.TransferAccount != "" {
row = append(row, "Transfer : "+transaction.TransferAccount)
} else {
row = append(row, transaction.Payee)
} }
if transaction.CategoryGroup != "" && transaction.Category != "" { if transaction.CategoryGroup != "" && transaction.Category != "" {
row = append(row, row = append(row,
transaction.CategoryGroup+" : "+transaction.Category, transaction.CategoryGroup+": "+transaction.Category,
transaction.CategoryGroup, transaction.CategoryGroup,
transaction.Category) transaction.Category)
} else { } else {