From 5e18d51b5d5cac4ead39603bf0cf36926922d151 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Wed, 8 Dec 2021 15:21:10 +0000 Subject: [PATCH] Only show last month's overflow --- http/budgeting.go | 7 ++++--- postgres/categories.sql.go | 35 +++++++++++++++++++++++++-------- postgres/queries/categories.sql | 19 +++++++++++++++--- web/budgeting.html | 2 ++ 4 files changed, 49 insertions(+), 14 deletions(-) diff --git a/http/budgeting.go b/http/budgeting.go index 7e95332..c13b31e 100644 --- a/http/budgeting.go +++ b/http/budgeting.go @@ -64,9 +64,10 @@ func (h *Handler) budgeting(c *gin.Context) { firstOfPreviousMonth := firstOfMonth.AddDate(0, -1, 0) params := postgres.GetCategoriesWithBalanceParams{ - BudgetID: budgetUUID, - FromDate: firstOfMonth, - ToDate: firstOfNextMonth, + BudgetID: budgetUUID, + FromDate: firstOfMonth, + ToDate: firstOfNextMonth, + PrevFromDate: firstOfMonth.AddDate(0, -1, 0), } categories, err := h.Service.DB.GetCategoriesWithBalance(c.Request.Context(), params) if err != nil { diff --git a/postgres/categories.sql.go b/postgres/categories.sql.go index 3377752..2b5d2ce 100644 --- a/postgres/categories.sql.go +++ b/postgres/categories.sql.go @@ -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 ), 0) )::decimal(12,2) as available, - ( - COALESCE(( + ( 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) + ), 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 @@ -153,15 +166,16 @@ SELECT categories.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 = $3 +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 - BudgetID uuid.UUID + ToDate time.Time + FromDate time.Time + PrevFromDate time.Time + BudgetID uuid.UUID } type GetCategoriesWithBalanceRow struct { @@ -175,7 +189,12 @@ type GetCategoriesWithBalanceRow struct { } 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 { return nil, err } diff --git a/postgres/queries/categories.sql b/postgres/queries/categories.sql index a322f22..22c4470 100644 --- a/postgres/queries/categories.sql +++ b/postgres/queries/categories.sql @@ -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 ), 0) )::decimal(12,2) as available, - ( - COALESCE(( + ( 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) + ), 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 diff --git a/web/budgeting.html b/web/budgeting.html index d424499..d3901e6 100644 --- a/web/budgeting.html +++ b/web/budgeting.html @@ -28,6 +28,7 @@ Category + Leftover Assigned Activity Available @@ -40,6 +41,7 @@ + {{template "amount-cell" .AvailableLastMonth}} {{template "amount-cell" .Assigned}} {{template "amount-cell" .Activity}} {{template "amount-cell" .Available}}