Add available balance
This commit is contained in:
parent
1d4bc158a8
commit
64822912d9
@ -13,6 +13,7 @@ import (
|
||||
type BudgetingData struct {
|
||||
AlwaysNeededData
|
||||
Categories []postgres.GetCategoriesWithBalanceRow
|
||||
AvailableBalance postgres.Numeric
|
||||
Date time.Time
|
||||
Next time.Time
|
||||
Previous time.Time
|
||||
@ -64,9 +65,16 @@ func (h *Handler) budgeting(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
availableBalance, err := h.Service.DB.GetAvailableBalance(c.Request.Context(), budgetUUID)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
d := BudgetingData{
|
||||
c.MustGet("data").(AlwaysNeededData),
|
||||
categories,
|
||||
availableBalance,
|
||||
firstOfMonth,
|
||||
firstOfNextMonth,
|
||||
firstOfPreviousMonth,
|
||||
|
@ -13,30 +13,40 @@ const createBudget = `-- name: CreateBudget :one
|
||||
INSERT INTO budgets
|
||||
(name, last_modification)
|
||||
VALUES ($1, NOW())
|
||||
RETURNING id, name, last_modification
|
||||
RETURNING id, name, last_modification, income_category_id
|
||||
`
|
||||
|
||||
func (q *Queries) CreateBudget(ctx context.Context, name string) (Budget, error) {
|
||||
row := q.db.QueryRowContext(ctx, createBudget, name)
|
||||
var i Budget
|
||||
err := row.Scan(&i.ID, &i.Name, &i.LastModification)
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.LastModification,
|
||||
&i.IncomeCategoryID,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getBudget = `-- name: GetBudget :one
|
||||
SELECT id, name, last_modification FROM budgets
|
||||
SELECT id, name, last_modification, income_category_id FROM budgets
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetBudget(ctx context.Context, id uuid.UUID) (Budget, error) {
|
||||
row := q.db.QueryRowContext(ctx, getBudget, id)
|
||||
var i Budget
|
||||
err := row.Scan(&i.ID, &i.Name, &i.LastModification)
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.LastModification,
|
||||
&i.IncomeCategoryID,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getBudgetsForUser = `-- name: GetBudgetsForUser :many
|
||||
SELECT budgets.id, budgets.name, budgets.last_modification FROM budgets
|
||||
SELECT budgets.id, budgets.name, budgets.last_modification, budgets.income_category_id FROM budgets
|
||||
LEFT JOIN user_budgets ON budgets.id = user_budgets.budget_id
|
||||
WHERE user_budgets.user_id = $1
|
||||
`
|
||||
@ -50,7 +60,12 @@ func (q *Queries) GetBudgetsForUser(ctx context.Context, userID uuid.UUID) ([]Bu
|
||||
var items []Budget
|
||||
for rows.Next() {
|
||||
var i Budget
|
||||
if err := rows.Scan(&i.ID, &i.Name, &i.LastModification); err != nil {
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.LastModification,
|
||||
&i.IncomeCategoryID,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
|
@ -48,6 +48,30 @@ 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
|
||||
WHERE budgets.id = $1
|
||||
) - (
|
||||
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
|
||||
))::decimal(12,2)
|
||||
`
|
||||
|
||||
func (q *Queries) GetAvailableBalance(ctx context.Context, budgetID uuid.UUID) (Numeric, error) {
|
||||
row := q.db.QueryRowContext(ctx, getAvailableBalance, budgetID)
|
||||
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
|
||||
@ -113,7 +137,16 @@ SELECT categories.id, categories.name, category_groups.name as group,
|
||||
WHERE categories.id = t_this.category_id
|
||||
AND t_this.date BETWEEN $1 AND $2
|
||||
)
|
||||
, 0)::decimal(12,2) as activity
|
||||
, 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 BETWEEN $1 AND $2
|
||||
)
|
||||
,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 = $3
|
||||
@ -133,6 +166,7 @@ type GetCategoriesWithBalanceRow struct {
|
||||
Group string
|
||||
Balance Numeric
|
||||
Activity Numeric
|
||||
Assigned Numeric
|
||||
}
|
||||
|
||||
func (q *Queries) GetCategoriesWithBalance(ctx context.Context, arg GetCategoriesWithBalanceParams) ([]GetCategoriesWithBalanceRow, error) {
|
||||
@ -150,6 +184,7 @@ func (q *Queries) GetCategoriesWithBalance(ctx context.Context, arg GetCategorie
|
||||
&i.Group,
|
||||
&i.Balance,
|
||||
&i.Activity,
|
||||
&i.Assigned,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ type Budget struct {
|
||||
ID uuid.UUID
|
||||
Name string
|
||||
LastModification sql.NullTime
|
||||
IncomeCategoryID uuid.UUID
|
||||
}
|
||||
|
||||
type Category struct {
|
||||
|
@ -43,9 +43,34 @@ SELECT categories.id, categories.name, category_groups.name as group,
|
||||
WHERE categories.id = t_this.category_id
|
||||
AND t_this.date BETWEEN @from_date AND @to_date
|
||||
)
|
||||
, 0)::decimal(12,2) as activity
|
||||
, 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 BETWEEN @from_date AND @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
|
||||
WHERE budgets.id = @budget_id
|
||||
) - (
|
||||
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
|
||||
))::decimal(12,2);
|
14
postgres/schema/202112072032_income-category.sql
Normal file
14
postgres/schema/202112072032_income-category.sql
Normal file
@ -0,0 +1,14 @@
|
||||
-- +goose Up
|
||||
ALTER TABLE budgets ADD COLUMN income_category_id uuid NULL REFERENCES categories (id);
|
||||
UPDATE budgets
|
||||
SET income_category_id = (
|
||||
SELECT categories.id
|
||||
FROM categories
|
||||
LEFT JOIN category_groups ON category_groups.id = categories.category_group_id
|
||||
WHERE categories.name = 'Ready to Assign'
|
||||
AND category_groups.budget_id = budgets.id
|
||||
);
|
||||
ALTER TABLE budgets ALTER COLUMN income_category_id SET NOT NULL;
|
||||
|
||||
-- +goose Down
|
||||
ALTER TABLE budgets DROP COLUMN income_category_id;
|
@ -19,12 +19,16 @@
|
||||
<a href="{{printf "/budget/%s" .Budget.ID}}">Current Month</a> -
|
||||
<a href="{{printf "/budget/%s/%d/%d" .Budget.ID .Next.Year .Next.Month}}">Next Month</a>
|
||||
</div>
|
||||
<div>
|
||||
<span>Available Balance: </span>{{template "amount" .AvailableBalance}}
|
||||
</div>
|
||||
<table class="container col-lg-12" id="content">
|
||||
<tr>
|
||||
<th>Group</th>
|
||||
<th>Category</th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
<th>Assigned</th>
|
||||
<th>Balance</th>
|
||||
<th>Activity</th>
|
||||
</tr>
|
||||
@ -36,6 +40,7 @@
|
||||
</td>
|
||||
<td>
|
||||
</td>
|
||||
{{template "amount-cell" .Assigned}}
|
||||
{{template "amount-cell" .Balance}}
|
||||
{{template "amount-cell" .Activity}}
|
||||
</tr>
|
||||
|
Loading…
x
Reference in New Issue
Block a user