From 399de9b5d1e5eefebcb70a62b05226cb3cde2771 Mon Sep 17 00:00:00 2001 From: Laurent Smet Date: Tue, 21 Nov 2023 16:22:30 +0000 Subject: [PATCH] [FIX] account: Fix tax & tax group country_id recomputation When changing the fiscal country of the company, the country of all taxes and tax groups belonging to this company are recomputed. However, moving taxes from one country to another doesn't make any sense since they are part of a chart template specific to a single country. This becomes more annoying when there are custom fields that depends of the current country in some localization. In this case, changing the country of the company will also erased some data coming from the chart template. At this point, the user will be forced to reload the whole chart template to recover those data. closes odoo/odoo#145423 X-original-commit: 58393169fb51db387562baaa67e9c9ca52ed066b Related: odoo/enterprise#52331 Signed-off-by: Olivier Colson (oco) Signed-off-by: Laurent Smet (las) --- addons/account/models/account_tax.py | 4 ++-- addons/account/tests/common.py | 10 ++++++++++ addons/sale/tests/test_sale_prices.py | 8 +++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/addons/account/models/account_tax.py b/addons/account/models/account_tax.py index 41815fe5c4c..f2596d064de 100644 --- a/addons/account/models/account_tax.py +++ b/addons/account/models/account_tax.py @@ -59,7 +59,7 @@ class AccountTaxGroup(models.Model): "If not set, the tax group will be displayed after the 'Untaxed amount' subtotal.", ) - @api.depends('company_id.account_fiscal_country_id') + @api.depends('company_id') def _compute_country_id(self): for group in self: group.country_id = group.company_id.account_fiscal_country_id or group.company_id.country_id @@ -219,7 +219,7 @@ class AccountTax(models.Model): if tax.is_used: raise ValidationError(_("This tax has been used in transactions. For that reason, it is forbidden to modify this field.")) - @api.depends('company_id.account_fiscal_country_id') + @api.depends('company_id') def _compute_country_id(self): for tax in self: tax.country_id = tax.company_id.account_fiscal_country_id or tax.company_id.country_id or tax.country_id diff --git a/addons/account/tests/common.py b/addons/account/tests/common.py index f6ae9050dc9..b11780425e7 100644 --- a/addons/account/tests/common.py +++ b/addons/account/tests/common.py @@ -178,6 +178,16 @@ class AccountTestInvoicingCommon(TransactionCase): cls.inbound_payment_method_line = bank_journal.inbound_payment_method_line_ids[0] cls.outbound_payment_method_line = bank_journal.outbound_payment_method_line_ids[0] + @classmethod + def change_company_country(cls, company, country): + company.country_id = country + company.account_fiscal_country_id = country + for model in ('account.tax', 'account.tax.group'): + cls.env.add_to_compute( + cls.env[model]._fields['country_id'], + cls.env[model].search([('company_id', '=', company.id)]), + ) + @classmethod def setup_company_data(cls, company_name, chart_template=None, **kwargs): ''' Create a new company having the name passed as parameter. diff --git a/addons/sale/tests/test_sale_prices.py b/addons/sale/tests/test_sale_prices.py index 68fa2a6668a..b8f3a87ca1d 100644 --- a/addons/sale/tests/test_sale_prices.py +++ b/addons/sale/tests/test_sale_prices.py @@ -23,7 +23,13 @@ class TestSalePrices(SaleCommon): # Needed when run without demo data # s.t. taxes creation doesn't fail - cls.env.company.account_fiscal_country_id = cls.env.ref('base.be') + belgium = cls.env.ref('base.be') + cls.env.company.account_fiscal_country_id = belgium + for model in ('account.tax', 'account.tax.group'): + cls.env.add_to_compute( + cls.env[model]._fields['country_id'], + cls.env[model].search([('company_id', '=', cls.env.company.id)]), + ) def _create_discount_pricelist_rule(self, **additional_values): return self.env['product.pricelist.item'].create({