diff --git a/product_restricted_tracking/README.rst b/product_restricted_tracking/README.rst new file mode 100644 index 00000000000..b47f0496fe9 --- /dev/null +++ b/product_restricted_tracking/README.rst @@ -0,0 +1,107 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +=========================== +Product Restricted Tracking +=========================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:b65fa4c533fef8a0750c7dae1be3e2afd44df9ad1f330793cee4d7990fe259f4 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/license-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/16.0/product_restricted_tracking + :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-16-0/product-attribute-16-0-product_restricted_tracking + :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=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module adds a 'restricted_tracking' field to the product category +model. With this optional selection field, you can enforce that all +products in a category must use a specific tracking method (Serial +Number, Lot, or None). + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +To configure this module: + +1. Go to *Inventory > Configuration > Product Categories* +2. Select a category and set a *Restricted Tracking* value (Serial + Number, Lot, or None) +3. When creating or editing products in this category, only the allowed + tracking method can be selected + +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 +------- + +* Quartile + +Contributors +------------ + +- Quartile + + - Aung Ko Ko Lin + +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. + +.. |maintainer-aungkokolin1997| image:: https://github.com/aungkokolin1997.png?size=40px + :target: https://github.com/aungkokolin1997 + :alt: aungkokolin1997 +.. |maintainer-yostashiro| image:: https://github.com/yostashiro.png?size=40px + :target: https://github.com/yostashiro + :alt: yostashiro + +Current `maintainers `__: + +|maintainer-aungkokolin1997| |maintainer-yostashiro| + +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_restricted_tracking/__init__.py b/product_restricted_tracking/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/product_restricted_tracking/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/product_restricted_tracking/__manifest__.py b/product_restricted_tracking/__manifest__.py new file mode 100644 index 00000000000..366931d6305 --- /dev/null +++ b/product_restricted_tracking/__manifest__.py @@ -0,0 +1,17 @@ +# Copyright 2026 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Product Restricted Tracking", + "version": "16.0.1.0.0", + "author": "Quartile, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/product-attribute", + "license": "AGPL-3", + "category": "Product", + "depends": ["stock"], + "data": [ + "views/product_category_views.xml", + ], + "installable": True, + "maintainers": ["aungkokolin1997", "yostashiro"], +} diff --git a/product_restricted_tracking/models/__init__.py b/product_restricted_tracking/models/__init__.py new file mode 100644 index 00000000000..f154bb52867 --- /dev/null +++ b/product_restricted_tracking/models/__init__.py @@ -0,0 +1,2 @@ +from . import product_category +from . import product_template diff --git a/product_restricted_tracking/models/product_category.py b/product_restricted_tracking/models/product_category.py new file mode 100644 index 00000000000..0aadb46d360 --- /dev/null +++ b/product_restricted_tracking/models/product_category.py @@ -0,0 +1,65 @@ +# Copyright 2026 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError + + +class ProductCategory(models.Model): + _inherit = "product.category" + + restricted_tracking = fields.Selection( + selection=lambda self: self.env["product.template"] + ._fields["tracking"] + ._description_selection(self.env), + ) + + @api.constrains("restricted_tracking") + def _check_restricted_tracking(self): + for categ in self: + if not categ.restricted_tracking: + continue + conflicting_products = self.env["product.template"].search( + [ + ("categ_id", "=", categ.id), + ("tracking", "!=", categ.restricted_tracking), + ] + ) + if conflicting_products: + tracking_label = dict( + categ._fields["restricted_tracking"]._description_selection( + self.env + ) + ).get(categ.restricted_tracking) + tracking_selection_dict = dict( + conflicting_products._fields["tracking"]._description_selection( + self.env + ) + ) + total_count = len(conflicting_products) + product_list = "\n".join( + [ + "- %s (%s)" + % ( + product.display_name, + tracking_selection_dict.get(product.tracking), + ) + for product in conflicting_products[:10] + ] + ) + more_info = ( + _("\n... and %s more product(s)", total_count - 10) + if total_count > 10 + else "" + ) + raise ValidationError( + _( + "Cannot set restricted tracking to '%(tracking)s' because " + "%(count)s product(s) in this category have different " + "tracking:\n%(products)s%(more)s", + tracking=tracking_label, + count=total_count, + products=product_list, + more=more_info, + ) + ) diff --git a/product_restricted_tracking/models/product_template.py b/product_restricted_tracking/models/product_template.py new file mode 100644 index 00000000000..60fc0cca498 --- /dev/null +++ b/product_restricted_tracking/models/product_template.py @@ -0,0 +1,31 @@ +# Copyright 2026 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import _, api, models +from odoo.exceptions import ValidationError + + +class ProductTemplate(models.Model): + _inherit = "product.template" + + @api.constrains("tracking", "categ_id") + def _check_tracking_matches_category(self): + tracking_dict = dict(self._fields["tracking"]._description_selection(self.env)) + for product in self: + if ( + product.categ_id.restricted_tracking + and product.tracking != product.categ_id.restricted_tracking + ): + raise ValidationError( + _( + "Product '%(product)s' has tracking '%(tracking)s' but is " + "assigned to category '%(category)s' which requires " + "tracking '%(required_tracking)s'.", + product=product.display_name, + tracking=tracking_dict.get(product.tracking), + category=product.categ_id.display_name, + required_tracking=tracking_dict.get( + product.categ_id.restricted_tracking + ), + ) + ) diff --git a/product_restricted_tracking/readme/CONFIGURE.md b/product_restricted_tracking/readme/CONFIGURE.md new file mode 100644 index 00000000000..89464288234 --- /dev/null +++ b/product_restricted_tracking/readme/CONFIGURE.md @@ -0,0 +1,5 @@ +To configure this module: + +1. Go to *Inventory > Configuration > Product Categories* +2. Select a category and set a *Restricted Tracking* value (Serial Number, Lot, or None) +3. When creating or editing products in this category, only the allowed tracking method can be selected diff --git a/product_restricted_tracking/readme/CONTRIBUTORS.md b/product_restricted_tracking/readme/CONTRIBUTORS.md new file mode 100644 index 00000000000..e451f2005d4 --- /dev/null +++ b/product_restricted_tracking/readme/CONTRIBUTORS.md @@ -0,0 +1,3 @@ +- Quartile \<\> + - Aung Ko Ko Lin + - Sanami Koshita diff --git a/product_restricted_tracking/readme/DESCRIPTION.md b/product_restricted_tracking/readme/DESCRIPTION.md new file mode 100644 index 00000000000..f178ef9af2d --- /dev/null +++ b/product_restricted_tracking/readme/DESCRIPTION.md @@ -0,0 +1,3 @@ +This module adds a 'restricted_tracking' field to the product category model. +With this optional selection field, you can enforce that all products in a +category must use a specific tracking method (Serial Number, Lot, or None). diff --git a/product_restricted_tracking/static/description/index.html b/product_restricted_tracking/static/description/index.html new file mode 100644 index 00000000000..f17487d6593 --- /dev/null +++ b/product_restricted_tracking/static/description/index.html @@ -0,0 +1,449 @@ + + + + + +README.rst + + + +
+ + + +Odoo Community Association + +
+

Product Restricted Tracking

+ +

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

+

This module adds a ‘restricted_tracking’ field to the product category +model. With this optional selection field, you can enforce that all +products in a category must use a specific tracking method (Serial +Number, Lot, or None).

+

Table of contents

+ +
+

Configuration

+

To configure this module:

+
    +
  1. Go to Inventory > Configuration > Product Categories
  2. +
  3. Select a category and set a Restricted Tracking value (Serial +Number, Lot, or None)
  4. +
  5. When creating or editing products in this category, only the allowed +tracking method can be selected
  6. +
+
+
+

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

+
    +
  • Quartile
  • +
+
+
+

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.

+

Current maintainers:

+

aungkokolin1997 yostashiro

+

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_restricted_tracking/tests/__init__.py b/product_restricted_tracking/tests/__init__.py new file mode 100644 index 00000000000..0acac4d7440 --- /dev/null +++ b/product_restricted_tracking/tests/__init__.py @@ -0,0 +1 @@ +from . import test_product_restricted_tracking diff --git a/product_restricted_tracking/tests/test_product_restricted_tracking.py b/product_restricted_tracking/tests/test_product_restricted_tracking.py new file mode 100644 index 00000000000..7141af7d625 --- /dev/null +++ b/product_restricted_tracking/tests/test_product_restricted_tracking.py @@ -0,0 +1,65 @@ +# Copyright 2026 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.exceptions import ValidationError +from odoo.tests.common import TransactionCase + + +class TestProductRestrictedTracking(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.category_serial = cls.env["product.category"].create( + {"name": "Serial Category", "restricted_tracking": "serial"} + ) + cls.category_lot = cls.env["product.category"].create( + {"name": "Lot Category", "restricted_tracking": "lot"} + ) + cls.category_unrestricted = cls.env["product.category"].create( + {"name": "Unrestricted Category"} + ) + cls.product_serial = cls.env["product.template"].create( + { + "name": "Serial Product", + "tracking": "serial", + "categ_id": cls.category_serial.id, + } + ) + + def test_product_mismatch_category_tracking_fails(self): + """Product with mismatched tracking raises validation error.""" + with self.assertRaises(ValidationError): + self.env["product.template"].create( + { + "name": "Lot Product", + "tracking": "lot", + "categ_id": self.category_serial.id, + } + ) + with self.assertRaises(ValidationError): + self.product_serial.tracking = "lot" + with self.assertRaises(ValidationError): + self.product_serial.categ_id = self.category_lot + + def test_unrestricted_category_allows_any_tracking(self): + """Unrestricted category accepts any tracking value.""" + product = self.env["product.template"].create( + { + "name": "Lot Product", + "tracking": "lot", + "categ_id": self.category_unrestricted.id, + } + ) + self.assertEqual(product.tracking, "lot") + product.tracking = "serial" + self.assertEqual(product.tracking, "serial") + product.tracking = "none" + self.assertEqual(product.tracking, "none") + + def test_category_restriction_change_with_conflicting_products_fails(self): + with self.assertRaises(ValidationError): + self.category_serial.restricted_tracking = "lot" + + def test_category_restriction_clear_succeeds(self): + self.category_serial.restricted_tracking = False + self.assertFalse(self.category_serial.restricted_tracking) diff --git a/product_restricted_tracking/views/product_category_views.xml b/product_restricted_tracking/views/product_category_views.xml new file mode 100644 index 00000000000..96632c1f84d --- /dev/null +++ b/product_restricted_tracking/views/product_category_views.xml @@ -0,0 +1,13 @@ + + + + product.category.form.restricted.tracking + product.category + + + + + + + + diff --git a/setup/product_restricted_tracking/odoo/addons/product_restricted_tracking b/setup/product_restricted_tracking/odoo/addons/product_restricted_tracking new file mode 120000 index 00000000000..ca148bbd8bb --- /dev/null +++ b/setup/product_restricted_tracking/odoo/addons/product_restricted_tracking @@ -0,0 +1 @@ +../../../../product_restricted_tracking \ No newline at end of file diff --git a/setup/product_restricted_tracking/setup.py b/setup/product_restricted_tracking/setup.py new file mode 100644 index 00000000000..28c57bb6403 --- /dev/null +++ b/setup/product_restricted_tracking/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)