diff --git a/product_attribute_custom_value_variant/README.rst b/product_attribute_custom_value_variant/README.rst new file mode 100644 index 00000000000..7285692685d --- /dev/null +++ b/product_attribute_custom_value_variant/README.rst @@ -0,0 +1,116 @@ +======================================== +Create product variant from custom value +======================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:cd9b2a07b3d2aa2243eed929758b4c9b4c69c058aa5772d09cb411de2d0ca13d + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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_attribute_custom_value_variant + :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_attribute_custom_value_variant + :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| + +Create variants when configured custom attributes are used. + +The created variants are useful to retrieve the stock quantity or for +purchase. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +In any custom attribute value, enable Create custom variant. + +Usage +===== + +When a product template is added to a sale order line, the configured +custom values become real attribute values. Here is an example: + +1. Create an attribute "Length (cm)" +2. Add some values to the attribute: + + - 5 + - 10 + - Custom + +3. For the "Custom" value, enable "Free text" and "Create custom + variant" (added by this module) +4. Create a product template "Glass" having the created attribute and + all its values +5. Create a sale order that includes the product template +6. Set the value 42 for the attribute "Length (cm)" + +With the above steps, this module creates: + +- | A new attribute value "42" for the attribute "Length (cm)" + | This attribute value is not linked to the product template, so it + does not appear as a choice when the template is sold. + +- A variant for the template, linked to the created attribute value + +The created variant can then be used to retrieve its stock quantity or +to purchase that exact product variant that had been sold. + +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 +------- + +* Aion Tech + +Contributors +------------ + +- Simone Rubino - Aion Tech +- Denis Roussel denis.roussel@acsone.eu + +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_attribute_custom_value_variant/__init__.py b/product_attribute_custom_value_variant/__init__.py new file mode 100644 index 00000000000..31660d6a965 --- /dev/null +++ b/product_attribute_custom_value_variant/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import models diff --git a/product_attribute_custom_value_variant/__manifest__.py b/product_attribute_custom_value_variant/__manifest__.py new file mode 100644 index 00000000000..fd3c47e3806 --- /dev/null +++ b/product_attribute_custom_value_variant/__manifest__.py @@ -0,0 +1,18 @@ +# Copyright 2025 Simone Rubino - Aion Tech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Create product variant from custom value", + "summary": "When a custom value is assigned, create a variant instead.", + "version": "18.0.1.0.0", + "website": "https://github.com/OCA/product-attribute", + "author": "Aion Tech, Odoo Community Association (OCA)", + "license": "AGPL-3", + "category": "Sales/Sales", + "depends": [ + "product", + ], + "data": [ + "views/product_attribute_value_views.xml", + ], +} diff --git a/product_attribute_custom_value_variant/models/__init__.py b/product_attribute_custom_value_variant/models/__init__.py new file mode 100644 index 00000000000..0a816426ab4 --- /dev/null +++ b/product_attribute_custom_value_variant/models/__init__.py @@ -0,0 +1,6 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import product_attribute_value +from . import product_attribute_custom_value +from . import product_attribute +from . import product_product diff --git a/product_attribute_custom_value_variant/models/product_attribute.py b/product_attribute_custom_value_variant/models/product_attribute.py new file mode 100644 index 00000000000..45c99309ecb --- /dev/null +++ b/product_attribute_custom_value_variant/models/product_attribute.py @@ -0,0 +1,35 @@ +# Copyright 2025 Simone Rubino - Aion Tech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + + +class ProductAttribute(models.Model): + _inherit = "product.attribute" + + def _prepare_variant_custom_attribute_value(self, custom_value): + self.ensure_one() + return { + "attribute_id": self.id, + "name": custom_value, + } + + def _get_variant_custom_attribute_value(self, custom_value): + self.ensure_one() + new_attribute_value = self.env["product.attribute.value"].search( + [ + ( + "attribute_id", + "=", + self.id, + ), + ("name", "=", custom_value), + ], + limit=1, + ) + if not new_attribute_value: + new_attribute_value = self.env["product.attribute.value"].create( + self._prepare_variant_custom_attribute_value(custom_value) + ) + + return new_attribute_value diff --git a/product_attribute_custom_value_variant/models/product_attribute_custom_value.py b/product_attribute_custom_value_variant/models/product_attribute_custom_value.py new file mode 100644 index 00000000000..18fc3575cb1 --- /dev/null +++ b/product_attribute_custom_value_variant/models/product_attribute_custom_value.py @@ -0,0 +1,41 @@ +# Copyright 2025 Simone Rubino - Aion Tech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + +from odoo.addons.product.models.product_attribute_custom_value import ( + ProductAttributeCustomValue, +) +from odoo.addons.product.models.product_attribute_value import ProductAttributeValue + + +class ProductAttribute(models.Model): + _inherit = "product.attribute.custom.value" + + def _get_new_and_unlink_custom_attribute( + self, + ) -> tuple[ProductAttributeValue, ProductAttributeCustomValue]: + """ + Retrieve the new custom attribute from custom value and + the one to remove + """ + values_to_unlink = self.env["product.attribute.custom.value"].browse() + new_attribute_values = self.env["product.attribute.value"].browse() + # Create new attribute values for each "Create custom variant" custom value + for custom_attribute_value in self: + template_attribute_value = ( + custom_attribute_value.custom_product_template_attribute_value_id + ) + attribute_value = template_attribute_value.product_attribute_value_id + if attribute_value.create_custom_variant: + attribute = template_attribute_value.attribute_id + new_attribute_value = attribute._get_variant_custom_attribute_value( + custom_attribute_value.custom_value, + ) + new_attribute_values |= new_attribute_value + template_attribute_value.attribute_line_id.value_ids |= ( + new_attribute_value + ) + + values_to_unlink |= custom_attribute_value + return new_attribute_values, values_to_unlink diff --git a/product_attribute_custom_value_variant/models/product_attribute_value.py b/product_attribute_custom_value_variant/models/product_attribute_value.py new file mode 100644 index 00000000000..3bae623a92a --- /dev/null +++ b/product_attribute_custom_value_variant/models/product_attribute_value.py @@ -0,0 +1,26 @@ +# Copyright 2025 Simone Rubino - Aion Tech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError + + +class ProductAttributeValue(models.Model): + _inherit = "product.attribute.value" + + create_custom_variant = fields.Boolean( + string="Create custom variant", + help="When this custom attribute is used, \ + a new value and variant will be generated.", + ) + + @api.constrains( + "create_custom_variant", + "is_custom", + ) + def _constrain_create_custom_variant(self): + for value in self: + if value.create_custom_variant and not value.is_custom: + raise ValidationError( + _("'Create custom variant' can only be set on custom values") + ) diff --git a/product_attribute_custom_value_variant/models/product_product.py b/product_attribute_custom_value_variant/models/product_product.py new file mode 100644 index 00000000000..2f9677843b4 --- /dev/null +++ b/product_attribute_custom_value_variant/models/product_product.py @@ -0,0 +1,65 @@ +# Copyright 2025 Simone Rubino - Aion Tech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from collections.abc import Generator +from contextlib import contextmanager + +from odoo import models + +from odoo.addons.product.models.product_product import ProductProduct as ProductBase + + +class ProductProduct(models.Model): + _inherit = "product.product" + + @contextmanager + def _get_attribute_custom_value_variant( + self, custom_values + ) -> Generator[ProductBase | None]: + """ + This will retrieve the new created variant as context manager return. + + At the end, it will: + + - Remove the attributes + - Remove the + """ + + new_attribute_values, custom_values_to_unlink = ( + custom_values._get_new_and_unlink_custom_attribute() + ) + # If new attribute values have been created, + # use them to create a new variant and set it in the line + attribute_lines = self.product_tmpl_id.attribute_line_ids + custom_combination = attribute_lines.product_template_value_ids.filtered( + lambda ptav, new=new_attribute_values: ptav.product_attribute_value_id + in new + ) + if custom_combination: + new_variant = self._create_custom_attribute_combination(custom_combination) + yield new_variant + # The new attribute values must not be available for new models + for attribute_line in attribute_lines: + attribute_line.with_context( + no_remove_custom_variants=new_variant.ids, + ).value_ids -= attribute_line.value_ids & new_attribute_values + custom_values_to_unlink.unlink() + + def _create_custom_attribute_combination(self, custom_combination): + self.ensure_one() + variant_combination = self.product_template_attribute_value_ids + new_variant_combination = ( + variant_combination.filtered( + lambda ptav: not ptav.product_attribute_value_id.create_custom_variant + ) + | custom_combination + ) + new_variant = self.product_tmpl_id._create_product_variant( + new_variant_combination + ) + return new_variant + + def _unlink_or_archive(self, check_access=True): + custom_variants_to_keep_ids = self.env.context.get("no_remove_custom_variants") + return super( + ProductProduct, self - self.browse(custom_variants_to_keep_ids) + )._unlink_or_archive(check_access=check_access) diff --git a/product_attribute_custom_value_variant/pyproject.toml b/product_attribute_custom_value_variant/pyproject.toml new file mode 100644 index 00000000000..4231d0cccb3 --- /dev/null +++ b/product_attribute_custom_value_variant/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/product_attribute_custom_value_variant/readme/CONFIGURE.md b/product_attribute_custom_value_variant/readme/CONFIGURE.md new file mode 100644 index 00000000000..7dad13f2098 --- /dev/null +++ b/product_attribute_custom_value_variant/readme/CONFIGURE.md @@ -0,0 +1 @@ +In any custom attribute value, enable Create custom variant. diff --git a/product_attribute_custom_value_variant/readme/CONTRIBUTORS.md b/product_attribute_custom_value_variant/readme/CONTRIBUTORS.md new file mode 100644 index 00000000000..2f7c3ea00ab --- /dev/null +++ b/product_attribute_custom_value_variant/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- Simone Rubino - Aion Tech +- Denis Roussel diff --git a/product_attribute_custom_value_variant/readme/DESCRIPTION.md b/product_attribute_custom_value_variant/readme/DESCRIPTION.md new file mode 100644 index 00000000000..1aa7d33e0e8 --- /dev/null +++ b/product_attribute_custom_value_variant/readme/DESCRIPTION.md @@ -0,0 +1,4 @@ +Create variants when configured custom attributes are used. + +The created variants are useful to retrieve the stock quantity or for +purchase. diff --git a/product_attribute_custom_value_variant/readme/USAGE.md b/product_attribute_custom_value_variant/readme/USAGE.md new file mode 100644 index 00000000000..00e3397d4be --- /dev/null +++ b/product_attribute_custom_value_variant/readme/USAGE.md @@ -0,0 +1,25 @@ +When a product template is added to a sale order line, the configured +custom values become real attribute values. Here is an example: + +1. Create an attribute "Length (cm)" +2. Add some values to the attribute: + - 5 + - 10 + - Custom +3. For the "Custom" value, enable "Free text" and "Create custom variant" + (added by this module) +4. Create a product template "Glass" having the created attribute and + all its values +5. Create a sale order that includes the product template +6. Set the value 42 for the attribute "Length (cm)" + +With the above steps, this module creates: + +- A new attribute value "42" for the attribute "Length (cm)" + This attribute value is not linked to the product template, so it does + not appear as a choice when the template is sold. + +- A variant for the template, linked to the created attribute value + +The created variant can then be used to retrieve its stock quantity or +to purchase that exact product variant that had been sold. diff --git a/product_attribute_custom_value_variant/static/description/index.html b/product_attribute_custom_value_variant/static/description/index.html new file mode 100644 index 00000000000..682d89513d6 --- /dev/null +++ b/product_attribute_custom_value_variant/static/description/index.html @@ -0,0 +1,465 @@ + + + + + +Create product variant from custom value + + + +
+

Create product variant from custom value

+ + +

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

+

Create variants when configured custom attributes are used.

+

The created variants are useful to retrieve the stock quantity or for +purchase.

+

Table of contents

+ +
+

Configuration

+

In any custom attribute value, enable Create custom variant.

+
+
+

Usage

+

When a product template is added to a sale order line, the configured +custom values become real attribute values. Here is an example:

+
    +
  1. Create an attribute “Length (cm)”
  2. +
  3. Add some values to the attribute:
      +
    • 5
    • +
    • 10
    • +
    • Custom
    • +
    +
  4. +
  5. For the “Custom” value, enable “Free text” and “Create custom +variant” (added by this module)
  6. +
  7. Create a product template “Glass” having the created attribute and +all its values
  8. +
  9. Create a sale order that includes the product template
  10. +
  11. Set the value 42 for the attribute “Length (cm)”
  12. +
+

With the above steps, this module creates:

+
    +
  • +
    A new attribute value “42” for the attribute “Length (cm)”
    +
    This attribute value is not linked to the product template, so it +does not appear as a choice when the template is sold.
    +
    +
  • +
  • A variant for the template, linked to the created attribute value

    +
  • +
+

The created variant can then be used to retrieve its stock quantity or +to purchase that exact product variant that had been sold.

+
+
+

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

+
    +
  • Aion Tech
  • +
+
+
+

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_attribute_custom_value_variant/tests/__init__.py b/product_attribute_custom_value_variant/tests/__init__.py new file mode 100644 index 00000000000..635cac74704 --- /dev/null +++ b/product_attribute_custom_value_variant/tests/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import test_product diff --git a/product_attribute_custom_value_variant/tests/common.py b/product_attribute_custom_value_variant/tests/common.py new file mode 100644 index 00000000000..d6750c1ed86 --- /dev/null +++ b/product_attribute_custom_value_variant/tests/common.py @@ -0,0 +1,37 @@ +# Copyright 2025 Simone Rubino - Aion Tech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo.tests import Form + +from odoo.addons.base.tests.common import BaseCommon + + +class AttributeValueVariantCommon(BaseCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.customer = cls.env["res.partner"].create( + { + "name": "Test customer", + } + ) + + length_attribute_form = Form(cls.env["product.attribute"]) + length_attribute_form.name = "Test length attribute" + with length_attribute_form.value_ids.new() as value: + value.name = "5" + with length_attribute_form.value_ids.new() as value: + value.name = "10" + with length_attribute_form.value_ids.new() as value: + value.name = "Custom" + value.is_custom = True + value.create_custom_variant = True + cls.length_attribute = length_attribute_form.save() + + glass_product_template_form = Form(cls.env["product.template"]) + glass_product_template_form.name = "Glass" + lines = glass_product_template_form.attribute_line_ids + with lines.new() as length_attribute_line: + length_attribute_line.attribute_id = cls.length_attribute + for value in cls.length_attribute.value_ids: + length_attribute_line.value_ids.add(value) + cls.glass_product_template = glass_product_template_form.save() diff --git a/product_attribute_custom_value_variant/tests/test_product.py b/product_attribute_custom_value_variant/tests/test_product.py new file mode 100644 index 00000000000..487491841cb --- /dev/null +++ b/product_attribute_custom_value_variant/tests/test_product.py @@ -0,0 +1,52 @@ +# Copyright 2025 Simone Rubino - Aion Tech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo.exceptions import ValidationError +from odoo.tests import Form + +from .common import AttributeValueVariantCommon + + +class TestSale(AttributeValueVariantCommon): + def test_creation(self): + product_template = self.glass_product_template + attribute = self.length_attribute + attribute_values = attribute.value_ids + attribute_value = attribute_values.filtered("create_custom_variant") + template_value = ( + product_template.attribute_line_ids.product_template_value_ids.filtered( + lambda ptav: ptav.product_attribute_value_id == attribute_value + ) + ) + product_variants = product_template.product_variant_ids + custom_product_variant = product_variants.filtered( + lambda variant: template_value + in variant.product_template_attribute_value_ids + ) + # pre-condition + self.assertTrue(template_value.product_attribute_value_id.create_custom_variant) + + # This will simulate the sale behavior that creates custom value at + # sale order line creation + custom_value = self.env["product.attribute.custom.value"].create( + { + "custom_product_template_attribute_value_id": template_value.id, + "custom_value": "TEST VALUE", + } + ) + + with custom_product_variant._get_attribute_custom_value_variant( + custom_value + ) as new_variant: + variant = new_variant + + self.assertNotIn(variant, product_variants) + + def test_custom_value_error(self): + length_attribute_form = Form(self.env["product.attribute"]) + length_attribute_form.name = "Test custom option attribute" + with length_attribute_form.value_ids.new() as value: + value.name = "Custom" + value.is_custom = False + value.create_custom_variant = True + with self.assertRaises(ValidationError): + self.length_attribute = length_attribute_form.save() diff --git a/product_attribute_custom_value_variant/views/product_attribute_value_views.xml b/product_attribute_custom_value_variant/views/product_attribute_value_views.xml new file mode 100644 index 00000000000..f92d9d8f1d2 --- /dev/null +++ b/product_attribute_custom_value_variant/views/product_attribute_value_views.xml @@ -0,0 +1,21 @@ + + + + + Add variant creation to attribute values + product.attribute + + + + + + + +