[IMP] account_advanced_protection_features: correct function button_draft when called on multiple moves #27

Merged
mondot merged 1 commits from bug-account-move-button-draft-for-many-moves into 16.0 2026-01-07 16:41:50 +00:00
3 changed files with 59 additions and 5 deletions
Showing only changes of commit a948a466cc - Show all commits

View File

@@ -5,11 +5,12 @@ class AccountMove(models.Model):
_inherit = 'account.move' _inherit = 'account.move'
def button_draft(self): def button_draft(self):
if self.is_move_sent and self.journal_id.prevent_reset_to_draft_sent_invoice: for move in self:
if move.is_move_sent and move.journal_id.prevent_reset_to_draft_sent_invoice:
raise UserError(_( raise UserError(_(
"You cannot reset to draft this invoice because it has been sent by email." "You cannot reset to draft this invoice because it has been sent by email."
)) ))
return super(AccountMove, self).button_draft() return super().button_draft()
@api.ondelete(at_uninstall=False) @api.ondelete(at_uninstall=False)
def _check_posted(self): def _check_posted(self):

View File

@@ -0,0 +1 @@
from . import test_account_move

View File

@@ -0,0 +1,52 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo.tests.common import TransactionCase
class TestAccountMoveButtonDraft(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.partner = cls.env["res.partner"].create({"name": "Test Partner"})
cls.journal = cls.env["account.journal"].search(
[("type", "=", "sale")], limit=1
)
cls.account = cls.env["account.account"].search(
[("account_type", "=", "income")], limit=1
)
def _create_invoice(self):
return self.env["account.move"].create({
"move_type": "out_invoice",
"partner_id": self.partner.id,
"journal_id": self.journal.id,
"invoice_line_ids": [
(0, 0, {
"name": "Test line",
"quantity": 1,
"price_unit": 100.0,
"account_id": self.account.id,
}),
],
})
def test_button_draft_multiple_moves(self):
"""Test that button_draft works on multiple account.move records.
"""
# Create two invoices
invoice1 = self._create_invoice()
invoice2 = self._create_invoice()
# Post both invoices
invoice1.action_post()
invoice2.action_post()
# Combine them into a recordset
invoices = invoice1 | invoice2
# This should not raise "Expected singleton" error
invoices.button_draft()
# Verify both are back to draft
self.assertEqual(invoice1.state, "draft")
self.assertEqual(invoice2.state, "draft")