diff --git a/product_uom_updatable/README.rst b/product_uom_updatable/README.rst new file mode 100644 index 000000000000..2fcc02e971a8 --- /dev/null +++ b/product_uom_updatable/README.rst @@ -0,0 +1,95 @@ +===================== +Product Uom Updatable +===================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:fe7aa0456f147d9cc06288c74d7f179d26dd7fcb22908c2a35a70c086c1b62ea + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :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/18.0/product_uom_updatable + :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-18-0/product-attribute-18-0-product_uom_updatable + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/product-attribute&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Allow to change the product UoM if the new UoM is in the same category +and has the same factor. + +**Table of contents** + +.. contents:: + :local: + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* ACSONE SA/NV +* Camptocamp + +Contributors +------------ + +- Anna Janiszewska + +- [Trobz] (https://trobz.com/): + + - Dung Tran + - Khoi (Kien Kim) + +- Hughes Damry + +Other credits +------------- + +The development of this module has been financially supported by: + +- Camptocamp + +The migration of this module from 17.0 to 18.0 was financially supported +by Camptocamp. + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/product-attribute `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/product_uom_updatable/__init__.py b/product_uom_updatable/__init__.py new file mode 100644 index 000000000000..0650744f6bc6 --- /dev/null +++ b/product_uom_updatable/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/product_uom_updatable/__manifest__.py b/product_uom_updatable/__manifest__.py new file mode 100644 index 000000000000..7c844a144366 --- /dev/null +++ b/product_uom_updatable/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2020 Acsone SA/NV +# Copyright 2020 Camptocamp +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Product Uom Updatable", + "summary": """ + allows products uom to be modified after be used in a stock picking if + the product uom is of the same category""", + "version": "18.0.1.0.0", + "license": "AGPL-3", + "author": "ACSONE SA/NV, Camptocamp, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/product-attribute", + "depends": ["stock"], +} diff --git a/product_uom_updatable/i18n/it.po b/product_uom_updatable/i18n/it.po new file mode 100644 index 000000000000..19df3151abc6 --- /dev/null +++ b/product_uom_updatable/i18n/it.po @@ -0,0 +1,33 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * product_uom_updatable +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2023-04-27 11:19+0000\n" +"Last-Translator: Sebastiano Picchi \n" +"Language-Team: none\n" +"Language: it\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" + +#. module: product_uom_updatable +#: model:ir.model,name:product_uom_updatable.model_product_template +msgid "Product" +msgstr "Prodotto" + +#. module: product_uom_updatable +#. odoo-python +#: code:addons/product_uom_updatable/models/product_template.py:0 +#, python-format +msgid "" +"You cannot change the unit of measure of a product to a new unit that " +"doesn't have the same category and factor" +msgstr "" +"Non puoi cambiare l'unità di misura di un prodotto ad una nuova unità che " +"non ha la stessa categoria e fattore" diff --git a/product_uom_updatable/i18n/product_uom_updatable.pot b/product_uom_updatable/i18n/product_uom_updatable.pot new file mode 100644 index 000000000000..c410286200f2 --- /dev/null +++ b/product_uom_updatable/i18n/product_uom_updatable.pot @@ -0,0 +1,28 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * product_uom_updatable +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \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: \n" + +#. module: product_uom_updatable +#: model:ir.model,name:product_uom_updatable.model_product_template +msgid "Product" +msgstr "" + +#. module: product_uom_updatable +#. odoo-python +#: code:addons/product_uom_updatable/models/product_template.py:0 +#, python-format +msgid "" +"You cannot change the unit of measure of a product to a new unit that " +"doesn't have the same category and factor" +msgstr "" diff --git a/product_uom_updatable/models/__init__.py b/product_uom_updatable/models/__init__.py new file mode 100644 index 000000000000..e8fa8f6bf1e0 --- /dev/null +++ b/product_uom_updatable/models/__init__.py @@ -0,0 +1 @@ +from . import product_template diff --git a/product_uom_updatable/models/product_template.py b/product_uom_updatable/models/product_template.py new file mode 100644 index 000000000000..7f36e73091c6 --- /dev/null +++ b/product_uom_updatable/models/product_template.py @@ -0,0 +1,59 @@ +# Copyright 2020 Acsone SA/NV +# Copyright 2020 Camptocamp +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from itertools import groupby + +from odoo import models +from odoo.exceptions import UserError +from odoo.tools import SQL + + +class ProductTemplate(models.Model): + _inherit = "product.template" + + def write(self, vals): + uom_id = vals.pop("uom_id", False) + uom_po_id = vals.pop("uom_po_id", False) + if uom_id: + self._update_uom(uom_id, "uom_id") + if uom_po_id: + self._update_uom(uom_po_id, "uom_po_id") + res = super().write(vals) + return res + + def _update_uom(self, uom_id, field_name): + uom_obj = self.env["uom.uom"] + new_uom = uom_obj.browse(uom_id) + sorted_items = sorted(self, key=lambda r: r[field_name]) + + for key, products_group in groupby(sorted_items, key=lambda r: r[field_name]): + product_ids = [p.id for p in products_group] + + if ( + key.category_id == new_uom.category_id + and key.factor_inv == new_uom.factor_inv + ): + # pylint: disable=sql-injection + self.env.cr.execute( + SQL( + """ + UPDATE product_template + SET %(field)s = %(new_uom)s + WHERE id IN %(product_ids)s + """, + field=SQL.identifier(field_name), + new_uom=new_uom.id, + product_ids=tuple(product_ids), + ) + ) + products = self.env["product.template"].browse(product_ids) + products.invalidate_recordset(fnames=[field_name]) + else: + raise UserError( + self.env._( + "You cannot change the unit of measure of a product " + "to a new unit that doesn't have the same category " + "and factor" + ) + ) diff --git a/product_uom_updatable/pyproject.toml b/product_uom_updatable/pyproject.toml new file mode 100644 index 000000000000..4231d0cccb3d --- /dev/null +++ b/product_uom_updatable/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/product_uom_updatable/readme/CONTRIBUTORS.md b/product_uom_updatable/readme/CONTRIBUTORS.md new file mode 100644 index 000000000000..66e546ca510d --- /dev/null +++ b/product_uom_updatable/readme/CONTRIBUTORS.md @@ -0,0 +1,7 @@ +- Anna Janiszewska \<\> + +- [Trobz] (https://trobz.com/): + - Dung Tran \<\> + - Khoi (Kien Kim) \<\> + +- Hughes Damry \<\> diff --git a/product_uom_updatable/readme/CREDITS.md b/product_uom_updatable/readme/CREDITS.md new file mode 100644 index 000000000000..f2929172847b --- /dev/null +++ b/product_uom_updatable/readme/CREDITS.md @@ -0,0 +1,5 @@ +The development of this module has been financially supported by: + +- Camptocamp + +The migration of this module from 17.0 to 18.0 was financially supported by Camptocamp. diff --git a/product_uom_updatable/readme/DESCRIPTION.md b/product_uom_updatable/readme/DESCRIPTION.md new file mode 100644 index 000000000000..0b3a21b70222 --- /dev/null +++ b/product_uom_updatable/readme/DESCRIPTION.md @@ -0,0 +1,2 @@ +Allow to change the product UoM if the new UoM is in the same category +and has the same factor. diff --git a/product_uom_updatable/static/description/icon.png b/product_uom_updatable/static/description/icon.png new file mode 100644 index 000000000000..3a0328b516c4 Binary files /dev/null and b/product_uom_updatable/static/description/icon.png differ diff --git a/product_uom_updatable/static/description/index.html b/product_uom_updatable/static/description/index.html new file mode 100644 index 000000000000..180ab7c0b0ac --- /dev/null +++ b/product_uom_updatable/static/description/index.html @@ -0,0 +1,441 @@ + + + + + +Product Uom Updatable + + + +
+

Product Uom Updatable

+ + +

Beta License: AGPL-3 OCA/product-attribute Translate me on Weblate Try me on Runboat

+

Allow to change the product UoM if the new UoM is in the same category +and has the same factor.

+

Table of contents

+ +
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • ACSONE SA/NV
  • +
  • Camptocamp
  • +
+
+
+

Contributors

+ +
+
+

Other credits

+

The development of this module has been financially supported by:

+
    +
  • Camptocamp
  • +
+

The migration of this module from 17.0 to 18.0 was financially supported +by Camptocamp.

+
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/product-attribute project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/product_uom_updatable/tests/__init__.py b/product_uom_updatable/tests/__init__.py new file mode 100644 index 000000000000..a0d5331be1f8 --- /dev/null +++ b/product_uom_updatable/tests/__init__.py @@ -0,0 +1 @@ +from . import test_product_uom_update diff --git a/product_uom_updatable/tests/test_product_uom_update.py b/product_uom_updatable/tests/test_product_uom_update.py new file mode 100644 index 000000000000..721afd64113e --- /dev/null +++ b/product_uom_updatable/tests/test_product_uom_update.py @@ -0,0 +1,85 @@ +# Copyright 2020 Acsone SA/NV +# Copyright 2020 Camptocamp +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.exceptions import UserError +from odoo.tests.common import TransactionCase + + +class TestProductUomUpdate(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + + cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) + cls.uom_unit = cls.env.ref("uom.product_uom_unit") + cls.uom_day = cls.env.ref("uom.product_uom_day") + cls.product = cls.env.ref("product.product_delivery_01") + cls.product_tmpl_id = cls.env.ref( + "product.product_delivery_01_product_template" + ) + cls.partner_id = cls.env.ref("base.res_partner_4") + cls.picking_type_id = cls.env.ref("stock.picking_type_in") + cls.location_id = cls.env.ref("stock.stock_location_suppliers") + cls.location_dest_id = cls.env.ref("stock.stock_location_stock") + cls.picking_in = cls.env["stock.picking"].create( + { + "picking_type_id": cls.picking_type_id.id, + "partner_id": cls.partner_id.id, + "location_id": cls.location_id.id, + "location_dest_id": cls.location_dest_id.id, + } + ) + + cls.env["stock.move"].create( + { + "name": cls.product.name, + "product_id": cls.product.id, + "product_uom_qty": 2, + "product_uom": cls.product.uom_id.id, + "picking_id": cls.picking_in.id, + "location_id": cls.location_id.id, + "location_dest_id": cls.location_dest_id.id, + } + ) + cls.new_uom = cls.env["uom.uom"].create( + { + "name": "new unit", + "category_id": cls.uom_unit.category_id.id, + "uom_type": "smaller", + } + ) + + cls.new_uom_other_category = cls.env["uom.uom"].create( + { + "name": "new unit 2", + "category_id": cls.uom_day.category_id.id, + "uom_type": "smaller", + } + ) + + def test_update_uom(self): + # verify that the product has stock_moves + self.assertTrue(self.product.stock_move_ids) + self.assertEqual(self.product.uom_id, self.uom_unit) + self.assertEqual(self.uom_unit.category_id, self.new_uom.category_id) + self.assertEqual( + self.uom_day.category_id, self.new_uom_other_category.category_id + ) + self.assertEqual(self.uom_unit.factor_inv, self.new_uom.factor_inv) + self.assertEqual( + self.uom_day.factor_inv, self.new_uom_other_category.factor_inv + ) + # uom is changed with another uom with the same category + self.product_tmpl_id.update({"uom_id": self.new_uom.id}) + self.assertEqual(self.product_tmpl_id.uom_id, self.new_uom) + # uom_po can also be changed with another uom with the same category + self.assertEqual(self.product_tmpl_id.uom_po_id, self.uom_unit) + self.product_tmpl_id.update({"uom_po_id": self.new_uom.id}) + self.assertEqual(self.product_tmpl_id.uom_po_id, self.new_uom) + # uom is changed with another uom from different category + with self.assertRaises(UserError): + self.product_tmpl_id.update({"uom_id": self.new_uom_other_category.id}) + # uom_po is changed with another uom from different category + with self.assertRaises(UserError): + self.product_tmpl_id.update({"uom_po_id": self.new_uom_other_category.id})