From f7b36faf543ea997aa5e6d9663846ead9320d428 Mon Sep 17 00:00:00 2001 From: "Tiffany Chang (tic)" Date: Tue, 22 Aug 2023 10:08:51 +0200 Subject: [PATCH] [FIX] stock_account: retain assigned automated accounts Steps to reproduce: 1- install stock_account and account_accountant 2- activate in Settings > Accounting > Automatic Accounting 3- open a Product Category > set Inventory Valuation = Automated [`real_time`] (optional step: can change the account stock properties values, but doesn't matter) 4- install mrp_account Expected result: - Accounts already set under the Account Stock Properties (i.e.: property_stock_valuation_account_id, property_stock_account_input_categ_id, etc) remain unchanged. Actual result: - Account stock properties for any Product Categories already set to Automated before installing mrp_account are wiped to nothing Issue: `_post_load_data` was resetting all property values (i.e. the accounts) to False since PR odoo/odoo#119564 to ensure correct accounts were used for `manual_periodic` valuation when stock_account and mrp_account are first installed. This was previously not an issue because no valuations could be set to `real_time` before stock_account was installed and mrp_account did not run `post_load_data` (i.e. an mrp account was added) until saas-16.3. So now we avoid wiping existing assigned accounts when mrp_account calls `_post_load_data` at its install time closes odoo/odoo#147958 Task: 3471065 X-original-commit: cc741525f9349077aefc7d137b6199a68fc87a23 Signed-off-by: Arnold Moyaux (arm) --- .../stock_account/models/account_chart_template.py | 13 ++++++++++--- addons/stock_account/models/product.py | 4 ++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/addons/stock_account/models/account_chart_template.py b/addons/stock_account/models/account_chart_template.py index e12931a7e72..0bea15728d4 100644 --- a/addons/stock_account/models/account_chart_template.py +++ b/addons/stock_account/models/account_chart_template.py @@ -11,9 +11,16 @@ class AccountChartTemplate(models.AbstractModel): def _post_load_data(self, template_code, company, template_data): super()._post_load_data(template_code, company, template_data) company = company or self.env.company - categ_values = {category.id: False for category in self.env['product.category'].search([])} - for fname in self.env['product.category']._get_stock_account_property_field_names(): - self.env['ir.property'].with_company(company.id)._set_multi(fname, 'product.category', categ_values, True) + fields_name = self.env['product.category']._get_stock_account_property_field_names() + account_fields = self.env['ir.model.fields'].search([('model', '=', 'product.category'), ('name', 'in', fields_name)]) + existing_props = self.env['ir.property'].sudo().search([ + ('fields_id', 'in', account_fields.ids), + ('company_id', '=', company.id), + ('res_id', '!=', False), + ]) + for fname in fields_name: + if fname in existing_props.mapped('fields_id.name'): + continue value = template_data.get(fname) if value: self.env['ir.property']._set_default(fname, 'product.category', self.ref(value).id, company=company) diff --git a/addons/stock_account/models/product.py b/addons/stock_account/models/product.py index eb8f75a8e5b..d64f2094270 100644 --- a/addons/stock_account/models/product.py +++ b/addons/stock_account/models/product.py @@ -856,7 +856,7 @@ class ProductCategory(models.Model): account_moves._post() return res + # delete in master @api.onchange('property_valuation') def onchange_property_valuation(self): - # Remove or set the account stock properties if necessary - self._check_valuation_accounts() + pass