diff --git a/product_packaging_template/README.rst b/product_packaging_template/README.rst new file mode 100644 index 00000000000..538b10a3cf7 --- /dev/null +++ b/product_packaging_template/README.rst @@ -0,0 +1,102 @@ +========================== +Product Packaging Template +========================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:05ed05917606253af93adef1e2c37e4249554220c75fac66caeb55644b3906a0 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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_packaging_template + :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_packaging_template + :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| + +Product packaging are defined at product variant level. It means that if +a product template has 25 variants, and all of them are sold in the same +packaging, one must define all the product packaging one by one on the +variants. + +The aim of this module is to be able to define the product packaging at +template level. It will then create the product packaging on all +variants automatically. + +Features +-------- + +- When a product packaging template is created, create the product + packaging on all variants; +- When a product packaging template is updated, update the product + packaging on all variants; +- When a product packaging template is deleted, delete the product + packaging on all variants; +- When a new product variant is created, create all the product + packaging defined by the product packaging templates. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To see the new table containing product packaging templates on product +templates, one must have the special group "Products: Can manage +packaging at template level". + +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 + +Contributors +------------ + +- Marie Lejeune + +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_packaging_template/__init__.py b/product_packaging_template/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/product_packaging_template/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/product_packaging_template/__manifest__.py b/product_packaging_template/__manifest__.py new file mode 100644 index 00000000000..2366cf61b5a --- /dev/null +++ b/product_packaging_template/__manifest__.py @@ -0,0 +1,21 @@ +# Copyright 2025 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Product Packaging Template", + "summary": """Allows to define product packaging at template level""", + "version": "18.0.1.0.0", + "license": "AGPL-3", + "author": "ACSONE SA/NV, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/product-attribute", + "depends": [ + "product", + ], + "data": [ + "security/groups.xml", + "security/product_packaging_template.xml", + "views/product_template.xml", + "views/product_packaging_template.xml", + ], + "installable": True, +} diff --git a/product_packaging_template/models/__init__.py b/product_packaging_template/models/__init__.py new file mode 100644 index 00000000000..524846154f0 --- /dev/null +++ b/product_packaging_template/models/__init__.py @@ -0,0 +1,4 @@ +from . import product_packaging +from . import product_packaging_template +from . import product_product +from . import product_template diff --git a/product_packaging_template/models/product_packaging.py b/product_packaging_template/models/product_packaging.py new file mode 100644 index 00000000000..c779ce234c1 --- /dev/null +++ b/product_packaging_template/models/product_packaging.py @@ -0,0 +1,10 @@ +# Copyright 2025 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ProductPackaging(models.Model): + _inherit = "product.packaging" + + packaging_tmpl_id = fields.Many2one("product.packaging.template", readonly=True) diff --git a/product_packaging_template/models/product_packaging_template.py b/product_packaging_template/models/product_packaging_template.py new file mode 100644 index 00000000000..1641af18eae --- /dev/null +++ b/product_packaging_template/models/product_packaging_template.py @@ -0,0 +1,68 @@ +# Copyright 2025 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import api, fields, models + + +class ProductPackagingTemplate(models.Model): + _name = "product.packaging.template" + _description = "Product Packaging Template" + + name = fields.Char("Product Packaging", required=True) + sequence = fields.Integer( + default=1, help="The first in the sequence is the default one." + ) + product_tmpl_id = fields.Many2one( + "product.template", + string="Product Template", + check_company=True, + required=True, + ondelete="cascade", + ) + qty = fields.Float( + "Contained Quantity", + default=1, + digits="Product Unit of Measure", + help="Quantity of products contained in the packaging.", + ) + company_id = fields.Many2one("res.company", "Company", index=True) + packaging_ids = fields.One2many("product.packaging", "packaging_tmpl_id") + + @api.model_create_multi + def create(self, vals_list): + res = super().create(vals_list) + for pt in res: + pt.product_tmpl_id.product_variant_ids._create_packaging_from_template( + template=pt + ) + return res + + def write(self, vals): + """ + Propagate changes on packages at variant level. + """ + res = super().write(vals) + self._propagate_to_packaging(vals) + return res + + @api.model + def _get_values_to_propagate(self, vals): + return {key: vals[key] for key in {"name", "sequence", "qty"} if key in vals} + + def _propagate_to_packaging(self, vals): + values_to_propagate = self._get_values_to_propagate(vals) + if values_to_propagate: + self.mapped("packaging_ids").write(values_to_propagate) + + def unlink(self): + self.mapped("packaging_ids").unlink() + return super().unlink() + + def _prepare_create_values_for_packaging(self): + self.ensure_one() + return { + "name": self.name, + "sequence": self.sequence, + "qty": self.qty, + "company_id": self.company_id.id, + "packaging_tmpl_id": self.id, + } diff --git a/product_packaging_template/models/product_product.py b/product_packaging_template/models/product_product.py new file mode 100644 index 00000000000..7d8ca432037 --- /dev/null +++ b/product_packaging_template/models/product_product.py @@ -0,0 +1,28 @@ +# Copyright 2025 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, models + + +class ProductProduct(models.Model): + _inherit = "product.product" + + def _create_packaging_from_template(self, template): + base_values = template._prepare_create_values_for_packaging() + create_values = [] + for product in self: + product_values = base_values.copy() + product_values.update({"product_id": product.id}) + create_values.append(product_values) + self.env["product.packaging"].create(create_values) + + @api.model_create_multi + def create(self, vals_list): + """ + Create all packaging from templates when creating a new variant + """ + res = super().create(vals_list) + for variant in res: + for pt in variant.product_tmpl_id.packaging_tmpl_ids: + variant._create_packaging_from_template(template=pt) + return res diff --git a/product_packaging_template/models/product_template.py b/product_packaging_template/models/product_template.py new file mode 100644 index 00000000000..85fe8a1e4f7 --- /dev/null +++ b/product_packaging_template/models/product_template.py @@ -0,0 +1,12 @@ +# Copyright 2025 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ProductTemplate(models.Model): + _inherit = "product.template" + + packaging_tmpl_ids = fields.One2many( + "product.packaging.template", "product_tmpl_id" + ) diff --git a/product_packaging_template/pyproject.toml b/product_packaging_template/pyproject.toml new file mode 100644 index 00000000000..4231d0cccb3 --- /dev/null +++ b/product_packaging_template/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/product_packaging_template/readme/CONTRIBUTORS.md b/product_packaging_template/readme/CONTRIBUTORS.md new file mode 100644 index 00000000000..9b0d5aafb43 --- /dev/null +++ b/product_packaging_template/readme/CONTRIBUTORS.md @@ -0,0 +1 @@ +- Marie Lejeune \<\> diff --git a/product_packaging_template/readme/DESCRIPTION.md b/product_packaging_template/readme/DESCRIPTION.md new file mode 100644 index 00000000000..d0c72aa6be0 --- /dev/null +++ b/product_packaging_template/readme/DESCRIPTION.md @@ -0,0 +1,14 @@ +Product packaging are defined at product variant level. It means that if a product template +has 25 variants, and all of them are sold in the same packaging, one must define all the product packaging +one by one on the variants. + +The aim of this module is to be able to define the product packaging at template level. +It will then create the product packaging on all variants automatically. + +Features +-------- + +* When a product packaging template is created, create the product packaging on all variants; +* When a product packaging template is updated, update the product packaging on all variants; +* When a product packaging template is deleted, delete the product packaging on all variants; +* When a new product variant is created, create all the product packaging defined by the product packaging templates. diff --git a/product_packaging_template/readme/USAGE.md b/product_packaging_template/readme/USAGE.md new file mode 100644 index 00000000000..ff0e956ce3f --- /dev/null +++ b/product_packaging_template/readme/USAGE.md @@ -0,0 +1 @@ +To see the new table containing product packaging templates on product templates, one must have the special group "Products: Can manage packaging at template level". diff --git a/product_packaging_template/security/groups.xml b/product_packaging_template/security/groups.xml new file mode 100644 index 00000000000..65f053d4640 --- /dev/null +++ b/product_packaging_template/security/groups.xml @@ -0,0 +1,14 @@ + + + + + Product: Can manage packaging at template level + + + + + diff --git a/product_packaging_template/security/product_packaging_template.xml b/product_packaging_template/security/product_packaging_template.xml new file mode 100644 index 00000000000..7944cac0f19 --- /dev/null +++ b/product_packaging_template/security/product_packaging_template.xml @@ -0,0 +1,17 @@ + + + + + product.packaging.template manager access + + + + + + + + diff --git a/product_packaging_template/static/description/index.html b/product_packaging_template/static/description/index.html new file mode 100644 index 00000000000..f8d5bed52f8 --- /dev/null +++ b/product_packaging_template/static/description/index.html @@ -0,0 +1,444 @@ + + + + + +Product Packaging Template + + + +
+

Product Packaging Template

+ + +

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

+

Product packaging are defined at product variant level. It means that if +a product template has 25 variants, and all of them are sold in the same +packaging, one must define all the product packaging one by one on the +variants.

+

The aim of this module is to be able to define the product packaging at +template level. It will then create the product packaging on all +variants automatically.

+
+

Features

+
    +
  • When a product packaging template is created, create the product +packaging on all variants;
  • +
  • When a product packaging template is updated, update the product +packaging on all variants;
  • +
  • When a product packaging template is deleted, delete the product +packaging on all variants;
  • +
  • When a new product variant is created, create all the product +packaging defined by the product packaging templates.
  • +
+

Table of contents

+ +
+

Usage

+

To see the new table containing product packaging templates on product +templates, one must have the special group “Products: Can manage +packaging at template level”.

+
+
+

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.

+
+ +
+
+

Authors

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

Contributors

+ +
+
+

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_packaging_template/tests/__init__.py b/product_packaging_template/tests/__init__.py new file mode 100644 index 00000000000..a15f23be0e5 --- /dev/null +++ b/product_packaging_template/tests/__init__.py @@ -0,0 +1 @@ +from . import test_product_packaging_template diff --git a/product_packaging_template/tests/test_product_packaging_template.py b/product_packaging_template/tests/test_product_packaging_template.py new file mode 100644 index 00000000000..ba2f9874c6d --- /dev/null +++ b/product_packaging_template/tests/test_product_packaging_template.py @@ -0,0 +1,76 @@ +# Copyright 2025 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from odoo import Command +from odoo.tests.common import TransactionCase + + +class TestProductPackagingTemplate(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.template = cls.env["product.template"].create( + { + "name": "Test Product", + } + ) + cls.attribute_color = cls.env.ref("product.product_attribute_2") + cls.attribute_line = cls.env["product.template.attribute.line"].create( + [ + { + "product_tmpl_id": cls.template.id, + "attribute_id": cls.attribute_color.id, + "value_ids": [ + Command.set( + [ + cls.env.ref("product.product_attribute_value_3").id, + cls.env.ref("product.product_attribute_value_4").id, + ] + ), + ], + } + ] + ) + cls.variant_1 = cls.template.product_variant_ids[0] + cls.variant_2 = cls.template.product_variant_ids[0] + + def test_create_packaging_template(self): + self.template.packaging_tmpl_ids = [ + Command.create({"name": "Box of 10", "qty": 10}) + ] + self.assertEqual(len(self.variant_1.packaging_ids), 1) + self.assertEqual(len(self.variant_2.packaging_ids), 1) + self.assertEqual(self.variant_1.packaging_ids.name, "Box of 10") + self.assertEqual(self.variant_2.packaging_ids.name, "Box of 10") + + def test_update_packaging_template(self): + self.template.packaging_tmpl_ids = [ + Command.create({"name": "Box of 10", "qty": 10}) + ] + self.template.packaging_tmpl_ids.write({"name": "Box of 12", "qty": 12}) + self.assertEqual(self.variant_1.packaging_ids.name, "Box of 12") + self.assertEqual(self.variant_2.packaging_ids.name, "Box of 12") + self.assertEqual(self.variant_1.packaging_ids.qty, 12) + self.assertEqual(self.variant_2.packaging_ids.qty, 12) + + def test_delete_packaging_template(self): + self.template.packaging_tmpl_ids = [ + Command.create({"name": "Box of 10", "qty": 10}) + ] + self.assertEqual(len(self.template.packaging_tmpl_ids.packaging_ids), 2) + # Manually delete the packaging on variant 2 + self.variant_2.packaging_ids.unlink() + self.assertEqual(len(self.template.packaging_tmpl_ids), 1) + self.assertEqual(len(self.template.packaging_tmpl_ids.packaging_ids), 1) + # Delete the template + self.template.packaging_tmpl_ids.unlink() + self.assertFalse(self.variant_1.packaging_ids) + + def test_create_new_variant(self): + self.template.packaging_tmpl_ids = [ + Command.create({"name": "Box of 10", "qty": 10}) + ] + self.assertEqual(len(self.template.packaging_tmpl_ids.packaging_ids), 2) + self.attribute_line.value_ids = [ + Command.link(self.env.ref("product.product_attribute_value_color_wood").id) + ] + self.assertEqual(len(self.template.packaging_tmpl_ids.packaging_ids), 3) diff --git a/product_packaging_template/views/product_packaging_template.xml b/product_packaging_template/views/product_packaging_template.xml new file mode 100644 index 00000000000..12c08b6eec6 --- /dev/null +++ b/product_packaging_template/views/product_packaging_template.xml @@ -0,0 +1,32 @@ + + + + + product.packaging.template + +
+
+ + + + + + + + + + + + + + product.packaging.template + + + + + + + + + diff --git a/product_packaging_template/views/product_template.xml b/product_packaging_template/views/product_template.xml new file mode 100644 index 00000000000..e1e181888bc --- /dev/null +++ b/product_packaging_template/views/product_template.xml @@ -0,0 +1,27 @@ + + + + + product.template + + + + + + + + + +