diff --git a/product_internal_reference_generator/README.rst b/product_internal_reference_generator/README.rst
index 3e885c42a998..aa4e5fd8a1e2 100644
--- a/product_internal_reference_generator/README.rst
+++ b/product_internal_reference_generator/README.rst
@@ -38,11 +38,16 @@ Internal Reference Prefix + progressive number for variant, eg: “Main0001001
"001" the variant identifier.
-
Every time a new variant is created, a new internal reference is automatically assigned with progressive variant code.
+A general setting allows to set the default Internal Reference Template for new products.
+
+Two specific access rights allow specific users to:
+
+- generate an internal reference in product
+
+- create internal reference templates, change internal reference template for a product template once an internal reference has been generated, as well as editing existing internal references.
-A specific access rights allows specific users to change internal reference template for a product template once an internal reference has been generated, as well as editing existing ones.
**Table of contents**
@@ -52,7 +57,7 @@ A specific access rights allows specific users to change internal reference temp
Usage
=====
-Go to inventory > Configuration > Internal Reference Templates and set:
+Go to Inventory > Configuration > Internal Reference Templates and set:
the sequence to be used for Internal Reference Prefix generation
@@ -62,6 +67,14 @@ Now go to product template, select an Internal Reference Template and generate a
Each time a new variant is created for this product template, an internal reference is automatically assigned.
+In settings > inventory > Default Internal Reference Template, select which template will be loaded automatically each time a new product template is created.
+
+In user, enable extra rights:
+
+- "Generate internal reference" to display "Generate" button in product template.
+
+- "Internal reference template manager" to manage Internal Reference Templates in inventory > configuration menu and edit internal reference templates already assigned to a product template.
+
Known issues / Roadmap
======================
diff --git a/product_internal_reference_generator/__manifest__.py b/product_internal_reference_generator/__manifest__.py
index c954c9e1cb77..590d114960ae 100644
--- a/product_internal_reference_generator/__manifest__.py
+++ b/product_internal_reference_generator/__manifest__.py
@@ -12,6 +12,7 @@
"security/groups.xml",
"security/ir.model.access.csv",
"views/product.xml",
+ "views/res_config_settings.xml",
],
"demo": ["demo/product_code_seq_demo.xml"],
"installable": True,
diff --git a/product_internal_reference_generator/models/__init__.py b/product_internal_reference_generator/models/__init__.py
index 46826c77a7ed..aa91d00e330e 100644
--- a/product_internal_reference_generator/models/__init__.py
+++ b/product_internal_reference_generator/models/__init__.py
@@ -1,3 +1,5 @@
from . import product_code_sequence
from . import product_template
from . import product_product
+from . import res_company
+from . import res_config_settings
diff --git a/product_internal_reference_generator/models/product_code_sequence.py b/product_internal_reference_generator/models/product_code_sequence.py
index 80fd7aac5d67..6bccd8720038 100644
--- a/product_internal_reference_generator/models/product_code_sequence.py
+++ b/product_internal_reference_generator/models/product_code_sequence.py
@@ -1,12 +1,28 @@
# Copyright 2023 Ooops - Ilyas
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
-from odoo import fields, models
+from odoo import _, exceptions, fields, models
class ProductCodeSequence(models.Model):
_name = "product.code.sequence"
_description = "Internal Reference Template"
+ active = fields.Boolean(default=True)
name = fields.Char(required=True)
sequence_id = fields.Many2one("ir.sequence", required=True)
variant_reference_numbers = fields.Integer("Digits", default=3, required=True)
+
+ def unlink(self):
+ for rec in self:
+ products = self.env["product.template"].search(
+ [("int_ref_template_id", "=", rec.id)]
+ )
+ if products:
+ raise exceptions.ValidationError(
+ _(
+ "You can't delete %s template because there is products "
+ "related to it. You can archive it instead."
+ " Products: %s" % (rec.name, products.mapped("display_name"))
+ )
+ )
+ return super().unlink()
diff --git a/product_internal_reference_generator/models/product_template.py b/product_internal_reference_generator/models/product_template.py
index 54b5a575c488..123cca2d8e09 100644
--- a/product_internal_reference_generator/models/product_template.py
+++ b/product_internal_reference_generator/models/product_template.py
@@ -7,12 +7,13 @@ class ProductTemplate(models.Model):
_inherit = "product.template"
int_ref_template_id = fields.Many2one(
- "product.code.sequence", "Internal Reference Template"
+ "product.code.sequence", "Internal Reference Template", copy=True
)
- variants_sequence_id = fields.Many2one("ir.sequence")
+ variants_sequence_id = fields.Many2one("ir.sequence", copy=False)
variants_prefix = fields.Char(
- "Internal Reference Prefix", readonly=True, tracking=True
+ "Internal Reference Prefix", readonly=True, tracking=True, copy=False
)
+ default_code = fields.Char(copy=False)
@api.onchange("int_ref_template_id")
def onchange_int_ref_template_id(self):
@@ -21,11 +22,15 @@ def onchange_int_ref_template_id(self):
def btn_generate_sequence(self):
self.ensure_one()
int_ref_next_val = self.int_ref_template_id.sequence_id.next_by_id()
- var_seq = self.env["ir.sequence"].create(
- {
- "name": "variants " + int_ref_next_val,
- "padding": self.int_ref_template_id.variant_reference_numbers,
- }
+ var_seq = (
+ self.env["ir.sequence"]
+ .sudo()
+ .create(
+ {
+ "name": "variants " + int_ref_next_val,
+ "padding": self.int_ref_template_id.variant_reference_numbers,
+ }
+ )
)
self.write(
{
diff --git a/product_internal_reference_generator/models/res_company.py b/product_internal_reference_generator/models/res_company.py
new file mode 100644
index 000000000000..f64609165367
--- /dev/null
+++ b/product_internal_reference_generator/models/res_company.py
@@ -0,0 +1,14 @@
+# Copyright 2023 Ooops - Ilyas
+# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl)
+
+from odoo import fields, models
+
+
+class ResCompany(models.Model):
+ _inherit = "res.company"
+
+ default_int_ref_template_id = fields.Many2one(
+ "product.code.sequence",
+ default_model="product.template",
+ string="Default Internal Reference Template",
+ )
diff --git a/product_internal_reference_generator/models/res_config_settings.py b/product_internal_reference_generator/models/res_config_settings.py
new file mode 100644
index 000000000000..902f9729f3fd
--- /dev/null
+++ b/product_internal_reference_generator/models/res_config_settings.py
@@ -0,0 +1,13 @@
+# Copyright 2023 Ooops - Ilyas
+# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl)
+
+from odoo import fields, models
+
+
+class ResConfigSettings(models.TransientModel):
+ _inherit = "res.config.settings"
+
+ default_int_ref_template_id = fields.Many2one(
+ related="company_id.default_int_ref_template_id",
+ readonly=False,
+ )
diff --git a/product_internal_reference_generator/readme/DESCRIPTION.rst b/product_internal_reference_generator/readme/DESCRIPTION.rst
index d1a21cb27901..363896f4bcbf 100644
--- a/product_internal_reference_generator/readme/DESCRIPTION.rst
+++ b/product_internal_reference_generator/readme/DESCRIPTION.rst
@@ -8,8 +8,13 @@ Internal Reference Prefix + progressive number for variant, eg: “Main0001001
"001" the variant identifier.
-
Every time a new variant is created, a new internal reference is automatically assigned with progressive variant code.
+A general setting allows to set the default Internal Reference Template for new products.
+
+Two specific access rights allow specific users to:
+
+- generate an internal reference in product
+
+- create internal reference templates, change internal reference template for a product template once an internal reference has been generated, as well as editing existing internal references.
-A specific access rights allows specific users to change internal reference template for a product template once an internal reference has been generated, as well as editing existing ones.
diff --git a/product_internal_reference_generator/readme/USAGE.rst b/product_internal_reference_generator/readme/USAGE.rst
index a53d8f2307d8..a6811c50b1b3 100644
--- a/product_internal_reference_generator/readme/USAGE.rst
+++ b/product_internal_reference_generator/readme/USAGE.rst
@@ -1,4 +1,4 @@
-Go to inventory > Configuration > Internal Reference Templates and set:
+Go to Inventory > Configuration > Internal Reference Templates and set:
the sequence to be used for Internal Reference Prefix generation
@@ -7,3 +7,11 @@ the number of digits to be used for variants code (standard is 3)
Now go to product template, select an Internal Reference Template and generate an Internal Reference Prefix. Internal reference field is now read-only.
Each time a new variant is created for this product template, an internal reference is automatically assigned.
+
+In settings > inventory > Default Internal Reference Template, select which template will be loaded automatically each time a new product template is created.
+
+In user, enable extra rights:
+
+- "Generate internal reference" to display "Generate" button in product template.
+
+- "Internal reference template manager" to manage Internal Reference Templates in inventory > configuration menu and edit internal reference templates already assigned to a product template.
diff --git a/product_internal_reference_generator/security/groups.xml b/product_internal_reference_generator/security/groups.xml
index f5359480ef96..91621dc72a72 100644
--- a/product_internal_reference_generator/security/groups.xml
+++ b/product_internal_reference_generator/security/groups.xml
@@ -1,7 +1,11 @@
“Main0001” is the prefix generated by sequence and assigned to product template, and
“001” the variant identifier.
Every time a new variant is created, a new internal reference is automatically assigned with progressive variant code.
-A specific access rights allows specific users to change internal reference template for a product template once an internal reference has been generated, as well as editing existing ones.
+A general setting allows to set the default Internal Reference Template for new products.
+Two specific access rights allow specific users to:
+Table of contents
Go to inventory > Configuration > Internal Reference Templates and set:
+Go to Inventory > Configuration > Internal Reference Templates and set:
the sequence to be used for Internal Reference Prefix generation
the number of digits to be used for variants code (standard is 3)
Now go to product template, select an Internal Reference Template and generate an Internal Reference Prefix. Internal reference field is now read-only.
Each time a new variant is created for this product template, an internal reference is automatically assigned.
+In settings > inventory > Default Internal Reference Template, select which template will be loaded automatically each time a new product template is created.
+In user, enable extra rights:
+