Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

fix(categories): preserve budget schedules when transferring entries (#1094)

Albert Dolkund80d3139a9bf
diff --git a/backend/app/services/category_service.py b/backend/app/services/category_service.py
index 9c3c48f7..80cff24d 100644
--- a/backend/app/services/category_service.py
+++ b/backend/app/services/category_service.py
@@ -1,5 +1,7 @@
import uuid
from dataclasses import dataclass, field
+from decimal import Decimal
+from itertools import groupby
from typing import Optional
from sqlalchemy import func, or_, select, update as sa_update
@@ -284,18 +286,20 @@ async def get_category_usage(
return CategoryUsage(**counts, rules=rules)
+_Limit = tuple[Decimal, Decimal | None]
+
+
async def _merge_budgets(
session: AsyncSession,
workspace_id: uuid.UUID,
category_id: uuid.UUID,
destination_id: uuid.UUID,
) -> None:
- """Move the budgets over, adding up the ones that would land on each other.
+ """Combine the workspace's effective monthly limits without losing either side.
- A budget is unique per user, category, month and kind, so moving one onto a
- month the destination already budgets would break that constraint. The two
- limits are added instead: they are the same person's ceiling for the same
- month, and dropping either would quietly lower it.
+ Recurring rows change a limit from their month onward; a one-off row only
+ overrides its own category for that month. Merge those schedules before
+ moving the rows, or a later recurring row would hide the earlier limit.
"""
moving = (
(
@@ -324,23 +328,47 @@ async def _merge_budgets(
.scalars()
.all()
)
- by_slot = {(b.user_id, b.month, b.is_recurring): b for b in existing}
-
- for budget in moving:
- slot = (budget.user_id, budget.month, budget.is_recurring)
- target = by_slot.get(slot)
- if target is None:
- budget.category_id = destination_id
- by_slot[slot] = budget
- continue
-
- target.amount += budget.amount
- # The converted amount is only a sum when both sides carry one.
- if target.amount_primary is not None and budget.amount_primary is not None:
- target.amount_primary += budget.amount_primary
- else:
- target.amount_primary = None
- await session.delete(budget)
+ # Readers take every member's row in a category's latest slot, so the rows
+ # of each (month, kind) slot must add up to both categories' limits. Each
+ # member keeps one row carrying their own limits, as the unique constraint
+ # requires; a limit whose author has no row in the slot (the other
+ # category's carried-over default) lands on a kept row, destination first.
+ ordered = sorted([*moving, *existing], key=lambda b: (b.month, not b.is_recurring))
+ recurring: dict[uuid.UUID, dict[uuid.UUID, _Limit]] = {}
+ for (_, is_recurring), slot in groupby(ordered, key=lambda b: (b.month, b.is_recurring)):
+ rows = list(slot)
+ limits: dict[uuid.UUID, dict[uuid.UUID, _Limit]] = {}
+ for budget in rows:
+ limits.setdefault(budget.category_id, {})[budget.user_id] = (
+ budget.amount, budget.amount_primary,
+ )
+ if is_recurring:
+ # Store immutable amounts before changing any ORM row. An
+ # override below must still see each original category's limit.
+ recurring.update(limits)
+ effective = recurring if is_recurring else recurring | limits
+
+ # Keeping the destination row means no source row is moved onto a
+ # slot the same member still holds, which would fail at flush.
+ kept: dict[uuid.UUID, Budget] = {}
+ for budget in sorted(rows, key=lambda b: b.category_id != destination_id):
+ kept.setdefault(budget.user_id, budget)
+ fallback = next(iter(kept))
+ shares: dict[uuid.UUID, list[_Limit]] = {user_id: [] for user_id in kept}
+ for by_user in effective.values():
+ for user_id, limit in by_user.items():
+ shares[user_id if user_id in kept else fallback].append(limit)
+
+ for user_id, target in kept.items():
+ target.category_id = destination_id
+ target.amount = sum((amount for amount, _ in shares[user_id]), Decimal("0"))
+ primary = [value for _, value in shares[user_id] if value is not None]
+ target.amount_primary = (
+ sum(primary, Decimal("0")) if len(primary) == len(shares[user_id]) else None
+ )
+ for budget in rows:
+ if kept[budget.user_id] is not budget:
+ await session.delete(budget)
async def _repoint_rules(
diff --git a/backend/tests/test_category_delete_transfer.py b/backend/tests/test_category_delete_transfer.py
index 38e9d52d..b10db0d6 100644
--- a/backend/tests/test_category_delete_transfer.py
+++ b/backend/tests/test_category_delete_transfer.py
@@ -22,7 +22,7 @@ from app.models.rule import Rule
from app.models.transaction import Transaction
from app.models.account import Account
from app.models.user import User
-from app.services import category_service
+from app.services import budget_service, category_service
Row = TypeVar("Row")
@@ -315,7 +315,7 @@ async def test_a_recurring_budget_does_not_absorb_a_one_off_one(
test_workspace,
test_categories: list[Category],
):
- """The two kinds live side by side on the same month, so they stay apart."""
+ """The one-off includes both limits without changing the recurring default."""
source, destination = test_categories[0], test_categories[1]
month = date.today().replace(day=1)
moving = await _make_budget(
@@ -337,7 +337,210 @@ async def test_a_recurring_budget_does_not_absorb_a_one_off_one(
assert response.status_code == 204
session.expire_all()
assert (await _reload(session, Budget, moving_id)).category_id == destination_id
- assert (await _reload(session, Budget, standing_id)).amount == Decimal("500.00")
+ assert (await _reload(session, Budget, moving_id)).amount == Decimal("300.00")
+ assert (await _reload(session, Budget, standing_id)).amount == Decimal("800.00")
+
+
[email protected]("different_authors", [False, True], ids=["one-author", "two-authors"])
+ ("source_rows", "destination_rows", "expected"),
+ [
+ pytest.param(
+ [(1, "100", True)], [(3, "200", True)],
+ [(1, "100", True), (2, "100", True), (3, "300", True), (4, "300", True)],
+ id="source-starts-first",
+ ),
+ pytest.param(
+ [(3, "100", True)], [(1, "200", True)],
+ [(1, "200", True), (2, "200", True), (3, "300", True), (4, "300", True)],
+ id="destination-starts-first",
+ ),
+ pytest.param(
+ [(1, "100", True)], [(1, "200", True)],
+ [(1, "300", True), (2, "300", True)],
+ id="same-start-month",
+ ),
+ pytest.param(
+ [(1, "100", True), (2, "50", False)], [(1, "200", True)],
+ [(1, "300", True), (2, "250", False), (3, "300", True)],
+ id="source-monthly-override",
+ ),
+ pytest.param(
+ [(1, "100", True)], [(1, "200", True), (2, "50", False)],
+ [(1, "300", True), (2, "150", False), (3, "300", True)],
+ id="destination-monthly-override",
+ ),
+ pytest.param(
+ [(1, "100", True), (2, "50", False)],
+ [(1, "200", True), (2, "80", False)],
+ [(1, "300", True), (2, "130", False), (3, "300", True)],
+ id="both-monthly-overrides",
+ ),
+ pytest.param(
+ [(1, "100", True), (7, "150", True)],
+ [(3, "200", True), (9, "300", True)],
+ [
+ (1, "100", True), (3, "300", True), (6, "300", True),
+ (7, "350", True), (9, "450", True), (10, "450", True),
+ ],
+ id="future-changes-on-both-sides",
+ ),
+ ],
+)
+async def test_category_transfer_preserves_effective_budget_schedule(
+ session: AsyncSession,
+ test_user: User,
+ test_workspace,
+ test_categories: list[Category],
+ source_rows: list[tuple[int, str, bool]],
+ destination_rows: list[tuple[int, str, bool]],
+ expected: list[tuple[int, str, bool]],
+ different_authors: bool,
+):
+ source, destination = test_categories[:2]
+ destination_author = test_user
+ if different_authors:
+ destination_author = User(
+ id=uuid.uuid4(), email="[email protected]", hashed_password="synthetic",
+ )
+ session.add(destination_author)
+ await session.commit()
+ destination_budget_ids: list[uuid.UUID] = []
+ for category, rows in ((source, source_rows), (destination, destination_rows)):
+ for month, amount, recurring in rows:
+ budget = await _make_budget(
+ session, destination_author if category is destination else test_user,
+ test_workspace, category.id,
+ amount=amount, month=date(2025, month, 1), is_recurring=recurring,
+ )
+ if category is destination:
+ destination_budget_ids.append(budget.id)
+
+ assert await category_service.delete_category(
+ session, source.id, test_workspace.id, transfer_to_id=destination.id
+ )
+
+ for month, amount, recurring in expected:
+ budgets = await budget_service.get_budgets(
+ session, test_workspace.id, month=date(2025, month, 1)
+ )
+ if not different_authors:
+ assert [(b.category_id, b.amount, b.is_recurring) for b in budgets] == [
+ (destination.id, Decimal(amount), recurring)
+ ]
+ continue
+ # Two authors on the same slot keep a row each, so compare the total.
+ assert {(b.category_id, b.is_recurring) for b in budgets} == {
+ (destination.id, recurring)
+ }
+ assert sum(b.amount for b in budgets) == Decimal(amount)
+ for budget_id in destination_budget_ids:
+ assert (await _reload(session, Budget, budget_id)).user_id == destination_author.id
+
+
+ ("source_rows", "destination_rows", "expected"),
+ [
+ pytest.param(
+ [("first", 1, "100", True), ("second", 1, "200", True)], [],
+ [(1, True, {"first": "100", "second": "200"})],
+ id="both-members-on-source",
+ ),
+ pytest.param(
+ [("first", 1, "50", True)],
+ [("first", 1, "100", True), ("second", 1, "200", True)],
+ [(1, True, {"first": "150", "second": "200"})],
+ id="both-members-on-destination",
+ ),
+ pytest.param(
+ [("first", 2, "100", False), ("second", 2, "200", False)],
+ [("first", 1, "300", True)],
+ [(1, True, {"first": "300"}), (2, False, {"first": "400", "second": "200"})],
+ id="both-members-monthly-override",
+ ),
+ ],
+)
+async def test_category_transfer_keeps_each_members_budget_on_a_shared_slot(
+ session: AsyncSession,
+ test_user: User,
+ test_workspace,
+ test_categories: list[Category],
+ source_rows: list[tuple[str, int, str, bool]],
+ destination_rows: list[tuple[str, int, str, bool]],
+ expected: list[tuple[int, bool, dict[str, str]]],
+):
+ source, destination = test_categories[:2]
+ second = User(id=uuid.uuid4(), email="[email protected]", hashed_password="synthetic")
+ session.add(second)
+ await session.commit()
+ members = {"first": test_user, "second": second}
+ names = {user.id: name for name, user in members.items()}
+ workspace_id, destination_id = test_workspace.id, destination.id
+ for category, rows in ((source, source_rows), (destination, destination_rows)):
+ for member, month, amount, recurring in rows:
+ await _make_budget(
+ session, members[member], test_workspace, category.id,
+ amount=amount, month=date(2025, month, 1), is_recurring=recurring,
+ )
+
+ assert await category_service.delete_category(
+ session, source.id, workspace_id, transfer_to_id=destination_id
+ )
+
+ session.expire_all()
+ remaining = (
+ await session.execute(select(Budget).where(Budget.workspace_id == workspace_id))
+ ).scalars().all()
+ assert {b.category_id for b in remaining} == {destination_id}
+ for month, recurring, amounts in expected:
+ assert {
+ names[b.user_id]: b.amount
+ for b in remaining
+ if b.month == date(2025, month, 1) and b.is_recurring == recurring
+ } == {member: Decimal(amount) for member, amount in amounts.items()}
+ assert len(remaining) == sum(len(amounts) for _, _, amounts in expected)
+
+
+async def test_a_newer_slot_by_another_member_still_replaces_the_older_one(
+ session: AsyncSession,
+ test_user: User,
+ test_workspace,
+ test_categories: list[Category],
+):
+ """The destination read 100, then 200 once the second member's row began.
+
+ Merging must keep that: from month 3 the limit is 200 plus the source's 50,
+ not the first member's superseded 100 on top.
+ """
+ source, destination = test_categories[:2]
+ second = User(id=uuid.uuid4(), email="[email protected]", hashed_password="synthetic")
+ session.add(second)
+ await session.commit()
+ workspace_id, destination_id = test_workspace.id, destination.id
+ for user, category, month, amount in (
+ (test_user, destination, 1, "100"),
+ (second, destination, 3, "200"),
+ (test_user, source, 1, "50"),
+ ):
+ await _make_budget(
+ session, user, test_workspace, category.id,
+ amount=amount, month=date(2025, month, 1), is_recurring=True,
+ )
+
+ assert await category_service.delete_category(
+ session, source.id, workspace_id, transfer_to_id=destination_id
+ )
+
+ session.expire_all()
+ for month, limit in ((1, "150"), (2, "150"), (3, "250"), (4, "250")):
+ budgets = await budget_service.get_budgets(
+ session, workspace_id, month=date(2025, month, 1)
+ )
+ assert {b.category_id for b in budgets} == {destination_id}
+ assert sum(b.amount for b in budgets) == Decimal(limit)
@pytest.mark.asyncio