From fb215afb82d7241134bf4b41eefd4f7f46d3bc8a Mon Sep 17 00:00:00 2001 From: guva-odoo Date: Thu, 2 Nov 2023 09:30:39 +0100 Subject: [PATCH] [FIX] account, point_of_sale: check existing accounting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When creating a sub company with l10n_ar, we get a ValidationError due to the check on existing accounting entries on root company. Steps: - Create a new company with l10n_ar, and create some moves - Create a branch for the company and save -> ValidationError: "Could not change the AFIP Responsibility of this company because there are already accounting entries." In `account.res_company._existing_accounting`, we check if there are existing move lines in both parent and child companies, while we would like to check only new company when it is calling from `l10n_ar.res_company._check_accounting_info`. With this commit, we change just change the domain and use `self.id` instead of `self.root_id.id` and we call `_existing_accounting` with `root_id` where needed. opw-3572926 closes odoo/odoo#140795 X-original-commit: 575971458d454d546d967ec4141d8c47fc28e666 Signed-off-by: William André (wan) Signed-off-by: Guillaume Vanleynseele (guva) --- addons/account/models/chart_template.py | 4 ++-- addons/account/models/company.py | 4 ++-- addons/account/models/res_config_settings.py | 2 +- addons/point_of_sale/models/pos_config.py | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/addons/account/models/chart_template.py b/addons/account/models/chart_template.py index 566c16aed16..564e8328e19 100644 --- a/addons/account/models/chart_template.py +++ b/addons/account/models/chart_template.py @@ -172,7 +172,7 @@ class AccountChartTemplate(models.AbstractModel): reload_template = template_code == company.chart_template company.chart_template = template_code - if not reload_template and (not company._existing_accounting() or self.env.ref('base.module_account').demo): + if not reload_template and (not company.root_id._existing_accounting() or self.env.ref('base.module_account').demo): for model in ('account.move',) + TEMPLATE_MODELS[::-1]: if not company.parent_id: self.env[model].sudo().with_context(active_test=False).search([('company_id', 'child_of', company.id)]).with_context({MODULE_UNINSTALL_FLAG: True}).unlink() @@ -376,7 +376,7 @@ class AccountChartTemplate(models.AbstractModel): # Set the currency to the fiscal country's currency vals = {key: val for key, val in template_data.items() if filter_properties(key)} - if not company._existing_accounting(): + if not company.root_id._existing_accounting(): if company.parent_id: vals['currency_id'] = company.parent_id.currency_id.id else: diff --git a/addons/account/models/company.py b/addons/account/models/company.py index e82f838d682..a1510d22571 100644 --- a/addons/account/models/company.py +++ b/addons/account/models/company.py @@ -393,7 +393,7 @@ class ResCompany(models.Model): #forbid the change of currency_id if there are already some accounting entries existing if 'currency_id' in values and values['currency_id'] != company.currency_id.id: - if company._existing_accounting(): + if company.root_id._existing_accounting(): raise UserError(_('You cannot change the currency of the company since some journal items already exist')) return super(ResCompany, self).write(values) @@ -534,7 +534,7 @@ class ResCompany(models.Model): def _existing_accounting(self) -> bool: """Return True iff some accounting entries have already been made for the current company.""" self.ensure_one() - return bool(self.env['account.move.line'].search([('company_id', 'child_of', self.root_id.id)], limit=1)) + return bool(self.env['account.move.line'].search([('company_id', 'child_of', self.id)], limit=1)) def _chart_template_selection(self): return self.env['account.chart.template']._select_chart_template(self.country_id) diff --git a/addons/account/models/res_config_settings.py b/addons/account/models/res_config_settings.py index 01f7d3f8dc7..139a8b995ec 100644 --- a/addons/account/models/res_config_settings.py +++ b/addons/account/models/res_config_settings.py @@ -251,7 +251,7 @@ class ResConfigSettings(models.TransientModel): @api.depends('company_id') def _compute_has_chart_of_accounts(self): self.has_chart_of_accounts = bool(self.company_id.chart_template) - self.has_accounting_entries = self.company_id._existing_accounting() + self.has_accounting_entries = self.company_id.root_id._existing_accounting() @api.onchange('group_analytic_accounting') def onchange_analytic_accounting(self): diff --git a/addons/point_of_sale/models/pos_config.py b/addons/point_of_sale/models/pos_config.py index 062d3823fc4..9f878edd7b0 100644 --- a/addons/point_of_sale/models/pos_config.py +++ b/addons/point_of_sale/models/pos_config.py @@ -192,7 +192,7 @@ class PosConfig(models.Model): @api.depends('company_id') def _compute_company_has_template(self): for config in self: - config.company_has_template = config.company_id.sudo()._existing_accounting() or config.company_id.chart_template + config.company_has_template = config.company_id.root_id.sudo()._existing_accounting() or config.company_id.chart_template def _compute_is_installed_account_accountant(self): account_accountant = self.env['ir.module.module'].sudo().search([('name', '=', 'account_accountant'), ('state', '=', 'installed')])