From 4532677e4941dd4ad6f5e49bb600041f42384e8a Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Fri, 13 Nov 2026 18:50:17 +0000 Subject: [PATCH 1/2] 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. --- app/apps/transactions/models.py | 79 +++++--- app/apps/transactions/tests/test_transfers.py | 176 ++++++++++++++++-- 2 files changed, 217 insertions(+), 38 deletions(-) diff --git a/app/apps/transactions/models.py b/app/apps/transactions/models.py index da76eaf9..bc99a82c 100644 --- a/app/apps/transactions/models.py +++ b/app/apps/transactions/models.py @@ -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): """ diff --git a/app/apps/transactions/tests/test_transfers.py b/app/apps/transactions/tests/test_transfers.py index adf31b52..9f400967 100644 --- a/app/apps/transactions/tests/test_transfers.py +++ b/app/apps/transactions/tests/test_transfers.py @@ -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( { From ed513a21716819c03f67756119a9f2275370588d Mon Sep 17 00:00:00 2001 From: Herculino Trotta Date: Sat, 14 Nov 2026 11:02:38 -0300 Subject: [PATCH 2/2] fix(transactions): unlink outside transfers broken by a recurrence edit An unpaid occurrence linked to a transaction outside its recurrence (e.g. through a DCA entry) could end up as two expenses, or both sides in one account, after the recurrence's type or account changed. The link is now dropped in that case; the partner is left untouched. --- app/apps/transactions/models.py | 17 +++++++++++ app/apps/transactions/tests/test_transfers.py | 30 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/app/apps/transactions/models.py b/app/apps/transactions/models.py index bc99a82c..d5eac13a 100644 --- a/app/apps/transactions/models.py +++ b/app/apps/transactions/models.py @@ -1399,6 +1399,23 @@ class RecurringTransaction(TransferTemplate): to_side.linked_transaction = None to_side.delete(_with_partner=False) + partner = existing_transaction.linked_transaction + if ( + partner + and partner.recurring_transaction_id != self.pk + and ( + {self.transaction_type, partner.type} + != {Transaction.Type.INCOME, Transaction.Type.EXPENSE} + or self.account_id == partner.account_id + ) + ): + # A link made elsewhere (e.g. a DCA entry) that the new type or account + # would break: unlink it and keep the partner + Transaction.all_objects.filter(pk=partner.pk).update( + linked_transaction=None + ) + existing_transaction.linked_transaction = None + self._apply_to( existing_transaction, self.account, diff --git a/app/apps/transactions/tests/test_transfers.py b/app/apps/transactions/tests/test_transfers.py index 9f400967..3cd72112 100644 --- a/app/apps/transactions/tests/test_transfers.py +++ b/app/apps/transactions/tests/test_transfers.py @@ -418,6 +418,36 @@ class TransferTemplateTests(TransferTestBase): self.assertEqual(income.amount, Decimal("120")) self.assertTrue(Transaction.objects.filter(pk=expense.pk).exists()) + def test_recurring_update_unlinks_transfer_linked_by_hand_when_broken(self): + for change in ({"type": RecurringTransaction.Type.EXPENSE}, {"account": self.savings}): + with self.subTest(change=change): + recurring = self._recurring(type=RecurringTransaction.Type.INCOME) + recurring.create_upcoming_transactions() + income = recurring.transactions.earliest("date") + outside = Transaction.objects.create( + account=self.savings, + type=Transaction.Type.EXPENSE, + amount=Decimal("100"), + date=income.date, + description="Outside", + is_paid=False, + ) + outside.link_transfer(income) + + for field, value in change.items(): + setattr(recurring, field, value) + recurring.save() + recurring.update_unpaid_transactions() + + income.refresh_from_db() + outside.refresh_from_db() + self.assertIsNone(income.linked_transaction) + self.assertIsNone(outside.linked_transaction) + self.assertEqual( + (outside.account, outside.type, outside.amount), + (self.savings, Transaction.Type.EXPENSE, Decimal("100")), + ) + def test_edit_view_moves_unpaid_transactions(self): recurring = self._recurring(type=RecurringTransaction.Type.EXPENSE) recurring.create_upcoming_transactions()