Only show last month's overflow

This commit is contained in:
Jan Bader 2021-12-08 15:21:10 +00:00
parent 11179a1593
commit 5e18d51b5d
4 changed files with 49 additions and 14 deletions

View File

@ -64,9 +64,10 @@ func (h *Handler) budgeting(c *gin.Context) {
firstOfPreviousMonth := firstOfMonth.AddDate(0, -1, 0) firstOfPreviousMonth := firstOfMonth.AddDate(0, -1, 0)
params := postgres.GetCategoriesWithBalanceParams{ params := postgres.GetCategoriesWithBalanceParams{
BudgetID: budgetUUID, BudgetID: budgetUUID,
FromDate: firstOfMonth, FromDate: firstOfMonth,
ToDate: firstOfNextMonth, ToDate: firstOfNextMonth,
PrevFromDate: firstOfMonth.AddDate(0, -1, 0),
} }
categories, err := h.Service.DB.GetCategoriesWithBalance(c.Request.Context(), params) categories, err := h.Service.DB.GetCategoriesWithBalance(c.Request.Context(), params)
if err != nil { if err != nil {

View File

@ -133,14 +133,27 @@ SELECT categories.id, categories.name, category_groups.name as group,
WHERE categories.id = t_hist.category_id AND t_hist.date < $1 WHERE categories.id = t_hist.category_id AND t_hist.date < $1
), 0) ), 0)
)::decimal(12,2) as available, )::decimal(12,2) as available,
( ( COALESCE((
COALESCE((
SELECT SUM(a_hist.amount) FROM assignments a_hist SELECT SUM(a_hist.amount) FROM assignments a_hist
WHERE categories.id = a_hist.category_id AND a_hist.date < $2 WHERE categories.id = a_hist.category_id AND a_hist.date < $2
), 0)+COALESCE(( ), 0)+COALESCE((
SELECT SUM(t_hist.amount) FROM transactions t_hist SELECT SUM(t_hist.amount) FROM transactions t_hist
WHERE categories.id = t_hist.category_id AND t_hist.date < $2 WHERE categories.id = t_hist.category_id AND t_hist.date < $2
), 0) ), 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, )::decimal(12,2) as available_last_month,
COALESCE(( COALESCE((
SELECT SUM(t_this.amount) FROM transactions t_this SELECT SUM(t_this.amount) FROM transactions t_this
@ -153,15 +166,16 @@ SELECT categories.id, categories.name, category_groups.name as group,
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 = $4
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
` `
type GetCategoriesWithBalanceParams struct { type GetCategoriesWithBalanceParams struct {
ToDate time.Time ToDate time.Time
FromDate time.Time FromDate time.Time
BudgetID uuid.UUID PrevFromDate time.Time
BudgetID uuid.UUID
} }
type GetCategoriesWithBalanceRow struct { type GetCategoriesWithBalanceRow struct {
@ -175,7 +189,12 @@ type GetCategoriesWithBalanceRow struct {
} }
func (q *Queries) GetCategoriesWithBalance(ctx context.Context, arg GetCategoriesWithBalanceParams) ([]GetCategoriesWithBalanceRow, error) { func (q *Queries) GetCategoriesWithBalance(ctx context.Context, arg GetCategoriesWithBalanceParams) ([]GetCategoriesWithBalanceRow, error) {
rows, err := q.db.QueryContext(ctx, getCategoriesWithBalance, arg.ToDate, arg.FromDate, arg.BudgetID) rows, err := q.db.QueryContext(ctx, getCategoriesWithBalance,
arg.ToDate,
arg.FromDate,
arg.PrevFromDate,
arg.BudgetID,
)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -30,14 +30,27 @@ SELECT categories.id, categories.name, category_groups.name as group,
WHERE categories.id = t_hist.category_id AND t_hist.date < @to_date WHERE categories.id = t_hist.category_id AND t_hist.date < @to_date
), 0) ), 0)
)::decimal(12,2) as available, )::decimal(12,2) as available,
( ( COALESCE((
COALESCE((
SELECT SUM(a_hist.amount) FROM assignments a_hist SELECT SUM(a_hist.amount) FROM assignments a_hist
WHERE categories.id = a_hist.category_id AND a_hist.date < @from_date WHERE categories.id = a_hist.category_id AND a_hist.date < @from_date
), 0)+COALESCE(( ), 0)+COALESCE((
SELECT SUM(t_hist.amount) FROM transactions t_hist SELECT SUM(t_hist.amount) FROM transactions t_hist
WHERE categories.id = t_hist.category_id AND t_hist.date < @from_date WHERE categories.id = t_hist.category_id AND t_hist.date < @from_date
), 0) ), 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, )::decimal(12,2) as available_last_month,
COALESCE(( COALESCE((
SELECT SUM(t_this.amount) FROM transactions t_this SELECT SUM(t_this.amount) FROM transactions t_this

View File

@ -28,6 +28,7 @@
<th>Category</th> <th>Category</th>
<th></th> <th></th>
<th></th> <th></th>
<th>Leftover</th>
<th>Assigned</th> <th>Assigned</th>
<th>Activity</th> <th>Activity</th>
<th>Available</th> <th>Available</th>
@ -40,6 +41,7 @@
</td> </td>
<td> <td>
</td> </td>
{{template "amount-cell" .AvailableLastMonth}}
{{template "amount-cell" .Assigned}} {{template "amount-cell" .Assigned}}
{{template "amount-cell" .Activity}} {{template "amount-cell" .Activity}}
{{template "amount-cell" .Available}} {{template "amount-cell" .Available}}