From 57b713cb7e5dc03d20be43c26e7a5a7d6e99cfa8 Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Fri, 3 Nov 2023 13:59:34 +0100 Subject: [PATCH] [FIX] account: ensure fiscal position belongs to the company of the invoice MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When bank statements are imported using Enterprise's account_online_synchronization, or when invoices are created from incoming email using mail aliases on `account.journal`, journal entries can end up with a fiscal position from the default company of the user that the `account.move` creation runs as, or the import fails with an error "'Draft Entry' belongs to company X and 'Fiscal Position' belongs to another company". While account.move's `_compute_fiscal_company_id` calls `account.fiscal.position`'s `_get_fiscal_position` with actual company of the move, it passes one or two `res.partner` records to this method that were still browsed with the default company of the executing user which could result in a fiscal position from that company, instead of the company of the journal entry. closes odoo/odoo#141059 X-original-commit: 71bd4f1378f56b333830cbc5aa11c1c55dcefcc3 Signed-off-by: William André (wan) Co-authored-by: William Andre --- addons/account/models/partner.py | 5 ++++- .../account/tests/test_account_move_entry.py | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/addons/account/models/partner.py b/addons/account/models/partner.py index b91ad868005..0de1b7287bf 100644 --- a/addons/account/models/partner.py +++ b/addons/account/models/partner.py @@ -237,7 +237,10 @@ class AccountFiscalPosition(models.Model): delivery = partner # partner manually set fiscal position always win - manual_fiscal_position = delivery.property_account_position_id or partner.property_account_position_id + manual_fiscal_position = ( + delivery.with_company(company).property_account_position_id + or partner.with_company(company).property_account_position_id + ) if manual_fiscal_position: return manual_fiscal_position diff --git a/addons/account/tests/test_account_move_entry.py b/addons/account/tests/test_account_move_entry.py index bb280527c52..ef4bcb17bbf 100644 --- a/addons/account/tests/test_account_move_entry.py +++ b/addons/account/tests/test_account_move_entry.py @@ -134,6 +134,28 @@ class TestAccountMove(AccountTestInvoicingCommon): self.test_move.line_ids[0].account_id = custom_account + def test_fiscal_position_multicompany(self): + """A move is assigned a fiscal position that matches its own company.""" + company1 = self.company_data["company"] + company2 = self.company_data_2["company"] + partner = self.env['res.partner'].create({'name': 'Belouga'}) + fpos1 = self.env["account.fiscal.position"].create( + { + "name": company1.name, + "company_id": company1.id, + } + ) + fpos2 = self.env["account.fiscal.position"].create( + { + "name": company2.name, + "company_id": company2.id, + } + ) + partner.with_company(company1).property_account_position_id = fpos1 + partner.with_company(company2).property_account_position_id = fpos2 + self.test_move.sudo().with_company(company2).partner_id = partner + self.assertEqual(self.test_move.fiscal_position_id, fpos1) + def test_misc_fiscalyear_lock_date_1(self): self.test_move.action_post()