Implement reconciliation #26

Merged
jacob1123 merged 13 commits from reconcilation into master 2022-02-27 22:16:02 +01:00
4 changed files with 65 additions and 114 deletions
Showing only changes of commit 81aacf339e - Show all commits

View File

@ -72,6 +72,24 @@ type CategoryGroup struct {
Name string Name string
} }
type DisplayTransaction struct {
ID uuid.UUID
Date time.Time
Memo string
Amount numeric.Numeric
GroupID uuid.NullUUID
Status TransactionStatus
Account string
PayeeID uuid.NullUUID
CategoryID uuid.NullUUID
Payee string
CategoryGroup string
Category string
TransferAccount string
BudgetID uuid.UUID
AccountID uuid.UUID
}
type Payee struct { type Payee struct {
ID uuid.UUID ID uuid.UUID
BudgetID uuid.UUID BudgetID uuid.UUID

View File

@ -27,47 +27,15 @@ DELETE FROM transactions
WHERE id = $1; WHERE id = $1;
-- name: GetAllTransactionsForBudget :many -- name: GetAllTransactionsForBudget :many
SELECT transactions.id, transactions.date, transactions.memo, SELECT t.*
transactions.amount, transactions.group_id, transactions.status, FROM display_transactions AS t
accounts.name as account, transactions.payee_id, transactions.category_id, WHERE t.budget_id = $1
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
INNER JOIN accounts ON accounts.id = transactions.account_id
LEFT JOIN payees ON payees.id = transactions.payee_id
LEFT JOIN categories ON categories.id = transactions.category_id
LEFT JOIN category_groups ON category_groups.id = categories.category_group_id
WHERE accounts.budget_id = $1
ORDER BY transactions.date DESC; ORDER BY transactions.date DESC;
-- name: GetTransactionsForAccount :many -- name: GetTransactionsForAccount :many
SELECT transactions.id, transactions.date, transactions.memo, SELECT t.*
transactions.amount, transactions.group_id, transactions.status, FROM display_transactions AS t
accounts.name as account, transactions.payee_id, transactions.category_id, WHERE t.account_id = $1
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
INNER JOIN accounts ON accounts.id = transactions.account_id
LEFT JOIN payees ON payees.id = transactions.payee_id
LEFT JOIN categories ON categories.id = transactions.category_id
LEFT JOIN category_groups ON category_groups.id = categories.category_group_id
WHERE transactions.account_id = $1
ORDER BY transactions.date DESC ORDER BY transactions.date DESC
LIMIT 200; LIMIT 200;

View File

@ -0,0 +1,25 @@
-- +goose Up
CREATE VIEW display_transactions AS
SELECT transactions.id, transactions.date, transactions.memo,
transactions.amount, transactions.group_id, transactions.status,
accounts.name as account, transactions.payee_id, transactions.category_id,
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,
accounts.budget_id, transactions.account_id
FROM transactions
INNER JOIN accounts ON accounts.id = transactions.account_id
LEFT JOIN payees ON payees.id = transactions.payee_id
LEFT JOIN categories ON categories.id = transactions.category_id
LEFT JOIN category_groups ON category_groups.id = categories.category_group_id
ORDER BY transactions.date DESC;
-- +goose Down
DROP VIEW display_transactions;

View File

@ -81,53 +81,21 @@ 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, SELECT t.id, t.date, t.memo, t.amount, t.group_id, t.status, t.account, t.payee_id, t.category_id, t.payee, t.category_group, t.category, t.transfer_account, t.budget_id, t.account_id
transactions.amount, transactions.group_id, transactions.status, FROM display_transactions AS t
accounts.name as account, transactions.payee_id, transactions.category_id, WHERE t.budget_id = $1
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
INNER JOIN accounts ON accounts.id = transactions.account_id
LEFT JOIN payees ON payees.id = transactions.payee_id
LEFT JOIN categories ON categories.id = transactions.category_id
LEFT JOIN category_groups ON category_groups.id = categories.category_group_id
WHERE accounts.budget_id = $1
ORDER BY transactions.date DESC ORDER BY transactions.date DESC
` `
type GetAllTransactionsForBudgetRow struct { func (q *Queries) GetAllTransactionsForBudget(ctx context.Context, budgetID uuid.UUID) ([]DisplayTransaction, error) {
ID uuid.UUID
Date time.Time
Memo string
Amount numeric.Numeric
GroupID uuid.NullUUID
Status TransactionStatus
Account string
PayeeID uuid.NullUUID
CategoryID uuid.NullUUID
Payee string
CategoryGroup string
Category string
TransferAccount string
}
func (q *Queries) GetAllTransactionsForBudget(ctx context.Context, budgetID uuid.UUID) ([]GetAllTransactionsForBudgetRow, error) {
rows, err := q.db.QueryContext(ctx, getAllTransactionsForBudget, budgetID) rows, err := q.db.QueryContext(ctx, getAllTransactionsForBudget, budgetID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rows.Close() defer rows.Close()
var items []GetAllTransactionsForBudgetRow var items []DisplayTransaction
for rows.Next() { for rows.Next() {
var i GetAllTransactionsForBudgetRow var i DisplayTransaction
if err := rows.Scan( if err := rows.Scan(
&i.ID, &i.ID,
&i.Date, &i.Date,
@ -142,6 +110,8 @@ func (q *Queries) GetAllTransactionsForBudget(ctx context.Context, budgetID uuid
&i.CategoryGroup, &i.CategoryGroup,
&i.Category, &i.Category,
&i.TransferAccount, &i.TransferAccount,
&i.BudgetID,
&i.AccountID,
); err != nil { ); err != nil {
return nil, err return nil, err
} }
@ -213,54 +183,22 @@ func (q *Queries) GetTransactionsByMonthAndCategory(ctx context.Context, budgetI
} }
const getTransactionsForAccount = `-- name: GetTransactionsForAccount :many const getTransactionsForAccount = `-- name: GetTransactionsForAccount :many
SELECT transactions.id, transactions.date, transactions.memo, SELECT t.id, t.date, t.memo, t.amount, t.group_id, t.status, t.account, t.payee_id, t.category_id, t.payee, t.category_group, t.category, t.transfer_account, t.budget_id, t.account_id
transactions.amount, transactions.group_id, transactions.status, FROM display_transactions AS t
accounts.name as account, transactions.payee_id, transactions.category_id, WHERE t.account_id = $1
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
INNER JOIN accounts ON accounts.id = transactions.account_id
LEFT JOIN payees ON payees.id = transactions.payee_id
LEFT JOIN categories ON categories.id = transactions.category_id
LEFT JOIN category_groups ON category_groups.id = categories.category_group_id
WHERE transactions.account_id = $1
ORDER BY transactions.date DESC ORDER BY transactions.date DESC
LIMIT 200 LIMIT 200
` `
type GetTransactionsForAccountRow struct { func (q *Queries) GetTransactionsForAccount(ctx context.Context, accountID uuid.UUID) ([]DisplayTransaction, error) {
ID uuid.UUID
Date time.Time
Memo string
Amount numeric.Numeric
GroupID uuid.NullUUID
Status TransactionStatus
Account string
PayeeID uuid.NullUUID
CategoryID uuid.NullUUID
Payee string
CategoryGroup string
Category string
TransferAccount string
}
func (q *Queries) GetTransactionsForAccount(ctx context.Context, accountID uuid.UUID) ([]GetTransactionsForAccountRow, error) {
rows, err := q.db.QueryContext(ctx, getTransactionsForAccount, accountID) rows, err := q.db.QueryContext(ctx, getTransactionsForAccount, accountID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rows.Close() defer rows.Close()
var items []GetTransactionsForAccountRow var items []DisplayTransaction
for rows.Next() { for rows.Next() {
var i GetTransactionsForAccountRow var i DisplayTransaction
if err := rows.Scan( if err := rows.Scan(
&i.ID, &i.ID,
&i.Date, &i.Date,
@ -275,6 +213,8 @@ func (q *Queries) GetTransactionsForAccount(ctx context.Context, accountID uuid.
&i.CategoryGroup, &i.CategoryGroup,
&i.Category, &i.Category,
&i.TransferAccount, &i.TransferAccount,
&i.BudgetID,
&i.AccountID,
); err != nil { ); err != nil {
return nil, err return nil, err
} }