Implement budgeting views by calculating most values locally
This commit is contained in:
@ -5,7 +5,6 @@ package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@ -48,43 +47,11 @@ func (q *Queries) CreateCategoryGroup(ctx context.Context, arg CreateCategoryGro
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getAvailableBalance = `-- name: GetAvailableBalance :one
|
||||
SELECT
|
||||
((
|
||||
SELECT SUM(transactions.amount)
|
||||
FROM transactions
|
||||
LEFT JOIN categories ON categories.id = transactions.category_id
|
||||
LEFT JOIN budgets ON budgets.income_category_id = categories.id
|
||||
INNER JOIN accounts ON accounts.id = transactions.account_id
|
||||
WHERE budgets.id = $1
|
||||
AND transactions.date < $2
|
||||
AND accounts.on_budget
|
||||
) - (
|
||||
SELECT SUM(assignments.amount)
|
||||
FROM assignments
|
||||
INNER JOIN categories ON categories.id = assignments.category_id
|
||||
INNER JOIN category_groups ON category_groups.id = categories.category_group_id
|
||||
WHERE category_groups.budget_id = $1
|
||||
AND assignments.date < $2
|
||||
))::decimal(12,2)
|
||||
`
|
||||
|
||||
type GetAvailableBalanceParams struct {
|
||||
BudgetID uuid.UUID
|
||||
FromDate time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) GetAvailableBalance(ctx context.Context, arg GetAvailableBalanceParams) (Numeric, error) {
|
||||
row := q.db.QueryRowContext(ctx, getAvailableBalance, arg.BudgetID, arg.FromDate)
|
||||
var column_1 Numeric
|
||||
err := row.Scan(&column_1)
|
||||
return column_1, err
|
||||
}
|
||||
|
||||
const getCategories = `-- name: GetCategories :many
|
||||
SELECT categories.id, categories.category_group_id, categories.name, category_groups.name as group FROM categories
|
||||
INNER JOIN category_groups ON categories.category_group_id = category_groups.id
|
||||
WHERE category_groups.budget_id = $1
|
||||
ORDER BY category_groups.name, categories.name
|
||||
`
|
||||
|
||||
type GetCategoriesRow struct {
|
||||
@ -122,109 +89,6 @@ func (q *Queries) GetCategories(ctx context.Context, budgetID uuid.UUID) ([]GetC
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getCategoriesWithBalance = `-- name: GetCategoriesWithBalance :many
|
||||
SELECT categories.id, categories.name, category_groups.name as group,
|
||||
(
|
||||
COALESCE((
|
||||
SELECT SUM(a_hist.amount) FROM assignments a_hist
|
||||
WHERE categories.id = a_hist.category_id AND a_hist.date < $1
|
||||
), 0)+COALESCE((
|
||||
SELECT SUM(t_hist.amount) FROM transactions t_hist
|
||||
WHERE categories.id = t_hist.category_id AND t_hist.date < $1
|
||||
), 0)
|
||||
)::decimal(12,2) as available,
|
||||
(
|
||||
COALESCE((
|
||||
SELECT SUM(a_hist.amount) FROM assignments a_hist
|
||||
WHERE categories.id = a_hist.category_id AND a_hist.date < $2
|
||||
), 0)+COALESCE((
|
||||
SELECT SUM(t_hist.amount) FROM transactions t_hist
|
||||
WHERE categories.id = t_hist.category_id AND t_hist.date < $2
|
||||
), 0)-CASE WHEN (COALESCE((
|
||||
SELECT SUM(a_hist.amount) FROM assignments a_hist
|
||||
WHERE categories.id = a_hist.category_id AND a_hist.date < $3
|
||||
), 0)+COALESCE((
|
||||
SELECT SUM(t_hist.amount) FROM transactions t_hist
|
||||
WHERE categories.id = t_hist.category_id AND t_hist.date < $3
|
||||
), 0)) < 0 THEN (COALESCE((
|
||||
SELECT SUM(a_hist.amount) FROM assignments a_hist
|
||||
WHERE categories.id = a_hist.category_id AND a_hist.date < $3
|
||||
), 0)+COALESCE((
|
||||
SELECT SUM(t_hist.amount) FROM transactions t_hist
|
||||
WHERE categories.id = t_hist.category_id AND t_hist.date < $3
|
||||
), 0)) ELSE 0 END
|
||||
|
||||
|
||||
)::decimal(12,2) as available_last_month,
|
||||
COALESCE((
|
||||
SELECT SUM(t_this.amount) FROM transactions t_this
|
||||
WHERE categories.id = t_this.category_id AND t_this.date >= $2 AND t_this.date < $1
|
||||
), 0)::decimal(12,2) as activity,
|
||||
COALESCE((
|
||||
SELECT SUM(a_hist.amount) FROM assignments a_hist
|
||||
WHERE categories.id = a_hist.category_id AND a_hist.date >= $2 AND a_hist.date < $1
|
||||
), 0)::decimal(12,2) as assigned
|
||||
|
||||
FROM categories
|
||||
INNER JOIN category_groups ON categories.category_group_id = category_groups.id
|
||||
WHERE category_groups.budget_id = $4
|
||||
GROUP BY categories.id, categories.name, category_groups.name
|
||||
ORDER BY category_groups.name, categories.name
|
||||
`
|
||||
|
||||
type GetCategoriesWithBalanceParams struct {
|
||||
ToDate time.Time
|
||||
FromDate time.Time
|
||||
PrevFromDate time.Time
|
||||
BudgetID uuid.UUID
|
||||
}
|
||||
|
||||
type GetCategoriesWithBalanceRow struct {
|
||||
ID uuid.UUID
|
||||
Name string
|
||||
Group string
|
||||
Available Numeric
|
||||
AvailableLastMonth Numeric
|
||||
Activity Numeric
|
||||
Assigned Numeric
|
||||
}
|
||||
|
||||
func (q *Queries) GetCategoriesWithBalance(ctx context.Context, arg GetCategoriesWithBalanceParams) ([]GetCategoriesWithBalanceRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getCategoriesWithBalance,
|
||||
arg.ToDate,
|
||||
arg.FromDate,
|
||||
arg.PrevFromDate,
|
||||
arg.BudgetID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetCategoriesWithBalanceRow
|
||||
for rows.Next() {
|
||||
var i GetCategoriesWithBalanceRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.Group,
|
||||
&i.Available,
|
||||
&i.AvailableLastMonth,
|
||||
&i.Activity,
|
||||
&i.Assigned,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getCategoryGroups = `-- name: GetCategoryGroups :many
|
||||
SELECT category_groups.id, category_groups.budget_id, category_groups.name FROM category_groups
|
||||
WHERE category_groups.budget_id = $1
|
||||
|
@ -17,73 +17,5 @@ RETURNING *;
|
||||
-- name: GetCategories :many
|
||||
SELECT categories.*, category_groups.name as group FROM categories
|
||||
INNER JOIN category_groups ON categories.category_group_id = category_groups.id
|
||||
WHERE category_groups.budget_id = $1;
|
||||
|
||||
-- name: GetCategoriesWithBalance :many
|
||||
SELECT categories.id, categories.name, category_groups.name as group,
|
||||
(
|
||||
COALESCE((
|
||||
SELECT SUM(a_hist.amount) FROM assignments a_hist
|
||||
WHERE categories.id = a_hist.category_id AND a_hist.date < @to_date
|
||||
), 0)+COALESCE((
|
||||
SELECT SUM(t_hist.amount) FROM transactions t_hist
|
||||
WHERE categories.id = t_hist.category_id AND t_hist.date < @to_date
|
||||
), 0)
|
||||
)::decimal(12,2) as available,
|
||||
(
|
||||
COALESCE((
|
||||
SELECT SUM(a_hist.amount) FROM assignments a_hist
|
||||
WHERE categories.id = a_hist.category_id AND a_hist.date < @from_date
|
||||
), 0)+COALESCE((
|
||||
SELECT SUM(t_hist.amount) FROM transactions t_hist
|
||||
WHERE categories.id = t_hist.category_id AND t_hist.date < @from_date
|
||||
), 0)-CASE WHEN (COALESCE((
|
||||
SELECT SUM(a_hist.amount) FROM assignments a_hist
|
||||
WHERE categories.id = a_hist.category_id AND a_hist.date < @prev_from_date
|
||||
), 0)+COALESCE((
|
||||
SELECT SUM(t_hist.amount) FROM transactions t_hist
|
||||
WHERE categories.id = t_hist.category_id AND t_hist.date < @prev_from_date
|
||||
), 0)) < 0 THEN (COALESCE((
|
||||
SELECT SUM(a_hist.amount) FROM assignments a_hist
|
||||
WHERE categories.id = a_hist.category_id AND a_hist.date < @prev_from_date
|
||||
), 0)+COALESCE((
|
||||
SELECT SUM(t_hist.amount) FROM transactions t_hist
|
||||
WHERE categories.id = t_hist.category_id AND t_hist.date < @prev_from_date
|
||||
), 0)) ELSE 0 END
|
||||
|
||||
|
||||
)::decimal(12,2) as available_last_month,
|
||||
COALESCE((
|
||||
SELECT SUM(t_this.amount) FROM transactions t_this
|
||||
WHERE categories.id = t_this.category_id AND t_this.date >= @from_date AND t_this.date < @to_date
|
||||
), 0)::decimal(12,2) as activity,
|
||||
COALESCE((
|
||||
SELECT SUM(a_hist.amount) FROM assignments a_hist
|
||||
WHERE categories.id = a_hist.category_id AND a_hist.date >= @from_date AND a_hist.date < @to_date
|
||||
), 0)::decimal(12,2) as assigned
|
||||
|
||||
FROM categories
|
||||
INNER JOIN category_groups ON categories.category_group_id = category_groups.id
|
||||
WHERE category_groups.budget_id = @budget_id
|
||||
GROUP BY categories.id, categories.name, category_groups.name
|
||||
ORDER BY category_groups.name, categories.name;
|
||||
|
||||
-- name: GetAvailableBalance :one
|
||||
SELECT
|
||||
((
|
||||
SELECT SUM(transactions.amount)
|
||||
FROM transactions
|
||||
LEFT JOIN categories ON categories.id = transactions.category_id
|
||||
LEFT JOIN budgets ON budgets.income_category_id = categories.id
|
||||
INNER JOIN accounts ON accounts.id = transactions.account_id
|
||||
WHERE budgets.id = @budget_id
|
||||
AND transactions.date < @from_date
|
||||
AND accounts.on_budget
|
||||
) - (
|
||||
SELECT SUM(assignments.amount)
|
||||
FROM assignments
|
||||
INNER JOIN categories ON categories.id = assignments.category_id
|
||||
INNER JOIN category_groups ON category_groups.id = categories.category_group_id
|
||||
WHERE category_groups.budget_id = @budget_id
|
||||
AND assignments.date < @from_date
|
||||
))::decimal(12,2);
|
||||
WHERE category_groups.budget_id = $1
|
||||
ORDER BY category_groups.name, categories.name;
|
Reference in New Issue
Block a user