From 39a1fe5064512c275c841fbcb1cb5de970ed1b24 Mon Sep 17 00:00:00 2001 From: remihb Date: Tue, 18 Apr 2023 15:29:06 +0200 Subject: [PATCH] [MIG] sale_order_secondary_unit: Migration to 16.0 --- sale_order_secondary_unit/__manifest__.py | 2 +- sale_order_secondary_unit/models/__init__.py | 1 + .../models/product_product.py | 17 ++++++ .../models/product_template.py | 60 ++++++++++++++++++- .../models/sale_order.py | 24 ++------ .../tests/test_sale_order_secondary_unit.py | 5 ++ .../views/product_views.xml | 7 +-- .../views/sale_order_views.xml | 56 ++++++++++++----- .../odoo/addons/sale_order_secondary_unit | 1 + setup/sale_order_secondary_unit/setup.py | 6 ++ 10 files changed, 136 insertions(+), 43 deletions(-) create mode 100644 sale_order_secondary_unit/models/product_product.py create mode 120000 setup/sale_order_secondary_unit/odoo/addons/sale_order_secondary_unit create mode 100644 setup/sale_order_secondary_unit/setup.py diff --git a/sale_order_secondary_unit/__manifest__.py b/sale_order_secondary_unit/__manifest__.py index 5ad25f8ab6a1..9b31ce1dca69 100644 --- a/sale_order_secondary_unit/__manifest__.py +++ b/sale_order_secondary_unit/__manifest__.py @@ -3,7 +3,7 @@ { "name": "Sale Order Secondary Unit", "summary": "Sale product in a secondary unit", - "version": "15.0.1.2.0", + "version": "16.0.1.0.0", "development_status": "Production/Stable", "category": "Sale", "website": "https://github.com/OCA/sale-workflow", diff --git a/sale_order_secondary_unit/models/__init__.py b/sale_order_secondary_unit/models/__init__.py index f4b27e67842c..a99b7ffff1d3 100644 --- a/sale_order_secondary_unit/models/__init__.py +++ b/sale_order_secondary_unit/models/__init__.py @@ -1,3 +1,4 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from . import product_product from . import product_template from . import sale_order diff --git a/sale_order_secondary_unit/models/product_product.py b/sale_order_secondary_unit/models/product_product.py new file mode 100644 index 000000000000..c76d013eb958 --- /dev/null +++ b/sale_order_secondary_unit/models/product_product.py @@ -0,0 +1,17 @@ +# Copyright 2022 Tecnativa - Sergio Teruel +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import fields, models + + +class ProductProduct(models.Model): + _inherit = "product.product" + + sale_secondary_uom_id = fields.Many2one( + comodel_name="product.secondary.unit", + string="Default secondary unit for sales", + help="In order to set a value, please first add at least one record" + " in 'Secondary Unit of Measure'", + domain="['|', ('product_id', '=', id)," + "'&', ('product_tmpl_id', '=', product_tmpl_id)," + " ('product_id', '=', False)]", + ) diff --git a/sale_order_secondary_unit/models/product_template.py b/sale_order_secondary_unit/models/product_template.py index 5983c837d92c..03e8b85f0c5d 100644 --- a/sale_order_secondary_unit/models/product_template.py +++ b/sale_order_secondary_unit/models/product_template.py @@ -1,6 +1,6 @@ # Copyright 2018-2020 Tecnativa - Sergio Teruel # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo import fields, models +from odoo import _, api, fields, models class ProductTemplate(models.Model): @@ -9,6 +9,64 @@ class ProductTemplate(models.Model): sale_secondary_uom_id = fields.Many2one( comodel_name="product.secondary.unit", string="Default secondary unit for sales", + compute="_compute_sale_secondary_uom_id", + inverse="_inverse_sale_secondary_uom_id", help="In order to set a value, please first add at least one record" " in 'Secondary Unit of Measure'", + domain="[('product_tmpl_id', '=', id), ('product_id', '=', False)]", + store=True, ) + + @api.depends("product_variant_ids", "product_variant_ids.sale_secondary_uom_id") + def _compute_sale_secondary_uom_id(self): + unique_variants = self.filtered(lambda tmpl: tmpl.product_variant_count == 1) + for template in unique_variants: + template.sale_secondary_uom_id = ( + template.product_variant_ids.sale_secondary_uom_id + ) + for template in self - unique_variants: + if len(template.product_variant_ids.sale_secondary_uom_id) == 1: + template.sale_secondary_uom_id = ( + template.product_variant_ids.sale_secondary_uom_id + ) + else: + template.sale_secondary_uom_id = False + + def _inverse_sale_secondary_uom_id(self): + for template in self: + # if template.product_variant_count == 1: + template.product_variant_ids.sale_secondary_uom_id = ( + template.sale_secondary_uom_id + ) + + @api.onchange("sale_secondary_uom_id") + def onchange_sale_secondary_uom_id(self): + if len(self.product_variant_ids.sale_secondary_uom_id) > 1: + return { + "warning": { + "title": _("Warning"), + "message": _( + "Product variants have distinct sale secondary uom:" + "\n{secondary_uom}\n" + "All variants will be written with new secondary uom" + ).format( + secondary_uom="\n".join( + self.product_variant_ids.mapped( + "sale_secondary_uom_id.name" + ) + ) + ), + } + } + + @api.model_create_multi + def create(self, vals_list): + templates = super(ProductTemplate, self).create(vals_list) + # This is needed to set given values to first variant after creation + for template, vals in zip(templates, vals_list): + related_vals = {} + if vals.get("sale_secondary_uom_id"): + related_vals["sale_secondary_uom_id"] = vals["sale_secondary_uom_id"] + if related_vals: + template.write(related_vals) + return templates diff --git a/sale_order_secondary_unit/models/sale_order.py b/sale_order_secondary_unit/models/sale_order.py index 5daa91099106..26b4952b1676 100644 --- a/sale_order_secondary_unit/models/sale_order.py +++ b/sale_order_secondary_unit/models/sale_order.py @@ -11,18 +11,9 @@ class SaleOrderLine(models.Model): "uom_field": "product_uom", } - secondary_uom_qty = fields.Float(string="2nd Qty", digits="Product Unit of Measure") - secondary_uom_id = fields.Many2one( - comodel_name="product.secondary.unit", - string="2nd uom", - ondelete="restrict", - ) - secondary_uom_unit_price = fields.Float( string="2nd unit price", - digits="Product Unit of Measure", - store=False, - readonly=True, + digits="Product Price", compute="_compute_secondary_uom_unit_price", ) @@ -48,17 +39,12 @@ def product_id_change(self): """ # Determine if we compute the sale line with one unit of secondary uom as default. # Based on method of sale order line _update_taxes - default_secondary_qty = 0.0 - if ( - not self.product_uom or (self.product_id.uom_id.id != self.product_uom.id) - ) and not self.product_uom_qty: - default_secondary_qty = 1.0 res = super().product_id_change() + line_uom_qty = self.product_uom_qty + self.secondary_uom_id = self.product_id.sale_secondary_uom_id if self.product_id.sale_secondary_uom_id: - line_uom_qty = self.product_uom_qty - self.secondary_uom_id = self.product_id.sale_secondary_uom_id - if default_secondary_qty: - self.secondary_uom_qty = default_secondary_qty + if line_uom_qty == 1.0: + self.secondary_uom_qty = 1.0 self.onchange_product_uom_for_secondary() else: self.product_uom_qty = line_uom_qty diff --git a/sale_order_secondary_unit/tests/test_sale_order_secondary_unit.py b/sale_order_secondary_unit/tests/test_sale_order_secondary_unit.py index f43d87fbaf4f..83d2e4048983 100644 --- a/sale_order_secondary_unit/tests/test_sale_order_secondary_unit.py +++ b/sale_order_secondary_unit/tests/test_sale_order_secondary_unit.py @@ -16,6 +16,11 @@ def setUpClass(cls): "name": "test", "uom_id": cls.product_uom_kg.id, "uom_po_id": cls.product_uom_kg.id, + } + ) + # Set secondary uom on product template + cls.product.product_tmpl_id.write( + { "secondary_uom_ids": [ ( 0, diff --git a/sale_order_secondary_unit/views/product_views.xml b/sale_order_secondary_unit/views/product_views.xml index db1761906e0e..c52c6de57e03 100644 --- a/sale_order_secondary_unit/views/product_views.xml +++ b/sale_order_secondary_unit/views/product_views.xml @@ -6,14 +6,9 @@ Product template Secondary Unit product.template - - + diff --git a/sale_order_secondary_unit/views/sale_order_views.xml b/sale_order_secondary_unit/views/sale_order_views.xml index db29cd798050..5f6db39e087b 100644 --- a/sale_order_secondary_unit/views/sale_order_views.xml +++ b/sale_order_secondary_unit/views/sale_order_views.xml @@ -6,26 +6,30 @@ Sale Order Secondary Unit sale.order - - - - + + + + + + + + + + + diff --git a/setup/sale_order_secondary_unit/odoo/addons/sale_order_secondary_unit b/setup/sale_order_secondary_unit/odoo/addons/sale_order_secondary_unit new file mode 120000 index 000000000000..c1ca3bc48b31 --- /dev/null +++ b/setup/sale_order_secondary_unit/odoo/addons/sale_order_secondary_unit @@ -0,0 +1 @@ +../../../../sale_order_secondary_unit \ No newline at end of file diff --git a/setup/sale_order_secondary_unit/setup.py b/setup/sale_order_secondary_unit/setup.py new file mode 100644 index 000000000000..28c57bb64031 --- /dev/null +++ b/setup/sale_order_secondary_unit/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)