diff --git a/product_variant_template_reassign/README.rst b/product_variant_template_reassign/README.rst new file mode 100644 index 000000000..db9febe5d --- /dev/null +++ b/product_variant_template_reassign/README.rst @@ -0,0 +1,106 @@ +================================= +Product variant template reassign +================================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:ddd8d22221dce3a8d71dc1c17660f015635e79cf6c4934c6f8fbec84fee1b183 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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--variant-lightgray.png?logo=github + :target: https://github.com/OCA/product-variant/tree/15.0/product_variant_template_reassign + :alt: OCA/product-variant +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/product-variant-15-0/product-variant-15-0-product_variant_template_reassign + :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-variant&target_branch=15.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Be able to assign a unique variant to a multi-variant template. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +The merging options are quite limited when it comes to keep the original +record values. To ensure, that the orignal variant info is preserved, +you can go to the ``product_variant_template_reassign.keep_fields`` +config parameter and add the field to the list. + +Usage +===== + +To assign variants, go to a single variant product template. + +1. In actions, choose: *Reassign variant*. +2. Choose the target product template. +3. Choose the variant attributes if available (otherwise you won't be + able to reassign the variant) +4. Click on *Reassign*. + +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 +------- + +* Tecnativa + +Contributors +------------ + +- `Tecnativa `__ + + - David Vidal + - Pedro M. Baeza + +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-chienandalu| image:: https://github.com/chienandalu.png?size=40px + :target: https://github.com/chienandalu + :alt: chienandalu + +Current `maintainer `__: + +|maintainer-chienandalu| + +This module is part of the `OCA/product-variant `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/product_variant_template_reassign/__init__.py b/product_variant_template_reassign/__init__.py new file mode 100644 index 000000000..5cb1c4914 --- /dev/null +++ b/product_variant_template_reassign/__init__.py @@ -0,0 +1 @@ +from . import wizards diff --git a/product_variant_template_reassign/__manifest__.py b/product_variant_template_reassign/__manifest__.py new file mode 100644 index 000000000..e2fdcc827 --- /dev/null +++ b/product_variant_template_reassign/__manifest__.py @@ -0,0 +1,20 @@ +# Copyright 2024 Tecnativa - David Vidal +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Product variant template reassign", + "summary": "Reassign variants to templates", + "version": "15.0.1.0.0", + "development_status": "Beta", + "category": "Product", + "website": "https://github.com/OCA/product-variant", + "author": "Tecnativa, Odoo Community Association (OCA)", + "maintainers": ["chienandalu"], + "license": "AGPL-3", + "depends": ["product"], + "external_dependencies": {"python": ["openupgradelib"]}, + "data": [ + "data/data.xml", + "security/ir.model.access.csv", + "wizards/reassign_variant_views.xml", + ], +} diff --git a/product_variant_template_reassign/data/data.xml b/product_variant_template_reassign/data/data.xml new file mode 100644 index 000000000..9d49945d8 --- /dev/null +++ b/product_variant_template_reassign/data/data.xml @@ -0,0 +1,13 @@ + + + + product_variant_template_reassign.keep_fields + list_price,standard_price,default_code,barcode,weight,volume + + diff --git a/product_variant_template_reassign/i18n/es.po b/product_variant_template_reassign/i18n/es.po new file mode 100644 index 000000000..02f16dbd3 --- /dev/null +++ b/product_variant_template_reassign/i18n/es.po @@ -0,0 +1,176 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * product_variant_template_reassign +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 15.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2024-11-05 17:35+0000\n" +"PO-Revision-Date: 2024-11-05 18:38+0100\n" +"Last-Translator: \n" +"Language-Team: \n" +"Language: es\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"X-Generator: Poedit 3.4.4\n" + +#. module: product_variant_template_reassign +#: model_terms:ir.ui.view,arch_db:product_variant_template_reassign.reassign_variant_form +msgid "" +" No attributes available for " +"create a new variant in the target template" +msgstr "" +" No hay atributos disponibles para " +"crear una nueva variante en la plantilla de destino" + +#. module: product_variant_template_reassign +#: model_terms:ir.ui.view,arch_db:product_variant_template_reassign.reassign_variant_form +msgid "" +" Reassigning this template variant " +"to the selected target\n" +" can't be undone" +msgstr "" +" Reasignar la variante de esta " +"plantilla al objetivo seleccionado no se puede deshacer" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__allowed_attribute_value_ids +msgid "Allowed Attribute Value" +msgstr "Valor de atributo permitido" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__allowed_target_product_template_ids +msgid "Allowed Target Product Template" +msgstr "Plantilla de producto destino permitida" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__attribute_value_ids +msgid "Attribute Value" +msgstr "Valor de atributo" + +#. module: product_variant_template_reassign +#: model_terms:ir.ui.view,arch_db:product_variant_template_reassign.reassign_variant_form +msgid "Cancel" +msgstr "Cancelar" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__create_uid +msgid "Created by" +msgstr "Creado por" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__create_date +msgid "Created on" +msgstr "Creado el" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__display_name +msgid "Display Name" +msgstr "Nombre mostrado" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__id +msgid "ID" +msgstr "ID (identificación)" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant____last_update +msgid "Last Modified on" +msgstr "Última modificación en" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__write_uid +msgid "Last Updated by" +msgstr "Última actualización de" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__write_date +msgid "Last Updated on" +msgstr "Última actualización en" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__method +msgid "Method" +msgstr "Método" + +#. module: product_variant_template_reassign +#: model:ir.model.fields.selection,name:product_variant_template_reassign.selection__reassign_variant__method__orm +msgid "ORM - Try to respect Odoo's data flows" +msgstr "ORM - Intentar respetar los flujos de datos de Odoo" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__origin_product_template_id +msgid "Origin Product Template" +msgstr "Plantilla de producto de origen" + +#. module: product_variant_template_reassign +#: model_terms:ir.ui.view,arch_db:product_variant_template_reassign.reassign_variant_form +msgid "Reassign" +msgstr "Reasignar" + +#. module: product_variant_template_reassign +#: model:ir.actions.act_window,name:product_variant_template_reassign.reassign_variant_action +msgid "Reassign variant" +msgstr "Reasignar variante" + +#. module: product_variant_template_reassign +#: model:ir.model,name:product_variant_template_reassign.model_reassign_variant +msgid "Reassign variant template" +msgstr "Reasignar plantilla de producto" + +#. module: product_variant_template_reassign +#: model:ir.model.fields.selection,name:product_variant_template_reassign.selection__reassign_variant__method__sql +msgid "SQL - When the first on fails" +msgstr "SQL - Cuando anterior falla" + +#. module: product_variant_template_reassign +#: model_terms:ir.ui.view,arch_db:product_variant_template_reassign.reassign_variant_form +msgid "Select the target template" +msgstr "Seleccionar la plantilla de destino" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__target_product_template_id +msgid "Target Product Template" +msgstr "Plantilla de producto de destino" + +#. module: product_variant_template_reassign +#: code:addons/product_variant_template_reassign/wizards/reassign_variant.py:0 +#, python-format +msgid "" +"The selected attributes didn't generate a variant in the target template" +msgstr "" +"Los atributos seleccionados no han generado una variante en la plantilla de " +"destino" + +#. module: product_variant_template_reassign +#: code:addons/product_variant_template_reassign/wizards/reassign_variant.py:0 +#, python-format +msgid "" +"The selected attributes generate more than one variant. Refine your " +"configuration" +msgstr "" +"Los atributos seleccionados generan más de una variante. Refine su " +"configuración" + +#. module: product_variant_template_reassign +#: model_terms:ir.ui.view,arch_db:product_variant_template_reassign.reassign_variant_form +msgid "" +"This will merge the current template into the selected one and it can't be " +"undone" +msgstr "" +"Estos fusionará la plantilla de origen en la plantilla destino y esto no se " +"puede deshacer" + +#. module: product_variant_template_reassign +#: model_terms:ir.ui.view,arch_db:product_variant_template_reassign.reassign_variant_form +msgid "Variant to move" +msgstr "Variante a mover" + +#. module: product_variant_template_reassign +#: code:addons/product_variant_template_reassign/wizards/reassign_variant.py:0 +#, python-format +msgid "You can only reassign unique variant products" +msgstr "Solo se pueden reasignar plantillas con variante única" diff --git a/product_variant_template_reassign/i18n/product_variant_template_reassign.pot b/product_variant_template_reassign/i18n/product_variant_template_reassign.pot new file mode 100644 index 000000000..7221f547c --- /dev/null +++ b/product_variant_template_reassign/i18n/product_variant_template_reassign.pot @@ -0,0 +1,163 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * product_variant_template_reassign +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 15.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2024-11-05 17:35+0000\n" +"PO-Revision-Date: 2024-11-05 17:35+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: \n" + +#. module: product_variant_template_reassign +#: model_terms:ir.ui.view,arch_db:product_variant_template_reassign.reassign_variant_form +msgid "" +" No attributes available for create" +" a new variant in the target template" +msgstr "" + +#. module: product_variant_template_reassign +#: model_terms:ir.ui.view,arch_db:product_variant_template_reassign.reassign_variant_form +msgid "" +" Reassigning this template variant to the selected target\n" +" can't be undone" +msgstr "" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__allowed_attribute_value_ids +msgid "Allowed Attribute Value" +msgstr "" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__allowed_target_product_template_ids +msgid "Allowed Target Product Template" +msgstr "" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__attribute_value_ids +msgid "Attribute Value" +msgstr "" + +#. module: product_variant_template_reassign +#: model_terms:ir.ui.view,arch_db:product_variant_template_reassign.reassign_variant_form +msgid "Cancel" +msgstr "" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__create_uid +msgid "Created by" +msgstr "" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__create_date +msgid "Created on" +msgstr "" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__display_name +msgid "Display Name" +msgstr "" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__id +msgid "ID" +msgstr "" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant____last_update +msgid "Last Modified on" +msgstr "" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__write_uid +msgid "Last Updated by" +msgstr "" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__write_date +msgid "Last Updated on" +msgstr "" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__method +msgid "Method" +msgstr "" + +#. module: product_variant_template_reassign +#: model:ir.model.fields.selection,name:product_variant_template_reassign.selection__reassign_variant__method__orm +msgid "ORM - Try to respect Odoo's data flows" +msgstr "" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__origin_product_template_id +msgid "Origin Product Template" +msgstr "" + +#. module: product_variant_template_reassign +#: model_terms:ir.ui.view,arch_db:product_variant_template_reassign.reassign_variant_form +msgid "Reassign" +msgstr "" + +#. module: product_variant_template_reassign +#: model:ir.actions.act_window,name:product_variant_template_reassign.reassign_variant_action +msgid "Reassign variant" +msgstr "" + +#. module: product_variant_template_reassign +#: model:ir.model,name:product_variant_template_reassign.model_reassign_variant +msgid "Reassign variant template" +msgstr "" + +#. module: product_variant_template_reassign +#: model:ir.model.fields.selection,name:product_variant_template_reassign.selection__reassign_variant__method__sql +msgid "SQL - When the first on fails" +msgstr "" + +#. module: product_variant_template_reassign +#: model_terms:ir.ui.view,arch_db:product_variant_template_reassign.reassign_variant_form +msgid "Select the target template" +msgstr "" + +#. module: product_variant_template_reassign +#: model:ir.model.fields,field_description:product_variant_template_reassign.field_reassign_variant__target_product_template_id +msgid "Target Product Template" +msgstr "" + +#. module: product_variant_template_reassign +#: code:addons/product_variant_template_reassign/wizards/reassign_variant.py:0 +#, python-format +msgid "" +"The selected attributes didn't generate a variant in the target template" +msgstr "" + +#. module: product_variant_template_reassign +#: code:addons/product_variant_template_reassign/wizards/reassign_variant.py:0 +#, python-format +msgid "" +"The selected attributes generate more than one variant. Refine your " +"configuration" +msgstr "" + +#. module: product_variant_template_reassign +#: model_terms:ir.ui.view,arch_db:product_variant_template_reassign.reassign_variant_form +msgid "" +"This will merge the current template into the selected one and it can't be " +"undone" +msgstr "" + +#. module: product_variant_template_reassign +#: model_terms:ir.ui.view,arch_db:product_variant_template_reassign.reassign_variant_form +msgid "Variant to move" +msgstr "" + +#. module: product_variant_template_reassign +#: code:addons/product_variant_template_reassign/wizards/reassign_variant.py:0 +#, python-format +msgid "You can only reassign unique variant products" +msgstr "" diff --git a/product_variant_template_reassign/readme/CONFIGURE.md b/product_variant_template_reassign/readme/CONFIGURE.md new file mode 100644 index 000000000..2ccfb603d --- /dev/null +++ b/product_variant_template_reassign/readme/CONFIGURE.md @@ -0,0 +1,4 @@ +The merging options are quite limited when it comes to keep the original record values. +To ensure, that the orignal variant info is preserved, you can go to the +`product_variant_template_reassign.keep_fields` config parameter and add the field +to the list. diff --git a/product_variant_template_reassign/readme/CONTRIBUTORS.md b/product_variant_template_reassign/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..1be173bca --- /dev/null +++ b/product_variant_template_reassign/readme/CONTRIBUTORS.md @@ -0,0 +1,3 @@ +- [Tecnativa](https://tecnativa.com) + - David Vidal + - Pedro M. Baeza diff --git a/product_variant_template_reassign/readme/DESCRIPTION.md b/product_variant_template_reassign/readme/DESCRIPTION.md new file mode 100644 index 000000000..840cd59db --- /dev/null +++ b/product_variant_template_reassign/readme/DESCRIPTION.md @@ -0,0 +1 @@ +Be able to assign a unique variant to a multi-variant template. diff --git a/product_variant_template_reassign/readme/USAGE.md b/product_variant_template_reassign/readme/USAGE.md new file mode 100644 index 000000000..a4eca5715 --- /dev/null +++ b/product_variant_template_reassign/readme/USAGE.md @@ -0,0 +1,7 @@ +To assign variants, go to a single variant product template. + +1. In actions, choose: *Reassign variant*. +2. Choose the target product template. +3. Choose the variant attributes if available (otherwise you won't be able to reassign + the variant) +4. Click on *Reassign*. diff --git a/product_variant_template_reassign/security/ir.model.access.csv b/product_variant_template_reassign/security/ir.model.access.csv new file mode 100644 index 000000000..e122b90a7 --- /dev/null +++ b/product_variant_template_reassign/security/ir.model.access.csv @@ -0,0 +1,2 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +product_variant_template_reassign.access_reassign_variant,access_reassign_variant,product_variant_template_reassign.model_reassign_variant,base.group_user,1,1,1,1 diff --git a/product_variant_template_reassign/static/description/index.html b/product_variant_template_reassign/static/description/index.html new file mode 100644 index 000000000..5b4c66317 --- /dev/null +++ b/product_variant_template_reassign/static/description/index.html @@ -0,0 +1,449 @@ + + + + + +Product variant template reassign + + + +
+

Product variant template reassign

+ + +

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

+

Be able to assign a unique variant to a multi-variant template.

+

Table of contents

+ +
+

Configuration

+

The merging options are quite limited when it comes to keep the original +record values. To ensure, that the orignal variant info is preserved, +you can go to the product_variant_template_reassign.keep_fields +config parameter and add the field to the list.

+
+
+

Usage

+

To assign variants, go to a single variant product template.

+
    +
  1. In actions, choose: Reassign variant.
  2. +
  3. Choose the target product template.
  4. +
  5. Choose the variant attributes if available (otherwise you won’t be +able to reassign the variant)
  6. +
  7. Click on Reassign.
  8. +
+
+
+

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

+
    +
  • Tecnativa
  • +
+
+
+

Contributors

+
    +
  • Tecnativa
      +
    • David Vidal
    • +
    • Pedro M. Baeza
    • +
    +
  • +
+
+
+

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 maintainer:

+

chienandalu

+

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

+

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

+
+
+
+ + diff --git a/product_variant_template_reassign/tests/__init__.py b/product_variant_template_reassign/tests/__init__.py new file mode 100644 index 000000000..f86c166af --- /dev/null +++ b/product_variant_template_reassign/tests/__init__.py @@ -0,0 +1 @@ +from . import test_product_variant_reassign diff --git a/product_variant_template_reassign/tests/test_product_variant_reassign.py b/product_variant_template_reassign/tests/test_product_variant_reassign.py new file mode 100644 index 000000000..4f4dea086 --- /dev/null +++ b/product_variant_template_reassign/tests/test_product_variant_reassign.py @@ -0,0 +1,121 @@ +# Copyright 2024 Tecnativa - David Vidal +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import Command +from odoo.tests import Form, TransactionCase + +TEST_IMAGE = ( + b"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8" + b"/wcAAgAB/UTeG8kAAAAASUVORK5CYII=" +) + + +class ProductVariantReassignCase(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.att_color = cls.env["product.attribute"].create({"name": "Color (test)"}) + cls.att_size = cls.env["product.attribute"].create({"name": "Size (test)"}) + cls.att_origin = cls.env["product.attribute"].create( + {"name": "Origin (test)", "create_variant": "no_variant"} + ) + cls.att_color_blue = cls.env["product.attribute.value"].create( + {"name": "Blue", "attribute_id": cls.att_color.id} + ) + cls.att_color_red = cls.env["product.attribute.value"].create( + {"name": "Red", "attribute_id": cls.att_color.id} + ) + cls.att_color_green = cls.env["product.attribute.value"].create( + {"name": "Green", "attribute_id": cls.att_color.id} + ) + cls.att_sice_xxl = cls.env["product.attribute.value"].create( + {"name": "XXL", "attribute_id": cls.att_size.id} + ) + cls.att_origin_nepal = cls.env["product.attribute.value"].create( + {"name": "Nepal", "attribute_id": cls.att_origin.id} + ) + cls.color_scarf = cls.env["product.template"].create( + { + "name": "Test color_scarf", + "list_price": 25.0, + "attribute_line_ids": [ + Command.create( + { + "attribute_id": cls.att_color.id, + "value_ids": [ + Command.set( + [cls.att_color_blue.id, cls.att_color_red.id] + ), + ], + } + ), + Command.create( + { + "attribute_id": cls.att_origin.id, + "value_ids": [ + Command.set([cls.att_origin_nepal.id]), + ], + } + ), + ], + } + ) + cls.green_scarf = cls.env["product.template"].create( + { + "name": "Test color_scarf", + "default_code": "TST-GREEN", + "list_price": 30, + "standard_price": 15, + "image_1920": TEST_IMAGE, + "attribute_line_ids": [ + Command.create( + { + "attribute_id": cls.att_size.id, + "value_ids": [ + Command.set([cls.att_sice_xxl.id]), + ], + } + ), + Command.create( + { + "attribute_id": cls.att_origin.id, + "value_ids": [ + Command.set([cls.att_origin_nepal.id]), + ], + } + ), + ], + } + ) + + def test_reassign_product(self): + existing_color_scarves = self.color_scarf.product_variant_ids + wiz_form = Form( + self.env["reassign.variant"].with_context(active_id=self.green_scarf.id) + ) + self.assertTrue( + self.color_scarf in wiz_form.allowed_target_product_template_ids + ) + wiz_form.target_product_template_id = self.color_scarf + self.assertTrue(self.att_color_green in wiz_form.allowed_attribute_value_ids) + self.assertFalse(self.att_color_red in wiz_form.allowed_attribute_value_ids) + wiz_form.attribute_value_ids.add(self.att_color_green) + wiz = wiz_form.save() + wiz.reassign() + new_color_scarve = self.color_scarf.product_variant_ids - existing_color_scarves + self.assertEqual( + new_color_scarve.product_template_attribute_value_ids.product_attribute_value_id, + self.att_color_green, + ) + self.assertEqual(new_color_scarve.default_code, "TST-GREEN") + self.assertAlmostEqual(new_color_scarve.list_price, 30) + self.assertAlmostEqual(new_color_scarve.standard_price, 15) + self.assertEqual(new_color_scarve.image_variant_1920, TEST_IMAGE) + self.assertFalse(bool(self.green_scarf.exists())) + self.assertTrue( + len( + self.color_scarf.attribute_line_ids.filtered( + lambda x: x.attribute_id == self.att_origin + ) + ) + == 1 + ) diff --git a/product_variant_template_reassign/wizards/__init__.py b/product_variant_template_reassign/wizards/__init__.py new file mode 100644 index 000000000..df7a1ea3f --- /dev/null +++ b/product_variant_template_reassign/wizards/__init__.py @@ -0,0 +1 @@ +from . import reassign_variant diff --git a/product_variant_template_reassign/wizards/reassign_variant.py b/product_variant_template_reassign/wizards/reassign_variant.py new file mode 100644 index 000000000..a3c507268 --- /dev/null +++ b/product_variant_template_reassign/wizards/reassign_variant.py @@ -0,0 +1,209 @@ +# Copyright 2024 Tecnativa - David Vidal +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from openupgradelib.openupgrade_merge_records import merge_records + +from odoo import _, api, fields, models +from odoo.exceptions import UserError + + +class ReassignVariant(models.TransientModel): + _name = "reassign.variant" + _description = "Reassign variant template" + + origin_product_template_id = fields.Many2one(comodel_name="product.template") + allowed_target_product_template_ids = fields.Many2many( + comodel_name="product.template", + compute="_compute_allowed_target_product_template_ids", + ) + target_product_template_id = fields.Many2one( + comodel_name="product.template", + domain="[('id', 'in', allowed_target_product_template_ids)]", + ) + allowed_attribute_value_ids = fields.Many2many( + comodel_name="product.attribute.value", + compute="_compute_allowed_attribute_value_ids", + ) + attribute_value_ids = fields.Many2many( + comodel_name="product.attribute.value", + domain="[('id', 'in', allowed_attribute_value_ids)]", + compute="_compute_attribute_value_ids", + readonly=False, + store=True, + ) + + @api.model + def default_get(self, fields_list): + product_template = self.env["product.template"].browse( + self.env.context.get("active_id", 0) + ) + if not product_template.check_access_rights("create", raise_exception=False): + raise UserError( + _( + "Only users with permissions to create products can reassign variants" + ) + ) + if product_template and product_template.product_variant_count != 1: + raise UserError(_("You can only reassign unique variant products")) + res = super().default_get(fields_list) + if product_template: + res["origin_product_template_id"] = product_template.id + return res + + @api.depends("origin_product_template_id") + def _compute_allowed_target_product_template_ids(self): + self.allowed_target_product_template_ids = False + for wiz in self.filtered("origin_product_template_id"): + wiz.allowed_target_product_template_ids = self.env[ + "product.template" + ].search( + [ + ("type", "=", wiz.origin_product_template_id.type), + ("uom_id", "=", wiz.origin_product_template_id.uom_id.id), + ("id", "!=", wiz.origin_product_template_id.id), + ("attribute_line_ids", "!=", False), + ] + ) + + @api.depends("target_product_template_id") + def _compute_allowed_attribute_value_ids(self): + self.allowed_attribute_value_ids = False + for wiz in self.filtered("target_product_template_id"): + wiz.allowed_attribute_value_ids = ( + self.env["product.attribute.value"].search( + [ + ( + "attribute_id", + "in", + wiz.target_product_template_id.attribute_line_ids.attribute_id.ids, + ), + ("attribute_id.create_variant", "=", "always"), + ] + ) + - wiz.target_product_template_id.attribute_line_ids.value_ids + ) + + @api.depends("target_product_template_id") + def _compute_attribute_value_ids(self): + """Whenever the target changes we must wipe the attribute values""" + self.attribute_value_ids = False + + def _get_field_spec(self) -> dict: + """Override to add merging rules. See openupgradelib _adjust_merged_values_orm + https://github.com/OCA/openupgradelib/blob/8d04b103b70d6927f805b94ef26ecf53b26e51ed/openupgradelib/openupgrade_merge_records.py#L532-L587 # noqa + """ + return { + "default_code": "first_not_null", + } + + def _preprocess_data(self): + """Called before the merge. Some of origin variant data might be problematic. + Override to deal with other issues""" + variant = self.origin_product_template_id.product_variant_id + self.origin_product_template_id.attribute_line_ids.filtered( + lambda x: x.attribute_id.create_variant != "no_variant" + ).unlink() + # Take a snapshot of the original variant data before the merge + original_values = variant.read()[0] + return original_values + + def _clear_duplicated_attributes(self): + """No variant attributes are merged into the resulting template. We need to + get rid of duplicates""" + attribute_lines = self.target_product_template_id.attribute_line_ids.filtered( + lambda x: x.attribute_id.create_variant == "no_variant" + ) + dupes_to_delete = self.env["product.template.attribute.line"] + for attribute in attribute_lines.attribute_id: + att_lines = attribute_lines.filtered(lambda x: x.attribute_id == attribute) + # There's only one... + if len(att_lines) == 1: + continue + definitive_line = att_lines[0] + definitive_line.value_ids = att_lines.value_ids + dupes_to_delete += att_lines - definitive_line + dupes_to_delete.unlink() + + def _postprocess_data(self, new_variant, original_data): + """Called after the merge. Some info might not be merged correctly. Override + here to reset the original data""" + # We need to reload the ORM cache to get rid of old stuff + self.env["product.template"].invalidate_cache() + fields_to_update = ( + self.env["ir.config_parameter"] + .sudo() + .get_param("product_variant_template_reassign.keep_fields") + or "" + ) + vals_to_keep = { + field: original_data[field] + for field in fields_to_update.split(",") + if field in original_data + } + # Ensure that we keep the image + vals_to_keep["image_variant_1920"] = ( + vals_to_keep.get("image_variant_1920") + or original_data.get("image_variant_1920") + or original_data.get("image_1920") + ) + new_variant.update(vals_to_keep) + self._clear_duplicated_attributes() + + def reassign(self): + """No way back! The potential new variant from the selected attributes is + merged with the origin variant""" + # 1. Create variant + existing_variants = self.target_product_template_id.product_variant_ids + for attribute_value in self.attribute_value_ids: + self.target_product_template_id.attribute_line_ids.filtered( + lambda x: x.attribute_id == attribute_value.attribute_id + ).value_ids += attribute_value + new_variant = ( + self.target_product_template_id.product_variant_ids - existing_variants + ) + if not new_variant: + raise UserError( + _( + "The selected attributes didn't generate a variant in the target template" + ) + ) + try: + new_variant.ensure_one() + except ValueError: + raise UserError( + _( + "The selected attributes generate more than one variant. " + "Refine your configuration" + ) + ) from None + # Pre-process potential problematic stuff with the variant + original_values = self._preprocess_data() + # 2. Merge existing variant into the new one. Do it with SQL to avoid ORM locks + merge_records( + self.env, + "product.product", + self.origin_product_template_id.product_variant_id.ids, + new_variant.id, + field_spec=self._get_field_spec(), + method="sql", + ) + # 3. Merge origin template into the target template. Do it with SQL to avoid ORM + # locks. + merge_records( + self.env, + "product.template", + self.origin_product_template_id.ids, + self.target_product_template_id.id, + method="sql", + ) + self._postprocess_data(new_variant, original_values) + body = ( + f'' + f'{new_variant.display_name} {_("reassigned to this template")}' + ) + self.target_product_template_id.message_post(body=body) + return { + "type": "ir.actions.act_window", + "res_model": "product.template", + "view_mode": "form", + "res_id": self.target_product_template_id.id, + } diff --git a/product_variant_template_reassign/wizards/reassign_variant_views.xml b/product_variant_template_reassign/wizards/reassign_variant_views.xml new file mode 100644 index 000000000..8242d7bab --- /dev/null +++ b/product_variant_template_reassign/wizards/reassign_variant_views.xml @@ -0,0 +1,70 @@ + + + + reassign.variant + +
+ + + + + + + + + + + +
+ +
+ +
+
+ + Reassign variant + + reassign.variant + form + new + +
diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 000000000..180fc4978 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +# generated from manifests external_dependencies +openupgradelib diff --git a/setup/product_variant_template_reassign/odoo/addons/product_variant_template_reassign b/setup/product_variant_template_reassign/odoo/addons/product_variant_template_reassign new file mode 120000 index 000000000..59136a85a --- /dev/null +++ b/setup/product_variant_template_reassign/odoo/addons/product_variant_template_reassign @@ -0,0 +1 @@ +../../../../product_variant_template_reassign \ No newline at end of file diff --git a/setup/product_variant_template_reassign/setup.py b/setup/product_variant_template_reassign/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/product_variant_template_reassign/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)