From 036c4a4886f58b5df4e398d4965151bd897b7229 Mon Sep 17 00:00:00 2001 From: Victor Feyens Date: Mon, 12 Dec 2022 11:25:57 +0000 Subject: [PATCH] [FIX] sale_management: do not recompute quote template on company change closes odoo/odoo#108380 X-original-commit: f143893af2d1bd2c167c1aa2e23e0bedc1a46c7a Signed-off-by: Victor Feyens (vfe) --- addons/sale_management/__init__.py | 6 ++++++ addons/sale_management/__manifest__.py | 7 ++++--- addons/sale_management/models/sale_order.py | 17 +++++++++-------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/addons/sale_management/__init__.py b/addons/sale_management/__init__.py index 69eb6a58901..3212725f414 100644 --- a/addons/sale_management/__init__.py +++ b/addons/sale_management/__init__.py @@ -4,8 +4,14 @@ from . import models from . import controllers from odoo.api import Environment, SUPERUSER_ID +from odoo.tools import column_exists, create_column +def pre_init_hook(cr): + """Do not compute the sale_order_template_id field on existing SOs.""" + if not column_exists(cr, "sale_order", "sale_order_template_id"): + create_column(cr, "sale_order", "sale_order_template_id", "int4") + def uninstall_hook(cr, registry): env = Environment(cr, SUPERUSER_ID, {}) res_ids = env['ir.model.data'].search([ diff --git a/addons/sale_management/__manifest__.py b/addons/sale_management/__manifest__.py index d80364c194f..fd167088021 100644 --- a/addons/sale_management/__manifest__.py +++ b/addons/sale_management/__manifest__.py @@ -58,13 +58,14 @@ The Dashboard for the Sales Manager will include 'demo': [ 'data/sale_order_template_demo.xml', ], - 'application': True, - 'uninstall_hook': 'uninstall_hook', - 'post_init_hook': 'post_init_hook', 'assets': { 'web.assets_frontend': [ 'sale_management/static/src/js/**/*', ], }, + 'application': True, + 'pre_init_hook': 'pre_init_hook', + 'post_init_hook': 'post_init_hook', + 'uninstall_hook': 'uninstall_hook', 'license': 'LGPL-3', } diff --git a/addons/sale_management/models/sale_order.py b/addons/sale_management/models/sale_order.py index 2ddf453d69e..40dbe9d78f8 100644 --- a/addons/sale_management/models/sale_order.py +++ b/addons/sale_management/models/sale_order.py @@ -28,16 +28,10 @@ class SaleOrder(models.Model): #=== COMPUTE METHODS ===# - @api.depends('company_id') + # Do not make it depend on `company_id` field + # It is triggered manually by the _onchange_company_id below iff the SO has not been saved. def _compute_sale_order_template_id(self): for order in self: - if order._origin.id: # If record has already been saved - # 1) Do NOT update existing SO's template and dependent fields - # Especially when installing sale_management in a db - # already containing SO records - # 2) Only apply the company default if the company is modified before the record is saved - # to make sure the lines are not magically reset when the company is modified (internal odoo issue) - continue company_template = order.company_id.sale_order_template_id if company_template and order.sale_order_template_id != company_template: order.sale_order_template_id = order.company_id.sale_order_template_id.id @@ -86,6 +80,13 @@ class SaleOrder(models.Model): #=== ONCHANGE METHODS ===# + @api.onchange('company_id') + def _onchange_company_id(self): + """Trigger quotation template recomputation on unsaved records company change""" + if self._origin.id: + return + self._compute_sale_order_template_id() + @api.onchange('sale_order_template_id') def _onchange_sale_order_template_id(self): sale_order_template = self.sale_order_template_id.with_context(lang=self.partner_id.lang)