-
-
Notifications
You must be signed in to change notification settings - Fork 714
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] product_category_code_unique: restriction options
- Loading branch information
1 parent
2e4ac76
commit d7949da
Showing
11 changed files
with
414 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,22 @@ If no code is provided, a code is generated by a sequence. | |
.. contents:: | ||
:local: | ||
|
||
Configuration | ||
============= | ||
|
||
There are 3 restriction options, which can be set from Settings > General Settings > Category Code Unique, under the "Product Category Code" section. | ||
|
||
#. **Whole system**. Product category codes must be unique within the whole system. | ||
#. **Parent-Children**. Product category code for a given product category must be unique considering its parent category and its child categories. | ||
#. **Category Hierarchy**. Product category codes must be unique within the same categories hierarchy. | ||
|
||
Usage | ||
===== | ||
|
||
All product category codes are checked every time the category restriction is changed. An error message is shown when it is not possible to change the restriction because some codes are duplicated depending on the restriction selected. | ||
|
||
If no code is manually set in a product category, one is automatically assigned following a sequence. | ||
|
||
Bug Tracker | ||
=========== | ||
|
||
|
@@ -59,6 +75,7 @@ Contributors | |
|
||
* Denis Roussel <[email protected]> | ||
* Rolando Duarte <[email protected]> | ||
* Manuel Regidor <[email protected]> | ||
|
||
Maintainers | ||
~~~~~~~~~~~ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from . import product_category | ||
from . import res_config_settings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,73 @@ | ||
# Copyright 2021 ACSONE SA/NV | ||
# Copyright 2024 Manuel Regidor <[email protected]> | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
|
||
from odoo import api, models | ||
from odoo import _, api, models | ||
from odoo.exceptions import ValidationError | ||
|
||
|
||
class ProductCategory(models.Model): | ||
|
||
_inherit = "product.category" | ||
|
||
_sql_constraints = [ | ||
("uniq_code", "unique(code)", "The category code must be unique!"), | ||
] | ||
def _get_parents(self): | ||
self.ensure_one() | ||
return self.search([("parent_id", "parent_of", self.id)]) | ||
|
||
def _get_children(self): | ||
self.ensure_one() | ||
return self.search([("parent_id", "child_of", self.id)]) | ||
|
||
def _get_hierarchy_categories_for_code_unique(self): | ||
self.ensure_one() | ||
return self._get_parents() + self._get_children() | ||
|
||
def _code_restriction(self, restriction=False): | ||
restriction = restriction or self.env["ir.config_parameter"].get_param( | ||
"product_code_unique.product_code_unique_restriction" | ||
) | ||
if restriction: | ||
for cat in self: | ||
domain = [] | ||
within = "" | ||
if restriction == "system": | ||
domain = [("code", "=", cat.code)] | ||
within = _("the system") | ||
elif restriction == "direct": | ||
to_check = cat.parent_id + cat.child_id | ||
domain = [ | ||
"|", | ||
("id", "=", cat.id), | ||
"&", | ||
("code", "=", cat.code), | ||
("id", "in", to_check.ids), | ||
] | ||
within = _("parent and children") | ||
elif restriction == "hierarchy": | ||
to_check = cat._get_hierarchy_categories_for_code_unique() | ||
domain = [("code", "=", cat.code), ("id", "in", to_check.ids)] | ||
within = _("category hierarchy") | ||
if self.search_count(domain) > 1: | ||
raise ValidationError( | ||
_("The category code must be unique within {within}!").format( | ||
within=within | ||
) | ||
) | ||
|
||
@api.constrains("code", "parent_id", "child_id") | ||
def _check_code(self): | ||
self._code_restriction() | ||
|
||
@api.model | ||
def _get_next_code(self): | ||
return self.env["ir.sequence"].next_by_code("product.category") | ||
|
||
@api.model | ||
def create(self, vals): | ||
if "code" not in vals or vals["code"] == "/": | ||
vals["code"] = self._get_next_code() | ||
return super().create(vals) | ||
@api.model_create_multi | ||
def create(self, vals_list): | ||
for vals in vals_list: | ||
if "code" not in vals or vals["code"] == "/": | ||
vals["code"] = self._get_next_code() | ||
return super().create(vals_list) | ||
|
||
def write(self, vals): | ||
for category in self: | ||
|
36 changes: 36 additions & 0 deletions
36
product_category_code_unique/models/res_config_settings.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Copyright 2023 ACSONE SA/NV | ||
# Copyright 2024 Manuel Regidor <[email protected]> | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
||
from odoo import api, fields, models | ||
|
||
|
||
class ResConfigSettings(models.TransientModel): | ||
|
||
_inherit = "res.config.settings" | ||
|
||
product_cat_code_unique_restriction = fields.Selection( | ||
[ | ||
("system", "Whole System"), | ||
("direct", "Parent-Children"), | ||
("hierarchy", "Category Hierarchy"), | ||
], | ||
string="Product Category Code Uniqueness Restriction", | ||
config_parameter="product_code_unique.product_code_unique_restriction", | ||
help=( | ||
"If no option is selected, no restriction applies.\n" | ||
"If you select:\n" | ||
"- Whole Sytem: Product Category Codes cannot be duplicated within the " | ||
" whole system\n" | ||
"- Parent-Children: Parent and Children Product Category Codes of the" | ||
" same category cannot be duplicated.\n" | ||
"- Category Hierarchy: Product Category Codes cannot be duplicated " | ||
"within the same category hierarchy.\n" | ||
), | ||
) | ||
|
||
@api.constrains("product_cat_code_unique_restriction") | ||
def _check_product_cat_code_unique_restriction(self): | ||
self.env["product.category"].search([])._code_restriction( | ||
self.product_cat_code_unique_restriction | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
There are 3 restriction options, which can be set from Settings > General Settings > Category Code Unique, under the "Product Category Code" section. | ||
|
||
#. **Whole system**. Product category codes must be unique within the whole system. | ||
#. **Parent-Children**. Product category code for a given product category must be unique considering its parent category and its child categories. | ||
#. **Category Hierarchy**. Product category codes must be unique within the same categories hierarchy. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
* Denis Roussel <[email protected]> | ||
* Rolando Duarte <[email protected]> | ||
* Manuel Regidor <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
All product category codes are checked every time the category restriction is changed. An error message is shown when it is not possible to change the restriction because some codes are duplicated depending on the restriction selected. | ||
|
||
If no code is manually set in a product category, one is automatically assigned following a sequence. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.