[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) <jpp@odoo.com>
Co-authored-by: Laurent Smet <las@odoo.com>
This commit is contained in:
Jorge Pinna Puissant
2021-04-06 08:36:08 +00:00
co-authored by Laurent Smet
parent 4044e46861
commit 76afde138f
2 changed files with 37 additions and 4 deletions
@@ -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
# -------------------------------------------------------------------------
@@ -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()