From 71229be03edc1767dfa0ba2ca96ea80786aee226 Mon Sep 17 00:00:00 2001
From: "Nicolas (vin)" <vin@odoo.com>
Date: Fri, 2 Dec 2022 08:06:34 +0000
Subject: [PATCH] [FIX] account: Disallow to post move using deprecated
 account.

A move in draft created with an account that is later deprecated can
still be posted, while it shouldn't be allowed.
Thus, we add a new error blocking this wrong behaviour.

Task id #3087763

closes odoo/odoo#107130

X-original-commit: c251974799844fc80cfb62b3dada17c85b44f077
Signed-off-by: Florian Gilbert (flg) <flg@odoo.com>
Signed-off-by: Nicolas Viseur (vin) <vin@odoo.com>
---
 addons/account/i18n/account.pot               |  7 +++++++
 addons/account/models/account_move.py         |  3 +++
 .../tests/test_account_move_out_invoice.py    | 19 +++++++++++++++++++
 3 files changed, 29 insertions(+)

diff --git a/addons/account/i18n/account.pot b/addons/account/i18n/account.pot
index 4023f1757fbf..b434ae11906e 100644
--- a/addons/account/i18n/account.pot
+++ b/addons/account/i18n/account.pot
@@ -1043,6 +1043,13 @@ msgid ""
 "                related to the day-to-day business."
 msgstr ""
 
+#. module: account
+#: code:addons/account/models/account_move.py:0
+#, python-format
+msgid ""
+"A line of this move is using a deprecated account, you cannot post it."
+msgstr ""
+
 #. module: account
 #: code:addons/account/models/account_payment.py:0
 #, python-format
diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py
index d983e84df821..c40a09dccb75 100644
--- a/addons/account/models/account_move.py
+++ b/addons/account/models/account_move.py
@@ -3048,6 +3048,9 @@ class AccountMove(models.Model):
                 raise UserError(_("You cannot validate an invoice with an inactive currency: %s",
                                   move.currency_id.name))
 
+            if move.line_ids.account_id.filtered(lambda account: account.deprecated):
+                raise UserError(_("A line of this move is using a deprecated account, you cannot post it."))
+
             # Handle case when the invoice_date is not set. In that case, the invoice_date is set at today and then,
             # lines are recomputed accordingly.
             # /!\ 'check_move_validity' must be there since the dynamic lines will be recomputed outside the 'onchange'
diff --git a/addons/account/tests/test_account_move_out_invoice.py b/addons/account/tests/test_account_move_out_invoice.py
index 7875daaa076a..0f61961cccf7 100644
--- a/addons/account/tests/test_account_move_out_invoice.py
+++ b/addons/account/tests/test_account_move_out_invoice.py
@@ -3450,3 +3450,22 @@ class TestAccountMoveOutInvoiceOnchanges(AccountTestInvoicingCommon):
                 },
             ],
         )
+
+    def test_out_invoice_depreciated_account(self):
+        move = self.env['account.move'].create({
+            'move_type': 'out_invoice',
+            'currency_id': self.currency_data['currency'].id,
+            'partner_id': self.partner_a.id,
+            'journal_id': self.company_data['default_journal_sale'].id,
+            'invoice_line_ids': [
+                (0, 0, {
+                    'name': 'My super product.',
+                    'quantity': 1.0,
+                    'price_unit': 750.0,
+                    'account_id': self.product_a.property_account_income_id.id,
+                })
+            ],
+        })
+        self.product_a.property_account_income_id.deprecated = True
+        with self.assertRaises(UserError), self.cr.savepoint():
+            move.action_post()
-- 
GitLab