Skip to content

Commit

Permalink
[ADD] product_internal_reference_generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyasProgrammer committed Nov 3, 2023
1 parent b71e3a5 commit 98c64c1
Show file tree
Hide file tree
Showing 19 changed files with 499 additions and 0 deletions.
111 changes: 111 additions & 0 deletions product_internal_reference_generator/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
====================================
Product Internal Reference Generator
====================================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:2f4c725133a9e785da3be2153b37f6201bde01124a67820bb37776713e208285
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |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/14.0/product_internal_reference_generator
: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-14-0/product-variant-14-0-product_internal_reference_generator
: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=14.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module allows to generate internal references for Product templates and variants using sequences, setting codes as read-only.

In product template, it's possible to choose among different Internal Reference Templates related to a sequence, and then generate an internal reference with the following structure:

Internal Reference Prefix + progressive number for variant, eg: “Main0001001”, where:

"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.

**Table of contents**

.. contents::
:local:

Usage
=====

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.

Known issues / Roadmap
======================

Due to odoo's behavior in handling internal reference for variants (see for example https://github.com/odoo/odoo/issues/140356 ), removing all attributes from a product template with variants will generate a new internal reference for product template.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/product-variant/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 <https://github.com/OCA/product-variant/issues/new?body=module:%20product_internal_reference_generator%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Ilyas
* Ooops

Contributors
~~~~~~~~~~~~

* `Ooops404 <https://www.ooops404.com>`__:

* Ilyas <[email protected]>

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-variant <https://github.com/OCA/product-variant/tree/14.0/product_internal_reference_generator>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions product_internal_reference_generator/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
18 changes: 18 additions & 0 deletions product_internal_reference_generator/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "Product Internal Reference Generator",
"summary": """Product template and variant reference based on sequence""",
"author": "Ilyas, Ooops, Odoo Community Association (OCA)",
"license": "AGPL-3",
"website": "https://github.com/OCA/product-variant",
"category": "Sale",
"version": "14.0.1.0.0",
"depends": ["stock", "base_view_inheritance_extension"],
"data": [
"security/groups.xml",
"security/ir.model.access.csv",
"views/product.xml",
],
"demo": ["demo/product_code_seq_demo.xml"],
"installable": True,
"application": False,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version='1.0' encoding='utf-8' ?>
<odoo>

<record model="ir.sequence" id="demo_pcs_seq_1">
<field name="name">Good products sequence</field>
<field name="prefix">GOOD</field>
<field name="padding">4</field>
</record>

<record model="ir.sequence" id="demo_pcs_seq_2">
<field name="name">Bad products sequence</field>
<field name="prefix">BAD</field>
<field name="padding">5</field>
</record>

<record model="product.code.sequence" id="demo_pcs_1">
<field name="name">Good products</field>
<field name="variant_reference_numbers">3</field>
<field
name="sequence_id"
ref="product_internal_reference_generator.demo_pcs_seq_1"
/>
</record>

<record model="product.code.sequence" id="demo_pcs_2">
<field name="name">Bad products</field>
<field name="variant_reference_numbers">4</field>
<field
name="sequence_id"
ref="product_internal_reference_generator.demo_pcs_seq_2"
/>
</record>

</odoo>
3 changes: 3 additions & 0 deletions product_internal_reference_generator/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import product_code_sequence
from . import product_template
from . import product_product
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright 2023 Ooops - Ilyas
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import fields, models


class ProductCodeSequence(models.Model):
_name = "product.code.sequence"
_description = "Internal Reference Template"

name = fields.Char(required=True)
sequence_id = fields.Many2one("ir.sequence", required=True)
variant_reference_numbers = fields.Integer("Digits", default=3, required=True)
17 changes: 17 additions & 0 deletions product_internal_reference_generator/models/product_product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2023 Ooops - Ilyas
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import api, models


class ProductProduct(models.Model):
_inherit = "product.product"

@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
if vals.get("product_tmpl_id"):
pt = self.env["product.template"].browse(vals["product_tmpl_id"])
if pt.variants_sequence_id:
vals["default_code"] = pt.get_variant_next_default_code()
res = super().create(vals_list)
return res
44 changes: 44 additions & 0 deletions product_internal_reference_generator/models/product_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright 2023 Ooops - Ilyas
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import api, fields, models


class ProductTemplate(models.Model):
_inherit = "product.template"

int_ref_template_id = fields.Many2one(
"product.code.sequence", "Internal Reference Template"
)
variants_sequence_id = fields.Many2one("ir.sequence")
variants_prefix = fields.Char(
"Internal Reference Prefix", readonly=True, tracking=True
)

@api.onchange("int_ref_template_id")
def onchange_int_ref_template_id(self):
self.variants_prefix = False

def btn_generate_sequence(self):
self.ensure_one()
int_ref_next_val = self.int_ref_template_id.sequence_id.next_by_id()
self.variants_prefix = int_ref_next_val
var_seq = self.env["ir.sequence"].create(
{
"name": "variants " + int_ref_next_val,
"padding": self.int_ref_template_id.variant_reference_numbers,
}
)
self.variants_sequence_id = var_seq
self.default_code = int_ref_next_val + var_seq.get_next_char(0)
self.update_variants_default_code()

def update_variants_default_code(self):
for pp in self.product_variant_ids:
if not pp.default_code:
pp.default_code = self.get_variant_next_default_code()

def get_variant_next_default_code(self):
if self.variants_sequence_id:
res = self.variants_prefix + self.variants_sequence_id.next_by_id()
return res
return False
3 changes: 3 additions & 0 deletions product_internal_reference_generator/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* `Ooops404 <https://www.ooops404.com>`__:

* Ilyas <[email protected]>
15 changes: 15 additions & 0 deletions product_internal_reference_generator/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
This module allows to generate internal references for Product templates and variants using sequences, setting codes as read-only.

In product template, it's possible to choose among different Internal Reference Templates related to a sequence, and then generate an internal reference with the following structure:

Internal Reference Prefix + progressive number for variant, eg: “Main0001001”, where:

"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.
1 change: 1 addition & 0 deletions product_internal_reference_generator/readme/ROADMAP.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Due to odoo's behavior in handling internal reference for variants (see for example https://github.com/odoo/odoo/issues/140356 ), removing all attributes from a product template with variants will generate a new internal reference for product template.
9 changes: 9 additions & 0 deletions product_internal_reference_generator/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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.
7 changes: 7 additions & 0 deletions product_internal_reference_generator/security/groups.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="group_int_ref_template_always_visible" model="res.groups">
<field name="name">Internal reference template always visible</field>
<field name="category_id" ref="base.module_category_hidden" />
</record>
</odoo>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
pcs1,pcs1,model_product_code_sequence,base.group_user,1,1,0,0
pcs2,pcs2,model_product_code_sequence,base.group_system,1,1,1,1
3 changes: 3 additions & 0 deletions product_internal_reference_generator/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from . import test_product_internal_reference_generator
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright 2023 Ooops - Ilyas
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo.tests import Form, SavepointCase


class TestProductInternalReferenceGenerator(SavepointCase):
@classmethod
def setUpClass(cls):
super(TestProductInternalReferenceGenerator, cls).setUpClass()
attr_model = cls.env["product.attribute"]
val_model = cls.env["product.attribute.value"]
pt_model = cls.env["product.template"]
cls.group_int_ref = cls.env.ref(
"product_internal_reference_generator.group_int_ref_template_always_visible"
)
cls.pt_seq = cls.env.ref("product_internal_reference_generator.demo_pcs_1")
cls.attr1 = attr_model.create({"name": "Wheels", "create_variant": "always"})
cls.attr2 = attr_model.create({"name": "Frame"})
cls.val1 = val_model.create({"name": "Big", "attribute_id": cls.attr1.id})
cls.val2 = val_model.create({"name": "Very Big", "attribute_id": cls.attr1.id})
cls.val3 = val_model.create({"name": "Carbon", "attribute_id": cls.attr2.id})
cls.val4 = val_model.create({"name": "Magnesium", "attribute_id": cls.attr2.id})
cls.pt_bicycle = pt_model.create({"name": "Bicycle"})
cls.pt_car = pt_model.create({"name": "Car"})
cls.pt_plane = pt_model.create({"name": "Plane"})

def test_all(self):
with Form(self.pt_bicycle) as pt:
pt.int_ref_template_id = self.pt_seq
pt.save()
self.assertFalse(pt.variants_prefix)
self.pt_bicycle.btn_generate_sequence()
df = str(self.pt_seq.sequence_id.number_next_actual - 1) + "000"
self.assertIn(df, pt.product_variant_id.default_code)

with Form(self.pt_car) as pt_car:
pt_car.int_ref_template_id = self.pt_seq
pt_car.save()
with pt_car.attribute_line_ids.new() as line:
line.attribute_id = self.attr1
line.value_ids.add(self.val1)
line.value_ids.add(self.val2)
line.save()
pt_car.save()
self.pt_car.btn_generate_sequence()
df1 = str(self.pt_seq.sequence_id.number_next_actual - 1) + "001"
self.assertIn(df1, self.pt_car.product_variant_ids[0].default_code)
df2 = str(self.pt_seq.sequence_id.number_next_actual - 1) + "002"
self.assertIn(df2, self.pt_car.product_variant_ids[1].default_code)

with pt_car.attribute_line_ids.new() as line:
line.attribute_id = self.attr2
line.value_ids.add(self.val3)
line.value_ids.add(self.val4)
line.save()
self.assertIn(df1, self.pt_car.product_variant_ids[0].default_code)
self.assertIn(df2, self.pt_car.product_variant_ids[1].default_code)
df3 = str(self.pt_seq.sequence_id.number_next_actual - 1) + "003"
self.assertIn(df3, self.pt_car.product_variant_ids[2].default_code)
df4 = str(self.pt_seq.sequence_id.number_next_actual - 1) + "004"
self.assertIn(df4, self.pt_car.product_variant_ids[3].default_code)
self.assertFalse(self.pt_plane.get_variant_next_default_code())
Loading

0 comments on commit 98c64c1

Please sign in to comment.