diff --git a/addons/account/models/account_partial_reconcile.py b/addons/account/models/account_partial_reconcile.py index 7707c0dcafa..5ed93aec365 100644 --- a/addons/account/models/account_partial_reconcile.py +++ b/addons/account/models/account_partial_reconcile.py @@ -107,8 +107,16 @@ class AccountPartialReconcile(models.Model): if not self: return True - # Reverse all exchange moves at once. + # Retrieve the matching number to unlink. + full_to_unlink = self.full_reconcile_id + + # Retrieve the CABA entries to reverse. moves_to_reverse = self.env['account.move'].search([('tax_cash_basis_rec_id', 'in', self.ids)]) + + # Unlink partials before doing anything else to avoid 'Record has already been deleted' due to the recursion. + res = super().unlink() + + # Reverse CABA entries. today = fields.Date.context_today(self) default_values_list = [{ 'date': move.date if move.date > (move.company_id.period_lock_date or date.min) else today, @@ -116,10 +124,9 @@ class AccountPartialReconcile(models.Model): } for move in moves_to_reverse] moves_to_reverse._reverse_moves(default_values_list, cancel=True) - # Unlink partials then the full in this order to avoid a recursive call to the same partials. - full_to_unlink = self.full_reconcile_id - res = super().unlink() + # Remove the matching numbers. full_to_unlink.unlink() + return res # ------------------------------------------------------------------------- diff --git a/addons/account/tests/test_account_move_reconcile.py b/addons/account/tests/test_account_move_reconcile.py index d69b61f6116..22d8940fb3b 100644 --- a/addons/account/tests/test_account_move_reconcile.py +++ b/addons/account/tests/test_account_move_reconcile.py @@ -2125,3 +2125,29 @@ class TestAccountMoveReconcile(AccountTestInvoicingCommon): # Check full reconciliation self.assertTrue(all(line.full_reconcile_id for line in lines_to_reconcile), "All tax lines should be fully reconciled") + + def test_caba_undo_reconciliation(self): + ''' Make sure there is no traceback like "Record has already been deleted" during the deletion of partials. ''' + self.cash_basis_transfer_account.reconcile = True + + bill = self.env['account.move'].create({ + 'move_type': 'in_invoice', + 'partner_id': self.partner_a.id, + 'invoice_date': '2019-01-01', + 'date': '2019-01-01', + 'invoice_line_ids': [(0, 0, { + 'name': 'line', + 'account_id': self.company_data['default_account_expense'].id, + 'price_unit': 1000.0, + 'tax_ids': [(6, 0, self.cash_basis_tax_a_third_amount.ids)], + })], + }) + bill.action_post() + + # Register a payment creating the CABA journal entry on the fly and reconcile it with the tax line. + self.env['account.payment.register']\ + .with_context(active_ids=bill.ids, active_model='account.move')\ + .create({})\ + ._create_payments() + + bill.button_draft()