From 78389e4beba71db1390637576269569ca8ead1c5 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Fri, 25 Feb 2022 15:04:09 +0000 Subject: [PATCH 1/7] Also export transfers --- postgres/queries/transactions.sql | 17 ++++++++++--- postgres/transactions.sql.go | 41 ++++++++++++++++++++----------- postgres/ynab-export.go | 7 +++++- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/postgres/queries/transactions.sql b/postgres/queries/transactions.sql index 291b357..b651da0 100644 --- a/postgres/queries/transactions.sql +++ b/postgres/queries/transactions.sql @@ -23,8 +23,19 @@ DELETE FROM transactions WHERE id = $1; -- name: GetAllTransactionsForBudget :many -SELECT transactions.id, transactions.date, transactions.memo, 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 +SELECT transactions.id, transactions.date, transactions.memo, + 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, + ( + 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 INNER JOIN accounts ON accounts.id = transactions.account_id LEFT JOIN payees ON payees.id = transactions.payee_id @@ -46,7 +57,7 @@ SELECT transactions.id, transactions.date, transactions.memo, LEFT JOIN accounts otherAccounts ON otherAccounts.id = otherTransactions.account_id WHERE otherTransactions.group_id = transactions.group_id AND otherTransactions.id != transactions.id - ) as transfer_account + )::text as transfer_account FROM transactions INNER JOIN accounts ON accounts.id = transactions.account_id LEFT JOIN payees ON payees.id = transactions.payee_id diff --git a/postgres/transactions.sql.go b/postgres/transactions.sql.go index 0b57967..cdf2e6f 100644 --- a/postgres/transactions.sql.go +++ b/postgres/transactions.sql.go @@ -81,8 +81,19 @@ func (q *Queries) DeleteTransaction(ctx context.Context, id uuid.UUID) error { } const getAllTransactionsForBudget = `-- name: GetAllTransactionsForBudget :many -SELECT transactions.id, transactions.date, transactions.memo, 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 +SELECT transactions.id, transactions.date, transactions.memo, + 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, + ( + 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 INNER JOIN accounts ON accounts.id = transactions.account_id LEFT JOIN payees ON payees.id = transactions.payee_id @@ -93,16 +104,17 @@ ORDER BY transactions.date DESC ` type GetAllTransactionsForBudgetRow struct { - ID uuid.UUID - Date time.Time - Memo string - Amount numeric.Numeric - GroupID uuid.NullUUID - Status TransactionStatus - Account string - Payee string - CategoryGroup string - Category string + ID uuid.UUID + Date time.Time + Memo string + Amount numeric.Numeric + GroupID uuid.NullUUID + Status TransactionStatus + Account string + Payee string + CategoryGroup string + Category string + TransferAccount string } 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.CategoryGroup, &i.Category, + &i.TransferAccount, ); err != nil { return nil, err } @@ -208,7 +221,7 @@ SELECT transactions.id, transactions.date, transactions.memo, LEFT JOIN accounts otherAccounts ON otherAccounts.id = otherTransactions.account_id WHERE otherTransactions.group_id = transactions.group_id AND otherTransactions.id != transactions.id - ) as transfer_account + )::text as transfer_account FROM transactions INNER JOIN accounts ON accounts.id = transactions.account_id LEFT JOIN payees ON payees.id = transactions.payee_id @@ -230,7 +243,7 @@ type GetTransactionsForAccountRow struct { Payee string CategoryGroup string Category string - TransferAccount interface{} + TransferAccount string } func (q *Queries) GetTransactionsForAccount(ctx context.Context, accountID uuid.UUID) ([]GetTransactionsForAccountRow, error) { diff --git a/postgres/ynab-export.go b/postgres/ynab-export.go index eefc9dd..688e19e 100644 --- a/postgres/ynab-export.go +++ b/postgres/ynab-export.go @@ -115,7 +115,12 @@ func GetTransactionRow(transaction GetAllTransactionsForBudgetRow) []string { transaction.Account, "", // Flag 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 != "" { From 9ed4df7053200d9951b6a73a915a7b686bafac35 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Fri, 25 Feb 2022 15:04:30 +0000 Subject: [PATCH 2/7] Fix leading space before category separator --- postgres/ynab-export.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/postgres/ynab-export.go b/postgres/ynab-export.go index 688e19e..35d822b 100644 --- a/postgres/ynab-export.go +++ b/postgres/ynab-export.go @@ -125,7 +125,7 @@ func GetTransactionRow(transaction GetAllTransactionsForBudgetRow) []string { if transaction.CategoryGroup != "" && transaction.Category != "" { row = append(row, - transaction.CategoryGroup+" : "+transaction.Category, + transaction.CategoryGroup+": "+transaction.Category, transaction.CategoryGroup, transaction.Category) } else { From 4f72351ee9b506f6123a1885247f71d1763e133f Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Fri, 25 Feb 2022 15:26:39 +0000 Subject: [PATCH 3/7] Add more unittests for numeric --- postgres/numeric/numeric_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/postgres/numeric/numeric_test.go b/postgres/numeric/numeric_test.go index b7ef5e9..9e89a38 100644 --- a/postgres/numeric/numeric_test.go +++ b/postgres/numeric/numeric_test.go @@ -17,6 +17,15 @@ func TestMarshalJSON(t *testing.T) { {numeric.Zero(), `0`}, {numeric.MustParse("1.23"), "1.23"}, {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("123456789.12345"), "123456789.12345"}, {numeric.MustParse("123456789.12345"), "123456789.12345"}, {numeric.MustParse("-1.23"), "-1.23"}, {numeric.MustParse("-1,24"), "-1.24"}, @@ -53,6 +62,11 @@ func TestParse(t *testing.T) { {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(124, -2), "1,24"}, {numeric.FromInt64WithExp(12345678912345, -5), "123456789.12345"}, From f9e512c593795c0c2bcccbb446d8ba68ba8cae2f Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Fri, 25 Feb 2022 15:26:51 +0000 Subject: [PATCH 4/7] Fix floating points < 1 --- postgres/numeric/numeric.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/postgres/numeric/numeric.go b/postgres/numeric/numeric.go index 753feff..73fdb16 100644 --- a/postgres/numeric/numeric.go +++ b/postgres/numeric/numeric.go @@ -129,7 +129,7 @@ func (n Numeric) String() string { exp = -exp for length <= exp { - bytes = append(bytes, byte('0')) + bytes = append([]byte{byte('0')}, bytes...) length++ } @@ -166,7 +166,7 @@ func (n Numeric) MarshalJSON() ([]byte, error) { exp = -exp for length <= exp { - bytes = append(bytes, byte('0')) + bytes = append([]byte{byte('0')}, bytes...) length++ } From 5bb2c9c8b8040242302e60533f0fa7cf650a1ee6 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Fri, 25 Feb 2022 15:27:06 +0000 Subject: [PATCH 5/7] Use empty string as fallback for TransferAccount --- postgres/queries/transactions.sql | 8 ++++---- postgres/transactions.sql.go | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/postgres/queries/transactions.sql b/postgres/queries/transactions.sql index b651da0..8ad3504 100644 --- a/postgres/queries/transactions.sql +++ b/postgres/queries/transactions.sql @@ -29,13 +29,13 @@ SELECT transactions.id, transactions.date, transactions.memo, 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 + ), '')::text as transfer_account FROM transactions INNER JOIN accounts ON accounts.id = transactions.account_id LEFT JOIN payees ON payees.id = transactions.payee_id @@ -51,13 +51,13 @@ SELECT transactions.id, transactions.date, transactions.memo, 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 + ), '')::text as transfer_account FROM transactions INNER JOIN accounts ON accounts.id = transactions.account_id LEFT JOIN payees ON payees.id = transactions.payee_id diff --git a/postgres/transactions.sql.go b/postgres/transactions.sql.go index cdf2e6f..e9bf8f8 100644 --- a/postgres/transactions.sql.go +++ b/postgres/transactions.sql.go @@ -87,13 +87,13 @@ SELECT transactions.id, transactions.date, transactions.memo, 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 + ), '')::text as transfer_account FROM transactions INNER JOIN accounts ON accounts.id = transactions.account_id LEFT JOIN payees ON payees.id = transactions.payee_id @@ -215,13 +215,13 @@ SELECT transactions.id, transactions.date, transactions.memo, 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 + ), '')::text as transfer_account FROM transactions INNER JOIN accounts ON accounts.id = transactions.account_id LEFT JOIN payees ON payees.id = transactions.payee_id From 1ea3590fd608dbe0feaed7ef60c9ff0fc487d549 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Fri, 25 Feb 2022 15:32:32 +0000 Subject: [PATCH 6/7] Add tests with negative numbers --- postgres/numeric/numeric_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/postgres/numeric/numeric_test.go b/postgres/numeric/numeric_test.go index 9e89a38..061ff9b 100644 --- a/postgres/numeric/numeric_test.go +++ b/postgres/numeric/numeric_test.go @@ -25,6 +25,14 @@ func TestMarshalJSON(t *testing.T) { {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("-1.23"), "-1.23"}, @@ -67,6 +75,14 @@ func TestParse(t *testing.T) { {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(124, -2), "1,24"}, {numeric.FromInt64WithExp(12345678912345, -5), "123456789.12345"}, From 79c0fceafef020cbc8bb2553c15611cd500190f2 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Fri, 25 Feb 2022 15:32:39 +0000 Subject: [PATCH 7/7] Fix formatting for negative numbers --- postgres/numeric/numeric.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/postgres/numeric/numeric.go b/postgres/numeric/numeric.go index 73fdb16..8d0cc5e 100644 --- a/postgres/numeric/numeric.go +++ b/postgres/numeric/numeric.go @@ -129,7 +129,11 @@ func (n Numeric) String() string { exp = -exp for length <= exp { - bytes = append([]byte{byte('0')}, bytes...) + if n.Int.Int64() < 0 { + bytes = append([]byte{bytes[0], byte('0')}, bytes[1:]...) + } else { + bytes = append([]byte{byte('0')}, bytes...) + } length++ } @@ -166,7 +170,11 @@ func (n Numeric) MarshalJSON() ([]byte, error) { exp = -exp for length <= exp { - bytes = append([]byte{byte('0')}, bytes...) + if n.Int.Int64() < 0 { + bytes = append([]byte{bytes[0], byte('0')}, bytes[1:]...) + } else { + bytes = append([]byte{byte('0')}, bytes...) + } length++ }