From 78389e4beba71db1390637576269569ca8ead1c5 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Fri, 25 Feb 2022 15:04:09 +0000 Subject: [PATCH] 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 != "" {