From 76afde138f097de0ec51777100ef6dbc9422ed30 Mon Sep 17 00:00:00 2001 From: Jorge Pinna Puissant Date: Thu, 25 Mar 2021 11:28:42 +0000 Subject: [PATCH] [FIX]account: error when rest bill to draft - On accounting settings, activate 'Cash Basis'; - Select a 'Base Tax Received Account'; - Modify a purchase taxes, and set the 'Tax Due' to 'Based on Payment'; - Set a 'Cash Basis Transition Account'; - Be sure that the previously selected account allows reconciliation; - Create a Vendors Bills, using the modified purchase tax; - Register a payment; - Reset to draft the vendors bill. Before this commit, an error was raised : 'Record does not exist or has been deleted.' This occurs because when trying to unlink the partial reconcile, it will first reverse and unlink the tax cash basis partial reconcile moves. After when trying to access the partial reconcile to unlink the error is raised because one is already unlink. Now, the vendors bill is correctly reset to draft. opw-2477526 closes odoo/odoo#68754 X-original-commit: 72c29b105bdb4da5b306cd3b48a1683c1bf3e620 Signed-off-by: Jorge Pinna Puissant (jpp) Co-authored-by: Laurent Smet --- .../models/account_partial_reconcile.py | 15 ++++++++--- .../tests/test_account_move_reconcile.py | 26 +++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) 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()