Only show last month's overflow
This commit is contained in:
parent
11179a1593
commit
5e18d51b5d
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -28,6 +28,7 @@
|
||||
<th>Category</th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
<th>Leftover</th>
|
||||
<th>Assigned</th>
|
||||
<th>Activity</th>
|
||||
<th>Available</th>
|
||||
@ -40,6 +41,7 @@
|
||||
</td>
|
||||
<td>
|
||||
</td>
|
||||
{{template "amount-cell" .AvailableLastMonth}}
|
||||
{{template "amount-cell" .Assigned}}
|
||||
{{template "amount-cell" .Activity}}
|
||||
{{template "amount-cell" .Available}}
|
||||
|
Loading…
x
Reference in New Issue
Block a user