From 0eacfccc0e0cac15a5965c5852bbca6ba828f2a9 Mon Sep 17 00:00:00 2001 From: sergio-teruel Date: Wed, 1 Mar 2023 22:57:31 +0100 Subject: [PATCH] [IMP] product_net_weight: Adapt module like Odoo manage weight field with multiple variants --- product_net_weight/README.rst | 16 ++++--- product_net_weight/i18n/es.po | 43 +++++++------------ .../i18n/product_net_weight.pot | 30 +++++-------- product_net_weight/models/product_product.py | 11 ++++- product_net_weight/models/product_template.py | 33 +++++++++++--- product_net_weight/readme/CONTRIBUTORS.rst | 4 ++ .../static/description/index.html | 8 +++- product_net_weight/tests/__init__.py | 2 + 8 files changed, 85 insertions(+), 62 deletions(-) create mode 100644 product_net_weight/tests/__init__.py diff --git a/product_net_weight/README.rst b/product_net_weight/README.rst index b1621238682..ebadda9bb14 100644 --- a/product_net_weight/README.rst +++ b/product_net_weight/README.rst @@ -14,16 +14,16 @@ Products - Net Weight :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html :alt: License: AGPL-3 .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fproduct--attribute-lightgray.png?logo=github - :target: https://github.com/OCA/product-attribute/tree/15.0/product_net_weight + :target: https://github.com/OCA/product-attribute/tree/16.0/product_net_weight :alt: OCA/product-attribute .. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png :target: https://translation.odoo-community.org/projects/product-attribute-15-0/product-attribute-15-0-product_net_weight :alt: Translate me on Weblate -.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png - :target: https://runbot.odoo-community.org/runbot/135/15.0 - :alt: Try me on Runbot +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/webui/builds.html?repo=OCA/product-attribute&target_branch=16.0 + :alt: Try me on Runboat -|badge1| |badge2| |badge3| |badge4| |badge5| +|badge1| |badge2| |badge3| |badge4| |badge5| This module extends the functionality of stock module to support net weight. (container excluded) @@ -78,6 +78,10 @@ Contributors * Fernando La Chica +* `Tecnativa `_: + + * Sergio Teruel + Maintainers ~~~~~~~~~~~ @@ -97,7 +101,7 @@ promote its widespread use. Current `maintainer `__: -|maintainer-legalsylvain| +|maintainer-legalsylvain| This module is part of the `OCA/product-attribute `_ project on GitHub. diff --git a/product_net_weight/i18n/es.po b/product_net_weight/i18n/es.po index bd94d950e0e..9263531fe7d 100644 --- a/product_net_weight/i18n/es.po +++ b/product_net_weight/i18n/es.po @@ -4,17 +4,16 @@ # msgid "" msgstr "" -"Project-Id-Version: Odoo Server 15.0\n" +"Project-Id-Version: Odoo Server 16.0+e\n" "Report-Msgid-Bugs-To: \n" -"PO-Revision-Date: 2023-01-09 13:45+0000\n" -"Last-Translator: Carlos Dauden \n" -"Language-Team: none\n" -"Language: es\n" +"POT-Creation-Date: 2023-03-02 08:36+0000\n" +"PO-Revision-Date: 2023-03-02 08:36+0000\n" +"Last-Translator: \n" +"Language-Team: \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: \n" -"Plural-Forms: nplurals=2; plural=n != 1;\n" -"X-Generator: Weblate 4.14.1\n" +"Plural-Forms: \n" #. module: product_net_weight #: model:ir.model.fields,field_description:product_net_weight.field_product_product__weight @@ -35,35 +34,23 @@ msgid "Net Weight of the product, container excluded." msgstr "Peso neto del producto, contenedor excluido." #. module: product_net_weight -#: model:ir.model,name:product_net_weight.model_product_product +#: model:ir.model,name:product_net_weight.model_product_template msgid "Product" msgstr "Producto" #. module: product_net_weight -#: model:ir.model,name:product_net_weight.model_product_template -msgid "Product Template" -msgstr "Plantilla de producto" +#: model:ir.model,name:product_net_weight.model_product_product +msgid "Product Variant" +msgstr "Variantes de producto" #. module: product_net_weight -#: model:product.product,name:product_net_weight.product_strawberry_jam #: model:product.template,name:product_net_weight.product_strawberry_jam_product_template msgid "Strawberry Jam (Jar of 250 grams)" msgstr "" #. module: product_net_weight -#: model:product.product,uom_name:product_net_weight.product_strawberry_jam -#: model:product.template,uom_name:product_net_weight.product_strawberry_jam_product_template -msgid "Units" -msgstr "Unidades" - -#. module: product_net_weight -#: model:product.product,weight_uom_name:product_net_weight.product_strawberry_jam -#: model:product.template,weight_uom_name:product_net_weight.product_strawberry_jam_product_template -msgid "kg" -msgstr "kg" - -#. module: product_net_weight -#: model:product.product,volume_uom_name:product_net_weight.product_strawberry_jam -#: model:product.template,volume_uom_name:product_net_weight.product_strawberry_jam_product_template -msgid "m³" -msgstr "m³" +#. odoo-python +#: code:addons/product_net_weight/models/product_product.py:0 +#, python-format +msgid "The net weight of product must be lower than gross weight." +msgstr "El peso neto tiene que ser menor que el peso bruto." diff --git a/product_net_weight/i18n/product_net_weight.pot b/product_net_weight/i18n/product_net_weight.pot index c3658c33cf2..2a8c6150c65 100644 --- a/product_net_weight/i18n/product_net_weight.pot +++ b/product_net_weight/i18n/product_net_weight.pot @@ -4,8 +4,10 @@ # msgid "" msgstr "" -"Project-Id-Version: Odoo Server 15.0\n" +"Project-Id-Version: Odoo Server 16.0+e\n" "Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2023-03-02 08:36+0000\n" +"PO-Revision-Date: 2023-03-02 08:36+0000\n" "Last-Translator: \n" "Language-Team: \n" "MIME-Version: 1.0\n" @@ -32,35 +34,23 @@ msgid "Net Weight of the product, container excluded." msgstr "" #. module: product_net_weight -#: model:ir.model,name:product_net_weight.model_product_product +#: model:ir.model,name:product_net_weight.model_product_template msgid "Product" msgstr "" #. module: product_net_weight -#: model:ir.model,name:product_net_weight.model_product_template -msgid "Product Template" +#: model:ir.model,name:product_net_weight.model_product_product +msgid "Product Variant" msgstr "" #. module: product_net_weight -#: model:product.product,name:product_net_weight.product_strawberry_jam #: model:product.template,name:product_net_weight.product_strawberry_jam_product_template msgid "Strawberry Jam (Jar of 250 grams)" msgstr "" #. module: product_net_weight -#: model:product.product,uom_name:product_net_weight.product_strawberry_jam -#: model:product.template,uom_name:product_net_weight.product_strawberry_jam_product_template -msgid "Units" -msgstr "" - -#. module: product_net_weight -#: model:product.product,weight_uom_name:product_net_weight.product_strawberry_jam -#: model:product.template,weight_uom_name:product_net_weight.product_strawberry_jam_product_template -msgid "kg" -msgstr "" - -#. module: product_net_weight -#: model:product.product,volume_uom_name:product_net_weight.product_strawberry_jam -#: model:product.template,volume_uom_name:product_net_weight.product_strawberry_jam_product_template -msgid "m³" +#. odoo-python +#: code:addons/product_net_weight/models/product_product.py:0 +#, python-format +msgid "The net weight of product must be lower than gross weight." msgstr "" diff --git a/product_net_weight/models/product_product.py b/product_net_weight/models/product_product.py index 1e9143e323d..8ca02cf3955 100644 --- a/product_net_weight/models/product_product.py +++ b/product_net_weight/models/product_product.py @@ -2,7 +2,8 @@ # @author: Sylvain LE GAL (https://twitter.com/legalsylvain) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from odoo import fields, models +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError class ProductProduct(models.Model): @@ -15,3 +16,11 @@ class ProductProduct(models.Model): # Explicit field, renaming it weight = fields.Float(string="Gross Weight") + + @api.constrains("net_weight", "weight") + def _check_net_weight(self): + for product in self: + if product.weight and product.net_weight > product.weight: + raise ValidationError( + _("The net weight of product must be lower than gross weight.") + ) diff --git a/product_net_weight/models/product_template.py b/product_net_weight/models/product_template.py index 6c8e3f7b696..346b4715718 100644 --- a/product_net_weight/models/product_template.py +++ b/product_net_weight/models/product_template.py @@ -1,20 +1,43 @@ # Copyright (C) 2021 - Today: GRAP (http://www.grap.coop) # @author: Sylvain LE GAL (https://twitter.com/legalsylvain) -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +# Copyright 2023 Tecnativa - Sergio Teruel +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). -from odoo import fields, models +from odoo import api, fields, models class ProductTemplate(models.Model): _inherit = "product.template" net_weight = fields.Float( - string="Net Weight", - related="product_variant_ids.net_weight", + compute="_compute_net_weight", + inverse="_inverse_net_weight", digits="Stock Weight", help="Net Weight of the product, container excluded.", - readonly=False, + store=True, ) # Explicit field, renaming it weight = fields.Float(string="Gross Weight") + + @api.depends("product_variant_ids", "product_variant_ids.net_weight") + def _compute_net_weight(self): + for template in self: + if template.product_variant_count == 1: + template.net_weight = template.product_variant_ids.net_weight + else: + template.net_weight = 0.0 + + def _inverse_net_weight(self): + for template in self: + if len(template.product_variant_ids) == 1: + template.product_variant_ids.net_weight = template.net_weight + + @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): + if vals.get("net_weight"): + template.write({"net_weight": vals["net_weight"]}) + return templates diff --git a/product_net_weight/readme/CONTRIBUTORS.rst b/product_net_weight/readme/CONTRIBUTORS.rst index 7187c07b1db..01d367d04b7 100644 --- a/product_net_weight/readme/CONTRIBUTORS.rst +++ b/product_net_weight/readme/CONTRIBUTORS.rst @@ -2,3 +2,7 @@ * `Greenice `_: * Fernando La Chica + +* `Tecnativa `_: + + * Sergio Teruel diff --git a/product_net_weight/static/description/index.html b/product_net_weight/static/description/index.html index 01a89d5eeda..a25ced3f85d 100644 --- a/product_net_weight/static/description/index.html +++ b/product_net_weight/static/description/index.html @@ -3,7 +3,7 @@ - + Products - Net Weight