Make available balance date-dependent
This commit is contained in:
@ -65,7 +65,11 @@ func (h *Handler) budgeting(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
availableBalance, err := h.Service.DB.GetAvailableBalance(c.Request.Context(), budgetUUID)
|
||||
availableParams := postgres.GetAvailableBalanceParams{
|
||||
BudgetID: budgetUUID,
|
||||
FromDate: firstOfNextMonth,
|
||||
}
|
||||
availableBalance, err := h.Service.DB.GetAvailableBalance(c.Request.Context(), availableParams)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
|
@ -56,17 +56,24 @@ SELECT
|
||||
LEFT JOIN categories ON categories.id = transactions.category_id
|
||||
LEFT JOIN budgets ON budgets.income_category_id = categories.id
|
||||
WHERE budgets.id = $1
|
||||
AND transactions.date < $2
|
||||
) - (
|
||||
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
|
||||
AND assignments.date < $2
|
||||
))::decimal(12,2)
|
||||
`
|
||||
|
||||
func (q *Queries) GetAvailableBalance(ctx context.Context, budgetID uuid.UUID) (Numeric, error) {
|
||||
row := q.db.QueryRowContext(ctx, getAvailableBalance, budgetID)
|
||||
type GetAvailableBalanceParams struct {
|
||||
BudgetID uuid.UUID
|
||||
FromDate time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) GetAvailableBalance(ctx context.Context, arg GetAvailableBalanceParams) (Numeric, error) {
|
||||
row := q.db.QueryRowContext(ctx, getAvailableBalance, arg.BudgetID, arg.FromDate)
|
||||
var column_1 Numeric
|
||||
err := row.Scan(&column_1)
|
||||
return column_1, err
|
||||
|
@ -67,10 +67,12 @@ SELECT
|
||||
LEFT JOIN categories ON categories.id = transactions.category_id
|
||||
LEFT JOIN budgets ON budgets.income_category_id = categories.id
|
||||
WHERE budgets.id = @budget_id
|
||||
AND transactions.date < @from_date
|
||||
) - (
|
||||
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
|
||||
AND assignments.date < @from_date
|
||||
))::decimal(12,2);
|
Reference in New Issue
Block a user