fix(transactions): move unpaid recurring transactions when the account or type changes

Editing a recurring transaction only refreshed the amount, description,
category, notes, tags and entities of its unpaid transactions. Changing its
account or type left the already generated transactions behind: a monthly
expense moved to another account kept its upcoming months in the old one,
and an expense turned into an income kept pulling projected balances down.
New occurrences followed the new settings, so the recurrence ended up split
across two accounts.

Unpaid transactions now take the recurrence's account and type as well,
keeping their dates, the same way installment plans already update theirs.
For recurring transfers:

- the "to" sides follow the "to" account and amount;
- turning a recurrence into a transfer adds the missing "to" sides;
- turning a transfer into an income or expense removes the unpaid "to"
  sides and leaves the paid transfers linked.

A "to" side is now recognised by its partner belonging to the same
recurrence, so an occurrence the user linked by hand to another
transaction is updated like any other occurrence instead of being treated
as a generated "to" side.
This commit is contained in:
Asish Kumar committed 2026-11-14 17:00:17 +05:30
1 parent 93fa7b272e
commit 4532677e49
2 files changed
+217 -38

No files matched your search

+53 -26
View File
@@ -1359,38 +1359,65 @@ class RecurringTransaction(TransferTemplate):
"""
Updates all unpaid transactions associated with this RecurringTransaction.
Only unpaid transactions (`is_paid=False`) are modified. Updates fields like
amount, description, category, notes, and many-to-many relationships (tags, entities).
Only unpaid transactions (`is_paid=False`) are modified. They keep their dates
and take the account, type, amount, description, category, notes, tags and
entities of the recurrence; a transfer's "to" side takes the "to" account and
amount. Turning the recurrence into a transfer adds the missing "to" sides,
and turning it into an income or expense removes them.
"""
unpaid_transactions = self.transactions.filter(is_paid=False)
unpaid_transactions = list(
self.transactions.filter(is_paid=False).select_related(
"linked_transaction"
)
)
to_side_ids = {
existing_transaction.pk
for existing_transaction in unpaid_transactions
if self._is_to_side(existing_transaction)
}
for existing_transaction in unpaid_transactions:
# Update fields based on RecurringTransaction
is_to_side = (
existing_transaction.linked_transaction_id
and existing_transaction.type == Transaction.Type.INCOME
)
existing_transaction.amount = (
self.to_amount or self.amount if is_to_side else self.amount
)
existing_transaction.description = (
self.description if self.add_description_to_transaction else ""
)
existing_transaction.category = self.category
existing_transaction.notes = (
self.notes if self.add_notes_to_transaction else ""
)
if existing_transaction.pk in to_side_ids:
if self.is_transfer:
self._apply_to(
existing_transaction,
self.to_account,
Transaction.Type.INCOME,
self.to_amount or self.amount,
existing_transaction.date,
existing_transaction.reference_date,
)
# Otherwise its unpaid "from" side removes it below
continue
# Update many-to-many relationships (see create_transaction)
existing_transaction.tags.set(self.tags(manager="all_objects").all())
existing_transaction.entities.set(
self.entities(manager="all_objects").all()
if (
not self.is_transfer
and existing_transaction.linked_transaction_id in to_side_ids
):
to_side = existing_transaction.linked_transaction
existing_transaction.linked_transaction = None
to_side.linked_transaction = None
to_side.delete(_with_partner=False)
self._apply_to(
existing_transaction,
self.account,
self.transaction_type,
self.amount,
existing_transaction.date,
existing_transaction.reference_date,
)
if not existing_transaction.linked_transaction_id:
self.complete_transfer(existing_transaction)
existing_transaction.copy_sub_transactions_from(self)
# Save updated transaction
existing_transaction.save()
def _is_to_side(self, existing_transaction):
"""Whether a transaction is the "to" side of a transfer this generated."""
return bool(
existing_transaction.linked_transaction_id
and existing_transaction.type == Transaction.Type.INCOME
and existing_transaction.linked_transaction.recurring_transaction_id
== self.pk
)
def delete_unpaid_transactions(self):
"""
+164 -12
View File
@@ -251,19 +251,20 @@ class TransferTemplateTests(TransferTestBase):
"""Recurring and quick transactions of type Transfer."""
def _recurring(self, **overrides):
return RecurringTransaction.objects.create(
account=self.checking,
type=RecurringTransaction.Type.TRANSFER,
to_account=self.savings,
amount=Decimal("100"),
to_amount=Decimal("95"),
description="Savings",
start_date=timezone.localdate(),
recurrence_type=RecurringTransaction.RecurrenceType.MONTH,
recurrence_interval=1,
keep_at_most=2,
data = {
"account": self.checking,
"type": RecurringTransaction.Type.TRANSFER,
"to_account": self.savings,
"amount": Decimal("100"),
"to_amount": Decimal("95"),
"description": "Savings",
"start_date": timezone.localdate(),
"recurrence_type": RecurringTransaction.RecurrenceType.MONTH,
"recurrence_interval": 1,
"keep_at_most": 2,
**overrides,
)
}
return RecurringTransaction.objects.create(**data)
def test_recurring_transfer_generates_linked_pairs(self):
recurring = self._recurring()
@@ -294,6 +295,157 @@ class TransferTemplateTests(TransferTestBase):
{(Transaction.Type.EXPENSE, Decimal("10")), (Transaction.Type.INCOME, Decimal("9"))},
)
def test_recurring_update_moves_unpaid_to_new_account_and_type(self):
recurring = self._recurring(type=RecurringTransaction.Type.EXPENSE)
recurring.create_upcoming_transactions()
paid = recurring.transactions.earliest("date")
paid.is_paid = True
paid.save()
recurring.account = self.savings
recurring.type = RecurringTransaction.Type.INCOME
recurring.save()
recurring.update_unpaid_transactions()
paid.refresh_from_db()
self.assertEqual(
(paid.account, paid.type), (self.checking, Transaction.Type.EXPENSE)
)
unpaid = recurring.transactions.filter(is_paid=False)
self.assertTrue(unpaid.exists())
self.assertEqual(
set(unpaid.values_list("account", "type")),
{(self.savings.id, Transaction.Type.INCOME)},
)
def test_recurring_transfer_update_moves_to_sides_to_new_account(self):
recurring = self._recurring()
recurring.create_upcoming_transactions()
brokerage = Account.objects.create(
name="Brokerage", group=self.checking.group, currency=self.checking.currency
)
recurring.to_account = brokerage
recurring.save()
recurring.update_unpaid_transactions()
self.assertEqual(
set(recurring.transactions.values_list("type", "account")),
{
(Transaction.Type.EXPENSE, self.checking.id),
(Transaction.Type.INCOME, brokerage.id),
},
)
def test_recurring_update_into_transfer_adds_to_sides(self):
recurring = self._recurring(type=RecurringTransaction.Type.INCOME)
recurring.create_upcoming_transactions()
occurrences = recurring.transactions.count()
recurring.type = RecurringTransaction.Type.TRANSFER
recurring.save()
recurring.update_unpaid_transactions()
expenses = recurring.transactions.filter(type=Transaction.Type.EXPENSE)
self.assertEqual(expenses.count(), occurrences)
self.assertEqual(recurring.transactions.count(), occurrences * 2)
for from_t in expenses:
to_t = from_t.linked_transaction
self.assertEqual(from_t.account, self.checking)
self.assertEqual(to_t.linked_transaction, from_t)
self.assertEqual(to_t.account, self.savings)
self.assertEqual(to_t.amount, Decimal("95"))
self.assertEqual(to_t.recurring_transaction, recurring)
def _assert_update_out_of_transfer_removes_to_sides(self):
recurring = self._recurring()
recurring.create_upcoming_transactions()
paid_from = recurring.transactions.filter(
type=Transaction.Type.EXPENSE
).earliest("date")
paid_from.is_paid = True
paid_from.save()
paid_to = paid_from.linked_transaction
occurrences = recurring.transactions.filter(
type=Transaction.Type.EXPENSE
).count()
recurring.type = RecurringTransaction.Type.EXPENSE
recurring.save()
recurring.update_unpaid_transactions()
unpaid = recurring.transactions.filter(is_paid=False)
self.assertEqual(unpaid.count(), occurrences - 1)
self.assertEqual(
set(unpaid.values_list("account", "type", "linked_transaction")),
{(self.checking.id, Transaction.Type.EXPENSE, None)},
)
paid_from.refresh_from_db()
self.assertEqual(paid_from.linked_transaction, paid_to)
def test_recurring_update_out_of_transfer_removes_to_sides(self):
self._assert_update_out_of_transfer_removes_to_sides()
self.assertFalse(Transaction.deleted_objects.exists())
@override_settings(ENABLE_SOFT_DELETE=True)
def test_recurring_update_out_of_transfer_soft_deletes_to_sides(self):
self._assert_update_out_of_transfer_removes_to_sides()
self.assertTrue(Transaction.deleted_objects.exists())
self.assertFalse(
Transaction.deleted_objects.exclude(linked_transaction=None).exists()
)
def test_recurring_update_keeps_transfer_linked_by_hand(self):
recurring = self._recurring(type=RecurringTransaction.Type.INCOME)
recurring.create_upcoming_transactions()
income = recurring.transactions.earliest("date")
expense = Transaction.objects.create(
account=self.savings,
type=Transaction.Type.EXPENSE,
amount=Decimal("100"),
date=income.date,
description="Outside",
is_paid=False,
)
expense.link_transfer(income)
recurring.amount = Decimal("120")
recurring.save()
recurring.update_unpaid_transactions()
income.refresh_from_db()
self.assertEqual(income.linked_transaction, expense)
self.assertEqual(income.amount, Decimal("120"))
self.assertTrue(Transaction.objects.filter(pk=expense.pk).exists())
def test_edit_view_moves_unpaid_transactions(self):
recurring = self._recurring(type=RecurringTransaction.Type.EXPENSE)
recurring.create_upcoming_transactions()
response = self.client.post(
reverse(
"recurring_transaction_edit",
kwargs={"recurring_transaction_id": recurring.id},
),
{
"type": "EX",
"account": self.savings.id,
"amount": "100",
"description": "Savings",
"start_date": recurring.start_date.isoformat(),
"recurrence_type": "month",
"recurrence_interval": "1",
"keep_at_most": "2",
},
HTTP_HX_REQUEST="true",
)
self.assertEqual(response.status_code, 204)
self.assertEqual(
set(recurring.transactions.values_list("account", flat=True)),
{self.savings.id},
)
def test_transfer_type_requires_other_account(self):
form = RecurringTransactionForm(
{