Skip to content

Commit

Permalink
[IMP] product_net_weight: Adapt module like Odoo manage weight field …
Browse files Browse the repository at this point in the history
…with multiple variants
  • Loading branch information
sergio-teruel authored and ferran-S73 committed Jun 7, 2023
1 parent 98c4835 commit 0eacfcc
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 62 deletions.
16 changes: 10 additions & 6 deletions product_net_weight/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -78,6 +78,10 @@ Contributors

* Fernando La Chica <[email protected]>

* `Tecnativa <https://www.tecnativa.com>`_:

* Sergio Teruel

Maintainers
~~~~~~~~~~~

Expand All @@ -97,7 +101,7 @@ promote its widespread use.

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-legalsylvain|
|maintainer-legalsylvain|

This module is part of the `OCA/product-attribute <https://github.com/OCA/product-attribute/tree/15.0/product_net_weight>`_ project on GitHub.

Expand Down
43 changes: 15 additions & 28 deletions product_net_weight/i18n/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>\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
Expand All @@ -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."
30 changes: 10 additions & 20 deletions product_net_weight/i18n/product_net_weight.pot
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 ""
11 changes: 10 additions & 1 deletion product_net_weight/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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.")
)
33 changes: 28 additions & 5 deletions product_net_weight/models/product_template.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions product_net_weight/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
* `Greenice <https://www.greenice.com>`_:

* Fernando La Chica <[email protected]>

* `Tecnativa <https://www.tecnativa.com>`_:

* Sergio Teruel
8 changes: 6 additions & 2 deletions product_net_weight/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.15.1: http://docutils.sourceforge.net/" />
<meta name="generator" content="Docutils: http://docutils.sourceforge.net/" />
<title>Products - Net Weight</title>
<style type="text/css">

Expand Down Expand Up @@ -367,7 +367,7 @@ <h1 class="title">Products - Net Weight</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/product-attribute/tree/15.0/product_net_weight"><img alt="OCA/product-attribute" src="https://img.shields.io/badge/github-OCA%2Fproduct--attribute-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/product-attribute-15-0/product-attribute-15-0-product_net_weight"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/135/15.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/product-attribute/tree/15.0/product_net_weight"><img alt="OCA/product-attribute" src="https://img.shields.io/badge/github-OCA%2Fproduct--attribute-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/product-attribute-15-0/product-attribute-15-0-product_net_weight"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runboat.odoo-community.org/webui/builds.html?repo=OCA/product-attribute&amp;target_branch=15.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module extends the functionality of stock module to support net weight. (container excluded)</p>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
Expand Down Expand Up @@ -425,6 +425,10 @@ <h2><a class="toc-backref" href="#id6">Contributors</a></h2>
<li>Fernando La Chica &lt;<a class="reference external" href="mailto:fernandolachica&#64;gmail.com">fernandolachica&#64;gmail.com</a>&gt;</li>
</ul>
</li>
<li><a class="reference external" href="https://www.tecnativa.com">Tecnativa</a>:<ul>
<li>Sergio Teruel</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
2 changes: 2 additions & 0 deletions product_net_weight/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from . import test_product_net_weight

0 comments on commit 0eacfcc

Please sign in to comment.