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